/
ihateeh.cxx
199 lines (157 loc) · 3.64 KB
/
ihateeh.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for
// full license information.
#include <stdlib.h>
#include <stdio.h>
#define FALSE 0
#define TRUE 1
#define NO_CTOR_THROW 1
#define NO_DTOR_THROW 2
#define TEST_DTOR
#define MAX_OBJECTS 500
#define NOEXCEPT(_x)
int Object[MAX_OBJECTS];
int CurrentObjectNumber, ThrowCount, MaxObjectCount = 1;
int Fail;
void FAIL(int i) {
printf("FAILED on %d\n", i);
Fail++;
}
void dealloc(int i, int no_throw) {
/* Make sure i is valid, and object exists */
if (i < 0 || i >= MaxObjectCount || !Object[i])
FAIL(i);
Object[i] = 0;
/* Try throw in this dtor.. */
#ifdef TEST_DTOR
if (i + MaxObjectCount == ThrowCount && !no_throw) {
printf("Throwing\n");
throw(1);
}
#endif
}
void alloc(int i, int no_throw) {
if (CurrentObjectNumber > MaxObjectCount)
MaxObjectCount = CurrentObjectNumber;
/* Object already exists? */
if (Object[i])
FAIL(i);
/* Try throw in this ctor.. */
if (i == ThrowCount && !no_throw) {
printf("Throwing\n");
throw(1);
}
if (i >= MAX_OBJECTS) {
printf("\n*** Number of objects exceeded. Increase MAX_OBJECTS ***\n\n");
FAIL(i);
i = 0;
}
Object[i] = 1;
}
class B {
public:
int i;
int flag;
B();
B(int);
~B() NOEXCEPT(false);
};
B::B() {
i = CurrentObjectNumber++;
printf("B ctor. i = %d\n", i);
alloc(i, FALSE);
}
B::B(int f) {
i = CurrentObjectNumber++;
flag = f;
printf("B ctor. i = %d\n", i);
alloc(i, flag == NO_CTOR_THROW);
}
B::~B() NOEXCEPT(false) {
printf("B dtor. i = %d\n", i);
dealloc(i, flag == NO_DTOR_THROW);
}
class A {
public:
int i;
A();
A(int) {
i = CurrentObjectNumber++;
printf("A(int) ctor. i = %d\n", i);
alloc(i, FALSE);
}
A operator+(A a);
A(const A &a) {
/* Try objects in ctor */
B b1 = NO_DTOR_THROW, b2 = NO_DTOR_THROW;
i = CurrentObjectNumber++;
printf("A copy ctor. i = %d\n", i);
alloc(i, FALSE);
}
~A() NOEXCEPT(false) {
/* Try objects in dtor */
B b1 = NO_CTOR_THROW, b2 = NO_CTOR_THROW;
printf("A dtor. i = %d\n", i);
dealloc(i, FALSE);
};
};
A::A() {
i = CurrentObjectNumber++;
printf("A ctor. i = %d\n", i);
alloc(i, FALSE);
}
A A::operator+(A a) {
printf("A%d + A%d\n", i, a.i);
return A();
}
A foo(A a1, A a2) { return a1 + a2; };
int bar() {
A a;
return 666;
}
void foo2(int i, A a1, A a2, A a3) {
if (i != 666)
FAIL(666);
foo(a1, a3);
}
A test() {
puts("Try simple ctor");
A a1;
puts("Try question op ctor");
A a2 = (ThrowCount & 1) ? A() : ThrowCount;
puts("Try a more complex question op ctor");
A a3 = (ThrowCount & 1) ? A() + a1 + A() + a2 : a2 + A() + A() + ThrowCount;
puts("Try mbarg copy ctors, and return UDT");
A a4 = foo(a1, a2);
puts("Try a more complex mbarg copy ctors, and a function call");
foo2(bar(), a1 + a2, a2 + A() + a3 + a4, a3);
puts("Try temporary expressions, and return UDT");
return a1 + A() + a2 + A() + a3 + a4;
}
int main() {
int i;
/* Call test(), with a different ctor/dtor throwing each time */
for (ThrowCount = 0; ThrowCount < MaxObjectCount * 2; ThrowCount++) {
printf("ThrowCount = %d MaxObjectCount = %d\n", ThrowCount,
MaxObjectCount);
CurrentObjectNumber = 0;
try {
test();
} catch (int) {
printf("In catch\n");
}
/* Any objects which didn't get dtor'd? */
for (i = 0; i < MaxObjectCount; i++) {
if (Object[i]) {
FAIL(i);
Object[i] = 0;
}
}
printf("\n");
}
printf("\n");
if (Fail)
printf("FAILED %d tests\n", Fail);
else
printf("Passed\n");
}