Debian Codegraph @

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

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