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