Skip to content

Commit 7acd5ee

Browse files
committed
Merge tests
1 parent f2a7bc2 commit 7acd5ee

141 files changed

Lines changed: 2983 additions & 3045 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// translation-fail: refcount, unsafe
2+
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
3+
#include <cassert>
4+
#include <cstring>
5+
6+
// N4950 [basic.types.general]/2: the underlying bytes of an object of
7+
// trivially copyable type can be copied into an array of unsigned char; if
8+
// they are copied back, the object holds its original value.
9+
namespace p2_pointer_to_member_function_bytes_round_trip {
10+
struct HasTwoMemberFunctions {
11+
int first_calls = 0;
12+
int second_calls = 0;
13+
void first() { ++first_calls; }
14+
void second() { ++second_calls; }
15+
};
16+
17+
void test() {
18+
void (HasTwoMemberFunctions::*pmf)() = &HasTwoMemberFunctions::second;
19+
unsigned char bytes[sizeof(pmf)];
20+
std::memcpy(bytes, &pmf, sizeof(pmf));
21+
pmf = &HasTwoMemberFunctions::first;
22+
std::memcpy(&pmf, bytes, sizeof(pmf));
23+
assert(pmf == &HasTwoMemberFunctions::second);
24+
HasTwoMemberFunctions obj;
25+
(obj.*pmf)();
26+
assert(obj.first_calls == 0);
27+
assert(obj.second_calls == 1);
28+
29+
void (HasTwoMemberFunctions::*null_pmf)() = nullptr;
30+
std::memcpy(bytes, &null_pmf, sizeof(null_pmf));
31+
null_pmf = &HasTwoMemberFunctions::first;
32+
std::memcpy(&null_pmf, bytes, sizeof(null_pmf));
33+
assert(null_pmf == nullptr);
34+
}
35+
} // namespace p2_pointer_to_member_function_bytes_round_trip
36+
37+
// N4950 [basic.types.general]/2: the underlying bytes of an object of
38+
// trivially copyable type can be copied into an array of unsigned char; if
39+
// they are copied back, the object holds its original value.
40+
namespace p2_pointer_to_virtual_member_function_bytes_round_trip {
41+
static int base_calls;
42+
static int override_calls;
43+
44+
struct DeclaresVirtual {
45+
virtual void run() { ++base_calls; }
46+
void other() {}
47+
};
48+
49+
struct OverridesVirtual : DeclaresVirtual {
50+
void run() override { ++override_calls; }
51+
};
52+
53+
void test() {
54+
void (DeclaresVirtual::*pmf)() = &DeclaresVirtual::run;
55+
unsigned char bytes[sizeof(pmf)];
56+
std::memcpy(bytes, &pmf, sizeof(pmf));
57+
pmf = &DeclaresVirtual::other;
58+
std::memcpy(&pmf, bytes, sizeof(pmf));
59+
OverridesVirtual overrider;
60+
DeclaresVirtual &ref = overrider;
61+
(ref.*pmf)();
62+
assert(base_calls == 0);
63+
assert(override_calls == 1);
64+
}
65+
} // namespace p2_pointer_to_virtual_member_function_bytes_round_trip
66+
67+
// N4950 [basic.types.general]/3: if the underlying bytes of obj1 of trivially
68+
// copyable type are copied into obj2, obj2 holds the same value as obj1.
69+
namespace p3_class_holding_pointers_to_members_bytes_copied {
70+
struct HasValueAndIncrement {
71+
int padding;
72+
int value;
73+
void increment() { ++value; }
74+
};
75+
76+
struct HoldsPointersToMembers {
77+
int HasValueAndIncrement::*data;
78+
void (HasValueAndIncrement::*function)();
79+
};
80+
81+
void test() {
82+
HoldsPointersToMembers source{&HasValueAndIncrement::value,
83+
&HasValueAndIncrement::increment};
84+
HoldsPointersToMembers destination{nullptr, nullptr};
85+
std::memcpy(&destination, &source, sizeof(source));
86+
assert(destination.data == &HasValueAndIncrement::value);
87+
assert(destination.function == &HasValueAndIncrement::increment);
88+
HasValueAndIncrement obj{1, 2};
89+
(obj.*destination.function)();
90+
assert(obj.*destination.data == 3);
91+
assert(obj.padding == 1);
92+
}
93+
} // namespace p3_class_holding_pointers_to_members_bytes_copied
94+
95+
// N4950 [basic.types.general]/3: if the underlying bytes of obj1 of trivially
96+
// copyable type are copied into obj2, obj2 holds the same value as obj1.
97+
namespace p3_pointer_to_member_function_bytes_copied_to_other_object {
98+
struct HasTwoMemberFunctions {
99+
int first_calls = 0;
100+
int second_calls = 0;
101+
void first() { ++first_calls; }
102+
void second() { ++second_calls; }
103+
};
104+
105+
void test() {
106+
void (HasTwoMemberFunctions::*source)() = &HasTwoMemberFunctions::second;
107+
void (HasTwoMemberFunctions::*destination)() = &HasTwoMemberFunctions::first;
108+
std::memcpy(&destination, &source, sizeof(source));
109+
assert(destination == source);
110+
HasTwoMemberFunctions obj;
111+
(obj.*destination)();
112+
assert(obj.first_calls == 0);
113+
assert(obj.second_calls == 1);
114+
115+
void (HasTwoMemberFunctions::*null_source)() = nullptr;
116+
std::memcpy(&destination, &null_source, sizeof(null_source));
117+
assert(destination == nullptr);
118+
}
119+
} // namespace p3_pointer_to_member_function_bytes_copied_to_other_object
120+
121+
int main() {
122+
p2_pointer_to_member_function_bytes_round_trip::test();
123+
p2_pointer_to_virtual_member_function_bytes_round_trip::test();
124+
p3_class_holding_pointers_to_members_bytes_copied::test();
125+
p3_pointer_to_member_function_bytes_copied_to_other_object::test();
126+
return 0;
127+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
2+
#include <cassert>
3+
#include <cstring>
4+
5+
// N4950 [basic.types.general]/2: the underlying bytes of an object of
6+
// trivially copyable type can be copied into an array of unsigned char; if
7+
// they are copied back, the object holds its original value.
8+
namespace p2_pointer_to_data_member_bytes_round_trip {
9+
struct HasTwoInts {
10+
int first;
11+
int second;
12+
};
13+
14+
void test() {
15+
int HasTwoInts::*pm = &HasTwoInts::second;
16+
unsigned char bytes[sizeof(pm)];
17+
std::memcpy(bytes, &pm, sizeof(pm));
18+
pm = &HasTwoInts::first;
19+
std::memcpy(&pm, bytes, sizeof(pm));
20+
assert(pm == &HasTwoInts::second);
21+
HasTwoInts obj{3, 4};
22+
assert(obj.*pm == 4);
23+
24+
int HasTwoInts::*null_pm = nullptr;
25+
std::memcpy(bytes, &null_pm, sizeof(null_pm));
26+
null_pm = &HasTwoInts::first;
27+
std::memcpy(&null_pm, bytes, sizeof(null_pm));
28+
assert(null_pm == nullptr);
29+
}
30+
} // namespace p2_pointer_to_data_member_bytes_round_trip
31+
32+
// N4950 [basic.types.general]/3: if the underlying bytes of obj1 of trivially
33+
// copyable type are copied into obj2, obj2 holds the same value as obj1.
34+
namespace p3_pointer_to_data_member_bytes_copied_to_other_object {
35+
struct HasTwoInts {
36+
int first;
37+
int second;
38+
};
39+
40+
void test() {
41+
int HasTwoInts::*source = &HasTwoInts::second;
42+
int HasTwoInts::*destination = &HasTwoInts::first;
43+
std::memcpy(&destination, &source, sizeof(source));
44+
assert(destination == source);
45+
HasTwoInts obj{3, 4};
46+
assert(obj.*destination == 4);
47+
48+
int HasTwoInts::*null_source = nullptr;
49+
std::memcpy(&destination, &null_source, sizeof(null_source));
50+
assert(destination == nullptr);
51+
}
52+
} // namespace p3_pointer_to_data_member_bytes_copied_to_other_object
53+
54+
int main() {
55+
p2_pointer_to_data_member_bytes_round_trip::test();
56+
p3_pointer_to_data_member_bytes_copied_to_other_object::test();
57+
return 0;
58+
}

‎tests/std/basic/basic.types/basic.types.general/out/refcount/p2-pointer-to-data-member-bytes-round-trip.rs‎

Lines changed: 0 additions & 66 deletions
This file was deleted.

‎tests/std/basic/basic.types/basic.types.general/out/refcount/p3-pointer-to-data-member-bytes-copied-to-other-object.rs‎

Lines changed: 0 additions & 62 deletions
This file was deleted.

‎tests/std/basic/basic.types/basic.types.general/out/unsafe/p2-pointer-to-data-member-bytes-round-trip.rs‎

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)