match.pd: Fix indefinite recursion during exp-log transformations [PR118490]
[gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / copy_n / 108846.cc
blobd46fb9006e2d52bfe0bbc5b0528d820710f22f33
1 // { dg-do run { target c++11 } }
3 #include <algorithm>
4 #include <testsuite_hooks.h>
6 // PR libstdc++/108846 std::copy, std::copy_n and std::copy_backward
7 // on potentially overlapping subobjects
9 struct B {
10 B(int i, short j) : i(i), j(j) {}
11 int i;
12 short j;
14 struct D : B {
15 D(int i, short j, short x) : B(i, j), x(x) {}
16 short x; // Stored in tail padding of B
19 void
20 test_pr108846()
22 D ddst(1, 2, 3);
23 D dsrc(4, 5, 6);
24 B *dst = &ddst;
25 B *src = &dsrc;
26 // If this is optimized to memmove it will overwrite tail padding.
27 std::copy_n(src, 1, dst);
28 // Check tail padding is unchanged:
29 VERIFY(ddst.x == 3);
30 // Check B subobject was copied:
31 VERIFY(ddst.i == 4 && ddst.j == 5);
32 #if __cpp_lib_ranges >= 201911L
33 ddst.i = ddst.j = 99;
34 std::ranges::copy_n(src, 1, dst);
35 VERIFY(ddst.x == 3);
36 VERIFY(ddst.i == 4 && ddst.j == 5);
37 #endif
40 struct B2 {
41 B2(int i, short j) : i(i), j(j) {}
42 B2& operator=(B2& b) { i = b.i; j = b.j; return *this; }
43 int i;
44 short j;
46 struct D2 : B2 {
47 D2(int i, short j, short x) : B2(i, j), x(x) {}
48 short x; // Stored in tail padding of B2
51 void
52 test_non_const_copy_assign()
54 D2 ddst(1, 2, 3);
55 D2 dsrc(4, 5, 6);
56 B2 *dst = &ddst;
57 B2 *src = &dsrc;
58 // Ensure the not-taken trivial copy path works for this type.
59 std::copy_n(src, 1, dst);
60 // Check tail padding is unchanged:
61 VERIFY(ddst.x == 3);
62 // Check B subobject was copied:
63 VERIFY(ddst.i == 4 && ddst.j == 5);
64 #if __cpp_lib_ranges >= 201911L
65 ddst.i = ddst.j = 99;
66 std::ranges::copy_n(src, 1, dst);
67 VERIFY(ddst.x == 3);
68 VERIFY(ddst.i == 4 && ddst.j == 5);
69 #endif
72 struct B3 {
73 B3(int i, short j) : i(i), j(j) {}
74 B3& operator=(B3& b) = default;
75 int i;
76 short j;
78 struct D3 : B3 {
79 D3(int i, short j, short x) : B3(i, j), x(x) {}
80 short x; // Stored in tail padding of B3
83 void
84 test_non_const_copy_assign_trivial()
86 D3 ddst(1, 2, 3);
87 D3 dsrc(4, 5, 6);
88 B3 *dst = &ddst;
89 B3 *src = &dsrc;
90 // If this is optimized to memmove it will overwrite tail padding.
91 std::copy_n(src, 1, dst);
92 // Check tail padding is unchanged:
93 VERIFY(ddst.x == 3);
94 // Check B subobject was copied:
95 VERIFY(ddst.i == 4 && ddst.j == 5);
96 #if __cpp_lib_ranges >= 201911L
97 ddst.i = ddst.j = 99;
98 std::ranges::copy_n(src, 1, dst);
99 VERIFY(ddst.x == 3);
100 VERIFY(ddst.i == 4 && ddst.j == 5);
101 #endif
104 int main()
106 test_pr108846();
107 test_non_const_copy_assign();
108 test_non_const_copy_assign_trivial();