1 // RUNNABLE_PHOBOS_TEST
2 import std.stdio;
3
Tuple(A...)4 template Tuple(A...)
5 {
6 alias A Tuple;
7 }
8
eval(A...)9 template eval(A...)
10 {
11 const typeof(A[0]) eval = A[0];
12 }
13
14 /************************************************/
15
Foo1(int i)16 int Foo1(int i)
17 {
18 if (i == 0)
19 return 1;
20 else
21 return i * Foo1(i - 1);
22 }
23
test1()24 void test1()
25 {
26 static int f = Foo1(5);
27 printf("%d %d\n", f, 5*4*3*2);
28 assert(f == 120);
29 }
30
31 /************************************************/
32
find2(string s,char c)33 int find2(string s, char c)
34 {
35 if (s.length == 0)
36 return -1;
37 else if (c == s[0])
38 return 0;
39 else
40 return 1 + find2(s[1..$], c);
41 }
42
test2()43 void test2()
44 {
45 static int f = find2("hello", 'l');
46 printf("%d\n", f);
47 assert(f == 2);
48 }
49
50 /************************************************/
51
bar3(int i)52 int bar3(int i)
53 {
54 int j;
55 while (i)
56 {
57 j += i;
58 i--;
59 }
60 return j;
61 }
62
test3()63 void test3()
64 {
65 static b = bar3(7);
66 printf("b = %d, %d\n", b, bar3(7));
67 assert(b == 28);
68 }
69
70 /************************************************/
71
bar4(int i)72 int bar4(int i)
73 {
74 for (int j = 0; j < 10; j++)
75 i += j;
76 return i;
77 }
78
test4()79 void test4()
80 {
81 static b = bar4(7);
82 printf("b = %d, %d\n", b, bar4(7));
83 assert(b == 52);
84 }
85
86 /************************************************/
87
bar5(int i)88 int bar5(int i)
89 {
90 int j;
91 do
92 {
93 i += j;
94 j++;
95 } while (j < 10);
96 return i;
97 }
98
test5()99 void test5()
100 {
101 static b = bar5(7);
102 printf("b = %d, %d\n", b, bar5(7));
103 assert(b == 52);
104 }
105
106 /************************************************/
107
bar6(int i)108 int bar6(int i)
109 {
110 int j;
111 do
112 {
113 i += j;
114 j++;
115 if (j == 4)
116 break;
117 } while (j < 10);
118 return i;
119 }
120
test6()121 void test6()
122 {
123 static b = bar6(7);
124 printf("b = %d, %d\n", b, bar6(7));
125 assert(b == 13);
126 }
127
128 /************************************************/
129
bar7(int i)130 int bar7(int i)
131 {
132 int j;
133 do
134 {
135 i += j;
136 j++;
137 if (j == 4)
138 return 80;
139 } while (j < 10);
140 return i;
141 }
142
test7()143 void test7()
144 {
145 static b = bar7(7);
146 printf("b = %d, %d\n", b, bar7(7));
147 assert(b == 80);
148 }
149
150 /************************************************/
151
bar8(int i)152 int bar8(int i)
153 {
154 int j;
155 do
156 {
157 j++;
158 if (j == 4)
159 continue;
160 i += j;
161 } while (j < 10);
162 return i;
163 }
164
test8()165 void test8()
166 {
167 static b = bar8(7);
168 printf("b = %d, %d\n", b, bar8(7));
169 assert(b == 58);
170 }
171
172 /************************************************/
173
bar9(int i)174 int bar9(int i)
175 {
176 int j;
177 while (j < 10)
178 {
179 j++;
180 if (j == 4)
181 continue;
182 i += j;
183 }
184 return i;
185 }
186
test9()187 void test9()
188 {
189 static b = bar9(7);
190 printf("b = %d, %d\n", b, bar9(7));
191 assert(b == 58);
192 }
193
194 /************************************************/
195
bar10(int i)196 int bar10(int i)
197 {
198 int j;
199 while (j < 10)
200 {
201 j++;
202 if (j == 4)
203 break;
204 i += j;
205 }
206 return i;
207 }
208
test10()209 void test10()
210 {
211 static b = bar10(7);
212 printf("b = %d, %d\n", b, bar10(7));
213 assert(b == 13);
214 }
215
216 /************************************************/
217
bar11(int i)218 int bar11(int i)
219 {
220 int j;
221 while (j < 10)
222 {
223 j++;
224 if (j == 4)
225 return i << 3;
226 i += j;
227 }
228 return i;
229 }
230
test11()231 void test11()
232 {
233 static b = bar11(7);
234 printf("b = %d, %d\n", b, bar11(7));
235 assert(b == 104);
236 }
237
238 /************************************************/
239
bar12(int i)240 int bar12(int i)
241 {
242 for (int j; j < 10; j++)
243 {
244 if (j == 4)
245 return i << 3;
246 i += j;
247 }
248 return i;
249 }
250
test12()251 void test12()
252 {
253 static b = bar12(7);
254 printf("b = %d, %d\n", b, bar12(7));
255 assert(b == 104);
256 }
257
258 /************************************************/
259
bar13(int i)260 int bar13(int i)
261 {
262 for (int j; j < 10; j++)
263 {
264 if (j == 4)
265 break;
266 i += j;
267 }
268 return i;
269 }
270
test13()271 void test13()
272 {
273 static b = bar13(7);
274 printf("b = %d, %d\n", b, bar13(7));
275 assert(b == 13);
276 }
277
278 /************************************************/
279
bar14(int i)280 int bar14(int i)
281 {
282 for (int j; j < 10; j++)
283 {
284 if (j == 4)
285 continue;
286 i += j;
287 }
288 return i;
289 }
290
test14()291 void test14()
292 {
293 static b = bar14(7);
294 printf("b = %d, %d\n", b, bar14(7));
295 assert(b == 48);
296 }
297
298 /************************************************/
299
bar15(int i)300 int bar15(int i)
301 {
302 foreach (k, v; "hello")
303 {
304 i <<= 1;
305 if (k == 4)
306 continue;
307 i += v;
308 }
309 return i;
310 }
311
test15()312 void test15()
313 {
314 static b = bar15(7);
315 printf("b = %d, %d\n", b, bar15(7));
316 assert(b == 3344);
317 }
318
319 /************************************************/
320
bar16(int i)321 int bar16(int i)
322 {
323 foreach_reverse (k, v; "hello")
324 {
325 i <<= 1;
326 if (k == 4)
327 continue;
328 i += v;
329 }
330 return i;
331 }
332
test16()333 void test16()
334 {
335 static b = bar16(7);
336 printf("b = %d, %d\n", b, bar16(7));
337 assert(b == 1826);
338 }
339
340 /************************************************/
341
bar17(int i)342 int bar17(int i)
343 {
344 foreach (k, v; "hello")
345 {
346 i <<= 1;
347 if (k == 2)
348 break;
349 i += v;
350 }
351 return i;
352 }
353
test17()354 void test17()
355 {
356 static b = bar17(7);
357 printf("b = %d, %d\n", b, bar17(7));
358 assert(b == 674);
359 }
360
361 /************************************************/
362
bar18(int i)363 int bar18(int i)
364 {
365 foreach_reverse (k, v; "hello")
366 {
367 i <<= 1;
368 if (k == 2)
369 break;
370 i += v;
371 }
372 return i;
373 }
374
test18()375 void test18()
376 {
377 static b = bar18(7);
378 printf("b = %d, %d\n", b, bar18(7));
379 assert(b == 716);
380 }
381
382 /************************************************/
383
bar19(int i)384 int bar19(int i)
385 {
386 assert(i > 0);
387 foreach_reverse (k, v; "hello")
388 {
389 i <<= 1;
390 if (k == 2)
391 return 8;
392 i += v;
393 }
394 return i;
395 }
396
test19()397 void test19()
398 {
399 static b = bar19(7);
400 printf("b = %d, %d\n", b, bar19(7));
401 assert(b == 8);
402 }
403
404 /************************************************/
405
bar20(int i)406 int bar20(int i)
407 {
408 assert(i > 0);
409 foreach (k, v; "hello")
410 {
411 i <<= 1;
412 if (k == 2)
413 return 8;
414 i += v;
415 }
416 return i;
417 }
418
test20()419 void test20()
420 {
421 static b = bar20(7);
422 printf("b = %d, %d\n", b, bar20(7));
423 assert(b == 8);
424 }
425
426 /************************************************/
427
bar21(int i)428 int bar21(int i)
429 {
430 assert(i > 0);
431 foreach (v; Tuple!(57, 23, 8))
432 {
433 i <<= 1;
434 i += v;
435 }
436 return i;
437 }
438
test21()439 void test21()
440 {
441 static b = bar21(7);
442 printf("b = %d, %d\n", b, bar21(7));
443 assert(b == 338);
444 }
445
446 /************************************************/
447
bar22(int i)448 int bar22(int i)
449 {
450 assert(i > 0);
451 foreach_reverse (v; Tuple!(57, 23, 8))
452 {
453 i <<= 1;
454 i += v;
455 }
456 return i;
457 }
458
test22()459 void test22()
460 {
461 static b = bar22(7);
462 printf("b = %d, %d\n", b, bar22(7));
463 assert(b == 191);
464 }
465
466 /************************************************/
467
bar23(int i)468 int bar23(int i)
469 {
470 assert(i > 0);
471 foreach_reverse (v; Tuple!(57, 23, 8))
472 {
473 i <<= 1;
474 if (v == 23)
475 return i + 1;
476 i += v;
477 }
478 return i;
479 }
480
test23()481 void test23()
482 {
483 static b = bar23(7);
484 printf("b = %d, %d\n", b, bar23(7));
485 assert(b == 45);
486 }
487
488 /************************************************/
489
bar24(int i)490 int bar24(int i)
491 {
492 assert(i > 0);
493 foreach (v; Tuple!(57, 23, 8))
494 {
495 i <<= 1;
496 if (v == 23)
497 return i + 1;
498 i += v;
499 }
500 return i;
501 }
502
test24()503 void test24()
504 {
505 static b = bar24(7);
506 printf("b = %d, %d\n", b, bar24(7));
507 assert(b == 143);
508 }
509
510 /************************************************/
511
bar25(int i)512 int bar25(int i)
513 {
514 assert(i > 0);
515 foreach_reverse (v; Tuple!(57, 23, 8))
516 {
517 i <<= 1;
518 if (v == 23)
519 break;
520 i += v;
521 }
522 return i;
523 }
524
test25()525 void test25()
526 {
527 static b = bar25(7);
528 printf("b = %d, %d\n", b, bar25(7));
529 assert(b == 44);
530 }
531
532 /************************************************/
533
bar26(int i)534 int bar26(int i)
535 {
536 assert(i > 0);
537 foreach (v; Tuple!(57, 23, 8))
538 {
539 i <<= 1;
540 if (v == 23)
541 break;
542 i += v;
543 }
544 return i;
545 }
546
test26()547 void test26()
548 {
549 static b = bar26(7);
550 printf("b = %d, %d\n", b, bar26(7));
551 assert(b == 142);
552 }
553
554 /************************************************/
555
bar27(int i)556 int bar27(int i)
557 {
558 foreach_reverse (v; Tuple!(57, 23, 8))
559 {
560 i <<= 1;
561 if (v == 23)
562 continue;
563 i += v;
564 }
565 return i;
566 }
567
test27()568 void test27()
569 {
570 static b = bar27(7);
571 printf("b = %d, %d\n", b, bar27(7));
572 assert(b == 145);
573 }
574
575 /************************************************/
576
bar28(int i)577 int bar28(int i)
578 {
579 foreach (v; Tuple!(57, 23, 8))
580 {
581 i <<= 1;
582 if (v == 23)
583 continue;
584 i += v;
585 }
586 return i;
587 }
588
test28()589 void test28()
590 {
591 static b = bar28(7);
592 printf("b = %d, %d\n", b, bar28(7));
593 assert(b == 292);
594 }
595
596 /************************************************/
597
bar29(int i)598 int bar29(int i)
599 {
600 switch (i)
601 {
602 case 1:
603 i = 4;
604 break;
605 case 7:
606 i = 3;
607 break;
608 default: assert(0);
609 }
610 return i;
611 }
612
test29()613 void test29()
614 {
615 static b = bar29(7);
616 printf("b = %d, %d\n", b, bar29(7));
617 assert(b == 3);
618 }
619
620 /************************************************/
621
bar30(int i)622 int bar30(int i)
623 {
624 switch (i)
625 {
626 case 1:
627 i = 4;
628 break;
629 case 8:
630 i = 2;
631 break;
632 default:
633 i = 3;
634 break;
635 }
636 return i;
637 }
638
test30()639 void test30()
640 {
641 static b = bar30(7);
642 printf("b = %d, %d\n", b, bar30(7));
643 assert(b == 3);
644 }
645
646 /************************************************/
647
bar31(string s)648 int bar31(string s)
649 { int i;
650
651 switch (s)
652 {
653 case "hello":
654 i = 4;
655 break;
656 case "betty":
657 i = 2;
658 break;
659 default:
660 i = 3;
661 break;
662 }
663 return i;
664 }
665
test31()666 void test31()
667 {
668 static b = bar31("betty");
669 printf("b = %d, %d\n", b, bar31("betty"));
670 assert(b == 2);
671 }
672
673 /************************************************/
674
bar32(int i)675 int bar32(int i)
676 {
677 switch (i)
678 {
679 case 7:
680 i = 4;
681 goto case;
682 case 5:
683 i = 2;
684 break;
685 default:
686 i = 3;
687 break;
688 }
689 return i;
690 }
691
test32()692 void test32()
693 {
694 static b = bar32(7);
695 printf("b = %d, %d\n", b, bar32(7));
696 assert(b == 2);
697 }
698
699 /************************************************/
700
bar33(int i)701 int bar33(int i)
702 {
703 switch (i)
704 {
705 case 5:
706 i = 2;
707 break;
708 case 7:
709 i = 4;
710 goto case 5;
711 default:
712 i = 3;
713 break;
714 }
715 return i;
716 }
717
test33()718 void test33()
719 {
720 static b = bar33(7);
721 printf("b = %d, %d\n", b, bar33(7));
722 assert(b == 2);
723 }
724
725 /************************************************/
726
bar34(int i)727 int bar34(int i)
728 {
729 switch (i)
730 {
731 default:
732 i = 3;
733 break;
734 case 5:
735 i = 2;
736 break;
737 case 7:
738 i = 4;
739 goto default;
740 }
741 return i;
742 }
743
test34()744 void test34()
745 {
746 static b = bar34(7);
747 printf("b = %d, %d\n", b, bar34(7));
748 assert(b == 3);
749 }
750
751 /************************************************/
752
bar35(int i)753 int bar35(int i)
754 {
755 L1:
756 switch (i)
757 {
758 default:
759 i = 3;
760 break;
761 case 5:
762 i = 2;
763 break;
764 case 3:
765 return 8;
766 case 7:
767 i = 4;
768 goto default;
769 }
770 goto L1;
771 }
772
test35()773 void test35()
774 {
775 static b = bar35(7);
776 printf("b = %d, %d\n", b, bar35(7));
777 assert(b == 8);
778 }
779
780 /************************************************/
781
square36(int x)782 int square36(int x)
783 {
784 return x * x;
785 }
786
787 const int foo36 = square36(5);
788
test36()789 void test36()
790 {
791 assert(foo36 == 25);
792 }
793
794 /************************************************/
795
someCompileTimeFunction()796 string someCompileTimeFunction()
797 {
798 return "writefln(\"Wowza!\");";
799 }
800
test37()801 void test37()
802 {
803 mixin(someCompileTimeFunction());
804 }
805
806 /************************************************/
807
NReps(string x,int n)808 string NReps(string x, int n)
809 {
810 string ret = "";
811 for (int i = 0; i < n; i++)
812 {
813 ret ~= x;
814 }
815 return ret;
816 }
817
test38()818 void test38()
819 {
820 static x = NReps("3", 6);
821 assert(x == "333333");
822 }
823
824 /************************************************/
825
func39()826 bool func39() { return true; }
827
828 static if (func39())
829 {
830 pragma(msg, "true");
831 }
832 else
833 {
834 pragma(msg, "false");
835 }
836
test39()837 void test39()
838 {
839 }
840
841 /************************************************/
842
UpToSpace(string x)843 string UpToSpace(string x)
844 {
845 int i = 0;
846 while (i < x.length && x[i] != ' ')
847 {
848 i++;
849 }
850 return x[0..i];
851 }
852
test40()853 void test40()
854 {
855 const y = UpToSpace("first space was after first");
856 writeln(y);
857 assert(y == "first");
858 }
859
860 /************************************************/
861
bar41(ref int j)862 int bar41(ref int j)
863 {
864 return 5;
865 }
866
foo41(int i)867 int foo41(int i)
868 {
869 int x;
870 x = 3;
871 bar41(x);
872 return i + x;
873 }
874
test41()875 void test41()
876 {
877 const y = foo41(3);
878 writeln(y);
879 assert(y == 6);
880 }
881
882 /************************************************/
883
bar42(ref int j)884 int bar42(ref int j)
885 {
886 return 5;
887 }
888
foo42(int i)889 int foo42(int i)
890 {
891 int x;
892 x = 3;
893 bar42(x);
894 return i + x;
895 }
896
test42()897 void test42()
898 {
899 const y = foo42(3);
900 writeln(y);
901 assert(y == 6);
902 }
903
904 /************************************************/
905
bar(string a)906 int bar(string a)
907 {
908 int v;
909 for (int i = 0; i < a.length; i++)
910 {
911 if (a[i] != ' ')
912 {
913 v += a.length;
914 }
915 }
916 return v;
917 }
918
test43()919 void test43()
920 {
921 const int foo = bar("a b c d");
922 writeln(foo);
923 assert(foo == 28);
924 }
925
926 /************************************************/
927
foo44()928 string foo44() { return ("bar"); }
929
test44()930 void test44()
931 {
932 const string bar = foo44();
933 assert(bar == "bar");
934 }
935
936 /************************************************/
937
square45(int n)938 int square45(int n) { return (n * n); }
939
test45()940 void test45()
941 {
942 int bar = eval!(square45(5));
943 assert(bar == 25);
944 }
945
946 /************************************************/
947
948 const int foo46[5] = [0,1,2,3,4];
949
test46()950 void test46()
951 {
952 writeln(eval!(foo46[3]));
953 }
954
955 /************************************************/
956
foo47()957 string foo47()
958 {
959 string s;
960 s = s ~ 't';
961 return s ~ "foo";
962 }
963
test47()964 void test47()
965 {
966 static const x = foo47();
967 pragma(msg, x);
968 assert(x == "tfoo");
969 }
970
971 /************************************************/
972
foo48()973 string foo48()
974 {
975 string s;
976 s = s ~ 't';
977 s = s.idup;
978 return s ~ "foo";
979 }
980
test48()981 void test48()
982 {
983 static const x = foo48();
984 pragma(msg, x);
985 assert(x == "tfoo");
986 }
987
988 /************************************************/
989
testd49(dstring input)990 dstring testd49(dstring input)
991 {
992 if (input[3..5] != "rt")
993 {
994 return input[1..3];
995 }
996 return "my";
997 }
998
test49()999 void test49()
1000 {
1001 static x = testd49("hello");
1002 writeln(x);
1003 assert(x == "el");
1004 }
1005
1006 /************************************************/
1007
makePostfix50(int x)1008 string makePostfix50(int x)
1009 {
1010 string first;
1011 first = "bad";
1012 if (x)
1013 {
1014 first = "ok";
1015 makePostfix50(0);
1016 }
1017 return first;
1018 }
1019
test50()1020 void test50()
1021 {
1022 static const char [] q2 = makePostfix50(1);
1023 static assert(q2 == "ok", q2);
1024 }
1025
1026 /************************************************/
1027
exprLength(string s)1028 int exprLength(string s)
1029 {
1030 int numParens=0;
1031 for (int i = 0; i < s.length; ++i)
1032 {
1033 if (s[i] == '(') { numParens++; }
1034 if (s[i] == ')') { numParens--; }
1035 if (numParens == 0) { return i; }
1036 }
1037 assert(0);
1038 }
1039
makePostfix51(string operations)1040 string makePostfix51(string operations)
1041 {
1042 if (operations.length < 2)
1043 return "x";
1044 int x = exprLength(operations);
1045 string first="bad";
1046 if (x > 0)
1047 {
1048 first = "ok";
1049 string ignore = makePostfix51(operations[1..x]);
1050 }
1051 return first;
1052 }
1053
1054
test51()1055 void test51()
1056 {
1057 string q = makePostfix51("(a+b)*c");
1058 assert(q == "ok");
1059 static const string q2 = makePostfix51("(a+b)*c");
1060 static assert(q2 == "ok");
1061 static assert(makePostfix51("(a+b)*c") == "ok");
1062 }
1063
1064 /************************************************/
1065
foo52(ref int x)1066 int foo52(ref int x)
1067 {
1068 x = 7;
1069 return 3;
1070 }
1071
bar52(int y)1072 int bar52(int y)
1073 {
1074 y = 4;
1075 foo52(y);
1076 return y;
1077 }
1078
test52()1079 void test52()
1080 {
1081 printf("%d\n", bar52(2));
1082 static assert(bar52(2) == 7);
1083 }
1084
1085 /************************************************/
1086
bar53(out int x)1087 void bar53(out int x) { x = 2; }
1088
foo53()1089 int foo53() { int y; bar53(y); return y; }
1090
test53()1091 void test53()
1092 {
1093 const int z = foo53();
1094 assert(z == 2);
1095 }
1096
1097 /************************************************/
1098
test54()1099 void test54()
1100 {
1101 static assert(equals54("alphabet", "alphabet"));
1102 }
1103
equals54(string a,string b)1104 bool equals54(string a, string b)
1105 {
1106 return (a == b);
1107 }
1108
1109 /************************************************/
1110
1111 const string foo55[2] = ["a", "b"];
retsth55(int i)1112 string retsth55(int i) { return foo55[i]; }
1113
test55()1114 void test55()
1115 {
1116 writeln(eval!(foo55[0]));
1117 writeln(eval!(retsth55(0)));
1118 }
1119
1120 /************************************************/
1121
retsth56(int i)1122 string retsth56(int i)
1123 {
1124 static const string foo[2] = ["a", "b"];
1125 return foo[i];
1126 }
1127
test56()1128 void test56()
1129 {
1130 writeln(eval!(retsth56(0)));
1131 }
1132
1133 /************************************************/
1134
g57()1135 int g57()
1136 {
1137 pragma(msg, "g");
1138 return 2;
1139 }
1140
1141 const int a57 = g57();
1142
test57()1143 void test57()
1144 {
1145 assert(a57 == 2);
1146 }
1147
1148 /************************************************/
1149
Fun58(int x)1150 int[] Fun58(int x)
1151 {
1152 int[] result;
1153 result ~= x + 1;
1154 return result;
1155 }
1156
test58()1157 void test58()
1158 {
1159 static b = Fun58(1) ~ Fun58(2);
1160 assert(b.length == 2);
1161 assert(b[0] == 2);
1162 assert(b[1] == 3);
1163 writeln(b);
1164 }
1165
1166 /************************************************/
1167
Index59()1168 int Index59()
1169 {
1170 int[] data = [1];
1171 return data[0];
1172 }
1173
test59()1174 void test59()
1175 {
1176 static assert(Index59() == 1);
1177 }
1178
1179 /************************************************/
1180
foo60()1181 string[int] foo60()
1182 {
1183 return [3:"hello", 4:"betty"];
1184 }
1185
test60()1186 void test60()
1187 {
1188 static assert(foo60()[3] == "hello");
1189 static assert(foo60()[4] == "betty");
1190 }
1191
1192 /************************************************/
1193
foo61()1194 string[int] foo61()
1195 {
1196 return [3:"hello", 4:"betty", 3:"world"];
1197 }
1198
test61()1199 void test61()
1200 {
1201 static assert(foo61()[3] == "world");
1202 static assert(foo61()[4] == "betty");
1203 }
1204
1205 /************************************************/
1206
foo62(int k)1207 string foo62(int k)
1208 {
1209 string[int] aa;
1210 aa = [3:"hello", 4:"betty"];
1211 return aa[k];
1212 }
1213
test62()1214 void test62()
1215 {
1216 static assert(foo62(3) == "hello");
1217 static assert(foo62(4) == "betty");
1218 }
1219
1220 /************************************************/
1221
test63()1222 void test63()
1223 {
1224 static auto x = foo63();
1225 }
1226
foo63()1227 int foo63()
1228 {
1229 pragma(msg, "Crash!");
1230 return 2;
1231 }
1232
1233 /************************************************/
1234
testd64(dstring input)1235 dstring testd64(dstring input)
1236 {
1237 debug int x = 10;
1238 return "my";
1239 }
1240
test64()1241 void test64()
1242 {
1243 static x = testd64("hello");
1244 }
1245
1246 /************************************************/
1247
1248 struct S65
1249 {
1250 int i;
1251 int j = 3;
1252 }
1253
foo(S65 s1,S65 s2)1254 int foo(S65 s1, S65 s2)
1255 {
1256 return s1 == s2;
1257 }
1258
test65()1259 void test65()
1260 {
1261 static assert(foo(S65(1, 5), S65(1, 5)) == 1);
1262 static assert(foo(S65(1, 5), S65(1, 4)) == 0);
1263 }
1264
1265 /************************************************/
1266
1267 struct S66
1268 {
1269 int i;
1270 int j = 3;
1271 }
1272
foo66(S66 s1)1273 int foo66(S66 s1)
1274 {
1275 return s1.j;
1276 }
1277
test66()1278 void test66()
1279 {
1280 static assert(foo66(S66(1, 5)) == 5);
1281 }
1282
1283 /************************************************/
1284
1285 struct S67
1286 {
1287 int i;
1288 int j = 3;
1289 }
1290
foo67(S67 s1)1291 int foo67(S67 s1)
1292 {
1293 s1.j = 3;
1294 int i = (s1.j += 2);
1295 assert(i == 5);
1296 return s1.j + 4;
1297 }
1298
test67()1299 void test67()
1300 {
1301 static assert(foo67(S67(1, 5)) == 9);
1302 }
1303
1304 /************************************************/
1305
foo68(int[]a)1306 int foo68(int[] a)
1307 {
1308 a[1] = 3;
1309 int x = (a[0] += 7);
1310 assert(x == 8);
1311 return a[0] + a[1];
1312 }
1313
test68()1314 void test68()
1315 {
1316 static assert(foo68( [1,5] ) == 11);
1317 }
1318
1319 /************************************************/
1320
foo69(char[]a)1321 int foo69(char[] a)
1322 {
1323 a[1] = 'c';
1324 char x = (a[0] += 7);
1325 assert(x == 'h');
1326 assert(x == a[0]);
1327 return a[0] + a[1] - 'a';
1328 }
1329
test69()1330 void test69()
1331 {
1332 static assert(foo69(['a', 'b']) == 'j');
1333 }
1334
1335 /************************************************/
1336
foo70(int[string]a)1337 int foo70(int[string] a)
1338 {
1339 a["world"] = 5;
1340 auto x = (a["hello"] += 7);
1341 assert(x == 10);
1342 assert(x == a["hello"]);
1343 return a["hello"] + a["betty"] + a["world"];
1344 }
1345
test70()1346 void test70()
1347 {
1348 static assert(foo70(["hello":3, "betty":4]) == 19);
1349 }
1350
1351 /************************************************/
1352
foo71(int[string]a)1353 size_t foo71(int[string] a)
1354 {
1355 return a.length;
1356 }
1357
test71()1358 void test71()
1359 {
1360 static assert(foo71(["hello":3, "betty":4]) == 2);
1361 }
1362
1363 /************************************************/
1364
foo72(int[string]a)1365 string[] foo72(int[string] a)
1366 {
1367 return a.keys;
1368 }
1369
test72()1370 void test72()
1371 {
1372 static assert(foo72(["hello":3, "betty":4]) == ["hello", "betty"]);
1373 }
1374
1375 /************************************************/
1376
foo73(int[string]a)1377 int[] foo73(int[string] a)
1378 {
1379 return a.values;
1380 }
1381
test73()1382 void test73()
1383 {
1384 static assert(foo73(["hello":3, "betty":4]) == [3, 4]);
1385 }
1386
1387 /************************************************/
1388
b74()1389 bool b74()
1390 {
1391 string a = "abc";
1392 return (a[$-1] == 'c');
1393 }
1394
1395 const c74 = b74();
1396
test74()1397 void test74()
1398 {
1399 assert(c74 == true);
1400 }
1401
1402 /************************************************/
1403
1404 struct FormatSpec
1405 {
1406 uint leading;
1407 bool skip;
1408 uint width;
1409 char modifier;
1410 char format;
1411 uint formatStart;
1412 uint formatLength;
1413 uint length;
1414 }
1415
GetFormat(string s)1416 FormatSpec GetFormat(string s)
1417 {
1418 FormatSpec result;
1419 return result;
1420 }
1421
GetFormat2(string s)1422 FormatSpec GetFormat2(string s)
1423 {
1424 FormatSpec result = FormatSpec();
1425 result.length = 0;
1426 assert(result.length < s.length);
1427 while (result.length < s.length)
1428 {
1429 ++result.length;
1430 }
1431 return result;
1432 }
1433
test75()1434 void test75()
1435 {
1436 static FormatSpec spec = GetFormat("asd");
1437
1438 assert(spec.leading == 0);
1439 assert(spec.modifier == char.init);
1440
1441 static FormatSpec spec2 = GetFormat2("asd");
1442 assert(spec2.length == 3);
1443 }
1444
1445 /************************************************/
1446
f76()1447 int f76()
1448 {
1449 int[3] a = void;
1450 a[0] = 1;
1451 assert(a[0] == 1);
1452 return 1;
1453 }
1454
1455 const i76 = f76();
1456
test76()1457 void test76()
1458 {
1459 }
1460
1461 /************************************************/
1462
1463 struct V77
1464 {
1465 int a;
1466 int b;
1467 }
1468
f77()1469 V77 f77()
1470 {
1471 int q = 0;
1472 int unused;
1473 int unused2;
1474 return V77(q, 0);
1475 }
1476
test77()1477 void test77()
1478 {
1479 const w = f77();
1480 const v = f77().b;
1481 }
1482
1483 /************************************************/
1484
1485 struct Bar78
1486 {
1487 int x;
1488 }
1489
foo78()1490 int foo78()
1491 {
1492 Bar78 b = Bar78.init;
1493 Bar78 c;
1494 b.x = 1;
1495 b = bar(b);
1496 return b.x;
1497 }
1498
bar(Bar78 b)1499 Bar78 bar(Bar78 b)
1500 {
1501 return b;
1502 }
1503
test78()1504 void test78()
1505 {
1506 static x = foo78();
1507 }
1508
1509 /************************************************/
1510
1511 struct Bar79
1512 {
1513 int y,x;
1514 }
1515
foo79()1516 int foo79()
1517 {
1518 Bar79 b = Bar79.init;
1519
1520 b.x = 100;
1521
1522 for (size_t i = 0; i < b.x; i++) { }
1523
1524 b.x++;
1525 b.x = b.x + 1;
1526
1527 return b.x;
1528 }
1529
test79()1530 void test79()
1531 {
1532 static x = foo79();
1533 printf("x = %d\n", x);
1534 assert(x == 102);
1535 }
1536
1537 /************************************************/
1538
test80()1539 void test80()
1540 {
1541 }
1542
1543 /************************************************/
1544
foo81()1545 string foo81()
1546 {
1547 return "";
1548 }
1549
rod81(string[]a)1550 string rod81(string[] a)
1551 {
1552 return a[0];
1553 }
1554
test81()1555 void test81()
1556 {
1557 static x = rod81([foo81(), ""]);
1558 assert(x == "");
1559 }
1560
1561
1562 /************************************************/
1563
1564 struct S82
1565 {
1566 string name;
1567 }
1568
1569 const S82 item82 = {"item"};
1570
mixItemList82()1571 string mixItemList82()
1572 {
1573 return item82.name;
1574 }
1575
1576 const string s82 = mixItemList82();
1577
test82()1578 void test82()
1579 {
1580 assert(s82 == "item");
1581 }
1582
1583 /************************************************/
1584
1585 struct S83
1586 {
1587 string name;
1588 }
1589
1590 const S83[] items83 =
1591 [
1592 {"item"},
1593 ];
1594
mixItemList83()1595 string mixItemList83()
1596 {
1597 string s;
1598 foreach (item; items83)
1599 s ~= item.name;
1600 return s;
1601 }
1602
1603 const string s83 = mixItemList83();
1604
test83()1605 void test83()
1606 {
1607 writeln(s83);
1608 assert(s83 == "item");
1609 }
1610
1611 /************************************************/
1612
1613 struct S84 { int a; }
1614
func84()1615 int func84()
1616 {
1617 S84 [] s = [S84(7)];
1618 return s[0].a; // Error: cannot evaluate func() at compile time
1619 }
1620
test84()1621 void test84()
1622 {
1623 const int x = func84();
1624 assert(x == 7);
1625 }
1626
1627 /************************************************/
1628
1629 struct S85
1630 {
1631 int a;
1632 }
1633
func85()1634 size_t func85()
1635 {
1636 S85 [] s;
1637 s ~= S85(7);
1638 return s.length;
1639 }
1640
test85()1641 void test85()
1642 {
1643 const size_t x = func85();
1644 assert(x == 1);
1645 }
1646
1647 /************************************************/
1648
1649 struct Bar86
1650 {
1651 int x;
1652 char[] s;
1653 }
1654
foo86()1655 char[] foo86()
1656 {
1657 Bar86 bar;
1658 return bar.s;
1659 }
1660
test86()1661 void test86()
1662 {
1663 static x = foo86();
1664 assert(x == null);
1665 }
1666
1667 /************************************************/
1668
1669 struct Bar87
1670 {
1671 int x;
1672 }
1673
foo87()1674 int foo87()
1675 {
1676 Bar87 bar;
1677 bar.x += 1;
1678 bar.x++;
1679 return bar.x;
1680 }
1681
test87()1682 void test87()
1683 {
1684 static x = foo87();
1685 assert(x == 2);
1686 }
1687
1688 /************************************************/
1689
foo88()1690 int foo88()
1691 {
1692 char[] s;
1693 int i;
1694
1695 if (s)
1696 {
1697 i |= 1;
1698 }
1699
1700 if (s == null)
1701 {
1702 i |= 2;
1703 }
1704
1705 if (s is null)
1706 {
1707 i |= 4;
1708 }
1709
1710 if (s == "")
1711 {
1712 i |= 8;
1713 }
1714
1715 if (s.length)
1716 {
1717 i |= 16;
1718 }
1719
1720 if (s == ['c'][0..0])
1721 {
1722 i |= 32;
1723 }
1724
1725
1726 if (null == s)
1727 {
1728 i |= 64;
1729 }
1730
1731 if (null is s)
1732 {
1733 i |= 128;
1734 }
1735
1736 if ("" == s)
1737 {
1738 i |= 256;
1739 }
1740
1741 if (['c'][0..0] == s)
1742 {
1743 i |= 512;
1744 }
1745
1746 return i;
1747 }
1748
test88()1749 void test88()
1750 {
1751 static x = foo88();
1752 printf("x = %x\n", x);
1753 assert(x == (2|4|8|32|64|128|256|512));
1754 }
1755
1756 /************************************************/
1757
Tuple89(T...)1758 template Tuple89(T...)
1759 {
1760 alias T val;
1761 }
1762
1763 alias Tuple89!(int) Tup89;
1764
gen89()1765 string gen89()
1766 {
1767 foreach (i, type; Tup89.val)
1768 {
1769 assert(i == 0);
1770 assert(is(type == int));
1771 }
1772 return null;
1773 }
1774
test89()1775 void test89()
1776 {
1777 static const string text = gen89();
1778 assert(text is null);
1779 }
1780
1781 /************************************************/
1782
bar90(string z)1783 string bar90(string z)
1784 {
1785 return z;
1786 }
1787
foo90(string a,string b)1788 string foo90(string a, string b)
1789 {
1790 string f = a.length == 1 ? a: foo90("B", "C");
1791 string g = b.length == 1 ? b: bar90(foo90("YYY", "A"));
1792 return f;
1793 }
1794
test90()1795 void test90()
1796 {
1797 static const string xxx = foo90("A", "xxx");
1798 printf("%.*s\n", xxx.length, xxx.ptr);
1799 assert(xxx == "A");
1800 }
1801
1802 /************************************************/
1803
1804 struct PR91
1805 {
1806 }
1807
foo91()1808 int foo91()
1809 {
1810 PR91 pr;
1811 pr = PR91();
1812 return 0;
1813 }
1814
test91()1815 void test91()
1816 {
1817 static const i = foo91();
1818 }
1819
1820 /************************************************/
1821
find92(immutable (char)[7]buf)1822 char find92(immutable(char)[7] buf)
1823 {
1824 return buf[3];
1825 }
1826
1827
test92()1828 void test92()
1829 {
1830 static const pos = find92("abcdefg");
1831 assert(pos == 'd');
1832 }
1833
1834 /************************************************/
1835
hello93()1836 static string hello93()
1837 {
1838 string result = "";
1839 int i = 0;
1840 for (;;)
1841 {
1842 result ~= `abc`;
1843 i += 1;
1844 if (i == 3)
1845 break;
1846 }
1847 return result;
1848 }
1849
test93()1850 void test93()
1851 {
1852 static string s = hello93();
1853 assert(s == "abcabcabc");
1854 }
1855
1856 /************************************************/
1857
foo94(string[]list,string s)1858 int foo94 (string[] list, string s)
1859 {
1860 if (list.length == 0)
1861 return 1;
1862 else
1863 {
1864 return 2 + foo94(list[1..$], list[0]);
1865 }
1866 }
1867
test94()1868 void test94()
1869 {
1870 printf("test94\n");
1871 static const int x = foo94(["a", "b"], "");
1872 assert(x == 5);
1873 }
1874
1875 /************************************************/
1876
func95(immutable char[]s)1877 char[] func95(immutable char[] s)
1878 {
1879 char[] u = "".dup;
1880 u ~= s;
1881 u = u ~ s;
1882 return u;
1883 }
1884
test95()1885 void test95()
1886 {
1887 mixin(func95("{}"));
1888 }
1889
1890 /************************************************/
1891
func96(string s)1892 char[] func96(string s)
1893 {
1894 char[] u = "".dup;
1895 u ~= s;
1896 u = u ~ s;
1897 return u;
1898 }
1899
test96()1900 void test96()
1901 {
1902 mixin(func96("{}"));
1903 }
1904
1905 /************************************************/
1906
foo97()1907 string foo97()
1908 {
1909 string a;
1910 a ~= "abc"; // ok
1911 string[] b;
1912 b ~= "abc"; // ok
1913 string[][] c;
1914 c ~= ["abc", "def"];
1915 string[][] d = [];
1916 d ~= ["abc", "def"]; // ok
1917 return "abc";
1918 }
1919
test97()1920 void test97()
1921 {
1922 static const xx97 = foo97();
1923 }
1924
1925 /************************************************/
1926
immutable(int)1927 immutable(int)[] foo98(immutable(int)[][] ss)
1928 {
1929 immutable(int)[] r;
1930 r ~= ss[0]; // problem here
1931 return r;
1932 }
1933
test98()1934 void test98()
1935 {
1936 const r = foo98([[1], [2]]);
1937 }
1938
1939 /************************************************/
1940
1941 struct Number
1942 {
1943 public int value;
opCallNumber1944 static Number opCall(int value)
1945 {
1946 Number n = void;
1947 n.value = value;
1948 return n;
1949 }
1950 }
1951
1952 class Crash
1953 {
1954 Number number = Number(0);
1955 }
1956
test99()1957 void test99()
1958 {
1959 }
1960
1961 /************************************************/
1962
1963 int[] map100 = ([4:true, 5:true]).keys;
1964 bool[] foo100 = ([4:true, 5:true]).values;
1965
test100()1966 void test100()
1967 {
1968 }
1969
1970 /************************************************/
1971
foo101()1972 int foo101()
1973 {
1974 immutable bool [int] map = [4:true, 5:true];
1975 foreach (x; map.keys) {}
1976 return 3;
1977 }
1978
1979 static int x101 = foo101();
1980
test101()1981 void test101()
1982 {
1983 }
1984
1985 /************************************************/
1986
foo102()1987 int foo102()
1988 {
1989 foreach (i; 0 .. 1)
1990 return 1;
1991 return 0;
1992 }
1993 static assert(foo102() == 1);
1994
bar102()1995 int bar102()
1996 {
1997 foreach_reverse (i; 0 .. 1)
1998 return 1;
1999 return 0;
2000 }
2001 static assert(bar102() == 1);
2002
test102()2003 void test102()
2004 {
2005 }
2006
2007 /************************************************/
2008
foo103()2009 int foo103()
2010 {
2011 foreach (c; '0' .. '9') { }
2012 foreach_reverse (c; '9' .. '0') { }
2013 return 0;
2014 }
2015
2016 enum x103 = foo103();
2017
test103()2018 void test103()
2019 {
2020 }
2021
2022 /************************************************/
2023
2024 struct S
2025 {
2026 int x;
2027 char y;
2028 }
2029
2030 // Functions which should fail CTFE
2031
badfoo()2032 int badfoo()
2033 {
2034 S[2] c;
2035 int w = 4;
2036 c[w].x = 6; // array bounds error
2037 return 7;
2038 }
2039
2040 int badglobal = 1;
2041
badfoo3()2042 int badfoo3()
2043 {
2044 S[2] c;
2045 c[badglobal].x = 6; // global index error
2046 return 7;
2047 }
2048
badfoo4()2049 int badfoo4()
2050 {
2051 static S[2] c;
2052 c[0].x = 6; // Cannot access static
2053 return 7;
2054 }
2055
2056 /+ // This doesn't compile at runtime
2057 int badfoo5()
2058 {
2059 S[] c = void;
2060 c[0].x = 6; // c is uninitialized, and not a static array.
2061 return 1;
2062 }
2063 +/
2064
badfoo6()2065 int badfoo6()
2066 {
2067 S[] b = [S(7), S(15), S(56), S(12)];
2068 b[-2..4] = S(17); // exceeding (negative) array bounds
2069 return 1;
2070 }
2071
badfoo7()2072 int badfoo7()
2073 {
2074 S[] b = [S(7), S(15), S(56), S(12), S(67)];
2075 S[] c = [S(17), S(4)];
2076 b[1..4] = c[]; // slice mismatch in dynamic array
2077 return 1;
2078 }
2079
badfoo8()2080 int badfoo8()
2081 {
2082 S[] b;
2083 b[1..3] = [S(17), S(4)]; // slice assign to uninitialized dynamic array
2084 return 1;
2085 }
2086
Compileable(int z)2087 template Compileable(int z) { bool OK = true;}
2088 static assert(!is(typeof(Compileable!(badfoo()).OK)));
2089 static assert(!is(typeof(Compileable!(
2090 (){
2091 S[] c;
2092 return c[7].x; // uninitialized error
2093 }()).OK
2094 )));
2095 static assert( is(typeof(Compileable!(0).OK)));
2096 static assert(!is(typeof(Compileable!(badfoo3()).OK)));
2097 static assert(!is(typeof(Compileable!(badfoo4()).OK)));
2098 //static assert(!is(typeof(Compileable!(badfoo5()).OK)));
2099 static assert(!is(typeof(Compileable!(badfoo6()).OK)));
2100 static assert(!is(typeof(Compileable!(badfoo7()).OK)));
2101 static assert(!is(typeof(Compileable!(badfoo8()).OK)));
2102
2103 // Functions which should pass CTFE
2104
goodfoo1()2105 int goodfoo1()
2106 {
2107 int[8] w; // use static array in CTFE
2108 w[] = 7; // full slice assign
2109 w[$ - 1] = 538; // use of $ in index assignment
2110 assert(w[6] == 7);
2111 return w[7];
2112 }
2113 static assert(goodfoo1() == 538);
2114
goodfoo2()2115 int goodfoo2()
2116 {
2117 S[4] w = S(101); // Block-initialize array of structs
2118 w[$ - 2].x = 917; // use $ in index member assignment
2119 w[$ - 2].y = 58; // this must not clobber the prev assignment
2120 return w[2].x; // check we got the correct one
2121 }
2122 static assert(goodfoo2() == 917);
2123
2124 static assert(is(typeof(Compileable!(
2125 (){
2126 S[4] w = void; // uninitialized array of structs
2127 w[$ - 2].x = 217; // initialize one member
2128 return w[2].x;
2129 }()).OK
2130 )));
2131
goodfoo4()2132 int goodfoo4()
2133 {
2134 S[4] b = [S(7), S(15), S(56), S(12)]; // assign from array literal
2135 assert(b[3] == S(12));
2136 return b[2].x - 55;
2137 }
2138 static assert(goodfoo4()==1);
2139
goodfoo5()2140 int goodfoo5()
2141 {
2142 S[4] b = [S(7), S(15), S(56), S(12)];
2143 b[0..2] = [S(2), S(6)]; // slice assignment from array literal
2144 assert(b[3] == S(12));
2145 assert(b[1] == S(6));
2146 return b[0].x;
2147 }
2148 static assert(goodfoo5() == 2);
2149 static assert(goodfoo5() == 2); // check for memory corruption
2150
goodfoo6()2151 int goodfoo6()
2152 {
2153 S[6] b = void;
2154 b[2..5] = [S(2), S(6), S(17)]; // slice assign to uninitialized var
2155 assert(b[4] == S(17));
2156 return b[3].x;
2157 }
2158 static assert(goodfoo6() == 6);
2159
goodfoo7()2160 int goodfoo7()
2161 {
2162 S[8] b = void;
2163 b[2..5] = S(217); // slice assign to uninitialized var
2164 assert(b[4] == S(217));
2165 return b[3].x;
2166 }
2167 static assert(goodfoo7() == 217);
2168
goodfoo8()2169 int goodfoo8()
2170 {
2171 S[] b = [S(7), S(15), S(56), S(12), S(67)];
2172 b[2..4] = S(17); // dynamic array block slice assign
2173 assert(b[3] == S(17));
2174 assert(b[4] == S(67));
2175 return b[0].x;
2176 }
2177 static assert(goodfoo8() == 7);
2178
2179 // --------- CTFE MEMBER FUNCTION TESTS --------
2180 struct Q
2181 {
2182 int x;
2183 char y;
opAddAssignQ2184 int opAddAssign(int w)
2185 {
2186 x += w;
2187 return x + w;
2188 }
opSubAssignQ2189 Q opSubAssign(int w)
2190 {
2191 x -= w;
2192 version(D_Version2) { mixin("return this;"); } else { mixin("return *this;"); }
2193 }
booQ2194 int boo() { return 4; }
cooQ2195 int coo() { return x; }
fooQ2196 int foo() { return coo(); }
dooQ2197 int doo(int a)
2198 {
2199 Q z = Q(a, 'x');
2200 z.x += 5;
2201 return z.coo() + 3 * x;
2202 }
gooQ2203 void goo(int z) { x = z; }
hooQ2204 int hoo(int y, int z) { return y + z; }
jooQ2205 void joo(int z)
2206 {
2207 x += z;
2208 }
2209 }
2210
memtest1()2211 int memtest1()
2212 {
2213 Q b = Q(15, 'a');
2214 return b.hoo(3, 16); // simple const function
2215 }
2216
2217 static assert(memtest1() == 19);
2218
memtest2()2219 int memtest2()
2220 {
2221 Q b = Q(15, 'x');
2222 b.x -= 10;
2223 return b.coo();
2224 }
2225
2226 static assert(memtest2() == 5);
2227
memtest3()2228 int memtest3()
2229 {
2230 Q b = Q(15, 'x');
2231 b.x -= 10;
2232 return b.foo();
2233 }
2234
2235 static assert(memtest3() == 5);
2236
memtest4()2237 int memtest4()
2238 {
2239 Q b = Q(12, 'x');
2240 return b.doo(514);
2241 }
2242 static assert(memtest4() == 519 + 3 * 12);
2243
2244
memtest5()2245 int memtest5()
2246 {
2247 Q b = Q(132, 'x');
2248 b.goo(4178); // Call modifying member
2249 return b.x;
2250 }
2251 static assert(memtest5() == 4178);
2252
memtest6()2253 int memtest6()
2254 {
2255 Q q = Q(1);
2256 q += 3; // operator overloading
2257 return q.x;
2258 }
2259 static assert(memtest6() == 4);
2260
2261 static assert(!is(typeof(Compileable!(Q += 2).OK))); // Mustn't cause segfault
2262
memtest7()2263 int memtest7()
2264 {
2265 Q q = Q(57);
2266 q -= 35;
2267 return q.x;
2268 }
2269
2270 static assert(memtest7() == 57 - 35);
2271
memtest8()2272 int memtest8()
2273 {
2274 Q[3] w;
2275 w[2].x = 17;
2276 w[2].joo(6); // Modify member of array
2277 w[1].x += 18;
2278 return w[2].coo();
2279 }
2280
2281 static assert(memtest8() == 6 + 17);
2282
2283 // --------- CTFE REF PASSING TESTS --------
2284
2285 // Bugzilla 1950 - CTFE doesn't work correctly for structs passed by ref
2286 struct S1950
2287 {
2288 int x;
2289 }
2290
foo1950()2291 int foo1950()
2292 {
2293 S1950 s = S1950(5); // explicitly initialized
2294 bar1950(s);
2295 return s.x;
2296 }
2297
bar1950(ref S1950 w)2298 void bar1950(ref S1950 w)
2299 {
2300 w.x = 10;
2301 }
2302
2303 static assert(foo1950() == 10); // OK <- Fails, x is 0
2304
foo1950b()2305 int foo1950b()
2306 {
2307 S1950 s; // uninitialized
2308 bar1950(s);
2309 return s.x;
2310 }
2311
2312 static assert(foo1950b() == 10); // OK <- Fails, x is 0
2313
2314
2315 // More extreme case, related to 1950
2316
bar1950c(ref int w)2317 void bar1950c(ref int w)
2318 {
2319 w = 87;
2320 }
2321
foo1950c()2322 int foo1950c()
2323 {
2324 int[5] x;
2325 x[] = 56;
2326 bar1950c(x[1]); // Non-trivial ref parameters
2327 return x[1];
2328 }
2329
2330 static assert(foo1950c() == 87);
2331
bar1950d(ref int[]w)2332 void bar1950d(ref int[] w)
2333 {
2334 w[1..$] = 87;
2335 w[0] += 15;
2336 }
2337
foo1950d()2338 int foo1950d()
2339 {
2340 int[] x = [1, 2, 3, 4, 5];
2341 x[1..$] = 56;
2342 bar1950d(x); // Non-trivial ref parameters
2343 assert(x[0] == 16);
2344 return x[1];
2345 }
2346
2347 static assert(foo1950d() == 87);
2348
2349 // Nested functions
nested(int x)2350 int nested(int x)
2351 {
2352 int y = 3;
2353 int inner(int w)
2354 {
2355 int z = 2;
2356 ++z;
2357 y += w;
2358 return x + 3;
2359 }
2360
2361 int z = inner(14);
2362 assert(y == 17);
2363 inner(8);
2364 assert(y == 17 + 8);
2365 return z + y;
2366 }
2367
2368 static assert(nested(7) == 17 + 8 + 10);
2369 static assert(nested(7) == 17 + 8 + 10);
2370
2371 // Recursive nested functions
2372
nested2(int x)2373 int nested2(int x)
2374 {
2375 int y = 3;
2376 int inner(int w)
2377 {
2378 int z = 2;
2379 ++z;
2380 ++y;
2381 if (w <= 1)
2382 return x + 3;
2383 else
2384 return inner(w - 1);
2385 }
2386
2387 int z = inner(14);
2388 assert(y == 17);
2389
2390 inner(8);
2391 assert(y == 17 + 8);
2392 return z + y;
2393 }
2394
2395 static assert(nested2(7) == 17 + 8 + 10);
2396
2397 // 1605 D1 & D2. break in switch with goto breaks in ctfe
bug1605()2398 int bug1605()
2399 {
2400 int i = 0;
2401 while (true)
2402 {
2403 goto LABEL;
2404 LABEL:
2405 if (i != 0)
2406 return i;
2407 i = 27;
2408 }
2409 assert(i == 27);
2410 return 88; // unreachable
2411 }
2412
2413 static assert(bug1605() == 27);
2414
2415 // 2564. D2 only. CTFE: the index in a tuple foreach is uninitialized (bogus error)
2416 // NOTE: Beware of optimizer bug 3264.
2417
bug2564()2418 int bug2564()
2419 {
2420 version(D_Version2) { mixin("enum int Q = 0;"); }else {mixin("int Q = 0;"); }
2421 string [2] s = ["a", "b"];
2422 assert(s[Q].dup == "a");
2423 return 0;
2424 }
2425
2426 static int bug2564b = bug2564();
2427
2428
2429 // 1461 D1 + D2. Local variable as template alias parameter breaks CTFE
bug1461()2430 void bug1461()
2431 {
2432 int x;
2433 static assert(Gen1461!(x).generate() == null);
2434 }
2435
Gen1461(alias A)2436 template Gen1461(alias A)
2437 {
2438 string generate()
2439 {
2440 return null;
2441 }
2442 }
2443
2444 /************************************************/
2445
foo104(string[]a...)2446 string foo104(string[] a...)
2447 {
2448 string result = "";
2449 foreach (s; a)
2450 result ~= s;
2451 return result;
2452 }
2453
2454 mixin (foo104("int ", "x;"));
2455
2456 /************************************************/
2457
2458 struct SwineFlu
2459 {
2460 int a;
2461 int b;
2462 }
2463
2464 struct Infection
2465 {
2466 SwineFlu y;
2467 }
2468
2469 struct IveGotSwineFlu
2470 {
2471 Infection x;
2472 int z;
oinkIveGotSwineFlu2473 int oink() { return x.y.a + 10; }
2474 }
2475
quarantine()2476 int quarantine()
2477 {
2478 IveGotSwineFlu d;
2479 return d.oink();
2480 }
2481
2482 struct Mexico
2483 {
2484 Infection x;
2485 int z = 2;
oinkMexico2486 int oink() { return z + x.y.b; }
2487 }
2488
mediafrenzy()2489 int mediafrenzy()
2490 {
2491 Mexico m;
2492 return m.oink;
2493 }
2494
2495 static assert(quarantine() == 10);
2496 static assert(mediafrenzy() == 2);
2497
2498 /************************************************/
2499
ctfeArrayTest(int z)2500 int ctfeArrayTest(int z)
2501 {
2502 int[] a = new int[z];
2503 a[$ - 3] = 6;
2504 assert(a.length == z);
2505 return a[$ - 3];
2506 }
2507 static assert(ctfeArrayTest(15) == 6);
2508
2509 /************************************************/
2510
bugzilla1298()2511 char bugzilla1298()
2512 {
2513 char [4] q = "abcd".dup;
2514 char [4] r = ['a', 'b', 'c', 'd'];
2515 assert(q == r);
2516 q[0..2] = "xy";
2517 q[2] += 3;
2518 return q[2];
2519 }
2520
2521 static assert(bugzilla1298() == 'f');
2522
bugzilla1790(Types...)2523 int bugzilla1790(Types...)()
2524 {
2525 foreach (T; Types)
2526 {
2527 }
2528 return 0;
2529 }
2530
2531 const int bugs1790 = bugzilla1790!("")();
2532
ctfeStrTest1()2533 char ctfeStrTest1()
2534 {
2535 char [8] s = void;
2536 s[2..4] = 'x';
2537
2538 assert(s.length == 8);
2539 return s[3];
2540 }
2541
2542 static assert(ctfeStrTest1() == 'x');
2543
2544 //--------- DELEGATE TESTS ------
2545
2546 // Function + delegate literals inside CTFE
delegtest1()2547 int delegtest1()
2548 {
2549 assert(function int(int a){ return 7 + a; }(16) == 23);
2550 return delegate int(int a){ return 7 + a; }(6);
2551 }
2552
delegtest2()2553 int delegtest2()
2554 {
2555 int innerfunc1()
2556 {
2557 return delegate int(int a){ return 7 + a; }(6);
2558 }
2559 int delegate() f = &innerfunc1;
2560 return 3 * f();
2561 }
2562
delegtest3()2563 int delegtest3()
2564 {
2565 int function() f = &delegtest1;
2566 return 3 * f();
2567 }
2568
2569 struct DelegStruct
2570 {
2571 int a;
barDelegStruct2572 int bar(int x) { return a + x; }
2573 }
2574
delegtest4()2575 int delegtest4()
2576 {
2577 DelegStruct s;
2578 s.a = 5;
2579 auto f = &s.bar;
2580 return f(3);
2581 }
2582
2583 alias int delegate(int) DelegType;
2584
2585 // Test arrays of delegates
delegtest5()2586 int delegtest5()
2587 {
2588 DelegStruct s;
2589 s.a = 5;
2590 DelegType[4] w;
2591 w[] = &s.bar;
2592 return w[2](3);
2593 }
2594
2595 // Test arrays of structs of delegates
2596 struct FoolishStruct
2597 {
2598 DelegType z;
2599 }
2600
delegtest6()2601 int delegtest6()
2602 {
2603 DelegStruct s;
2604 s.a = 5;
2605 FoolishStruct k[3];
2606 DelegType u = &s.bar;
2607 k[1].z = u;
2608 return k[1].z(3);
2609 }
2610
2611 static assert(delegtest1() == 13);
2612 static assert(delegtest2() == 39);
2613 static assert(delegtest3() == 39);
2614 static assert(delegtest4() == 8);
2615 static assert(delegtest5() == 8);
2616 static assert(delegtest6() == 8);
2617
2618 // Function + delegate literals, module scope
2619 static assert(function int(int a){ return 17 + a; }(16) == 33);
2620 static assert( (int a){ return 7 + a; }(16) == 23);
2621
2622 // --- Test lazy ---
lazyTest1(lazy int y)2623 int lazyTest1(lazy int y)
2624 {
2625 return y + 1;
2626 }
2627
lazyTest2(int x)2628 int lazyTest2(int x)
2629 {
2630 return lazyTest1(x);
2631 }
2632
2633 static assert(lazyTest1(7) == 8);
2634 static assert(lazyTest2(17) == 18);
2635
2636 /************************************************/
2637
version(D_Version2)2638 version(D_Version2)
2639 {
2640 // Bug 4020 and 4027 are D2 only
2641
2642 struct PostblitCrash
2643 {
2644 int x;
2645 mixin("this(this) { ++x; }");
2646 }
2647
2648 int bug4020()
2649 {
2650 PostblitCrash f;
2651 f.x = 3;
2652 f = f;
2653 f = f;
2654 return f.x;
2655 }
2656 static assert(bug4020() == 5);
2657
2658 string delegate() bug4027(string s)
2659 {
2660 return { return s; };
2661 }
2662
2663 // If it compiles, it must not generate wrong code on D2.
2664 static if (is(typeof((){ static const s = bug4027("aaa")(); }()))) {
2665 static assert(bug4027("aaa")() == "aaa");
2666 static assert(bug4027("bbb")() == "bbb");
2667 }
2668 }
2669
2670 // ---
2671
bug4004a(ref int a)2672 void bug4004a(ref int a)
2673 {
2674 assert(a == 7);
2675 a += 3;
2676 }
2677
bug4004b(ref int b)2678 void bug4004b(ref int b)
2679 {
2680 b = 7;
2681 bug4004a(b);
2682 }
2683
bug4004c()2684 int bug4004c()
2685 {
2686 int offset = 5;
2687 bug4004b(offset);
2688 return offset;
2689 }
2690
2691 static assert(bug4004c() == 10);
2692
2693 // ---
2694
bug4019()2695 int bug4019()
2696 {
2697 int[int] aa;
2698 aa[1] = 2;
2699 aa[4] = 6;
2700 return aa[1] + aa[4];
2701 }
2702 static assert(bug4019() == 8);
2703
2704 // ---
2705
delegate()2706 string delegate() bug4029a()
2707 {
2708 return { return "abc"[]; };
2709 }
2710
bug4029()2711 string bug4029()
2712 {
2713 return bug4029a()();
2714 }
2715
2716 static assert(bug4029() == "abc");
2717
2718 /************************************************/
2719
bug4078()2720 int bug4078()
2721 {
2722 int[] arr = new int[1];
2723 return arr[0];
2724 }
2725 static assert(bug4078() == 0);
2726
bug4052()2727 int bug4052()
2728 {
2729 int[] arr = new int[1];
2730 int s;
2731 foreach (x; arr)
2732 s += x;
2733 foreach (x; arr)
2734 s += x * x;
2735 return 4052;
2736 }
2737 static assert(bug4052() == 4052);
2738
bug4252()2739 int bug4252()
2740 {
2741 char [] s = "abc".dup;
2742 s[15] = 'd'; // Array bounds error
2743 return 3;
2744 }
2745
2746 static assert(!is(typeof(Compileable!(bug4252()))));
2747
setlen1()2748 size_t setlen1()
2749 {
2750 int[] w = new int[4];
2751 w[] = 7;
2752 w.length = 6;
2753 return 21 + w.length;
2754 }
2755
2756 static assert(setlen1() == 27);
2757
setlen2()2758 size_t setlen2()
2759 {
2760 int[] w;
2761 w.length = 15;
2762 assert(w[3] == 0);
2763 w[2] = 8;
2764 w[14] = 7;
2765 w.length = 12; // check shrinking
2766 assert(w[2] == 8);
2767 return 2 + w.length;
2768 }
2769
2770 static assert(setlen2() == 14);
2771
2772 /************************************************/
2773
bug4257(ref int x)2774 int bug4257(ref int x)
2775 {
2776 return 3;
2777 }
2778
bug4257c(int x)2779 int bug4257c(int x)
2780 {
2781 return 3;
2782 }
2783
2784 struct Struct4257
2785 {
fooStruct42572786 int foo() { return 2; }
2787 }
2788
bug4257b()2789 void bug4257b()
2790 {
2791 int y;
2792 static assert(!is(typeof(Compileable!(bug4257(y)))));
2793 static assert(!is(typeof(Compileable!(bug4257c(y)))));
2794 Struct4257 s;
2795 static assert(!is(typeof(Compileable!(s.foo()))));
2796 }
2797
2798 /************************************************/
2799 // 5117
2800
2801 static int dummy5117 = test5117();
2802
test5117()2803 int test5117()
2804 {
2805 S5117 s;
2806 s.change();
2807 assert(s.value == 1); // (7) succeeds
2808
2809 R5117 r;
2810 r.s.change();
2811 assert(r.s.value == 1); // (11) fails, value == 0
2812
2813 return 0;
2814 }
2815
2816 struct S5117
2817 {
2818 int value;
changeS51172819 void change() { value = 1; }
2820 }
2821
2822 struct R5117
2823 {
2824 S5117 s;
2825 }
2826
2827 /************************************************/
2828
2829 enum dummy5117b = test5117b();
2830
test5117b()2831 int test5117b()
2832 {
2833 S5117b s;
2834 getRef5117b(s).change();
2835 assert(s.value == 1); // fails, value == 0
2836 return 0;
2837 }
getRef5117b(ref S5117b s)2838 ref S5117b getRef5117b(ref S5117b s) { return s; }
2839
2840 struct S5117b
2841 {
2842 int value;
changeS5117b2843 void change() { value = 1; }
2844 }
2845
2846 /************************************************/
2847 // 6439
2848
2849 struct A6439
2850 {
this(uint a,uint b)2851 this(uint a, uint b)
2852 {
2853 begin = a;
2854 end = b;
2855 }
2856 union
2857 {
2858 struct
2859 {
2860 uint begin, end;
2861 }
2862 uint[2] arr;
2863 }
2864 }
2865
2866 void test6439()
2867 {
2868 enum y = A6439(10, 20);
2869 A6439 y2 = A6439(10, 20);
2870 assert(y2.begin == y.begin && y2.end == y.end); //passes
2871 assert(y.arr != [0,0]);
2872 assert(y.arr == [10,20]);
2873 assert(y.arr == y2.arr);
2874 }
2875
2876 /************************************************/
2877 // from tests/fail_compilation/fail147
2878
2879 static assert(!is(typeof(Compileable!(
2880 (int i){
2881 int x = void;
2882 ++x; // used before initialization
2883 return i + x;
2884 }(3)
2885 ))));
2886
2887 // 6504 regression
2888 void test6504()
2889 {
2890 for (int i = 0; i < 3; ++i)
2891 {
2892 char[] x2 = "xxx" ~ ['c'];
2893 assert(x2[1] == 'x');
2894 x2[1] = 'q';
2895 }
2896 }
2897
2898 // 8818 regression
2899 void test8818()
2900 {
2901 static bool test()
2902 {
2903 string op1 = "aa";
2904 string op2 = "b";
2905 assert("b" >= "aa");
2906 assert(op2 >= op1);
2907 return true;
2908 }
2909 static assert(test());
2910 assert(test());
2911 }
2912
2913 /************************************************/
2914
2915 struct Test104Node
2916 {
2917 int val;
2918 Test104Node* next;
2919 }
2920
2921 Test104Node* CreateList(int[] arr)
2922 {
2923 if (!arr.length)
2924 return null;
2925 Test104Node* ret = new Test104Node;
2926 ret.val = arr[0];
2927 ret.next = CreateList(arr[1..$]);
2928 return ret;
2929 }
2930
2931 const(Test104Node)* root = CreateList([1, 2, 3, 4, 5]);
2932
2933 void test104()
2934 {
2935 assert(root.val == 1);
2936 assert(root.next.val == 2);
2937 assert(root.next.next.val == 3);
2938 assert(root.next.next.next.val == 4);
2939 assert(root.next.next.next.next.val == 5);
2940 }
2941
2942 /************************************************/
2943
2944 interface ITest105a
2945 {
2946 string test105a() const;
2947 }
2948
2949 class Test105a : ITest105a
2950 {
2951 char a;
2952 int b;
2953 char c = 'C';
2954 int d = 42;
2955 string test105a() const { return "test105a"; }
2956 }
2957
2958 interface ITest105b
2959 {
2960 string test105b() const;
2961 }
2962
2963 class Test105b : Test105a, ITest105b
2964 {
2965 char e;
2966 int f;
2967 this(char _e, int _f, char _a, int _b) pure
2968 {
2969 e = _e;
2970 f = _f;
2971 a = _a;
2972 b = _b;
2973 }
2974 string test105b() const { return "test105b"; }
2975 }
2976
2977 const Test105b t105b = new Test105b('E', 88, 'A', 99);
2978 const Test105a t105a = new Test105b('E', 88, 'A', 99);
2979 const ITest105b t105ib = new Test105b('E', 88, 'A', 99);
2980 const ITest105a t105ia = new Test105b('E', 88, 'A', 99);
2981 __gshared Test105b t105gs = new Test105b('E', 88, 'A', 99);
2982 shared Test105b t105bs = new shared(Test105b)('E', 88, 'A', 99);
2983 immutable Test105b t105bi = new immutable(Test105b)('E', 88, 'A', 99);
2984
2985 void test105()
2986 {
2987 assert(t105b.a == 'A');
2988 assert(t105b.b == 99);
2989 assert(t105b.c == 'C');
2990 assert(t105b.d == 42);
2991 assert(t105b.e == 'E');
2992 assert(t105b.f == 88);
2993 assert(t105b.test105a() == "test105a");
2994 assert(t105b.test105b() == "test105b");
2995
2996 assert(t105a.a == 'A');
2997 assert(t105a.b == 99);
2998 assert(t105a.c == 'C');
2999 assert(t105a.d == 42);
3000 assert(t105a.test105a() == "test105a");
3001
3002 assert(t105ia.test105a() == "test105a");
3003 assert(t105ib.test105b() == "test105b");
3004
3005 assert(t105a.classinfo is Test105b.classinfo);
3006 //t105b.d = -1;
3007 //assert(t105b.d == -1);
3008 //assert(t105a.d == 42);
3009
3010 assert(t105gs.a == 'A');
3011 assert(t105gs.b == 99);
3012 assert(t105gs.c == 'C');
3013 assert(t105gs.d == 42);
3014 assert(t105gs.e == 'E');
3015 assert(t105gs.f == 88);
3016 assert(t105gs.test105a() == "test105a");
3017 assert(t105gs.test105b() == "test105b");
3018
3019 assert(t105bs.a == 'A');
3020 assert(t105bs.b == 99);
3021 assert(t105bs.c == 'C');
3022 assert(t105bs.d == 42);
3023 assert(t105bs.e == 'E');
3024 assert(t105bs.f == 88);
3025
3026 assert(t105bi.a == 'A');
3027 assert(t105bi.b == 99);
3028 assert(t105bi.c == 'C');
3029 assert(t105bi.d == 42);
3030 assert(t105bi.e == 'E');
3031 assert(t105bi.f == 88);
3032 assert(t105bi.test105a() == "test105a");
3033 assert(t105bi.test105b() == "test105b");
3034 }
3035
3036 int bug9938()
3037 {
3038 assert(t105ia.test105a() == "test105a");
3039 return 1;
3040 }
3041
3042 static assert(t105ia.test105a() == "test105a");
3043 static assert(bug9938());
3044
3045 /************************************************/
3046
3047 struct Test106
3048 {
3049 Test106* f;
3050 Test106* s;
3051 }
3052
3053 Test106* ctfe106()
3054 {
3055 auto s = new Test106;
3056 auto s2 = new Test106;
3057 s.f = s2;
3058 s.s = s2;
3059 assert(s.f is s.s);
3060 return s;
3061 }
3062
3063 const(Test106)* t106 = ctfe106();
3064
3065 void test106()
3066 {
3067 assert(t106.f is t106.s);
3068 }
3069
3070 /************************************************/
3071
3072 class Test107
3073 {
3074 Test107 a;
3075 Test107 b;
3076
3077 this()
3078 {
3079 }
3080
3081 this(int)
3082 {
3083 a = new Test107();
3084 b = a;
3085 assert(a is b);
3086 }
3087 }
3088
3089 const Test107 t107 = new Test107(1);
3090
3091 void test107()
3092 {
3093 assert(t107.a is t107.b);
3094 }
3095
3096 /************************************************/
3097
3098 /*
3099 interface Getter
3100 {
3101 int getNum() const;
3102 }
3103
3104 class Test108 : Getter
3105 {
3106 int f;
3107 this(int v) inout
3108 {
3109 f = v;
3110 }
3111
3112 int getNum() const
3113 {
3114 return f;
3115 }
3116 }
3117
3118 enum const(Test108) t108 = new Test108(38);
3119
3120 void test108()
3121 {
3122 const Test108 obj = t108;
3123 assert(obj.classinfo is Test108.classinfo);
3124 assert(obj.f == 38);
3125
3126 const Getter iobj = t108;
3127 assert(iobj.getNum() == 38);
3128 assert((cast(Object)iobj).classinfo is Test108.classinfo);
3129 assert(t108 is t108);
3130 }
3131 */
3132
3133 /***** Bug 5678 *********************************/
3134
3135 /*
3136 struct Bug5678
3137 {
3138 this(int) {}
3139 }
3140
3141 enum const(Bug5678)* b5678 = new const(Bug5678)(0);
3142
3143 void test5678()
3144 {
3145 assert(b5678 is b5678);
3146 }*/
3147
3148 /************************************************/
3149
3150 class Test109C { this(){ this.c = this; } Test109C c; }
3151 const t109c = new Test109C();
3152
3153 struct Test109S { this(int){ this.s = &this; } Test109S* s; }
3154 const t109s = new Test109S(0);
3155 pragma(msg, t109s); // Make sure there is no infinite recursion.
3156
3157 void test109()
3158 {
3159 assert(t109c.c is t109c);
3160 assert(t109s.s is t109s);
3161 }
3162
3163 /************************************************/
3164
3165 struct Test110f { int f1; Test110s f2; }
3166 struct Test110s { this(int, int, int){} }
3167 auto test110 = [Test110f(1, Test110s(1, 2, 3))];
3168
3169 /************************************************/
3170 // 6907
3171
3172 int test6907()
3173 {
3174 int dtor1;
3175 class C { ~this() { ++dtor1; } }
3176
3177 // delete on Object
3178 { Object o; delete o; }
3179 { scope o = new Object(); }
3180 { Object o = new Object(); delete o; }
3181
3182 // delete on C
3183 { C c; delete c; }
3184 { { scope c = new C(); } assert(dtor1 == 1); }
3185 { { scope Object o = new C(); } assert(dtor1 == 2); }
3186 { C c = new C(); delete c; assert(dtor1 == 3); }
3187 { Object o = new C(); delete o; assert(dtor1 == 4); }
3188
3189 int dtor2;
3190 struct S1 { ~this() { ++dtor2; } }
3191
3192 // delete on S1
3193 { S1* p; delete p; }
3194 { S1* p = new S1(); delete p; assert(dtor2 == 1); }
3195
3196 // delete on S1[]
3197 { S1[] a = [S1(), S1()]; delete a; assert(dtor2 == 3); }
3198
3199 return 1;
3200 }
3201 static assert(test6907());
3202
3203 /************************************************/
3204 // 9023
3205
3206 bool test9023()
3207 {
3208 string[][string] aas;
3209 assert(aas.length == 0);
3210 aas["a"] ~= "anything";
3211 assert(aas.length == 1);
3212 assert(aas["a"] == ["anything"]);
3213 aas["a"] ~= "more";
3214 assert(aas.length == 1);
3215 assert(aas["a"] == ["anything", "more"]);
3216
3217 int[int] aan;
3218 assert(aan.length == 0);
3219 auto x = aan[0]++;
3220 assert(x == 0);
3221 assert(aan.length == 1);
3222 assert(aan[0] == 1);
3223
3224 return true;
3225 }
3226 static assert(test9023());
3227
3228 /************************************************/
3229 // 15817
3230
3231 S[] split15817(S)(S s)
3232 {
3233 size_t istart;
3234 S[] result;
3235
3236 foreach (i, c ; s)
3237 result ~= s[istart .. i];
3238 return result;
3239 }
3240
3241 int test15817()
3242 {
3243 auto targets = `a1`.split15817;
3244 uint[string] counts;
3245 foreach (a; targets)
3246 counts[a]++;
3247 assert(counts == ["":1u, "a":1]);
3248 return 1;
3249 }
3250 static assert(test15817());
3251
3252 /************************************************/
3253
3254 interface IBug9954
3255 {
3256 string foo() const;
3257 }
3258
3259 class Bug9954 : IBug9954
3260 {
3261 string foo() const { return "hello"; }
3262 }
3263
3264 IBug9954 makeIBug9954()
3265 {
3266 return new Bug9954;
3267 }
3268
3269 const IBug9954 b9954 = makeIBug9954();
3270
3271 void test9954()
3272 {
3273 assert(b9954.foo() == "hello");
3274 }
3275
3276 /************************************************/
3277 // 10483
3278
3279 struct Bug10483
3280 {
3281 int val[3][4];
3282 }
3283
3284 struct Outer10483
3285 {
3286 Bug10483 p = Bug10483(67);
3287 }
3288
3289 int k10483a = Outer10483.init.p.val[2][2]; // ICE(expression.c)
3290
3291 void test10483()
3292 {
3293 int k10483b = Outer10483.init.p.val[2][2]; // Segfault (backend/type.c)
3294 }
3295
3296 /************************************************/
3297
3298 struct S10669 { uint x; }
3299
3300 static const S10669 iid0_10669 = S10669(0);
3301
3302 class C10669
3303 {
3304 static const S10669 iid1_10669 = S10669(1);
3305 };
3306
3307 const S10669 IID0_10669 = iid0_10669;
3308 const S10669 IID1_10669 = C10669.iid1_10669;
3309
3310 /************************************************/
3311
3312 TypeInfo getTi()
3313 {
3314 return typeid(int);
3315 }
3316
3317 auto t112 = getTi();
3318
3319 void test112()
3320 {
3321 assert(t112.toString() == "int");
3322 }
3323
3324 /************************************************/
3325 // 10687
3326
3327 enum Foo10687 : uint { A, B, C, D, E }
3328 immutable uint[5][] m10687 = [[0, 1, 2, 3, 4]];
3329
3330 void test10687()
3331 {
3332 static immutable uint[5] a1 = [0, 1, 2, 3, 4];
3333 auto a2 = cast(immutable(Foo10687[5]))a1;
3334 static a3 = cast(immutable(Foo10687[5]))a1;
3335
3336 auto foos1 = cast(immutable(Foo10687[5][]))m10687;
3337 static foos2 = cast(immutable(Foo10687[5][]))m10687;
3338 }
3339
3340 /************************************************/
3341
3342 void test113()
3343 {
3344 import core.math;
3345
3346 static void compare(real a, real b)
3347 {
3348 writefln("compare(%30.30f, %30.30f);", a, b);
3349 assert(fabs(a - b) < 128 * real.epsilon);
3350 }
3351
3352 static if (__traits(compiles, (){ enum real ctval1 = yl2x(3.14, 1); }))
3353 {
3354 enum real ctval1 = yl2x(3.14, 1);
3355 enum real ctval2 = yl2x(2e1500L, 3);
3356 enum real ctval3 = yl2x(1, 5);
3357
3358 real rtval1 = yl2x(3.14, 1);
3359 real rtval2 = yl2x(2e1500L, 3);
3360 real rtval3 = yl2x(1, 5);
3361
3362 compare(ctval1, rtval1);
3363 compare(ctval2, rtval2);
3364 compare(ctval3, rtval3);
3365 }
3366
3367 static if (__traits(compiles, (){ enum real ctval4 = yl2xp1(3.14, 1); }))
3368 {
3369 enum real ctval4 = yl2xp1(3.14, 1);
3370 enum real ctval5 = yl2xp1(2e1500L, 3);
3371 enum real ctval6 = yl2xp1(1, 5);
3372
3373 real rtval4 = yl2xp1(3.14, 1);
3374 real rtval5 = yl2xp1(2e1500L, 3);
3375 real rtval6 = yl2xp1(1, 5);
3376
3377 compare(ctval4, rtval4);
3378 compare(ctval5, rtval5);
3379 compare(ctval6, rtval6);
3380 }
3381 }
3382
3383 /************************************************/
3384 // 14140
3385
3386 struct S14140
3387 {
3388 union
3389 {
3390 float[3][1] A;
3391 float[3] flat;
3392 }
3393
3394 this(in float[] args...)
3395 {
3396 flat[] = args[];
3397 }
3398 }
3399
3400 class C14140
3401 {
3402 union
3403 {
3404 float[3][1] A;
3405 float[3] flat;
3406 }
3407
3408 this(in float[] args...)
3409 {
3410 flat[] = args[];
3411 }
3412 }
3413
3414 immutable s14140 = S14140(0, 1, 0);
3415 const c14140 = new C14140(0, 1, 0);
3416
3417 void test14140()
3418 {
3419 auto s = s14140;
3420 assert(s.flat == [0, 1, 0]);
3421
3422 auto c = c14140;
3423 assert(c.flat == [0, 1, 0]);
3424 }
3425
3426 /************************************************/
3427 // 14862
3428
3429 struct S14862
3430 {
3431 union
3432 {
3433 struct { uint hi, lo; }
3434 ulong data;
3435 }
3436
3437 this(ulong data)
3438 {
3439 this.data = data;
3440 }
3441 }
3442
3443 void test14862()
3444 {
3445 S14862 s14862 = S14862(123UL);
3446 enum S14862 e14862 = S14862(123UL);
3447 static S14862 g14862 = S14862(123UL);
3448
3449 assert(s14862.data == 123UL); // OK
3450 assert(e14862.data == 123UL); // OK
3451 assert(g14862.data == 123UL); // OK <- fail
3452 }
3453
3454 /************************************************/
3455 // 15681
3456
3457 void test15681()
3458 {
3459 static struct A { float value; }
3460
3461 static struct S
3462 {
3463 A[2] values;
3464
3465 this(float)
3466 {
3467 values[0].value = 0;
3468 values[1].value = 1;
3469 }
3470 }
3471
3472 auto s1 = S(1.0f);
3473 assert(s1.values[0].value == 0); // OK
3474 assert(s1.values[1].value == 1); // OK
3475
3476 enum s2 = S(1.0f);
3477 static assert(s2.values[0].value == 0); // OK <- NG
3478 static assert(s2.values[1].value == 1); // OK
3479 assert(s2.values[0].value == 0); // OK <- NG
3480 assert(s2.values[1].value == 1); // OK
3481 }
3482
3483 /************************************************/
3484 // toPrec
3485
3486 void testToPrec()
3487 {
3488 import core.math;
3489
3490 enum real ctpir = 0xc.90fdaa22168c235p-2;
3491 enum double ctpid = 0x1.921fb54442d18p+1;
3492 enum float ctpif = 0x1.921fb6p+1;
3493 static assert(toPrec!float(ctpir) == ctpif);
3494 static assert(toPrec!double(ctpir) == ctpid);
3495 static assert(toPrec!real(ctpir) == ctpir);
3496 static assert(toPrec!float(ctpid) == ctpif);
3497 static assert(toPrec!double(ctpid) == ctpid);
3498 static assert(toPrec!real(ctpid) == ctpid);
3499 static assert(toPrec!float(ctpif) == ctpif);
3500 static assert(toPrec!double(ctpif) == ctpif);
3501 static assert(toPrec!real(ctpif) == ctpif);
3502
3503 assert(toPrec!float(ctpir) == ctpif);
3504 assert(toPrec!double(ctpir) == ctpid);
3505 assert(toPrec!real(ctpir) == ctpir);
3506 assert(toPrec!float(ctpid) == ctpif);
3507 assert(toPrec!double(ctpid) == ctpid);
3508 assert(toPrec!real(ctpid) == ctpid);
3509 assert(toPrec!float(ctpif) == ctpif);
3510 assert(toPrec!double(ctpif) == ctpif);
3511 assert(toPrec!real(ctpif) == ctpif);
3512
3513 static real rtpir = 0xc.90fdaa22168c235p-2;
3514 static double rtpid = 0x1.921fb54442d18p+1;
3515 static float rtpif = 0x1.921fb6p+1;
3516 assert(toPrec!float(rtpir) == rtpif);
3517 assert(toPrec!double(rtpir) == rtpid);
3518 assert(toPrec!real(rtpir) == rtpir);
3519 assert(toPrec!float(rtpid) == rtpif);
3520 assert(toPrec!double(rtpid) == rtpid);
3521 assert(toPrec!real(rtpid) == rtpid);
3522 assert(toPrec!float(rtpif) == rtpif);
3523 assert(toPrec!double(rtpif) == rtpif);
3524 assert(toPrec!real(rtpif) == rtpif);
3525 }
3526
3527 /************************************************/
3528
3529 int main()
3530 {
3531 test1();
3532 test2();
3533 test3();
3534 test4();
3535 test5();
3536 test6();
3537 test7();
3538 test8();
3539 test9();
3540 test10();
3541 test11();
3542 test12();
3543 test13();
3544 test14();
3545 test15();
3546 test16();
3547 test17();
3548 test18();
3549 test19();
3550 test20();
3551 test21();
3552 test22();
3553 test23();
3554 test24();
3555 test25();
3556 test26();
3557 test27();
3558 test28();
3559 test29();
3560 test30();
3561 test31();
3562 test32();
3563 test33();
3564 test34();
3565 test35();
3566 test36();
3567 test37();
3568 test38();
3569 test39();
3570 test40();
3571 test41();
3572 test42();
3573 test43();
3574 test44();
3575 test45();
3576 test46();
3577 test47();
3578 test48();
3579 test49();
3580 test50();
3581 test51();
3582 test52();
3583 test53();
3584 test54();
3585 test55();
3586 test56();
3587 test57();
3588 test58();
3589 test59();
3590 test60();
3591 test61();
3592 test62();
3593 test63();
3594 test64();
3595 test65();
3596 test66();
3597 test67();
3598 test68();
3599 test69();
3600 test70();
3601 test71();
3602 test72();
3603 test73();
3604 test74();
3605 test75();
3606 test76();
3607 test77();
3608 test78();
3609 test79();
3610 test80();
3611 test81();
3612 test82();
3613 test83();
3614 test84();
3615 test85();
3616 test86();
3617 test87();
3618 test88();
3619 test89();
3620 test90();
3621 test91();
3622 test92();
3623 test93();
3624 test94();
3625 test95();
3626 test96();
3627 test97();
3628 test98();
3629 test99();
3630 test100();
3631 test101();
3632 test102();
3633 test103();
3634 test104();
3635 test105();
3636 test106();
3637 test107();
3638 //test108();
3639 test109();
3640 test112();
3641 test113();
3642 test6439();
3643 test6504();
3644 test8818();
3645 test6907();
3646 test9023();
3647 test15817();
3648 test9954();
3649 test14140();
3650 test14862();
3651 test15681();
3652
3653 printf("Success\n");
3654 return 0;
3655 }
3656