Debian Codegraph @

gcc/testsuite/g++.dg/init/new36.C

raw
1 // Testcase for invocation of constructors/destructors in operator new[].
2 // { dg-do run }
3
4 #include <stdlib.h>
5
6 struct E {
7 virtual ~E() { }
8 };
9
10 struct S {
11 S();
12 ~S();
13 };
14
15 static int count;
16 static int max;
17 static int throwAfter = -1;
18 static S *pS;
19
20 S::S()
21 {
22 if (throwAfter >= 0 && count >= throwAfter)
23 throw E();
24 if (pS)
25 {
26 ++pS;
27 if (this != pS)
28 abort();
29 }
30 else
31 pS = this;
32 ++count;
33 max = count;
34 }
35
36 S::~S()
37 {
38 if (count > 1)
39 {
40 if (this != pS)
41 abort();
42 --pS;
43 }
44 else
45 pS = 0;
46 --count;
47 }
48
49 void __attribute__((noinline)) doit(int n)
50 {
51 {
52 S *s = new S[n];
53 if (count != n)
54 abort();
55 if (pS != s + n - 1)
56 abort();
57 delete [] s;
58 if (count != 0)
59 abort();
60 }
61 throwAfter = 2;
62 max = 0;
63 try
64 {
65 new S[n];
66 abort();
67 }
68 catch (E)
69 {
70 if (max != 2)
71 abort();
72 }
73 throwAfter = -1;
74 }
75
76 int main()
77 {
78 {
79 S s;
80 if (count != 1)
81 abort();
82 if (pS != &s)
83 abort();
84 }
85 if (count != 0)
86 abort();
87 {
88 S *s = new S;
89 if (count != 1)
90 abort();
91 if (pS != s)
92 abort();
93 delete s;
94 if (count != 0)
95 abort();
96 }
97 {
98 S *s = new S[1];
99 if (count != 1)
100 abort();
101 if (pS != s)
102 abort();
103 delete [] s;
104 if (count != 0)
105 abort();
106 }
107 {
108 S *s = new S[5];
109 if (count != 5)
110 abort();
111 if (pS != s + 4)
112 abort();
113 delete [] s;
114 if (count != 0)
115 abort();
116 }
117 typedef S A[5];
118 {
119 S *s = new A;
120 if (count != 5)
121 abort();
122 if (pS != s + 4)
123 abort();
124 delete [] s;
125 if (count != 0)
126 abort();
127 }
128 throwAfter = 2;
129 max = 0;
130 try
131 {
132 new S[5];
133 abort();
134 }
135 catch (E)
136 {
137 if (max != 2)
138 abort();
139 }
140 max = 0;
141 try
142 {
143 new A;
144 abort();
145 }
146 catch (E)
147 {
148 if (max != 2)
149 abort();
150 }
151 throwAfter = -1;
152 doit(5);
153 }