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