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