]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/runnable/test23.d
Add D front-end, libphobos library, and D2 testsuite.
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / test23.d
1 // REQUIRED_ARGS:
2
3 module test;
4
5 import core.vararg;
6 import core.stdc.stdlib;
7 import std.stdio;
8 import std.string;
9 import core.stdc.stdlib;
10
11
12 /*******************************************/
13
14 struct S
15 {
16 int opSliceAssign(int v, size_t i, size_t j)
17 {
18 assert(v == 5);
19 assert(i == 9);
20 assert(j == 10);
21 return 3;
22 }
23
24 int opSliceAssign(int v)
25 {
26 assert(v == 6);
27 return 11;
28 }
29 }
30
31 void test1()
32 {
33 S s;
34
35 assert((s[9 .. 10] = 5) == 3);
36 assert((s[] = 6) == 11);
37 }
38
39 /*******************************************/
40
41 static int i2 = 1;
42
43 void test2()
44 {
45 synchronized { int i2 = 2; }
46 assert(i2 == 1);
47 }
48
49 /*******************************************/
50
51 void test3()
52 {
53 size_t border = 8;
54
55 for(ulong i = 0; i < border; i++)
56 {
57 ulong test = 1;
58 test <<= i;
59 double r = test;
60 ulong result = cast(ulong)r;
61
62 if (result != test)
63 {
64 assert(0);
65 }
66 }
67 }
68
69 /*******************************************/
70
71 void test4()
72 {
73 writeln("",true);
74 }
75
76 /*******************************************/
77
78 void test5()
79 {
80 int[] qwert = new int[6];
81 int[] yuiop;
82 yuiop = qwert[2..5] = 3;
83 assert(yuiop.length == 3);
84 assert(yuiop[0] == 3);
85 assert(yuiop[1] == 3);
86 assert(yuiop[2] == 3);
87 }
88
89 /*******************************************/
90
91 struct Foo6
92 {
93 static int x;
94
95 static int[] farray()
96 {
97 printf("farray\n");
98 assert(x == 0);
99 x++;
100 return new int[6];
101 }
102
103 static int flwr()
104 {
105 printf("flwr\n");
106 assert(x == 1);
107 x++;
108 return 2;
109 }
110
111 static int fupr()
112 {
113 printf("fupr\n");
114 assert(x == 2);
115 x++;
116 return 1;
117 }
118 }
119
120 void test6()
121 {
122 int[] yuiop;
123 yuiop =
124 Foo6.farray()[Foo6.flwr() .. $ - Foo6.fupr()] = 3;
125 assert(Foo6.x == 3);
126 assert(yuiop.length == 3);
127 assert(yuiop[0] == 3);
128 assert(yuiop[1] == 3);
129 assert(yuiop[2] == 3);
130 }
131
132 /*******************************************/
133
134 void test7()
135 {
136 real a = 3.40483; // this is treated as 3.40483L
137 real b;
138 b = 3.40483;
139 assert(a==b);
140 assert(a==3.40483);
141 assert(a==3.40483L);
142 assert(a==3.40483F);
143 }
144
145 /*******************************************/
146
147 void test8()
148 {
149 real [5][5] m = 1;
150 m[1][1..3] = 2;
151
152 for (size_t i = 0; i < 5; i++)
153 for (size_t j = 0; j < 5; j++)
154 {
155 if (i == 1 && (j >= 1 && j < 3))
156 assert(m[i][j] == 2);
157 else
158 assert(m[i][j] == 1);
159 }
160 }
161
162 /*******************************************/
163
164 class ClassOf(Type)
165 {
166 Type val;
167
168 template refx()
169 {
170 alias val refx;
171 }
172 }
173
174 struct StructOf
175 {
176 int val;
177
178 template refx()
179 {
180 alias val refx;
181 }
182 }
183
184 void test9()
185 {
186 ClassOf!(int) c = new ClassOf!(int)();
187 StructOf s;
188 int x = 10;
189
190 c.refx!() = x;
191 x = c.refx!();
192 assert(x == 10);
193
194 x = 11;
195 s.refx!() = x;
196 x = s.refx!();
197 assert(x == 11);
198 }
199
200
201 /*******************************************/
202
203 void test10()
204 {
205 static if( int.mangleof.length > 1 && int.mangleof[1] == 'x' )
206 printf("'x' as second char\n");
207 }
208
209 /*******************************************/
210
211 class Foo11 : Bar11 { }
212
213 class Foo11T(V)
214 {
215 public void foo() {}
216 }
217
218 class Bar11
219 {
220 public this(){
221 f = new Foo11T!(int);
222 }
223 Foo11T!(int) f;
224 }
225
226 void test11()
227 {
228 Foo11 fooIt = new Foo11();
229 if (fooIt !is null)
230 writefln("fooIt should be valid");
231 fooIt.f.foo();
232 writefln("it worked");
233 }
234
235 /*******************************************/
236
237 struct A12 {
238 int a;
239 union {
240 int c;
241 B12 b;
242 }
243 }
244
245 struct B12 {
246 int b1;
247 int b2;
248 }
249
250 void test12()
251 {
252 A12 a;
253 printf("%d\n", A12.sizeof);
254 assert(A12.sizeof == 12);
255 }
256
257 /*******************************************/
258
259 template declare13(X) { X declare13; }
260
261 typeof(declare13!(int[0]).ptr[0]) x13;
262 typeof(declare13!(typeof(""))[0..$]) y13;
263
264 void test13()
265 {
266 }
267
268 /*******************************************/
269
270 interface Father {}
271
272 class Mother {
273 Father test() {
274 writefln("Called Mother.test!");
275 return new Child(42);
276 }
277 }
278
279 class Child : Mother, Father {
280 int data;
281
282 this(int d) { data = d; }
283
284 override Child test() {
285 writefln("Called Child.test!");
286 return new Child(69);
287 }
288 }
289
290 void test14()
291 {
292 Child aChild = new Child(105);
293 Mother childsMum = aChild;
294 Child childsChild = aChild.test();
295 Child mumsChild = cast(Child) childsMum.test();
296 writefln("Success2");
297 }
298
299
300 /*******************************************/
301
302 class A15
303 {
304 int a = 3;
305
306 class B
307 {
308 void bar()
309 {
310 assert(a == 3);
311 }
312 }
313
314 void fork()
315 {
316 assert(a == 3);
317 B b = new B(); // This is okay
318 b.bar();
319
320 void knife()
321 {
322 assert(a == 3);
323 B b = new B(); // No 'this' for nested class B
324 b.bar();
325 }
326 }
327 }
328
329 void test15()
330 {
331 A15 a = new A15();
332 a.fork();
333 }
334
335 /*******************************************/
336
337 creal x16;
338
339 void foo16()
340 {
341 x16 = -x16;
342 }
343
344 void bar16()
345 {
346 return foo16();
347 }
348
349 void test16()
350 {
351 x16 = 2.0L + 0.0Li;
352 bar16();
353 assert(x16 == -2.0L + 0.0Li);
354 }
355
356 /*******************************************/
357
358 void test17()
359 {
360 version (OSX)
361 {
362 }
363 else
364 {
365 /*const*/ float f = 1.2f;
366 float g = void;
367
368
369 version(D_SoftFloat)
370 {
371 g = f;
372 }
373 else version(GNU)
374 {
375 version(X86) asm
376 {
377 "flds %1; fstps %0;" : "=m" (g) : "m" (f) : ;
378 }
379 else version(X86_64) asm
380 {
381 "flds %1; fstps %0;" : "=m" (g) : "m" (f) : ;
382 }
383 else version(ARM) asm
384 {
385 "vldr d0, %1; vstr d0, %0;" : "=m" (g) : "m" (f), : "d0";
386 }
387 else static assert(false, "ASM code not implemented for this architecture");
388 }
389 else
390 {
391 asm
392 {
393 fld f; // doesn't work with PIC
394 fstp g;
395 }
396 }
397 assert(g == 1.2f);
398 }
399 }
400
401 /*******************************************/
402
403 class Foo18 : Bar18 {}
404 class FooT18(V){}
405 class Bar18 : FooT18!(int) {}
406
407 void test18()
408 {
409 }
410
411 /*******************************************/
412
413 struct STRUCTA19
414 {
415 union {
416 int a;
417 long b;
418 }
419 STRUCTB19 c;
420 }
421
422 struct STRUCTB19
423 {
424 int a;
425 }
426
427 void test19()
428 {
429 }
430
431 /*******************************************/
432
433 class Foo20
434 {
435 void bar (void * src)
436 {
437 void baz (void function (void *, size_t) xyz)
438 {
439 size_t foo (void [] dst)
440 {
441 size_t len = dst.length;
442 dst [0 .. len] = src [0 .. len];
443 xyz (dst.ptr, len);
444 return len;
445 }
446 }
447 }
448 }
449
450 void test20()
451 {
452 }
453
454 /*******************************************/
455
456 class Baz21
457 {
458 int opApply (int delegate(ref int) dg)
459 {
460 int i;
461 return dg(i);
462 }
463 }
464
465 class Foo21
466 {
467 Baz21 baz;
468
469 int foo (int delegate() dg)
470 {
471 foreach (b; baz)
472 if (bar ())
473 if (dg ())
474 break;
475 return 0;
476 }
477
478 bool bar ()
479 { return true; }
480 }
481
482 void test21()
483 {
484 }
485
486 /*******************************************/
487
488 struct Bar22 {
489 union {
490 struct {
491 union {
492 Foo22 A;
493 }
494 }
495 }
496 }
497
498 struct Foo22 {
499 double d = 3;
500 }
501
502 void test22()
503 {
504 printf("Bar22.sizeof = %zd, double.sizeof = %zd\n", Bar22.sizeof, double.sizeof);
505 assert(Bar22.sizeof == double.sizeof);
506 Bar22 b;
507 assert(b.A.d == 3);
508 }
509
510 /*******************************************/
511
512 struct Ag
513 {
514 static void func(){}
515
516 static void foo()
517 {
518 void function() fnp;
519 Ag a;
520
521 fnp = &func;
522 fnp = &Ag.func;
523 with(a) fnp = &Ag.func;
524
525 with(a) fnp = &func;
526 }
527 }
528
529 class Ah
530 {
531 static void func(){}
532
533 static void foo()
534 {
535 void function() fnp;
536 Ah a;
537
538 fnp = &func;
539 fnp = &Ah.func;
540 with(a) fnp = &Ah.func;
541
542 with(a) fnp = &func;
543 }
544 }
545
546 void test23()
547 {
548 }
549
550 /*******************************************/
551
552 void test24()
553 {
554 uint[10] arr1;
555 ulong idx = 3;
556 uint[] arr3 = arr1[ cast(int)(idx) .. (cast(int) idx) + 3 ]; // OK
557 uint[] arr4 = arr1[ cast(int) idx .. cast(int) idx + 3 ]; // OK
558 uint[] arr5 = arr1[ cast(int)(idx) .. cast(int)(idx) + 3 ]; // C style cast illegal, use cast(idx)+3
559 uint[] arr6 = arr1[ cast(int)(idx) .. cast(int)(idx + 3) ]; // OK
560 }
561
562 /*******************************************/
563
564 void test25()
565 {
566 char[6] cstr = "123456"c;
567 auto str1 = cast(wchar[3])(cstr);
568
569 writefln("str1: ", (cast(char[])str1).length , " : ", (cast(char[])str1));
570 assert(cast(char[])str1 == "123456"c);
571
572 auto str2 = cast(wchar[3])("789abc"c);
573 writefln("str2: ", (cast(char[])str2).length , " : ", (cast(char[])str2));
574 assert(cast(char[])str2 == "789abc"c);
575
576 auto str3 = cast(wchar[3])("defghi");
577 writefln("str3: ", (cast(char[])str3).length , " : ", (cast(char[])str3));
578 assert(cast(char[])str3 == "d\000e\000f\000"c);
579 }
580
581 /*******************************************/
582
583 void test26()
584 {
585 assert(foo26(5) == 25);
586 }
587
588 int foo26(int i)
589 {
590 if (auto j = i * i)
591 return j;
592 else
593 return 10;
594 }
595
596 /*******************************************/
597
598 class A27
599 {
600 int am;
601
602 class B
603 {
604 this()
605 {
606 assert(am == 3);
607 }
608 }
609
610 void fork()
611 {
612 B b = new B(); // This is okay
613
614 void knife()
615 {
616 B b = new B(); // No 'this' for nested class B
617 assert(am == 3);
618 }
619 }
620 }
621
622
623 void test27()
624 {
625 A27 a = new A27();
626 a.am = 3;
627
628 a.fork();
629 }
630
631 /*******************************************/
632
633 uint intRes()
634 {
635 return 4;
636 }
637
638 void test28()
639 {
640 auto s = std.string.format("%s", "abc123"[intRes() % $] );
641 writefln( "%s", s );
642 assert(s == "2");
643
644 static const char[] foo = "abc123";
645 s = std.string.format("%s", foo[intRes() % $] );
646 assert(s == "2");
647
648
649 static string bar = "abc123";
650 s = std.string.format("%s", bar[intRes() % $] );
651 assert(s == "2");
652
653 const char[] abc = "abc123";
654 s = std.string.format("%s", abc[intRes() % $] );
655 assert(s == "2");
656
657 string def = "abc123";
658 s = std.string.format("%s", def[intRes() % $] );
659 assert(s == "2");
660 }
661
662 /*******************************************/
663
664 class UA {
665 A29 f() { return null; }
666 }
667
668 class UB : UA {
669 override B29 f() { return null; }
670 }
671
672 class A29
673 {
674 }
675
676 class B29 : A29
677 {
678 }
679
680 void test29()
681 {
682 }
683
684 /*******************************************/
685
686 class Foo30 : Bar30 {}
687
688 class FooT30(V) {}
689
690 class Bar30 : FooT30!(int) {}
691
692 void test30()
693 {
694 }
695
696
697 /*******************************************/
698
699 int y31;
700
701 struct X31 { static void opCall() { y31 = 3; } }
702
703 void test31()
704 {
705 X31 x;
706 typeof(x)();
707 assert(y31 == 3);
708 }
709
710 /*******************************************/
711
712 class Foo32
713 {
714 static void* ps;
715
716 new (size_t sz)
717 {
718 void* p = core.stdc.stdlib.malloc(sz);
719 printf("new(sz = %d) = %p\n", sz, p);
720 ps = p;
721 return p;
722 }
723
724 delete(void* p)
725 {
726 printf("delete(p = %p)\n", p);
727 assert(p == ps);
728 if (p) core.stdc.stdlib.free(p);
729 }
730 }
731
732 void test32()
733 {
734 Foo32 f = new Foo32;
735 delete f;
736 }
737
738 /*******************************************/
739
740 class Foo33
741 {
742 // this() { printf("this()\n"); }
743 // ~this() { printf("~this()\n"); }
744
745 static void* ps;
746 static int del;
747
748 new (size_t sz, int i)
749 {
750 void* p = core.stdc.stdlib.malloc(sz);
751 printf("new(sz = %d) = %p\n", sz, p);
752 ps = p;
753 return p;
754 }
755
756 delete(void* p)
757 {
758 printf("delete(p = %p)\n", p);
759 assert(p == ps);
760 if (p) core.stdc.stdlib.free(p);
761 del += 1;
762 }
763 }
764
765 void foo33()
766 {
767 scope Foo33 f = new(3) Foo33;
768 }
769
770 void test33()
771 {
772 foo33();
773 assert(Foo33.del == 1);
774 }
775
776 /*******************************************/
777
778 struct o_O { int a; }
779 union O_O { int a; }
780 class O_o { int a; }
781
782 struct Foo34
783 {
784 int ok;
785 o_O foo;
786 O_O bar;
787 O_o baz;
788 }
789
790 void test34()
791 {
792 int o1 = Foo34.ok.offsetof;
793 assert(o1 == 0);
794 int o2 = Foo34.foo.offsetof;
795 assert(o2 == 4);
796 int o3 = Foo34.bar.offsetof;
797 assert(o3 == 8);
798 int o4 = Foo34.baz.offsetof;
799 assert((o4 % (void*).sizeof) == 0);
800 assert(o4 > o3);
801 }
802
803 /*******************************************/
804
805 class Foo37
806 {
807 float[4] array = 1.0;
808 int count = 10;
809 }
810
811 void test37()
812 {
813 Foo37 f = new Foo37();
814
815 writefln("Foo.array[0] = %s", f.array[0] );
816 writefln("Foo.array[1] = %s", f.array[1] );
817 writefln("Foo.array[2] = %s", f.array[2] );
818 writefln("Foo.array[3] = %s", f.array[3] );
819 writefln("Foo.count = %s", f.count );
820
821 assert(f.array[0] == 1.0);
822 assert(f.array[1] == 1.0);
823 assert(f.array[2] == 1.0);
824 assert(f.array[3] == 1.0);
825 assert(f.count == 10);
826 }
827
828 /*******************************************/
829
830 void test38()
831 in
832 {
833 static void checkParameters()
834 {
835 return;
836 }
837
838 checkParameters();
839 }
840 body
841 {
842 }
843
844 /*******************************************/
845
846 void delegate() foo39()
847 {
848 return &(new class
849 {
850
851 int a;
852
853 this() { a = 3; }
854
855 void dg()
856 {
857 writefln("delegate!");
858 assert(a == 3);
859 }
860 }).dg;
861 }
862
863 void test39()
864 {
865 void delegate() dg = foo39();
866
867 dg();
868 }
869
870 /*******************************************/
871
872 void test40()
873 {
874 assert( typeid(int) == typeid(int) );
875 assert( (typeid(int) != typeid(int)) == false );
876
877 int x;
878
879 bool b1 = (typeid(typeof(x)) != typeid(int));
880
881 TypeInfo t1 = typeid(typeof(x));
882 TypeInfo t2 = typeid(int);
883
884 bool b2 = (t1 != t2);
885
886 assert(b1 == b2);
887 }
888
889 /*******************************************/
890
891 int foo41(string s)
892 {
893 short shift = cast(short)(s.length * 3);
894 int answer;
895
896 for (size_t i = 0; i < s.length; i++){
897 answer = s[i] << shift;
898 }
899
900 return answer;
901 }
902
903 void test41()
904 {
905 if(foo41("\u0001") != 8){
906 assert(0);
907 }
908 }
909
910 /*******************************************/
911
912 struct S42
913 {
914 int i;
915
916 static S42 foo(int x){
917 S42 s;
918
919 s.i = x;
920
921 return s;
922 }
923 }
924
925 void test42()
926 {
927 S42[] s;
928
929 s = s ~ S42.foo(6);
930 s = s ~ S42.foo(1);
931
932 if(s.length != 2){
933 assert(0);
934 }
935 if(s[0].i != 6){
936 assert(0);
937 }
938 if(s[1].i != 1){
939 assert(0);
940 }
941 }
942
943 /*******************************************/
944
945 struct S43
946 {
947 int i,j;
948
949 static S43 foo(int x){
950 S43 s;
951
952 s.i = x;
953
954 return s;
955 }
956 }
957
958 void test43()
959 {
960 S43[] s;
961
962 s = s ~ S43.foo(6);
963 s = s ~ S43.foo(1);
964
965 if(s.length != 2){
966 assert(0);
967 }
968 if(s[0].i != 6){
969 assert(0);
970 }
971 if(s[1].i != 1){
972 assert(0);
973 }
974 }
975
976 /*******************************************/
977
978 struct S44
979 {
980 int i,j,k;
981
982 static S44 foo(int x){
983 S44 s;
984
985 s.i = x;
986
987 return s;
988 }
989 }
990
991 void test44()
992 {
993 S44[] s;
994
995 s = s ~ S44.foo(6);
996 s = s ~ S44.foo(1);
997
998 if(s.length != 2){
999 assert(0);
1000 }
1001 if(s[0].i != 6){
1002 assert(0);
1003 }
1004 if(s[1].i != 1){
1005 assert(0);
1006 }
1007 }
1008
1009 /*******************************************/
1010
1011 void test45()
1012 {
1013 char[] buffer = "abcdefghijklmnopqrstuvwxyz".dup;
1014 foreach(ref char c; buffer)
1015 {
1016 if('a' <= c && c <= 'z')
1017 {
1018 c -= cast(char)'a' - 'A'; // segfault here
1019 }
1020 }
1021 for(int i = 0; i < buffer.length; i++)
1022 {
1023 if('a' <= buffer[i] && buffer[i] <= 'z')
1024 {
1025 buffer[i] -= cast(char)'a' - 'A'; // segfault here
1026 }
1027 }
1028 writeln(buffer);
1029 }
1030
1031 /*******************************************/
1032
1033 struct st46
1034 {
1035 template t1()
1036 {
1037 template t2(int n2) { }
1038 }
1039 }
1040
1041 alias st46.t1!().t2 a46;
1042
1043 void test46()
1044 {
1045 }
1046
1047 /*******************************************/
1048
1049 struct A47
1050 {
1051 static int y;
1052 void opSliceAssign(int x)
1053 {
1054 printf("x = %d\n", x);
1055 y = x;
1056 }
1057 A47 d() { return this; }
1058 }
1059
1060 void test47()
1061 {
1062 A47 a;
1063 a[] = 3;
1064 printf("y = %d\n", a.y);
1065 a.d()[] = 5;
1066 printf("y = %d\n", a.y);
1067 assert(a.y == 5);
1068 a.d[] = 6;
1069 printf("y = %d\n", a.y);
1070 assert(a.y == 6);
1071 }
1072
1073 /*******************************************/
1074
1075 static uint[] sarray48 = void;
1076
1077 void test48()
1078 {
1079 static uint[] array = void;
1080
1081 assert(sarray48 == null);
1082 assert(array == null);
1083 }
1084
1085 /*******************************************/
1086
1087 int x = 2, y = 1;
1088
1089 void foo50(int z)
1090 {
1091 static int t;
1092 t++;
1093 assert(t == z);
1094 }
1095
1096 void test50()
1097 {
1098 printf("test50()\n");
1099 int res = 0;
1100 for(int i = 0; i < 10; i++)
1101 {
1102 res = res + x - y;
1103 foo50(res);
1104 }
1105 }
1106
1107 /*******************************************/
1108
1109 void test52()
1110 {
1111 printf("test52()\n");
1112 char[] s;
1113 s = ['a', 'b', 'c'];
1114 assert(s == "abc");
1115
1116 int[] x;
1117 x = [17, 18u, 29, 33];
1118 assert(x.length == 4);
1119 assert(x[0] == 17);
1120 assert(x[1] == 18);
1121 assert(x[2] == 29);
1122 assert(x[3] == 33);
1123 assert(x == [17, 18, 29, 33]);
1124 }
1125
1126 /*******************************************/
1127
1128 void test54()
1129 {
1130 printf("test54()\n");
1131 uint[500][] data;
1132
1133 data.length = 1;
1134 assert(data.length == 1);
1135 foreach (ref foo; data)
1136 {
1137 assert(foo.length == 500);
1138 foreach (ref u; foo)
1139 { //printf("u = %u\n", u);
1140 assert(u == 0);
1141 u = 23;
1142 }
1143 }
1144 foreach (ref foo; data)
1145 {
1146 assert(foo.length == 500);
1147 foreach (u; foo)
1148 { assert(u == 23);
1149 auto v = u;
1150 v = 23;
1151 }
1152 }
1153 }
1154
1155 /*******************************************/
1156
1157 class Base56
1158 {
1159 private string myfoo;
1160 private string mybar;
1161
1162 // Get/set properties that will be overridden.
1163 void foo(string s) { myfoo = s; }
1164 string foo() { return myfoo; }
1165
1166 // Get/set properties that will not be overridden.
1167 void bar(string s) { mybar = s; }
1168 string bar() { return mybar; }
1169 }
1170
1171 class Derived56: Base56
1172 {
1173 alias Base56.foo foo; // Bring in Base56's foo getter.
1174 override void foo(string s) { super.foo = s; } // Override foo setter.
1175 }
1176
1177 void test56()
1178 {
1179 Derived56 d = new Derived56;
1180 with (d)
1181 {
1182 foo = "hi";
1183 d.foo = "hi";
1184 bar = "hi";
1185 assert(foo == "hi");
1186 assert(d.foo == "hi");
1187 assert(bar == "hi");
1188 }
1189 }
1190
1191 /*******************************************/
1192
1193 bool[void[]] reg57;
1194
1195 void addToReg57(const(void)[] a, int b, bool v)
1196 {
1197 if (!v)
1198 writefln("X");
1199 auto key = a~(cast(void*)&b)[0..4];
1200 reg57[cast(immutable(void)[])key] = v;
1201 writefln("OK");
1202 }
1203
1204 void test57()
1205 {
1206 addToReg57("test", 1024, true);
1207 }
1208
1209 /*******************************************/
1210
1211 int bar58( string msg ){
1212 return 1;
1213 }
1214
1215 int foo58( lazy string dg ){
1216 return bar58( dg() );
1217 }
1218
1219 void test58()
1220 {
1221 printf("test58()\n");
1222 try{
1223 }
1224 finally{
1225 foo58("");
1226 }
1227 }
1228
1229 /*******************************************/
1230
1231 struct S59
1232 {
1233 string toString()
1234 {
1235 return "foo";
1236 }
1237 }
1238
1239 void test59()
1240 { S59 s;
1241 writefln("s = %s", s);
1242
1243 string p;
1244 p = std.string.format("s = %s", s);
1245 assert(p == "s = foo");
1246 }
1247
1248 /*******************************************/
1249
1250 void test60()
1251 {
1252 int[2][] a;
1253 a = [ [-1,2], [3,4] ];
1254 assert(a[0][0] == -1);
1255 assert(a[0][1] == 2);
1256 assert(a[1][0] == 3);
1257 assert(a[1][1] == 4);
1258
1259 int[][] b;
1260 b = [ [-1,2], [3,4] ];
1261 assert(b[0][0] == -1);
1262 assert(b[0][1] == 2);
1263 assert(b[1][0] == 3);
1264 assert(b[1][1] == 4);
1265 }
1266
1267 /*******************************************/
1268
1269 void test61()
1270 {
1271 int[][] f = [[1,2],[3,4]];
1272 assert(f[0][0] == 1);
1273 assert(f[0][1] == 2);
1274 assert(f[1][0] == 3);
1275 assert(f[1][1] == 4);
1276 writeln(f);
1277 }
1278
1279 /*******************************************/
1280
1281 struct List62 {
1282 void get() {}
1283 }
1284 struct Array62 {
1285 interface Model {
1286 List62 list();
1287 }
1288 List62 list() {
1289 return model ? model.list() : List62.init;
1290 }
1291 void item() {
1292 list.get();
1293 }
1294 private Model model;
1295 }
1296
1297 void test62()
1298 {
1299 }
1300
1301 /*******************************************/
1302
1303 void foo63(...)
1304 {
1305 }
1306
1307 void test63()
1308 {
1309 int[] arr;
1310 arr = [1] ~ 2;
1311
1312 // runtime crash, the length == 1
1313 printf("%d\n", arr.length);
1314 assert (arr.length == 2);
1315 assert(arr[0] == 1);
1316 assert(arr[1] == 2);
1317
1318 arr = 2 ~ [1];
1319 assert(arr.length == 2);
1320 assert(arr[0] == 2);
1321 assert(arr[1] == 1);
1322
1323 arr = [2, 3] ~ [1];
1324 assert(arr.length == 3);
1325 assert(arr[0] == 2);
1326 assert(arr[1] == 3);
1327 assert(arr[2] == 1);
1328
1329 foo63([1] ~ 2, 2 ~ [1], [1,2] ~ [3,4,5]);
1330 }
1331
1332 /*******************************************/
1333
1334 void test64()
1335 {
1336 printf("test64()\n");
1337 int[] x = [1,2,3,4];
1338 int j = 4;
1339
1340 foreach_reverse(v; x)
1341 {
1342 writeln(v);
1343 assert(j == v);
1344 j--;
1345 }
1346 assert(j == 0);
1347
1348 j = 4;
1349 foreach_reverse(i, v; x)
1350 {
1351 writefln("[%s] = %s", i, v);
1352 assert(i + 1 == j);
1353 assert(j == v);
1354 j--;
1355 }
1356 assert(j == 0);
1357 printf("-test64()\n");
1358 }
1359
1360 /*******************************************/
1361
1362 void test65()
1363 {
1364 // Bugzilla Issue 407.
1365 int i = *cast(int*)cast(char[4])['0', '0', '0', '0']; // compiler seg-fault
1366 printf("i = %x\n", i);
1367 }
1368
1369 /*******************************************/
1370
1371 void test66()
1372 {
1373 int[] ia;
1374 ia ~= 3;
1375 byte[] data = new byte[ia[0]];
1376 byte[] data2 = new byte[ cast(int)(ia[0])];
1377 }
1378
1379 /*******************************************/
1380
1381 class C68
1382 {
1383 static int value;
1384 }
1385
1386 void test68()
1387 {
1388 auto v1 = test.C68.value;
1389 auto v2 = C68.classinfo;
1390 auto v3 = test.C68.classinfo;
1391 assert(v2 == v3);
1392 }
1393
1394 /*******************************************/
1395
1396 void test69()
1397 {
1398 class Bug
1399 {
1400 char[12] str = "";
1401 uint t = 1;
1402 }
1403
1404 class NoBug
1405 {
1406 uint t = 2;
1407 char[12] str = "";
1408 }
1409
1410 class NoBug2
1411 {
1412 char[12] str;
1413 uint t = 3;
1414 }
1415
1416 auto b = new Bug;
1417 auto n = new NoBug;
1418 auto n2 = new NoBug2;
1419
1420 writefln("bug %d", b.t);
1421 assert(b.t == 1);
1422 writefln("nobug %d", n.t);
1423 assert(n.t == 2);
1424 writefln("nobug2 %d", n2.t);
1425 assert(n2.t == 3);
1426 }
1427
1428 /*******************************************/
1429
1430 void test70()
1431 {
1432 void foo(char[0] p)
1433 {
1434 }
1435
1436 static const char[0] altsep;
1437 string s = std.string.format("test%spath", altsep);
1438 assert(s == "testpath");
1439 foo(altsep);
1440 }
1441
1442 /*******************************************/
1443
1444
1445 class C71
1446 {
1447 static int cnt;
1448 this() { printf("C()\n"); cnt++; }
1449 ~this() { printf("~C()\n"); cnt--; }
1450 }
1451
1452 class D71
1453 {
1454 static int cnt;
1455 this() { printf("D()\n"); cnt++; }
1456 ~this() { printf("~D()\n"); cnt--; }
1457 }
1458
1459 class E71
1460 {
1461 static int cnt;
1462 this() { printf("E()\n"); cnt++; }
1463 ~this() { printf("~E()\n"); cnt--; }
1464 }
1465
1466 void test71()
1467 {
1468 {
1469 int i = 0;
1470 printf("start\n");
1471 scope D71 d = new D71();
1472 assert(D71.cnt == 1);
1473 for (scope E71 e = new E71(); i < 5; i++)
1474 {
1475 assert(D71.cnt == 1);
1476 assert(E71.cnt == 1);
1477 scope c = new C71();
1478 assert(C71.cnt == 1);
1479 }
1480 assert(C71.cnt == 0);
1481 assert(E71.cnt == 0);
1482 assert(D71.cnt == 1);
1483 printf("finish\n");
1484 }
1485 assert(D71.cnt == 0);
1486 }
1487
1488 /*******************************************/
1489
1490 size_t getLength(int[] arr) { return arr.length; }
1491
1492 void test13237()
1493 {
1494 int[] arr = [0];
1495 immutable size_t len = getLength(arr);
1496
1497 arr.length--;
1498
1499 assert(len == 1); // ok
1500 if (len) { auto l = len; }
1501 assert(len == 1); // len cannot be changed, but produces Assertion failure with "-O -inline"
1502 }
1503
1504 /*******************************************/
1505
1506 void main()
1507 {
1508 test1();
1509 test2();
1510 test3();
1511 test4();
1512 test5();
1513 test6();
1514 test7();
1515 test8();
1516 test9();
1517 test10();
1518 test11();
1519 test12();
1520 test13();
1521 test14();
1522 test15();
1523 test16();
1524 test17();
1525 test18();
1526 test19();
1527 test20();
1528 test21();
1529 test22();
1530 test23();
1531 test24();
1532 test25();
1533 test26();
1534 test27();
1535 test28();
1536 test29();
1537 test30();
1538 test31();
1539 test32();
1540 test33();
1541 test34();
1542 test37();
1543 test38();
1544 test39();
1545 test40();
1546 test41();
1547 test42();
1548 test43();
1549 test44();
1550 test45();
1551 test46();
1552 test47();
1553 test48();
1554 test50();
1555 test52();
1556 test54();
1557 test56();
1558 test57();
1559 test58();
1560 test59();
1561 test60();
1562 test61();
1563 test62();
1564 test63();
1565 test64();
1566 test65();
1567 test66();
1568 test68();
1569 test69();
1570 test70();
1571 test71();
1572 test13237();
1573
1574 printf("Success\n");
1575 }