1 // https://issues.dlang.org/show_bug.cgi?id=21097
2 
3 // The crucial part of the test cases is testing `destroy`. At the same time, we test
4 // `core.internal.lifetime.emplaceInitializer` (which is currently called by `destroy`).
5 
6 enum SIZE = 10_000_000; // 10 MB should exhaust the stack on most if not all test systems.
7 
8 import core.internal.lifetime;
9 
test_largestruct()10 void test_largestruct()
11 {
12     static struct LargeStruct
13     {
14         int[SIZE/2] a1;
15         int b = 42;
16         int[SIZE/2] a2;
17     }
18     static LargeStruct s = void;
19     emplaceInitializer(s);
20     assert(s.b == 42);
21     s.b = 101;
22     destroy(s);
23     assert(s.b == 42);
24 }
25 
test_largestruct_w_opassign()26 void test_largestruct_w_opassign()
27 {
28     static struct LargeStructOpAssign
29     {
30         int[SIZE/2] a1;
31         int b = 420;         // non-zero init
32         int[SIZE/2] a2;
33 
34         void opAssign(typeof(this)) {} // hasElaborateAssign == true
35     }
36     static LargeStructOpAssign s = void;
37     emplaceInitializer(s);
38     assert(s.b == 420);
39     s.b = 101;
40     destroy(s);
41     assert(s.b == 420);
42 }
43 
test_largearray()44 void test_largearray() {
45     static struct NonZero
46     {
47         int i = 123;
48     }
49     static NonZero[SIZE] s = void;
50     emplaceInitializer(s);
51     assert(s[SIZE/2] == NonZero.init);
52     s[10] = NonZero(101);
53     destroy(s);
54     assert(s[10] == NonZero.init);
55 }
56 
test_largearray_w_opassign()57 void test_largearray_w_opassign() {
58     static struct NonZeroWithOpAssign
59     {
60         int i = 123;
61         void opAssign(typeof(this)) {} // hasElaborateAssign == true
62     }
63     static NonZeroWithOpAssign[SIZE] s = void;
64     emplaceInitializer(s);
65     assert(s[SIZE/2] == NonZeroWithOpAssign.init);
66     s[10] = NonZeroWithOpAssign(101);
67     destroy(s);
68     assert(s[10] == NonZeroWithOpAssign.init);
69 }
70 
main()71 int main()
72 {
73     test_largestruct();
74     test_largestruct_w_opassign();
75     test_largearray();
76     test_largearray_w_opassign();
77     return 0;
78 }
79