Debian Codegraph @

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

raw
1 // REQUIRED_ARGS: -unittest
2
3 import std.algorithm: cmp;
4
5 extern(C) int printf(const char*, ...);
6
7 /* ================================ */
8
9 class Foo
10 {
11 int foo(int x) { return x + 3; }
12 }
13
14 class Bar : Foo
15 {
16 override int foo(int y) { return y + 4; }
17 }
18
19 void test1()
20 {
21 Bar e;
22
23 assert(e is null);
24 e = new Bar();
25 assert(e.foo(5) == 9);
26 }
27
28 /* ================================ */
29
30 class Foo2
31 {
32 int foo(int x)
33 {
34 return x + 3;
35 }
36 }
37
38 class Bar2 : Foo2
39 {
40 override int foo(int y)
41 {
42 assert(Foo2.foo(2) == 5);
43 return y + 4;
44 }
45 }
46
47 void test2()
48 {
49 Bar2 e;
50
51 assert(e is null);
52 e = new Bar2();
53 assert(e.foo(5) == 9);
54 assert(e.Foo2.foo(10) == 13);
55 }
56
57 /* ================================ */
58
59 void test3()
60 {
61 debug printf("debug\n");
62 debug(bar) printf("debug(bar)\n");
63 }
64
65 /* ================================ */
66
67 int x1;
68 int x2;
69
70 class Foo4
71 {
72 static this() { x1 = 3; printf("Foo4 ctor()\n"); }
73 static ~this() { x1 = 4; printf("Foo4 dtor()\n"); }
74 }
75
76 static this() { x2 = 5; printf("ctor()\n"); }
77 static ~this() { x2 = 6; printf("dtor()\n"); }
78
79 void test4()
80 {
81 printf("x1 = %d, x2 = %d\n", x1, x2);
82 assert(x1 == 3);
83 assert(x2 == 5);
84 }
85
86 /* ================================ */
87
88 void test5()
89 {
90 version (D_Bits)
91 {
92 printf("test5()\n");
93 static uint foo;
94 static uint x = 3;
95 static uint len = 32;
96
97 bool[] bools;
98
99 bools = (cast(bool *)&foo)[0..len];
100 bools[6] = true;
101 assert(foo == (1 << 6));
102 }
103 }
104
105 /* ================================ */
106
107 int[] test6_1(int[] a)
108 {
109 a.length = 6;
110 return a;
111 }
112
113 void test6()
114 {
115 printf("test6()\n");
116 int[3] b;
117 int[] a;
118
119 b[0] = 0;
120 b[1] = 1;
121 b[2] = 2;
122 assert(b.length == 3);
123 a = test6_1(b);
124 a[2] = 2;
125 assert(a.length == 6);
126 }
127
128 /* ================================ */
129
130 class OutBuffer7
131 {
132 char[] data;
133 uint offset;
134
135 void write(const(char) *p, uint nbytes)
136 {
137 data[offset .. offset + nbytes] = (cast(char *)p)[0 .. nbytes];
138 }
139 }
140
141
142 void test7()
143 {
144 printf("test7()\n");
145 int i;
146 OutBuffer7 ob = new OutBuffer7;
147
148 ob.data = new char[10];
149 printf("ob.data.length = %zd\n", ob.data.length);
150 assert(ob.data.length == 10);
151 for (i = 0; i < 10; i++)
152 assert(ob.data[i] == char.init);
153
154 printf("test7.1()\n");
155 ob.data[] = '-';
156 printf("test7.2()\n");
157 printf("ob.data[] = '%.*s'\n", cast(int)ob.data.length, ob.data.ptr);
158 for (i = 0; i < 10; i++)
159 assert(ob.data[i] == '-');
160
161 ob.offset = 3;
162 ob.write("foo", 3);
163 printf("ob.data.length = %zd\n", ob.data.length);
164 printf("ob.data[] = '%.*s'\n", cast(int)ob.data.length, ob.data.ptr);
165 for (i = 0; i < 10; i++)
166 {
167 if (i < 3 || i >= 6)
168 assert(ob.data[i] == '-');
169 }
170 assert(ob.data[3] == 'f');
171 assert(ob.data[4] == 'o');
172 assert(ob.data[5] == 'o');
173 }
174
175 /* ================================ */
176
177 class A8
178 {
179 enum { bar = 8, baz }
180 int foo;
181 }
182
183 void test8()
184 {
185 printf("test8()\n");
186 A8 a;
187 a = new A8();
188 a.foo = A8.bar;
189 assert(a.foo == 8);
190 }
191
192 /* ================================ */
193
194
195 int z9;
196
197 unittest
198 {
199 printf("module unittest 9\n");
200 z9 = 3;
201 }
202
203
204 void test9()
205 {
206 assert(z9 == 3);
207 }
208
209 /* ================================ */
210
211 void test10()
212 {
213 printf("test10()\n");
214 const int i = 8000;
215 assert(i == 8000);
216 static int j = 78;
217 assert(j == 78);
218 }
219
220 /* ================================ */
221
222 Object test11_a()
223 {
224 return null;
225 }
226
227 void test11()
228 {
229 assert(test11_a() is null);
230 }
231
232 /* ================================ */
233
234 class A12 { }
235 class B12 { }
236
237 int testx(A12 a) { return 1; }
238
239 int testx(B12 b) { return 2; }
240
241 void test12()
242 {
243 A12 a = new A12();
244 B12 b = new B12();
245
246 assert(testx(a) == 1);
247 assert(testx(b) == 2);
248 }
249
250 /* ================================ */
251
252 char[] tolower13(ref char[] s)
253 {
254 int i;
255
256 for (i = 0; i < s.length; i++)
257 {
258 char c = s[i];
259 if ('A' <= c && c <= 'Z')
260 s[i] = cast(char)(c + (cast(char)'a' - 'A'));
261 }
262 return s;
263 }
264
265 void test13()
266 {
267 char[] s1 = "FoL".dup;
268 char[] s2;
269
270 s1 = s1.dup;
271 s2 = tolower13(s1);
272 assert(cmp(s2, "fol") == 0);
273 assert(s2 == s1);
274 }
275
276 /* ================================ */
277
278 alias ABC14* LPABC14;
279 class ABC14 { }
280
281 alias DEF14* LPDEF14;
282 DEF14[3] foo;
283 struct DEF14 { int x; }
284
285 void test14()
286 {
287 assert(foo.sizeof == int.sizeof * 3);
288 }
289
290 /* ================================ */
291
292 class bools15
293 {
294 bool a = true, b = true, c = true;
295 void dump()
296 {
297 printf("%d %d %d\n", a, b, c);
298 }
299 }
300
301 void test15()
302 {
303 bools15 k = new bools15;
304 k.a = true; k.dump();
305 k.b = true; k.dump();
306 k.c = true; k.dump();
307 assert(k.a == true);
308 assert(k.b == true);
309 assert(k.c == true);
310 }
311
312
313 /* ================================ */
314
315 align(4) struct foo16
316 {
317 short s;
318 int i;
319 }
320
321 void test16()
322 {
323 assert(foo16.sizeof == 8);
324 }
325
326 /* ================================ */
327
328 enum Color { red, blue, green };
329 int[Color.max+1] colors1 = [ Color.blue:6, Color.green:2, Color.red:5 ];
330
331
332 enum { red, blue, green };
333 int[3] colors2 = [ blue:6, green:2, red:5 ];
334
335 void test17()
336 {
337 assert(colors1.length == 3);
338 assert(colors1[0] == 5);
339 assert(colors1[1] == 6);
340 assert(colors1[2] == 2);
341 assert(colors2[0] == 5);
342 assert(colors2[1] == 6);
343 assert(colors2[2] == 2);
344 }
345
346 /* ================================ */
347
348
349 alias void* HANDLE18;
350
351 HANDLE18 testx18()
352 {
353 return null;
354 }
355
356 void test18()
357 {
358 assert(testx18() is null);
359 }
360
361 /* ================================ */
362
363 class Test19 { struct { int a, b, c; } }
364
365 void test19()
366 {
367 Test19 t = new Test19();
368
369 t.a = 3;
370 assert(t.a == 3);
371 }
372
373 /* ================================ */
374
375 bool tested20;
376
377 struct S20
378 {
379 unittest
380 {
381 assert(!tested20);
382 tested20 = true;
383 }
384 }
385
386 void test20()
387 {
388 assert(tested20);
389 }
390
391 /* ================================ */
392 // https://issues.dlang.org/show_bug.cgi?id=7848
393
394 @safe pure nothrow void func7848() {}
395
396 @safe pure nothrow unittest
397 {
398 func7848();
399 }
400
401 /* ================================ */
402 // https://issues.dlang.org/show_bug.cgi?id=8128
403
404 int flag8128 = 0;
405
406 interface I8128
407 {
408 unittest
409 {
410 printf("utest, flag8128 = %d\n", flag8128);
411 flag8128 = 1;
412 }
413 }
414
415 void test8128()
416 {
417 printf("main, flag8128 = %d\n", flag8128);
418 assert(flag8128 == 1);
419 }
420
421 /* ================================ */
422
423 class C8635{
424 int x;
425 this(int x)
426 {
427 this.x = x;
428 }
429 }
430 void test8635()
431 {
432 assert(new C8635(2).x==2);
433 assert(new C8635(3).x==3);
434 }
435
436 /* ================================ */
437
438
439 int main()
440 {
441 test1();
442 test2();
443 test3();
444 test4();
445 test5();
446 test6();
447 test7();
448 test8();
449 test9();
450 test10();
451 test11();
452 test12();
453 test13();
454 test14();
455 test15();
456 test16();
457 test17();
458 test18();
459 test19();
460 test20();
461 test8128();
462 test8635();
463 printf("Success\n");
464 return 0;
465 }