1*f4a2713aSLionel Sambuc #include <stdio.h>
2*f4a2713aSLionel Sambuc #include <stdlib.h>
3*f4a2713aSLionel Sambuc
4*f4a2713aSLionel Sambuc #define N_FIELDS 7
5*f4a2713aSLionel Sambuc #define N_FUNCS 128
6*f4a2713aSLionel Sambuc #define FUNCSPACING 20
7*f4a2713aSLionel Sambuc #define N_STRUCTS 180 /* 1280 */
8*f4a2713aSLionel Sambuc #define N_BASES 6
9*f4a2713aSLionel Sambuc #define COVARIANT 0
10*f4a2713aSLionel Sambuc
11*f4a2713aSLionel Sambuc const char *simple_types[] = { "bool", "char", "short", "int", "float",
12*f4a2713aSLionel Sambuc "double", "long double", "wchar_t", "void *",
13*f4a2713aSLionel Sambuc "char *"
14*f4a2713aSLionel Sambuc };
15*f4a2713aSLionel Sambuc
gl(const char * c)16*f4a2713aSLionel Sambuc void gl(const char *c) {
17*f4a2713aSLionel Sambuc printf("%s\n", c);
18*f4a2713aSLionel Sambuc }
19*f4a2713aSLionel Sambuc
g(const char * c)20*f4a2713aSLionel Sambuc void g(const char *c) {
21*f4a2713aSLionel Sambuc printf("%s", c);
22*f4a2713aSLionel Sambuc }
23*f4a2713aSLionel Sambuc
g(int i)24*f4a2713aSLionel Sambuc void g(int i) {
25*f4a2713aSLionel Sambuc printf("%d", i);
26*f4a2713aSLionel Sambuc }
27*f4a2713aSLionel Sambuc
28*f4a2713aSLionel Sambuc int uuid = 0;
29*f4a2713aSLionel Sambuc char base_present[N_STRUCTS][N_STRUCTS];
30*f4a2713aSLionel Sambuc
31*f4a2713aSLionel Sambuc // The return type for each function when doing covariant testcase generation.
32*f4a2713aSLionel Sambuc short ret_types[N_STRUCTS][N_FUNCS*FUNCSPACING];
33*f4a2713aSLionel Sambuc
is_ambiguous(int s,int base)34*f4a2713aSLionel Sambuc bool is_ambiguous(int s, int base) {
35*f4a2713aSLionel Sambuc for (int i = 0; i < N_STRUCTS; ++i) {
36*f4a2713aSLionel Sambuc if ((base_present[base][i] & base_present[s][i]) == 1)
37*f4a2713aSLionel Sambuc return true;
38*f4a2713aSLionel Sambuc }
39*f4a2713aSLionel Sambuc return false;
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc
add_bases(int s,int base)42*f4a2713aSLionel Sambuc void add_bases(int s, int base) {
43*f4a2713aSLionel Sambuc for (int i = 0; i < N_STRUCTS; ++i)
44*f4a2713aSLionel Sambuc base_present[s][i] |= base_present[base][i];
45*f4a2713aSLionel Sambuc if (!COVARIANT)
46*f4a2713aSLionel Sambuc return;
47*f4a2713aSLionel Sambuc for (int i = 0; i < N_FUNCS*FUNCSPACING; ++i) {
48*f4a2713aSLionel Sambuc if (!ret_types[base][i])
49*f4a2713aSLionel Sambuc continue;
50*f4a2713aSLionel Sambuc if (!ret_types[s][i]) {
51*f4a2713aSLionel Sambuc ret_types[s][i] = ret_types[base][i];
52*f4a2713aSLionel Sambuc continue;
53*f4a2713aSLionel Sambuc }
54*f4a2713aSLionel Sambuc if (base_present[ret_types[base][i]][ret_types[s][i]])
55*f4a2713aSLionel Sambuc // If the return type of the function from this base dominates
56*f4a2713aSLionel Sambuc ret_types[s][i] = ret_types[base][i];
57*f4a2713aSLionel Sambuc if (base_present[ret_types[s][i]][ret_types[base][i]])
58*f4a2713aSLionel Sambuc // If a previous base dominates
59*f4a2713aSLionel Sambuc continue;
60*f4a2713aSLionel Sambuc // If neither dominates, we'll use this class.
61*f4a2713aSLionel Sambuc ret_types[s][i] = s;
62*f4a2713aSLionel Sambuc }
63*f4a2713aSLionel Sambuc }
64*f4a2713aSLionel Sambuc
65*f4a2713aSLionel Sambuc // This contains the class that has the final override for
66*f4a2713aSLionel Sambuc // each class, for each function.
67*f4a2713aSLionel Sambuc short final_override[N_STRUCTS][N_FUNCS*FUNCSPACING];
68*f4a2713aSLionel Sambuc
gs(int s)69*f4a2713aSLionel Sambuc void gs(int s) {
70*f4a2713aSLionel Sambuc bool polymorphic = false;
71*f4a2713aSLionel Sambuc
72*f4a2713aSLionel Sambuc static int bases[N_BASES];
73*f4a2713aSLionel Sambuc int i_bases = random() % (N_BASES*2);
74*f4a2713aSLionel Sambuc if (i_bases >= N_BASES)
75*f4a2713aSLionel Sambuc // PARAM: 1/2 of all clases should have no bases
76*f4a2713aSLionel Sambuc i_bases = 0;
77*f4a2713aSLionel Sambuc int n_bases = 0;
78*f4a2713aSLionel Sambuc bool first_base = true;
79*f4a2713aSLionel Sambuc
80*f4a2713aSLionel Sambuc // PARAM: 3/4 of all should be class, the rest are structs
81*f4a2713aSLionel Sambuc if (random() % 4 == 0)
82*f4a2713aSLionel Sambuc g("struct s");
83*f4a2713aSLionel Sambuc else
84*f4a2713aSLionel Sambuc g("class s");
85*f4a2713aSLionel Sambuc g(s);
86*f4a2713aSLionel Sambuc int old_base = -1;
87*f4a2713aSLionel Sambuc if (s == 0 || s == 1)
88*f4a2713aSLionel Sambuc i_bases = 0;
89*f4a2713aSLionel Sambuc while (i_bases) {
90*f4a2713aSLionel Sambuc --i_bases;
91*f4a2713aSLionel Sambuc int base = random() % (s-1) + 1;
92*f4a2713aSLionel Sambuc if (!base_present[s][base]) {
93*f4a2713aSLionel Sambuc if (is_ambiguous(s, base))
94*f4a2713aSLionel Sambuc continue;
95*f4a2713aSLionel Sambuc if (first_base) {
96*f4a2713aSLionel Sambuc first_base = false;
97*f4a2713aSLionel Sambuc g(": ");
98*f4a2713aSLionel Sambuc } else
99*f4a2713aSLionel Sambuc g(", ");
100*f4a2713aSLionel Sambuc int base_type = 1;
101*f4a2713aSLionel Sambuc if (random()%8 == 0) {
102*f4a2713aSLionel Sambuc // PARAM: 1/8th the bases are virtual
103*f4a2713aSLionel Sambuc g("virtual ");
104*f4a2713aSLionel Sambuc // We have a vtable and rtti, but technically we're not polymorphic
105*f4a2713aSLionel Sambuc // polymorphic = true;
106*f4a2713aSLionel Sambuc base_type = 3;
107*f4a2713aSLionel Sambuc }
108*f4a2713aSLionel Sambuc // PARAM: 1/4 are public, 1/8 are privare, 1/8 are protected, the reset, default
109*f4a2713aSLionel Sambuc int base_protection = 0;
110*f4a2713aSLionel Sambuc if (!COVARIANT)
111*f4a2713aSLionel Sambuc base_protection = random()%8;
112*f4a2713aSLionel Sambuc switch (base_protection) {
113*f4a2713aSLionel Sambuc case 0:
114*f4a2713aSLionel Sambuc case 1:
115*f4a2713aSLionel Sambuc g("public "); break;
116*f4a2713aSLionel Sambuc case 2:
117*f4a2713aSLionel Sambuc case 3:
118*f4a2713aSLionel Sambuc case 4:
119*f4a2713aSLionel Sambuc case 5:
120*f4a2713aSLionel Sambuc break;
121*f4a2713aSLionel Sambuc case 6:
122*f4a2713aSLionel Sambuc g("private "); break;
123*f4a2713aSLionel Sambuc case 7:
124*f4a2713aSLionel Sambuc g("protected "); break;
125*f4a2713aSLionel Sambuc }
126*f4a2713aSLionel Sambuc g("s");
127*f4a2713aSLionel Sambuc add_bases(s, base);
128*f4a2713aSLionel Sambuc bases[n_bases] = base;
129*f4a2713aSLionel Sambuc base_present[s][base] = base_type;
130*f4a2713aSLionel Sambuc ++n_bases;
131*f4a2713aSLionel Sambuc g(base);
132*f4a2713aSLionel Sambuc old_base = base;
133*f4a2713aSLionel Sambuc }
134*f4a2713aSLionel Sambuc }
135*f4a2713aSLionel Sambuc gl(" {");
136*f4a2713aSLionel Sambuc
137*f4a2713aSLionel Sambuc /* Fields */
138*f4a2713aSLionel Sambuc int n_fields = N_FIELDS == 0 ? 0 : random() % (N_FIELDS*4);
139*f4a2713aSLionel Sambuc // PARAM: 3/4 of all structs should have no members
140*f4a2713aSLionel Sambuc if (n_fields >= N_FIELDS)
141*f4a2713aSLionel Sambuc n_fields = 0;
142*f4a2713aSLionel Sambuc for (int i = 0; i < n_fields; ++i) {
143*f4a2713aSLionel Sambuc int t = random() % (sizeof(simple_types) / sizeof(simple_types[0]));
144*f4a2713aSLionel Sambuc g(" "); g(simple_types[t]); g(" field"); g(i); gl(";");
145*f4a2713aSLionel Sambuc }
146*f4a2713aSLionel Sambuc
147*f4a2713aSLionel Sambuc /* Virtual functions */
148*f4a2713aSLionel Sambuc static int funcs[N_FUNCS*FUNCSPACING];
149*f4a2713aSLionel Sambuc // PARAM: 1/2 of all structs should have no virtual functions
150*f4a2713aSLionel Sambuc int n_funcs = random() % (N_FUNCS*2);
151*f4a2713aSLionel Sambuc if (n_funcs > N_FUNCS)
152*f4a2713aSLionel Sambuc n_funcs = 0;
153*f4a2713aSLionel Sambuc int old_func = -1;
154*f4a2713aSLionel Sambuc for (int i = 0; i < n_funcs; ++i) {
155*f4a2713aSLionel Sambuc int fn = old_func + random() % FUNCSPACING + 1;
156*f4a2713aSLionel Sambuc funcs[i] = fn;
157*f4a2713aSLionel Sambuc int ret_type = 0;
158*f4a2713aSLionel Sambuc if (COVARIANT) {
159*f4a2713aSLionel Sambuc ret_type = random() % s + 1;
160*f4a2713aSLionel Sambuc if (!base_present[s][ret_type]
161*f4a2713aSLionel Sambuc || !base_present[ret_type][ret_types[s][fn]])
162*f4a2713aSLionel Sambuc if (ret_types[s][fn]) {
163*f4a2713aSLionel Sambuc printf(" // Found one for s%d for s%d* fun%d.\n", s,
164*f4a2713aSLionel Sambuc ret_types[s][fn], fn);
165*f4a2713aSLionel Sambuc ret_type = ret_types[s][fn];
166*f4a2713aSLionel Sambuc } else
167*f4a2713aSLionel Sambuc ret_type = s;
168*f4a2713aSLionel Sambuc else
169*f4a2713aSLionel Sambuc printf(" // Wow found one for s%d for fun%d.\n", s, fn);
170*f4a2713aSLionel Sambuc ret_types[s][fn] = ret_type;
171*f4a2713aSLionel Sambuc }
172*f4a2713aSLionel Sambuc if (ret_type) {
173*f4a2713aSLionel Sambuc g(" virtual s"); g(ret_type); g("* fun");
174*f4a2713aSLionel Sambuc } else
175*f4a2713aSLionel Sambuc g(" virtual void fun");
176*f4a2713aSLionel Sambuc g(fn); g("(char *t) { mix(\"vfn this offset\", (char *)this - t); mix(\"vfn uuid\", "); g(++uuid);
177*f4a2713aSLionel Sambuc if (ret_type)
178*f4a2713aSLionel Sambuc gl("); return 0; }");
179*f4a2713aSLionel Sambuc else
180*f4a2713aSLionel Sambuc gl("); }");
181*f4a2713aSLionel Sambuc final_override[s][fn] = s;
182*f4a2713aSLionel Sambuc old_func = fn;
183*f4a2713aSLionel Sambuc }
184*f4a2713aSLionel Sambuc
185*f4a2713aSLionel Sambuc // Add required overriders for correctness
186*f4a2713aSLionel Sambuc for (int i = 0; i < n_bases; ++i) {
187*f4a2713aSLionel Sambuc // For each base
188*f4a2713aSLionel Sambuc int base = bases[i];
189*f4a2713aSLionel Sambuc for (int fn = 0; fn < N_FUNCS*FUNCSPACING; ++fn) {
190*f4a2713aSLionel Sambuc // For each possible function
191*f4a2713aSLionel Sambuc int new_base = final_override[base][fn];
192*f4a2713aSLionel Sambuc if (new_base == 0)
193*f4a2713aSLionel Sambuc // If the base didn't have a final overrider, skip
194*f4a2713aSLionel Sambuc continue;
195*f4a2713aSLionel Sambuc
196*f4a2713aSLionel Sambuc int prev_base = final_override[s][fn];
197*f4a2713aSLionel Sambuc if (prev_base == s)
198*f4a2713aSLionel Sambuc // Skip functions defined in this class
199*f4a2713aSLionel Sambuc continue;
200*f4a2713aSLionel Sambuc
201*f4a2713aSLionel Sambuc // If we don't want to change the info, skip
202*f4a2713aSLionel Sambuc if (prev_base == new_base)
203*f4a2713aSLionel Sambuc continue;
204*f4a2713aSLionel Sambuc
205*f4a2713aSLionel Sambuc if (prev_base == 0) {
206*f4a2713aSLionel Sambuc // record the final override
207*f4a2713aSLionel Sambuc final_override[s][fn] = new_base;
208*f4a2713aSLionel Sambuc continue;
209*f4a2713aSLionel Sambuc }
210*f4a2713aSLionel Sambuc
211*f4a2713aSLionel Sambuc if (base_present[prev_base][new_base]) {
212*f4a2713aSLionel Sambuc // The previous base dominates the new base, no update necessary
213*f4a2713aSLionel Sambuc printf(" // No override for fun%d in s%d as s%d dominates s%d.\n",
214*f4a2713aSLionel Sambuc fn, s, prev_base, new_base);
215*f4a2713aSLionel Sambuc continue;
216*f4a2713aSLionel Sambuc }
217*f4a2713aSLionel Sambuc
218*f4a2713aSLionel Sambuc if (base_present[new_base][prev_base]) {
219*f4a2713aSLionel Sambuc // The new base dominates the old base, no override necessary
220*f4a2713aSLionel Sambuc printf(" // No override for fun%d in s%d as s%d dominates s%d.\n",
221*f4a2713aSLionel Sambuc fn, s, new_base, prev_base);
222*f4a2713aSLionel Sambuc // record the final override
223*f4a2713aSLionel Sambuc final_override[s][fn] = new_base;
224*f4a2713aSLionel Sambuc continue;
225*f4a2713aSLionel Sambuc }
226*f4a2713aSLionel Sambuc
227*f4a2713aSLionel Sambuc printf(" // Found we needed override for fun%d in s%d.\n", fn, s);
228*f4a2713aSLionel Sambuc
229*f4a2713aSLionel Sambuc // record the final override
230*f4a2713aSLionel Sambuc funcs[n_funcs++] = fn;
231*f4a2713aSLionel Sambuc if (n_funcs == (N_FUNCS*FUNCSPACING-1))
232*f4a2713aSLionel Sambuc abort();
233*f4a2713aSLionel Sambuc int ret_type = 0;
234*f4a2713aSLionel Sambuc if (COVARIANT) {
235*f4a2713aSLionel Sambuc if (!ret_types[s][fn]) {
236*f4a2713aSLionel Sambuc ret_types[s][fn] = ret_type = s;
237*f4a2713aSLionel Sambuc } else {
238*f4a2713aSLionel Sambuc ret_type = ret_types[s][fn];
239*f4a2713aSLionel Sambuc if (ret_type != s)
240*f4a2713aSLionel Sambuc printf(" // Calculated return type in s%d as s%d* fun%d.\n",
241*f4a2713aSLionel Sambuc s, ret_type, fn);
242*f4a2713aSLionel Sambuc }
243*f4a2713aSLionel Sambuc }
244*f4a2713aSLionel Sambuc if (ret_type) {
245*f4a2713aSLionel Sambuc g(" virtual s"); g(ret_type); g("* fun");
246*f4a2713aSLionel Sambuc } else
247*f4a2713aSLionel Sambuc g(" virtual void fun");
248*f4a2713aSLionel Sambuc g(fn); g("(char *t) { mix(\"vfn this offset\", (char *)this - t); mix(\"vfn uuid\", "); g(++uuid);
249*f4a2713aSLionel Sambuc if (ret_type)
250*f4a2713aSLionel Sambuc gl("); return 0; }");
251*f4a2713aSLionel Sambuc else
252*f4a2713aSLionel Sambuc gl("); }");
253*f4a2713aSLionel Sambuc final_override[s][fn] = s;
254*f4a2713aSLionel Sambuc }
255*f4a2713aSLionel Sambuc }
256*f4a2713aSLionel Sambuc
257*f4a2713aSLionel Sambuc gl("public:");
258*f4a2713aSLionel Sambuc gl(" void calc(char *t) {");
259*f4a2713aSLionel Sambuc
260*f4a2713aSLionel Sambuc // mix in the type number
261*f4a2713aSLionel Sambuc g(" mix(\"type num\", "); g(s); gl(");");
262*f4a2713aSLionel Sambuc // mix in the size
263*f4a2713aSLionel Sambuc g(" mix(\"type size\", sizeof (s"); g(s); gl("));");
264*f4a2713aSLionel Sambuc // mix in the this offset
265*f4a2713aSLionel Sambuc gl(" mix(\"subobject offset\", (char *)this - t);");
266*f4a2713aSLionel Sambuc if (n_funcs)
267*f4a2713aSLionel Sambuc polymorphic = true;
268*f4a2713aSLionel Sambuc if (polymorphic) {
269*f4a2713aSLionel Sambuc // mix in offset to the complete object under construction
270*f4a2713aSLionel Sambuc gl(" mix(\"real top v current top\", t - (char *)dynamic_cast<void*>(this));");
271*f4a2713aSLionel Sambuc }
272*f4a2713aSLionel Sambuc
273*f4a2713aSLionel Sambuc /* check base layout and overrides */
274*f4a2713aSLionel Sambuc for (int i = 0; i < n_bases; ++i) {
275*f4a2713aSLionel Sambuc g(" calc_s"); g(bases[i]); gl("(t);");
276*f4a2713aSLionel Sambuc }
277*f4a2713aSLionel Sambuc
278*f4a2713aSLionel Sambuc if (polymorphic) {
279*f4a2713aSLionel Sambuc /* check dynamic_cast to each direct base */
280*f4a2713aSLionel Sambuc for (int i = 0; i < n_bases; ++i) {
281*f4a2713aSLionel Sambuc g(" if ((char *)dynamic_cast<s"); g(bases[i]); gl("*>(this))");
282*f4a2713aSLionel Sambuc g(" mix(\"base dyn cast\", t - (char *)dynamic_cast<s"); g(bases[i]); gl("*>(this));");
283*f4a2713aSLionel Sambuc g(" else mix(\"no dyncast\", "); g(++uuid); gl(");");
284*f4a2713aSLionel Sambuc }
285*f4a2713aSLionel Sambuc }
286*f4a2713aSLionel Sambuc
287*f4a2713aSLionel Sambuc /* check field layout */
288*f4a2713aSLionel Sambuc for (int i = 0; i < n_fields; ++i) {
289*f4a2713aSLionel Sambuc g(" mix(\"field offset\", (char *)&field"); g(i); gl(" - (char *)this);");
290*f4a2713aSLionel Sambuc }
291*f4a2713aSLionel Sambuc if (n_fields == 0) {
292*f4a2713aSLionel Sambuc g(" mix(\"no fields\", "); g(++uuid); gl(");");
293*f4a2713aSLionel Sambuc }
294*f4a2713aSLionel Sambuc
295*f4a2713aSLionel Sambuc /* check functions */
296*f4a2713aSLionel Sambuc for (int i = 0; i < n_funcs; ++i) {
297*f4a2713aSLionel Sambuc g(" fun"); g(funcs[i]); gl("(t);");
298*f4a2713aSLionel Sambuc }
299*f4a2713aSLionel Sambuc if (n_funcs == 0) {
300*f4a2713aSLionel Sambuc g(" mix(\"no funcs\", "); g(++uuid); gl(");");
301*f4a2713aSLionel Sambuc }
302*f4a2713aSLionel Sambuc
303*f4a2713aSLionel Sambuc gl(" }");
304*f4a2713aSLionel Sambuc
305*f4a2713aSLionel Sambuc // default ctor
306*f4a2713aSLionel Sambuc g(" s"); g(s); g("() ");
307*f4a2713aSLionel Sambuc first_base = true;
308*f4a2713aSLionel Sambuc for (int i = 0; i < n_bases; ++i) {
309*f4a2713aSLionel Sambuc if (first_base) {
310*f4a2713aSLionel Sambuc g(": ");
311*f4a2713aSLionel Sambuc first_base = false;
312*f4a2713aSLionel Sambuc } else
313*f4a2713aSLionel Sambuc g(", ");
314*f4a2713aSLionel Sambuc g("s"); g(bases[i]); g("((char *)this)");
315*f4a2713aSLionel Sambuc }
316*f4a2713aSLionel Sambuc gl(" { calc((char *)this); }");
317*f4a2713aSLionel Sambuc g(" ~s"); g(s); gl("() { calc((char *)this); }");
318*f4a2713aSLionel Sambuc
319*f4a2713aSLionel Sambuc // ctor with this to the complete object
320*f4a2713aSLionel Sambuc g(" s"); g(s); gl("(char *t) { calc(t); }");
321*f4a2713aSLionel Sambuc g(" void calc_s"); g(s); gl("(char *t) { calc(t); }");
322*f4a2713aSLionel Sambuc g("} a"); g(s); gl(";");
323*f4a2713aSLionel Sambuc }
324*f4a2713aSLionel Sambuc
main(int argc,char ** argv)325*f4a2713aSLionel Sambuc main(int argc, char **argv) {
326*f4a2713aSLionel Sambuc unsigned seed = 0;
327*f4a2713aSLionel Sambuc char state[16];
328*f4a2713aSLionel Sambuc if (argc > 1)
329*f4a2713aSLionel Sambuc seed = atol(argv[1]);
330*f4a2713aSLionel Sambuc
331*f4a2713aSLionel Sambuc initstate(seed, state, sizeof(state));
332*f4a2713aSLionel Sambuc gl("extern \"C\" int printf(const char *...);");
333*f4a2713aSLionel Sambuc gl("");
334*f4a2713aSLionel Sambuc gl("long long sum;");
335*f4a2713aSLionel Sambuc gl("void mix(const char *desc, long long i) {");
336*f4a2713aSLionel Sambuc // If this ever becomes too slow, we can remove this after we improve the
337*f4a2713aSLionel Sambuc // mixing function
338*f4a2713aSLionel Sambuc gl(" printf(\"%s: %lld\\n\", desc, i);");
339*f4a2713aSLionel Sambuc gl(" sum += ((sum ^ i) << 3) + (sum<<1) - i;");
340*f4a2713aSLionel Sambuc gl("}");
341*f4a2713aSLionel Sambuc gl("");
342*f4a2713aSLionel Sambuc // PARAM: Randomly size testcases or large testcases?
343*f4a2713aSLionel Sambuc int n_structs = /* random() % */ N_STRUCTS;
344*f4a2713aSLionel Sambuc for (int i = 1; i < n_structs; ++i)
345*f4a2713aSLionel Sambuc gs(i);
346*f4a2713aSLionel Sambuc gl("int main() {");
347*f4a2713aSLionel Sambuc gl(" printf(\"%llx\\n\", sum);");
348*f4a2713aSLionel Sambuc gl("}");
349*f4a2713aSLionel Sambuc return 0;
350*f4a2713aSLionel Sambuc }
351