Debian Codegraph @

gcc/testsuite/gdc.test/runnable/placenew.d

raw
1 import core.stdc.stdio;
2 import core.stdc.stdlib;
3
4 /*************************************************/
5
6 struct S
7 {
8 float d;
9 int i;
10 char c;
11 }
12
13 void test1()
14 {
15 S s;
16 S* p = new (s) S();
17 assert(p.i == 0 && p.c == 0xFF);
18 }
19
20 void test2()
21 {
22 S s;
23 S* p = new (s) S(i:3);
24 assert(p.i == 3 && p.c == 0xFF);
25 }
26
27 /*************************************************/
28
29 struct S3
30 {
31 int i;
32 this(int i) { this.i = i + 3; }
33 }
34
35 void test3()
36 {
37 S3 s;
38 s.i = 20;
39 S3* p = new (s) S3(4);
40 assert(p.i == 7);
41 }
42
43
44 /*************************************************/
45
46 void test4()
47 {
48 int i = 3;
49 int* p = new(i) int;
50 *p = 4;
51 assert(i == 4);
52
53 p = new(i) int(7);
54 assert(i == 7);
55 }
56
57 /*************************************************/
58
59 class C5
60 {
61 int i, j = 4;
62 }
63
64 int test5()
65 {
66 int[10] k;
67 C5 c = new(k) C5;
68 //printf("c.j: %d\n", c.j);
69 assert(c.j == 4);
70 assert(cast(void*)c == cast(void*)k.ptr);
71 return c.j;
72 }
73
74 /*************************************************/
75
76 struct S6
77 {
78 int i = 1, j = 4, k = 9;
79 }
80
81 ref void[T.sizeof] mallocate(T)()
82 {
83 return *(cast(void[T.sizeof]*) malloc(T.sizeof));
84 }
85
86 void test6()
87 {
88 S6* ps = new(mallocate!S6()) S6;
89 assert(ps.i == 1);
90 assert(ps.j == 4);
91 assert(ps.k == 9);
92 }
93
94 /*************************************************/
95
96 int main()
97 {
98 test1();
99 test2();
100 test3();
101 test4();
102 test5();
103 test6();
104
105 return 0;
106 }