1 /*
2 RUNNABLE_PHOBOS_TEST
3 REQUIRED_ARGS: -mcpu=native
4 PERMUTE_ARGS: -O -inline
5 */
6
7 import core.stdc.stdio;
8
testgoto()9 void testgoto()
10 {
11 int i;
12
13 i = 3;
14 goto L4;
15 L3: i++;
16 goto L5;
17 L4: goto L3;
18 L5: assert(i == 4);
19 }
20
testswitch()21 int testswitch()
22 {
23 int i;
24
25 i = 3;
26 switch (i)
27 {
28 case 0:
29 case 1:
30 default:
31 assert(0);
32 case 3:
33 break;
34 }
35 return 0;
36 }
37
testdo()38 void testdo()
39 {
40 int x = 0;
41
42 do
43 {
44 x++;
45 } while (x < 10);
46 printf("x == %d\n", x);
47 assert(x == 10);
48 }
49
50
testbreak()51 void testbreak()
52 { int i, j;
53
54 Louter:
55 for (i = 0; i < 10; i++)
56 {
57 for (j = 0; j < 10; j++)
58 {
59 if (j == 3)
60 break Louter;
61 }
62 }
63
64 printf("i = %d, j = %d\n", i, j);
65 assert(i == 0);
66 assert(j == 3);
67 }
68
69 ///////////////////////
70
foo(string s)71 int foo(string s)
72 {
73 int i;
74
75 i = 0;
76 switch (s)
77 {
78 case "hello":
79 i = 1;
80 break;
81 case "goodbye":
82 i = 2;
83 break;
84 case "goodb":
85 i = 3;
86 break;
87 default:
88 i = 10;
89 break;
90 }
91 return i;
92 }
93
94
teststringswitch()95 void teststringswitch()
96 { int i;
97
98 i = foo("hello");
99 printf("i = %d\n", i);
100 assert(i == 1);
101
102 i = foo("goodbye");
103 printf("i = %d\n", i);
104 assert(i == 2);
105
106 i = foo("goodb");
107 printf("i = %d\n", i);
108 assert(i == 3);
109
110 i = foo("huzzah");
111 printf("i = %d\n", i);
112 assert(i == 10);
113 }
114
115
116 ///////////////////////
117
118 struct Foo
119 {
120 int a;
121 char b;
122 long c;
123 }
124
test(Foo f)125 Foo test(Foo f)
126 {
127 f.a += 1;
128 f.b += 3;
129 f.c += 4;
130 return f;
131 }
132
133
teststrarg()134 void teststrarg()
135 {
136 Foo g;
137 g.a = 1;
138 g.b = 2;
139 g.c = 3;
140
141 Foo q;
142 q = test(g);
143 assert(q.a == 2);
144 assert(q.b == 5);
145 assert(q.c == 7);
146 }
147
148 ///////////////////////
149
150 align (1) struct Foo1
151 {
152 align (1):
153 int a;
154 char b;
155 long c;
156 }
157
158 struct Foo2
159 {
160 int a;
161 char b;
162 long c;
163 }
164
165 struct Foo3
166 {
167 int a;
168 align (1) char b;
169 long c;
170 }
171
172 struct Foo4
173 {
174 int a;
175 struct { char b; }
176 long c;
177 }
178
testsizes()179 void testsizes()
180 {
181 printf("%d\n", Foo1.sizeof);
182 assert(Foo1.a.offsetof == 0);
183 assert(Foo1.b.offsetof == 4);
184 assert(Foo1.c.offsetof == 5);
185 assert(Foo1.sizeof == 13);
186
187 assert(Foo2.a.offsetof == 0);
188 assert(Foo2.b.offsetof == 4);
189 assert(Foo2.c.offsetof == 8);
190 assert(Foo2.sizeof == 16);
191
192 assert(Foo3.a.offsetof == 0);
193 assert(Foo3.b.offsetof == 4);
194 assert(Foo3.c.offsetof == 8);
195 assert(Foo3.b.sizeof == 1);
196 assert(Foo3.sizeof == 16);
197
198 assert(Foo4.sizeof == 16);
199 }
200
201 ///////////////////////
202
cond11565(size_t val)203 size_t cond11565(size_t val)
204 {
205 return val ? size_t.max : 0;
206 }
207
test11565()208 void test11565()
209 {
210 assert(cond11565(true) == size_t.max);
211 }
212
213 ///////////////////////
214
215 int[3] array1 = [1:1,2,0:3];
216
testarrayinit()217 void testarrayinit()
218 {
219 assert(array1[0] == 3);
220 assert(array1[1] == 1);
221 assert(array1[2] == 2);
222 }
223
224 ///////////////////////
225
test13023(ulong n)226 void test13023(ulong n)
227 {
228 static void func(bool b) {}
229
230 ulong k = 0;
231
232 func(k >= n / 2);
233
234 if (k >= n / 2)
235 assert(0);
236 }
237
238 ///////////////////////
239
240 struct U { int a; union { char c; int d; } long b; }
241
242 U f = { b:3, d:0x22222222, a:1 };
243
testU()244 void testU()
245 {
246 assert(f.b == 3);
247 assert(f.d == 0x22222222);
248 assert(f.c == 0x22);
249 assert(f.a == 1);
250 assert(f.sizeof == 16);
251 assert(U.sizeof == 16);
252 }
253
254
255 ///////////////////////
256
testulldiv()257 void testulldiv()
258 {
259 __gshared ulong[4][] vectors =
260 [
261 [10,3,3,1],
262 [10,1,10,0],
263 [3,10,0,3],
264 [10,10,1,0],
265 [10_000_000_000L, 11_000_000_000L, 0, 10_000_000_000L],
266 [11_000_000_000L, 10_000_000_000L, 1, 1_000_000_000L],
267 [11_000_000_000L, 11_000_000_000L, 1, 0],
268 [10_000_000_000L, 10, 1_000_000_000L, 0],
269 [0x8000_0000_0000_0000, 0x8000_0000_0000_0000, 1, 0],
270 [0x8000_0000_0000_0001, 0x8000_0000_0000_0001, 1, 0],
271 [0x8000_0001_0000_0000, 0x8000_0001_0000_0000, 1, 0],
272 [0x8000_0001_0000_0000, 0x8000_0000_0000_0000, 1, 0x1_0000_0000],
273 [0x8000_0001_0000_0000, 0x8000_0000_8000_0000, 1, 0x8000_0000],
274 [0x8000_0000_0000_0000, 0x7FFF_FFFF_FFFF_FFFF, 1, 1],
275 [0x8000_0000_0000_0000, 0x8000_0000_0000_0001, 0, 0x8000_0000_0000_0000],
276 [0x8000_0000_0000_0000, 0x8000_0001_0000_0000, 0, 0x8000_0000_0000_0000],
277 ];
278
279 for (size_t i = 0; i < vectors.length; i++)
280 {
281 ulong q = vectors[i][0] / vectors[i][1];
282 if (q != vectors[i][2])
283 printf("[%d] %lld / %lld = %lld, should be %lld\n",
284 vectors[i][0], vectors[i][1], q, vectors[i][2]);
285
286 ulong r = vectors[i][0] % vectors[i][1];
287 if (r != vectors[i][3])
288 printf("[%d] %lld %% %lld = %lld, should be %lld\n",
289 i, vectors[i][0], vectors[i][1], r, vectors[i][3]);
290 }
291 }
292
293 ////////////////////////////////////////////////////////////////////////
294
295
udiv10(uint x)296 uint udiv10(uint x)
297 {
298 return x / 10;
299 }
300
udiv14(uint x)301 uint udiv14(uint x)
302 {
303 return x / 14;
304 }
305
udiv14007(uint x)306 uint udiv14007(uint x)
307 {
308 return x / 14007;
309 }
310
umod10(uint x)311 uint umod10(uint x)
312 {
313 return x % 10;
314 }
315
umod14(uint x)316 uint umod14(uint x)
317 {
318 return x % 14;
319 }
320
umod14007(uint x)321 uint umod14007(uint x)
322 {
323 return x % 14007;
324 }
325
uremquo10(uint x)326 uint uremquo10(uint x)
327 {
328 return (x / 10) | (x % 10);
329 }
330
uremquo14(uint x)331 uint uremquo14(uint x)
332 {
333 return (x / 14) | (x % 14);
334 }
335
uremquo14007(uint x)336 uint uremquo14007(uint x)
337 {
338 return (x / 14007) | (x % 14007);
339 }
340
341
342
uldiv10(ulong x)343 ulong uldiv10(ulong x)
344 {
345 return x / 10;
346 }
347
uldiv14(ulong x)348 ulong uldiv14(ulong x)
349 {
350 return x / 14;
351 }
352
uldiv14007(ulong x)353 ulong uldiv14007(ulong x)
354 {
355 return x / 14007;
356 }
357
ulmod10(ulong x)358 ulong ulmod10(ulong x)
359 {
360 return x % 10;
361 }
362
ulmod14(ulong x)363 ulong ulmod14(ulong x)
364 {
365 return x % 14;
366 }
367
ulmod14007(ulong x)368 ulong ulmod14007(ulong x)
369 {
370 return x % 14007;
371 }
372
ulremquo10(ulong x)373 ulong ulremquo10(ulong x)
374 {
375 return (x / 10) | (x % 10);
376 }
377
ulremquo14(ulong x)378 ulong ulremquo14(ulong x)
379 {
380 return (x / 14) | (x % 14);
381 }
382
ulremquo14007(ulong x)383 ulong ulremquo14007(ulong x)
384 {
385 return (x / 14007) | (x % 14007);
386 }
387
388
testfastudiv()389 void testfastudiv()
390 {
391 {
392 static uint x10 = 10;
393 static uint x14 = 14;
394 static uint x14007 = 14007;
395
396 uint u = 10000;
397 uint r;
398 r = udiv10(u); assert(r == u/x10);
399 r = udiv14(u); assert(r == u/x14);
400 r = udiv14007(u); assert(r == u/x14007);
401 r = umod10(u); assert(r == u%x10);
402 r = umod14(u); assert(r == u%x14);
403 r = umod14007(u); assert(r == u%x14007);
404 r = uremquo10(u); assert(r == ((u/10)|(u%x10)));
405 r = uremquo14(u); assert(r == ((u/14)|(u%x14)));
406 r = uremquo14007(u); assert(r == ((u/14007)|(u%x14007)));
407 }
408 {
409 static ulong y10 = 10;
410 static ulong y14 = 14;
411 static ulong y14007 = 14007;
412
413 ulong u = 10000;
414 ulong r;
415 r = uldiv10(u); assert(r == u/y10);
416 r = uldiv14(u); assert(r == u/y14);
417 r = uldiv14007(u); assert(r == u/y14007);
418 r = ulmod10(u); assert(r == u%y10);
419 r = ulmod14(u); assert(r == u%y14);
420 r = ulmod14007(u); assert(r == u%y14007);
421 r = ulremquo10(u); assert(r == ((u/10)|(u%y10)));
422 r = ulremquo14(u); assert(r == ((u/14)|(u%y14)));
423 r = ulremquo14007(u); assert(r == ((u/14007)|(u%y14007)));
424 }
425 }
426
427
428 ////////////////////////////////////////////////////////////////////////
429
vfunc()430 void vfunc() {}
431
test12095(int k)432 void test12095(int k)
433 {
434 int e = 0;
435 e ? k || assert(0) : !e || vfunc();
436 e ? k || assert(0) : e && vfunc();
437 !e ? !e || vfunc() : k || assert(0);
438 }
439
440
441 ////////////////////////////////////////////////////////////////////////
442
443
test3918a(float t,real u)444 bool test3918a( float t, real u )
445 {
446 printf("%f\n", u );
447 return t && u;
448 }
449
test3918b(real t,float u)450 bool test3918b( real t, float u )
451 {
452 printf("%f\n", t );
453 return t && u;
454 }
455
test3918()456 void test3918()
457 {
458 assert(test3918a(float.nan, real.nan));
459 assert(test3918b(real.nan, float.nan));
460 }
461
462 ////////////////////////////////////////////////////////////////////////
463
464
div10(int x)465 int div10(int x)
466 {
467 return x / 10;
468 }
469
div14(int x)470 int div14(int x)
471 {
472 return x / 14;
473 }
474
div14007(int x)475 int div14007(int x)
476 {
477 return x / 14007;
478 }
479
mod10(int x)480 int mod10(int x)
481 {
482 return x % 10;
483 }
484
mod14(int x)485 int mod14(int x)
486 {
487 return x % 14;
488 }
489
mod14007(int x)490 int mod14007(int x)
491 {
492 return x % 14007;
493 }
494
remquo10(int x)495 int remquo10(int x)
496 {
497 return (x / 10) | (x % 10);
498 }
499
remquo14(int x)500 int remquo14(int x)
501 {
502 return (x / 14) | (x % 14);
503 }
504
remquo14007(int x)505 int remquo14007(int x)
506 {
507 return (x / 14007) | (x % 14007);
508 }
509
510 ////////////////////
511
mdiv10(int x)512 int mdiv10(int x)
513 {
514 return x / -10;
515 }
516
mdiv14(int x)517 int mdiv14(int x)
518 {
519 return x / -14;
520 }
521
mdiv14007(int x)522 int mdiv14007(int x)
523 {
524 return x / -14007;
525 }
526
mmod10(int x)527 int mmod10(int x)
528 {
529 return x % -10;
530 }
531
mmod14(int x)532 int mmod14(int x)
533 {
534 return x % -14;
535 }
536
mmod14007(int x)537 int mmod14007(int x)
538 {
539 return x % -14007;
540 }
541
mremquo10(int x)542 int mremquo10(int x)
543 {
544 return (x / -10) | (x % -10);
545 }
546
mremquo14(int x)547 int mremquo14(int x)
548 {
549 return (x / -14) | (x % -14);
550 }
551
mremquo14007(int x)552 int mremquo14007(int x)
553 {
554 return (x / -14007) | (x % -14007);
555 }
556
557 ////////////////////
558
559
ldiv10(long x)560 long ldiv10(long x)
561 {
562 return x / 10;
563 }
564
ldiv14(long x)565 long ldiv14(long x)
566 {
567 return x / 14;
568 }
569
ldiv14007(long x)570 long ldiv14007(long x)
571 {
572 return x / 14007;
573 }
574
lmod10(long x)575 long lmod10(long x)
576 {
577 return x % 10;
578 }
579
lmod14(long x)580 long lmod14(long x)
581 {
582 return x % 14;
583 }
584
lmod14007(long x)585 long lmod14007(long x)
586 {
587 return x % 14007;
588 }
589
lremquo10(long x)590 long lremquo10(long x)
591 {
592 return (x / 10) | (x % 10);
593 }
594
lremquo14(long x)595 long lremquo14(long x)
596 {
597 return (x / 14) | (x % 14);
598 }
599
lremquo14007(long x)600 long lremquo14007(long x)
601 {
602 return (x / 14007) | (x % 14007);
603 }
604
605
606 ////////////////////
607
608
mldiv10(long x)609 long mldiv10(long x)
610 {
611 return x / -10;
612 }
613
mldiv14(long x)614 long mldiv14(long x)
615 {
616 return x / -14;
617 }
618
mldiv14007(long x)619 long mldiv14007(long x)
620 {
621 return x / -14007;
622 }
623
mlmod10(long x)624 long mlmod10(long x)
625 {
626 return x % -10;
627 }
628
mlmod14(long x)629 long mlmod14(long x)
630 {
631 return x % -14;
632 }
633
mlmod14007(long x)634 long mlmod14007(long x)
635 {
636 return x % -14007;
637 }
638
mlremquo10(long x)639 long mlremquo10(long x)
640 {
641 return (x / -10) | (x % -10);
642 }
643
mlremquo14(long x)644 long mlremquo14(long x)
645 {
646 return (x / -14) | (x % -14);
647 }
648
mlremquo14007(long x)649 long mlremquo14007(long x)
650 {
651 return (x / -14007) | (x % -14007);
652 }
653
654
655
testfastdiv()656 void testfastdiv()
657 {
658 {
659 static int x10 = 10;
660 static int x14 = 14;
661 static int x14007 = 14007;
662
663 int u = 10000;
664 int r;
665 r = div10(u); assert(r == u/x10);
666 r = div14(u); assert(r == u/x14);
667 r = div14007(u); assert(r == u/x14007);
668 r = mod10(u); assert(r == u%x10);
669 r = mod14(u); assert(r == u%x14);
670 r = mod14007(u); assert(r == u%x14007);
671 r = remquo10(u); assert(r == ((u/x10)|(u%x10)));
672 r = remquo14(u); assert(r == ((u/x14)|(u%x14)));
673 r = remquo14007(u); assert(r == ((u/x14007)|(u%x14007)));
674 }
675 {
676 static int t10 = -10;
677 static int t14 = -14;
678 static int t14007 = -14007;
679
680 int u = 10000;
681 int r;
682 r = mdiv10(u); assert(r == u/t10);
683 r = mdiv14(u); assert(r == u/t14);
684 r = mdiv14007(u); assert(r == u/t14007);
685 r = mmod10(u); assert(r == u%t10);
686 r = mmod14(u); assert(r == u%t14);
687 r = mmod14007(u); assert(r == u%t14007);
688 r = mremquo10(u); assert(r == ((u/t10)|(u%t10)));
689 r = mremquo14(u); assert(r == ((u/t14)|(u%t14)));
690 r = mremquo14007(u); assert(r == ((u/t14007)|(u%t14007)));
691 }
692 {
693 static long y10 = 10;
694 static long y14 = 14;
695 static long y14007 = 14007;
696
697 long u = 10000;
698 long r;
699 r = ldiv10(u); assert(r == u/y10);
700 r = ldiv14(u); assert(r == u/y14);
701 r = ldiv14007(u); assert(r == u/y14007);
702 r = lmod10(u); assert(r == u%y10);
703 r = lmod14(u); assert(r == u%y14);
704 r = lmod14007(u); assert(r == u%y14007);
705 r = lremquo10(u); assert(r == ((u/y10)|(u%y10)));
706 r = lremquo14(u); assert(r == ((u/y14)|(u%y14)));
707 r = lremquo14007(u); assert(r == ((u/y14007)|(u%y14007)));
708 }
709 {
710 static long z10 = -10;
711 static long z14 = -14;
712 static long z14007 = -14007;
713
714 long u = 10000;
715 long r;
716 r = mldiv10(u); assert(r == u/z10);
717 r = mldiv14(u); assert(r == u/z14);
718 r = mldiv14007(u); assert(r == u/z14007);
719 r = mlmod10(u); assert(r == u%z10);
720 r = mlmod14(u); assert(r == u%z14);
721 r = mlmod14007(u); assert(r == u%z14007);
722 r = mlremquo10(u); assert(r == ((u/z10)|(u%z10)));
723 r = mlremquo14(u); assert(r == ((u/z14)|(u%z14)));
724 r = mlremquo14007(u); assert(r == ((u/z14007)|(u%z14007)));
725 }
726 }
727
728 ////////////////////////////////////////////////////////////////////////
729
730
docond1(T)731 T docond1(T)(T l, ubyte thresh, ubyte val) {
732 l += (thresh < val);
733 return l;
734 }
735
docond2(T)736 T docond2(T)(T l, ubyte thresh, ubyte val) {
737 l -= (thresh >= val);
738 return l;
739 }
740
docond3(T)741 T docond3(T)(T l, ubyte thresh, ubyte val) {
742 l += (thresh >= val);
743 return l;
744 }
745
docond4(T)746 T docond4(T)(T l, ubyte thresh, ubyte val) {
747 l -= (thresh < val);
748 return l;
749 }
750
testdocond()751 void testdocond()
752 {
753 assert(docond1!ubyte(10,3,5) == 11);
754 assert(docond1!ushort(10,3,5) == 11);
755 assert(docond1!uint(10,3,5) == 11);
756 assert(docond1!ulong(10,3,5) == 11);
757
758 assert(docond2!ubyte(10,3,5) == 10);
759 assert(docond2!ushort(10,3,5) == 10);
760 assert(docond2!uint(10,3,5) == 10);
761 assert(docond2!ulong(10,3,5) == 10);
762
763 assert(docond3!ubyte(10,3,5) == 10);
764 assert(docond3!ushort(10,3,5) == 10);
765 assert(docond3!uint(10,3,5) == 10);
766 assert(docond3!ulong(10,3,5) == 10);
767
768 assert(docond4!ubyte(10,3,5) == 9);
769 assert(docond4!ushort(10,3,5) == 9);
770 assert(docond4!uint(10,3,5) == 9);
771 assert(docond4!ulong(10,3,5) == 9);
772
773
774 assert(docond1!ubyte(10,5,3) == 10);
775 assert(docond1!ushort(10,5,3) == 10);
776 assert(docond1!uint(10,5,3) == 10);
777 assert(docond1!ulong(10,5,3) == 10);
778
779 assert(docond2!ubyte(10,5,3) == 9);
780 assert(docond2!ushort(10,5,3) == 9);
781 assert(docond2!uint(10,5,3) == 9);
782 assert(docond2!ulong(10,5,3) == 9);
783
784 assert(docond3!ubyte(10,5,3) == 11);
785 assert(docond3!ushort(10,5,3) == 11);
786 assert(docond3!uint(10,5,3) == 11);
787 assert(docond3!ulong(10,5,3) == 11);
788
789 assert(docond4!ubyte(10,5,3) == 10);
790 assert(docond4!ushort(10,5,3) == 10);
791 assert(docond4!uint(10,5,3) == 10);
792 assert(docond4!ulong(10,5,3) == 10);
793 }
794
795 ////////////////////////////////////////////////////////////////////////
796
797 struct S8658
798 {
799 int[16385] a;
800 }
801
foo8658(S8658 s)802 void foo8658(S8658 s)
803 {
804 int x;
805 }
806
test8658()807 void test8658()
808 {
809 S8658 s;
810 for(int i = 0; i < 1000; i++)
811 foo8658(s);
812 }
813
814 ////////////////////////////////////////////////////////////////////////
815
neg(uint i)816 uint neg(uint i)
817 {
818 return ~i + 1;
819 }
820
com(uint i)821 uint com(uint i)
822 {
823 return -i - 1;
824 }
825
com(float i)826 float com(float i)
827 {
828 return -i - 1;
829 }
830
com2(uint i)831 uint com2(uint i)
832 {
833 return -(i + 1);
834 }
835
testnegcom()836 void testnegcom()
837 {
838 assert(neg(3) == -3);
839 assert(com(3) == -4);
840 assert(com(3.0f) == -4.0f);
841 assert(com2(3) == -4);
842 }
843
844 ////////////////////////////////////////////////////////////////////////
845
oror1(char c)846 int oror1(char c)
847 {
848 return ((((((((((cast(int) c <= 32 || cast(int) c == 46) || cast(int) c == 44)
849 || cast(int) c == 58) || cast(int) c == 59) || cast(int) c == 60)
850 || cast(int) c == 62) || cast(int) c == 34) || cast(int) c == 92)
851 || cast(int) c == 39) != 0);
852 }
853
oror2(char c)854 int oror2(char c)
855 {
856 return ((((((((((c <= 32 || c == 46) || c == 44)
857 || c == 58) || c == 59) || c == 60)
858 || c == 62) || c == 34) || c == 92)
859 || c == 39) != 0);
860 }
861
testoror()862 void testoror()
863 {
864 assert(oror1(0) == 1);
865 assert(oror1(32) == 1);
866 assert(oror1(46) == 1);
867 assert(oror1(44) == 1);
868 assert(oror1(58) == 1);
869 assert(oror1(59) == 1);
870 assert(oror1(60) == 1);
871 assert(oror1(62) == 1);
872 assert(oror1(34) == 1);
873 assert(oror1(92) == 1);
874 assert(oror1(39) == 1);
875 assert(oror1(33) == 0);
876 assert(oror1(61) == 0);
877 assert(oror1(93) == 0);
878 assert(oror1(255) == 0);
879
880 assert(oror2(0) == 1);
881 assert(oror2(32) == 1);
882 assert(oror2(46) == 1);
883 assert(oror2(44) == 1);
884 assert(oror2(58) == 1);
885 assert(oror2(59) == 1);
886 assert(oror2(60) == 1);
887 assert(oror2(62) == 1);
888 assert(oror2(34) == 1);
889 assert(oror2(92) == 1);
890 assert(oror2(39) == 1);
891 assert(oror2(33) == 0);
892 assert(oror2(61) == 0);
893 assert(oror2(93) == 0);
894 assert(oror2(255) == 0);
895 }
896
897 ////////////////////////////////////////////////////////////////////////
898
bt1(int p,int a,int b)899 bool bt1(int p, int a, int b)
900 {
901 return p && ((1 << b) & a);
902 }
903
bt2(int p,long a,long b)904 bool bt2(int p, long a, long b)
905 {
906 return p && ((1L << b) & a);
907 }
908
testbt()909 void testbt()
910 {
911 assert(bt1(1,7,2) == 1);
912 assert(bt1(1,7,3) == 0);
913
914 assert(bt2(1,0x7_0000_0000,2+32) == 1);
915 assert(bt2(1,0x7_0000_0000,3+32) == 0);
916 }
917
918 ////////////////////////////////////////////////////////////////////////
919
test13383()920 void test13383()
921 {
922 foreach (k; 32..33)
923 {
924 if (1L & (1L << k))
925 {
926 assert(0);
927 }
928 }
929 }
930
931 ////////////////////////////////////////////////////////////////////////
932
andand1(int c)933 int andand1(int c)
934 {
935 return (c > 32 && c != 46 && c != 44
936 && c != 58 && c != 59
937 && c != 60 && c != 62
938 && c != 34 && c != 92
939 && c != 39) != 0;
940 }
941
andand2(long c)942 bool andand2(long c)
943 {
944 return (c > 32 && c != 46 && c != 44
945 && c != 58 && c != 59
946 && c != 60 && c != 62
947 && c != 34 && c != 92
948 && c != 39) != 0;
949 }
950
foox3()951 int foox3() { return 1; }
952
andand3(uint op)953 int andand3(uint op)
954 {
955 if (foox3() &&
956 op != 7 &&
957 op != 3 &&
958 op != 18 &&
959 op != 30 &&
960 foox3())
961 return 3;
962 return 4;
963 }
964
965
testandand()966 void testandand()
967 {
968 assert(andand1(0) == 0);
969 assert(andand1(32) == 0);
970 assert(andand1(46) == 0);
971 assert(andand1(44) == 0);
972 assert(andand1(58) == 0);
973 assert(andand1(59) == 0);
974 assert(andand1(60) == 0);
975 assert(andand1(62) == 0);
976 assert(andand1(34) == 0);
977 assert(andand1(92) == 0);
978 assert(andand1(39) == 0);
979 assert(andand1(33) == 1);
980 assert(andand1(61) == 1);
981 assert(andand1(93) == 1);
982 assert(andand1(255) == 1);
983
984 assert(andand2(0) == false);
985 assert(andand2(32) == false);
986 assert(andand2(46) == false);
987 assert(andand2(44) == false);
988 assert(andand2(58) == false);
989 assert(andand2(59) == false);
990 assert(andand2(60) == false);
991 assert(andand2(62) == false);
992 assert(andand2(34) == false);
993 assert(andand2(92) == false);
994 assert(andand2(39) == false);
995 assert(andand2(33) == true);
996 assert(andand2(61) == true);
997 assert(andand2(93) == true);
998 assert(andand2(255) == true);
999
1000 assert(andand3(6) == 3);
1001 assert(andand3(30) == 4);
1002 }
1003
1004 ////////////////////////////////////////////////////////////////////////
1005
bittest11508(char c)1006 bool bittest11508(char c)
1007 {
1008 return c=='_' || c=='-' || c=='+' || c=='.';
1009 }
1010
testbittest()1011 void testbittest()
1012 {
1013 assert(bittest11508('_'));
1014 }
1015
1016 ////////////////////////////////////////////////////////////////////////
1017
or1(ubyte x)1018 uint or1(ubyte x)
1019 {
1020 return x | (x<<8) | (x<<16) | (x<<24) | (x * 3);
1021 }
1022
testor_combine()1023 void testor_combine()
1024 {
1025 printf("%x\n", or1(1));
1026 assert(or1(5) == 5 * (0x1010101 | 3));
1027 }
1028
1029 ////////////////////////////////////////////////////////////////////////
1030
1031
shrshl(int i)1032 int shrshl(int i) {
1033 return ((i+1)>>1)<<1;
1034 }
1035
testshrshl()1036 void testshrshl()
1037 {
1038 assert(shrshl(6) == 6);
1039 assert(shrshl(7) == 8);
1040 }
1041
1042 ////////////////////////////////////////////////////////////////////////
1043
1044 struct S1
1045 {
1046 cdouble val;
1047 }
1048
formatTest(S1 s,double re,double im)1049 void formatTest(S1 s, double re, double im)
1050 {
1051 assert(s.val.re == re);
1052 assert(s.val.im == im);
1053 }
1054
test10639()1055 void test10639()
1056 {
1057 S1 s = S1(3+2.25i);
1058 formatTest(s, 3, 2.25);
1059 }
1060
1061 ////////////////////////////////////////////////////////////////////////
1062
bt10715(in uint[]ary,size_t bitnum)1063 bool bt10715(in uint[] ary, size_t bitnum)
1064 {
1065 return !!(ary[bitnum >> 5] & 1 << (bitnum & 31)); // uses bt
1066 }
1067
neg_bt10715(in uint[]ary,size_t bitnum)1068 bool neg_bt10715(in uint[] ary, size_t bitnum)
1069 {
1070 return !(ary[bitnum >> 5] & 1 << (bitnum & 31)); // does not use bt
1071 }
1072
test10715()1073 void test10715()
1074 {
1075 static uint[2] a1 = [0x1001_1100, 0x0220_0012];
1076
1077 if ( bt10715(a1,30)) assert(0);
1078 if (!bt10715(a1,8)) assert(0);
1079 if ( bt10715(a1,30+32)) assert(0);
1080 if (!bt10715(a1,1+32)) assert(0);
1081
1082 if (!neg_bt10715(a1,30)) assert(0);
1083 if ( neg_bt10715(a1,8)) assert(0);
1084 if (!neg_bt10715(a1,30+32)) assert(0);
1085 if ( neg_bt10715(a1,1+32)) assert(0);
1086 }
1087
1088 ////////////////////////////////////////////////////////////////////////
1089
compare12164(A12164 * rhsPA,A12164 * zis)1090 ptrdiff_t compare12164(A12164* rhsPA, A12164* zis)
1091 {
1092 if (*rhsPA == *zis)
1093 return 0;
1094 return ptrdiff_t.min;
1095 }
1096
1097 struct A12164
1098 {
1099 int a;
1100 }
1101
test12164()1102 void test12164()
1103 {
1104 auto a = A12164(3);
1105 auto b = A12164(2);
1106 assert(compare12164(&a, &b));
1107 }
1108
1109 ////////////////////////////////////////////////////////////////////////
1110
foo10678(char[5]txt)1111 int foo10678(char[5] txt)
1112 {
1113 return txt[0] + txt[1] + txt[4];
1114 }
1115
test10678()1116 void test10678()
1117 {
1118 char[5] hello = void;
1119 hello[0] = 8;
1120 hello[1] = 9;
1121 hello[4] = 10;
1122 int i = foo10678(hello);
1123 assert(i == 27);
1124 }
1125
1126 ////////////////////////////////////////////////////////////////////////
1127
1128 struct S12051
1129 {
thisS120511130 this(char c)
1131 {
1132 assert(c == 'P' || c == 'M');
1133 }
1134 }
1135
test12051()1136 void test12051()
1137 {
1138 auto ip = ["abc"];
1139 foreach (i, s; ip)
1140 {
1141 S12051(i < ip.length ? 'P' : 'M');
1142 }
1143 }
1144
1145 ////////////////////////////////////////////////////////////////////////
1146
bug7565(double x)1147 void bug7565( double x) { assert(x == 3); }
1148
test7565()1149 void test7565()
1150 {
1151 double y = 3;
1152 bug7565( y++ );
1153 assert(y == 4);
1154 }
1155
1156 ////////////////////////////////////////////////////////////////////////
1157
bug8525(int[]devt)1158 int bug8525(int[] devt)
1159 {
1160 return devt[$ - 1];
1161 }
1162
1163 ////////////////////////////////////////////////////////////////////////
1164
func13190(int)1165 void func13190(int) {}
1166
1167 struct Struct13190
1168 {
1169 ulong a;
1170 uint b;
1171 };
1172
1173 __gshared Struct13190* table13190 =
1174 [
1175 Struct13190(1, 1),
1176 Struct13190(0, 2)
1177 ];
1178
test13190()1179 void test13190()
1180 {
1181 for (int i = 0; table13190[i].a; i++)
1182 {
1183 ulong tbl = table13190[i].a;
1184 func13190(i);
1185 if (1 + tbl)
1186 {
1187 if (tbl == 0x80000)
1188 return;
1189 }
1190 }
1191 }
1192
1193 ////////////////////////////////////////////////////////////////////////
1194
foo13485(double c,double d)1195 double foo13485(double c, double d)
1196 {
1197 // This must not be optimized to c += (d + d)
1198 c += d;
1199 c += d;
1200 return c;
1201 }
1202
test13485()1203 void test13485()
1204 {
1205 enum double d = 0X1P+1023;
1206 assert(foo13485(-d, d) == d);
1207 }
1208
1209 ////////////////////////////////////////////////////////////////////////
1210
test12833a(int a)1211 void test12833a(int a)
1212 {
1213 long x = cast(long)a;
1214
1215 switch (cast(int)(cast(ushort)(x >> 16 & 65535L)))
1216 {
1217 case 1:
1218 {
1219 break;
1220 }
1221 default:
1222 {
1223 assert(0);
1224 }
1225 }
1226 }
1227
test12833()1228 void test12833()
1229 {
1230 test12833a(0x1_0000);
1231 }
1232
1233 /***********************************************/
1234
1235 struct Point9449
1236 {
1237 double f = 3.0;
1238 double g = 4.0;
1239 }
1240
test9449()1241 void test9449()
1242 {
1243 Point9449[1] arr;
1244 if (arr[0].f != 3.0) assert(0);
1245 if (arr[0].g != 4.0) assert(0);
1246 }
1247
1248 ////////////////////////////////////////////////////////////////////////
1249 // https://issues.dlang.org/show_bug.cgi?id=12057
1250
prop12057(real x)1251 bool prop12057(real x) { return false; }
f12057(real)1252 double f12057(real) { return double.init; }
test12057()1253 void test12057()
1254 {
1255 real fc = f12057(real.init);
1256 if (fc == 0 || fc.prop12057) {}
1257 }
1258
1259
1260 ////////////////////////////////////////////////////////////////////////
1261
modulo24(long ticks)1262 long modulo24 (long ticks)
1263 {
1264 ticks %= 864000000000;
1265 if (ticks < 0)
1266 ticks += 864000000000;
1267 return ticks;
1268 }
1269
test13784()1270 void test13784()
1271 {
1272 assert (modulo24(-141600000000) == 722400000000);
1273 }
1274
1275
1276 ////////////////////////////////////////////////////////////////////////
1277
1278 struct S13969 {
1279 int x, y;
1280 }
1281
test13969(const S13969 * f)1282 int test13969(const S13969* f) {
1283 return 0 % ((f.y > 0) ? f.x / f.y : f.x / -f.y);
1284 }
1285
1286 ////////////////////////////////////////////////////////////////////////
1287
1288 int[] arr14436;
test14436()1289 void test14436()
1290 {
1291 assert(arr14436 == null);
1292 arr14436 = [1, 2, 3];
1293 assert(arr14436 != null);
1294 }
1295
1296 ////////////////////////////////////////////////////////////////////////
1297
test14220()1298 void test14220()
1299 {
1300 auto a = toString(14);
1301
1302 printf("a.ptr = %p, a.length = %d\n", a.ptr, cast(int)a.length);
1303 return;
1304 }
1305
toString(int value)1306 auto toString(int value)
1307 {
1308 uint mValue = value;
1309
1310 char[int.sizeof * 3] buffer = void;
1311 size_t index = buffer.length;
1312
1313 do
1314 {
1315 uint div = cast(int)(mValue / 10);
1316 char mod = mValue % 10 + '0';
1317 buffer[--index] = mod; // Line 22
1318 mValue = div;
1319 } while (mValue);
1320
1321 //printf("buffer.ptr = %p, index = %d\n", buffer.ptr, cast(int)index);
1322 return dup(buffer[index .. $]);
1323 }
1324
dup(char[]a)1325 char[] dup(char[] a)
1326 {
1327 //printf("a.ptr = %p, a.length = %d\n", a.ptr, cast(int)a.length);
1328 a[0] = 1; // segfault
1329 return a;
1330 }
1331
1332 ////////////////////////////////////////////////////////////////////////
1333
stripLeft(int str,int dc)1334 int stripLeft(int str, int dc)
1335 {
1336 while (true)
1337 {
1338 int a = str;
1339 int s = a;
1340 str += 1;
1341 if (dc) return s;
1342 }
1343 }
1344
test14829()1345 void test14829()
1346 {
1347 if (stripLeft(3, 1) != 3) // fails with -O
1348 assert(0);
1349 }
1350
1351
1352 ////////////////////////////////////////////////////////////////////////
1353
test2()1354 void test2()
1355 {
1356 void test(cdouble v)
1357 {
1358 auto x2 = cdouble(v);
1359 assert(x2 == v);
1360 }
1361 test(1.2+3.4i);
1362 }
1363
1364 ////////////////////////////////////////////////////////////////////////
1365
test3()1366 void test3()
1367 {
1368 int[6] a;
1369 int[] b;
1370 b = a;
1371 b = (b.ptr + b.length - 5)[0 .. b.ptr + b.length - 1 - a.ptr];
1372 assert(b.ptr == a.ptr + 1);
1373 assert(b.length == 5);
1374 }
1375
1376 ////////////////////////////////////////////////////////////////////////
1377 // 14782
1378
1379
test14782()1380 void test14782()
1381 {
1382 static struct Foo
1383 {
1384 long a = 8;
1385 int b = 7;
1386 }
1387
1388 static Foo[1] fun() { Foo[1] a; return a; }
1389
1390 auto result = fun();
1391 assert(result[0].a == 8);
1392 assert(result[0].b == 7);
1393 }
1394
1395 ////////////////////////////////////////////////////////////////////////
1396
test14987()1397 void test14987()
1398 {
1399 static struct Foo
1400 {
1401 int b = 7;
1402 }
1403 static assert((Foo[4]).sizeof == 16);
1404
1405 static Foo[4] fun() { Foo[4] a; return a; }
1406
1407 auto result = fun();
1408 assert(result[0].b == 7);
1409 assert(result[1].b == 7);
1410 assert(result[2].b == 7);
1411 assert(result[3].b == 7);
1412 }
1413
1414 ////////////////////////////////////////////////////////////////////////
1415
calloc15272(size_t bc)1416 void[] calloc15272(size_t bc) nothrow pure
1417 {
1418 assert(bc == 1);
1419 return new void[1];
1420 }
1421
test15272()1422 void test15272()
1423 {
1424 void[] scache = cast(void[])"abc";
1425 size_t count = 1;
1426 void[]* buckets = &scache;
1427 *buckets = calloc15272(count)[0 .. count];
1428 }
1429
1430 /*****************************************
1431 * https://issues.dlang.org/show_bug.cgi?id=15861
1432 */
1433
test15861()1434 void test15861()
1435 {
1436 double val = 4286853117.;
1437
1438 (){
1439 assert(val == 4286853117.);
1440 }();
1441 }
1442
1443 ////////////////////////////////////////////////////////////////////////
1444
1445 // https://issues.dlang.org/show_bug.cgi?id=15629 comment 3
1446 // -O
1447
test15629()1448 void test15629()
1449 {
1450 int[] a = [3];
1451 int value = a[0] >= 0 ? a[0] : -a[0];
1452 assert(a[0] == 3);
1453 writeln(value, a);
1454 }
1455
writeln(int v,int[]a)1456 void writeln(int v, int[] a)
1457 {
1458 }
1459
1460 ////////////////////////////////////////////////////////////////////////
1461
binPosPow2()1462 real binPosPow2() { return 1.0L; }
1463
binPow2()1464 real binPow2()
1465 {
1466 return 1.0L/binPosPow2();
1467 }
1468
test4()1469 void test4()
1470 {
1471 assert(binPow2() == 1.0L);
1472 }
1473
1474 ////////////////////////////////////////////////////////////////////////
1475 // https://issues.dlang.org/show_bug.cgi?id=13474
1476
1477
1478 double sumKBN(double s = 0.0)
1479 {
1480 import std.math : fabs;
1481 double c = 0.0;
foreach(double x;[1,1e100,1,-1e100])1482 foreach(double x; [1, 1e100, 1, -1e100])
1483 {
1484 x = multiply(x);
1485 double t = s + x;
1486 if(s.fabs >= x.fabs)
1487 {
1488 double y = s-t;
1489 c += y+x;
1490 }
1491 else
1492 {
1493 double y = x-t;
1494 c += y+s;
1495 }
1496 s = t;
1497 }
1498 return s + c;
1499 }
1500
multiply(double a)1501 double multiply(double a) { return a * 10000; }
1502
test13474()1503 void test13474()
1504 {
1505 double r = 20000;
1506 assert(r == sumKBN());
1507 }
1508
1509 ////////////////////////////////////////////////////////////////////////
1510 // https://issues.dlang.org/show_bug.cgi?id=16699
1511
parseDateRange()1512 ulong[1] parseDateRange()
1513 {
1514 try
1515 {
1516 ulong[1] result;
1517 result[0] = 6;
1518 return result;
1519 }
1520 finally
1521 {
1522 }
1523 }
1524
test16699()1525 void test16699()
1526 {
1527 ulong[1] range = parseDateRange();
1528 assert(range[0] == 6);
1529 }
1530
1531 ////////////////////////////////////////////////////////////////////////
1532
1533 // https://issues.dlang.org/show_bug.cgi?id=16102
1534
~thisS161021535 struct S16102 { ~this() { } }
1536
f16102()1537 long[1] f16102()
1538 {
1539 S16102 a;
1540 return [1];
1541 }
1542
test16102()1543 void test16102()
1544 {
1545 assert( f16102() == [1] );
1546 }
1547
1548 ////////////////////////////////////////////////////////////////////////
1549
1550
1551 /* Test the pattern:
1552 * replace ((i / C1) / C2) with (i / (C1 * C2))
1553 * when e1 is 0 or 1 and (i2-i1) is a power of 2.
1554 */
1555
divdiv(T,T C1,T C2)1556 void divdiv(T, T C1, T C2)(T i)
1557 {
1558 auto a = (i / C1) / C2;
1559 auto b = i / (C1 * C2);
1560 if (a != b) assert(0);
1561 }
1562
testdivdiv()1563 void testdivdiv()
1564 {
1565 divdiv!(int,10,20)(30);
1566 divdiv!(uint,10,20)(30);
1567 divdiv!(long,10,20)(30);
1568 divdiv!(ulong,10,20)(30);
1569
1570 divdiv!(int,-10,20)(30);
1571 divdiv!(long,-10,20)(30);
1572
1573 divdiv!(int,-10,-20)(-30);
1574 divdiv!(long,-10,-20)(-30);
1575 }
1576
1577 ////////////////////////////////////////////////////////////////////////
1578
test5a(ulong x,ulong y)1579 void test5a(ulong x, ulong y)
1580 {
1581 int a;
1582 if (x >> 32)
1583 a = 1;
1584 else
1585 a = 2;
1586 assert(a == 1);
1587
1588 if (y >> 32)
1589 a = 1;
1590 else
1591 a = 2;
1592 assert(a == 2);
1593 }
1594
test5()1595 void test5()
1596 {
1597 test5a(uint.max + 1L, uint.max);
1598 }
1599
1600 ////////////////////////////////////////////////////////////////////////
1601
1602 /* Test the pattern:
1603 * replace (e ? i1 : i2) with (i1 + e * (i2 - i1))
1604 * when e1 is 0 or 1 and (i2-i1) is a power of 2.
1605 */
1606
foo61(int i)1607 int foo61(int i)
1608 {
1609 return (i % 2 != 0) ? 4 : 2;
1610 }
1611
foo62(int i)1612 int foo62(int i)
1613 {
1614 return (i % 2 != 0) ? 2 : 4;
1615 }
1616
bar6(bool b)1617 bool bar6(bool b) { return b; }
1618
foo63(bool b)1619 int foo63(bool b)
1620 {
1621 return bar6(b) ? 16 : 8;
1622 }
1623
foo64(bool b)1624 int foo64(bool b)
1625 {
1626 return bar6(b) ? 8 : 16;
1627 }
1628
test6()1629 void test6()
1630 {
1631 if (foo61(0) != 2) assert(0);
1632 if (foo61(1) != 4) assert(0);
1633 if (foo62(0) != 4) assert(0);
1634 if (foo62(1) != 2) assert(0);
1635 if (foo63(0) != 8) assert(0);
1636 if (foo63(1) != 16) assert(0);
1637 if (foo64(0) != 16) assert(0);
1638 if (foo64(1) != 8) assert(0);
1639 }
1640
1641 ////////////////////////////////////////////////////////////////////////
1642
dataflow(int b)1643 int dataflow(int b) {
1644 int ret;
1645
1646 if (b==4)
1647 ret = 3;
1648 else
1649 ret = 5;
1650
1651 if (ret == 4)
1652 return 0;
1653 else
1654 return 1;
1655 }
1656
testeqeqranges()1657 void testeqeqranges()
1658 {
1659 int i = dataflow(4);
1660 if (i != 1)
1661 assert(0);
1662 }
1663
1664 ////////////////////////////////////////////////////////////////////////
1665
main()1666 int main()
1667 {
1668 testgoto();
1669 testswitch();
1670 testdo();
1671 testbreak();
1672 teststringswitch();
1673 teststrarg();
1674 test12164();
1675 testsizes();
1676 testarrayinit();
1677 testU();
1678 testulldiv();
1679 testbittest();
1680 test8658();
1681 testfastudiv();
1682 testfastdiv();
1683 test3918();
1684 test12051();
1685 testdocond();
1686 testnegcom();
1687 test11565();
1688 testoror();
1689 testbt();
1690 test12095(0);
1691 testandand();
1692 testor_combine();
1693 testshrshl();
1694 test13383();
1695 test13190();
1696 test13485();
1697 test14436();
1698 test10639();
1699 test10715();
1700 test10678();
1701 test7565();
1702 test13023(0x10_0000_0000);
1703 test12833();
1704 test9449();
1705 test12057();
1706 test13784();
1707 test14220();
1708 test14829();
1709 test2();
1710 test3();
1711 test14782();
1712 test14987();
1713 test15272();
1714 test15861();
1715 test15629();
1716 test4();
1717 test13474();
1718 test16699();
1719 test16102();
1720 testdivdiv();
1721 test5();
1722 test6();
1723 testeqeqranges();
1724 printf("Success\n");
1725 return 0;
1726 }
1727