d: Merge upstream dmd 47871363d, druntime, c52e28b7, phobos 99e9c1b77.
[official-gcc.git] / gcc / testsuite / gdc.test / runnable / test11.d
blob6735a817c5edc5a4f3d8e7abd2097007dc72cb21
1 // REQUIRED_ARGS:
2 // EXTRA_SOURCES: imports/std11file.d
4 extern(C) int printf(const char*, ...);
5 extern(C) size_t strlen(const char*);
7 /**************************************/
9 alias strlen foo1;
11 void test1()
13 const(char) *p = "bar";
14 size_t i = foo1(p);
15 assert(i == 3);
18 /**************************************/
20 template Foo2(T)
22 alias T t;
25 alias Foo2!(int) t1;
26 alias Foo2!(int).t t2;
27 alias t1.t t3;
28 alias t2 t4;
29 alias Foo2!(int) t5;
31 void test2()
33 t1.t v1;
34 t2 v2;
35 t3 v3;
36 t4 v4;
37 t5.t v5;
38 int *p;
40 p = &v1;
41 p = &v2;
42 p = &v3;
43 p = &v4;
44 p = &v5;
48 /**************************************/
50 debug = stdchar;
52 debug(mychar)
54 alias byte mychar;
57 void test3()
59 debug(mychar)
61 mychar[] line=cast(mychar[])cast(char[])"It is a long line.";
62 mychar[] delimiter=cast(mychar[])cast(string)"is";
65 debug(stdchar)
67 string line="It is a long line.";
68 string delimiter="is";
71 debug(stdbyte)
73 byte[] line=cast(byte[])cast(string)"It is a long line.";
74 byte[] delimiter=cast(byte[])cast(string)"is";
77 debug(stdwchar)
79 wstring line="It is a long line.";
80 wstring delimiter="is";
82 int ptr=3;
84 size_t dl=delimiter.length;
85 size_t pl=ptr+dl;
87 assert(line[ptr..pl]==delimiter[]);
91 /**************************************/
93 void test4()
95 byte* p;
97 version(D_LP64)
98 assert(p.sizeof == 8);
99 else
100 assert(p.sizeof == 4);
104 /**************************************/
107 class Foo6
111 void test6()
113 Foo6 foo = new Foo6();
115 with (foo)
117 int x;
119 x = 4;
123 /**************************************/
125 int i7 = 3;
127 void test7()
129 switch (i7)
131 default: assert(0);
132 case 3:
133 int x;
135 x = 4;
139 /**************************************/
141 void test8()
143 string a = "a
146 assert(a.length == 5);
147 assert(a[1] == '\n');
150 /**************************************/
153 struct Foo9 { char c; char bar() { return c; } }
155 Foo9 makeFoo() { Foo9 f; return f; }
157 void callFoo (Foo9 a)
159 a.bar();
162 void test9()
164 callFoo(makeFoo ());
167 /**************************************/
170 struct Foo10 { }
172 Foo10 makeFoo10() { Foo10 f; return f; }
174 void callFoo (Foo10 a)
178 void test10()
180 callFoo(makeFoo10());
183 /**************************************/
185 struct Color
186 { int x; }
188 Color[3] colors;
190 Color eval(float x, float y)
192 colors[1].x = 7;
193 return colors[1];
196 void test11()
198 Color c;
200 c = eval(1.0, 2.0);
201 assert(c.x == 7);
204 /**************************************/
206 struct Size12
208 int width;
209 int height;
212 int x12;
214 void foo12(out Size12 sz)
216 sz.width = 2;
218 if(sz.width == 2)
219 x12 = 1;
223 void test12()
225 Size12 sz;
227 foo12(sz);
228 assert(x12 == 1);
229 assert(sz.width == 2);
232 /**************************************/
235 interface D13
237 void setHostFrame();
240 class A13 : D13
242 void setHostFrame()
246 char group;
250 void setLayout(D13 lo)
252 printf("lo = %p\n", lo);
253 lo.setHostFrame();
254 printf("ok\n");
258 void test13()
260 A13 a = new A13();
261 printf("a = %p\n", a);
262 setLayout(a);
265 /**************************************/
267 void test14()
269 while(false)
271 static int a;
276 /**************************************/
278 alias void delegate(int) t_func;
280 class Foo15
282 t_func func1;
283 int x;
285 void dothis()
287 if (func1)
288 func1(4);
289 else
290 x = 3;
293 void func(int num) { x = num; }
296 void test15()
298 Foo15 a = new Foo15;
299 a.dothis();
300 assert(a.x == 3);
301 a.func1 = &a.func;
302 a.dothis();
303 assert(a.x == 4);
307 /**************************************/
309 int[] foo16(byte[] a)
311 return cast(int[])a;
314 void test16()
316 byte[12] b;
317 int[] i;
319 i = foo16(b);
320 assert(i.length == 3);
323 /**************************************/
325 void test17()
328 float x = 10;
329 x %= 4;
330 printf("x = %g\n", x);
331 assert(x == 2);
332 x = 10;
333 x = x % 4;
334 printf("x = %g\n", x);
335 assert(x == 2);
336 x = 4;
337 x = 10 % x;
338 printf("x = %g\n", x);
339 assert(x == 2);
342 double y = 10;
343 y %= 4;
344 printf("y = %g\n", y);
345 assert(y == 2);
346 y = 10;
347 y = y % 4;
348 printf("y = %g\n", y);
349 assert(y == 2);
350 y = 4;
351 y = 10 % y;
352 printf("y = %g\n", y);
353 assert(y == 2);
356 real z = 10;
357 z %= 4;
358 printf("z = %Lg\n", z);
359 assert(z == 2);
360 z = 10;
361 z = z % 4;
362 printf("z = %Lg\n", z);
363 assert(z == 2);
364 z = 4;
365 z = 10 % z;
366 printf("z = %Lg\n", z);
367 assert(z == 2);
372 /**************************************/
374 struct Bar18 { }
376 struct Foo18
378 static Bar18 x = { };
381 void test18()
383 const Bar18 b = Foo18.x;
387 /**************************************/
389 int x19 = 10;
391 void test19()
392 { bool b;
394 b = cast(bool)x19;
395 assert(b == true);
398 /**************************************/
400 class A20
402 int abc() { return 3; }
404 alias abc def;
407 void test20()
409 int i;
410 A20 a = new A20();
412 i = a.def();
413 assert(i == 3);
417 /**************************************/
419 void test21()
421 string s;
422 s = 1 ? "a" : "b";
423 assert(s == "a");
426 /**************************************/
428 class Foo22
432 class Bar22 : Foo22
436 class Abc22
438 Foo22 test() { return null; }
441 class Def22 : Abc22
443 override Bar22 test() { return new Bar22; }
446 void testx22(Abc22 a)
448 assert(a.test() !is null);
451 void test22()
453 Def22 d = new Def22();
455 testx22(d);
458 /**************************************/
460 struct foo23
462 static struct bar
464 int x;
468 void test23()
470 //printf ("%d\n", foo23.bar.sizeof);
471 assert(foo23.bar.sizeof == int.sizeof);
475 /**************************************/
477 void test24()
479 struct Test
481 int i;
483 bool bar(int a)
485 i = a;
486 return true;
490 Test t;
491 assert(t.bar(3));
495 /**************************************/
497 void test25()
499 { const int [] list = [ 1, 2 ];
500 assert(list[0] == 1);
501 assert(list[1] == 2);
504 { const int [] list = [ 3, 4 ];
505 assert(list[0] == 3);
506 assert(list[1] == 4);
511 /**************************************/
513 void test26()
515 while (0)
517 int x;
520 while (0)
522 int x;
527 /**************************************/
529 struct NODE27 {
530 int data;
531 shared(NODE27) *next;
534 static shared NODE27[3] nodetbl =
536 { 0,cast(shared(NODE27)*)nodetbl + 1},
537 { 0,cast(shared(NODE27)*)nodetbl + 2},
538 { 0,null}
541 static shared NODE27[3] nodetbl2 = [
542 { 0,&nodetbl2[1]},
543 { 0,&nodetbl2[2]},
544 { 0,null}
547 void test27()
552 /**************************************/
554 class Foo28
556 protected int x;
558 static class Bar
560 Foo28 f;
562 int method () { return f.x; }
566 void test28()
571 /**************************************/
573 void test29()
575 int[immutable(byte)[]] foo;
577 static immutable(byte)[] bar = [ 65, 66, 67 ];
579 foo[bar] = 1;
580 assert(foo[bar] == 1);
583 /**************************************/
585 class A30
587 static class Child
593 class B30
595 static class Child
597 static int value = 6;
601 void test30()
603 printf ("%d\n", B30.Child.value);
604 assert(B30.Child.value == 6);
608 /**************************************/
610 void test31()
612 float b;
613 b -= 1.0;
614 b += 1.0;
617 /**************************************/
619 class Foo32
621 struct Bar
623 int x;
627 void test32()
629 with (new Foo32)
631 Bar z;
632 z.x = 5;
637 /**************************************/
639 string[2][] foo33;
641 void test33()
643 string[2] bar;
645 bar[1] = "hello";
646 foo33 ~= bar;
647 assert(foo33[0][1] == "hello");
651 /**************************************/
654 void test34()
656 try {
657 int i = 0;
658 printf( "i:%d\n", i );
659 } finally {
660 printf( "Done\n" );
662 try {
663 int i = 1;
664 printf( "i:%d\n", i );
665 } finally {
666 printf( "Done\n" );
671 /**************************************/
673 class Bar35 {}
675 template Foo35( T )
677 void func() { };
680 void test35()
682 try {
683 alias Foo35!( Bar35 ) filter;
684 } catch (Exception e) {
685 printf( "Exception %.*s", cast(int)e.msg.length, e.msg.ptr );
686 } finally {
687 printf( "Done0." );
692 /**************************************/
694 void test36()
696 enum {A=1}
697 enum {B=A?0:1}
698 assert(A == 1);
699 assert(B == 0);
702 /**************************************/
704 struct A37
706 int a;
709 struct B37
711 int a;
712 int b;
715 struct C37
717 int a;
718 int b;
719 int c;
722 struct D37
724 byte a,b,c;
727 void test37()
729 A37 a;
730 B37 b;
731 C37 c;
732 D37 d;
734 assert(a.a == 0);
735 assert(b.a == 0 && b.b == 0);
736 assert(c.a == 0 && c.b == 0 && c.c == 0);
737 assert(d.a == 0 && d.b == 0 && d.c == 0);
741 /**************************************/
743 int function() fp18;
745 extern(Windows) int func18()
747 static int otherfunc()
748 { return 18; }
750 fp18 = &otherfunc;
751 return fp18();
754 void test38()
756 assert(func18() == 18);
760 /**************************************/
762 class bar39
764 struct _sub
766 bool a;
767 string d;
769 _sub mySub;
772 class foo39
774 bar39._sub[] subArray;
776 this(bar39[] arr)
778 for(int i=0; i<arr.length; i++)
779 subArray ~= arr[i].mySub;
784 void test39()
789 /**************************************/
791 void test40()
793 void* h;
795 h = h.init;
796 assert(h == cast(void*)0);
799 /**************************************/
801 int test41()
803 label:
804 int foo;
805 foo = 0;
806 return foo;
810 /**************************************/
812 struct A42
814 invariant()
818 B42 *findPool()
820 return null;
825 struct B42
827 int cmp(B42 *p2)
829 return 0;
833 void test42()
838 /**************************************/
840 void test43()
842 real a = 0.9;
843 ulong b = cast(ulong) a;
844 printf("%u", cast(uint) b);
845 assert(cast(uint) b == 0);
847 int c = cast(int) a;
848 printf("%i", c);
849 assert(c == 0);
853 /**************************************/
855 void test44()
857 switch("asdf")
859 case "asdf":
860 printf("asdf\n");
861 break;
864 case "jkl":
865 printf("jkl\n");
866 assert(0);
867 break;
870 default:
871 printf("default\n");
872 assert(0);
877 /**************************************/
879 void func45(string a)
881 assert(a.length == 5);
884 void test45()
886 char[5] foo;
888 foo[] = "hello";
889 printf("'%.*s'\n", cast(int)foo.length, foo.ptr);
890 func45(cast(string)foo);
893 /**************************************/
895 struct Foo46
897 int x;
900 void test46()
902 Foo46 f;
904 with (f)
906 x = 3;
908 assert(f.x == 3);
911 /**************************************/
913 struct Bar48
915 uint k;
916 ubyte m;
919 Bar48 makebar48() { Bar48 b; return b; }
921 void test48()
923 Bar48 b = makebar48();
926 /**************************************/
928 void testx49() { printf("testx49()\n"); }
930 void test49() { return testx49(); }
932 /**************************************/
934 int testx50() { printf("testx50()\n"); return 3; }
936 void test50() { return cast(void)testx50(); }
938 /**************************************/
940 class A51
942 static typeof(this) foo()
944 return new A51();
947 this()
949 bar = 3;
952 int bar;
955 class B51 : A51
957 static typeof(super) b;
960 struct C51
962 typeof(&this) x;
965 void test51()
967 A51 a = A51.foo();
968 assert(a.bar == 3);
970 B51.b = a;
971 assert(B51.b.bar == 3);
972 assert(B51.b.classinfo == A51.classinfo);
974 C51 c;
975 c.x = &c;
979 /**************************************/
981 class A52
983 char get() { return 'A'; }
985 char foo() { return typeof(this).get(); }
986 char bar() { return A52.get(); }
989 class B52 : A52
991 override char get() { return 'B'; }
994 void test52()
996 B52 b = new B52();
998 assert(b.foo() == 'A');
999 assert(b.bar() == 'A');
1000 assert(b.get() == 'B');
1003 /**************************************/
1005 struct A53
1007 int b() { return 1; }
1010 int x53()
1012 A53 a;
1013 return a.b;
1016 void test53()
1018 assert(x53() == 1);
1021 /**************************************/
1023 class A54
1025 void a()
1027 printf("A54.a\n");
1030 void b()
1032 typeof(this).a();
1036 class B54 : A54
1038 override void a()
1040 printf("B54.a\n");
1041 assert(0);
1045 void test54()
1047 B54 b = new B54();
1049 b.b();
1053 /**************************************/
1055 int foo55(int x = 5)
1057 printf("x = %d\n", x);
1058 return x;
1061 void test55()
1062 { int i;
1064 i = foo55(6);
1065 assert(i == 6);
1066 i = foo55();
1067 assert(i == 5);
1070 /**************************************/
1072 class A56
1074 int foo(int x = 5)
1076 printf("A56.x = %d\n", x);
1077 return x;
1081 class B56 : A56
1083 override int foo(int x = 7)
1085 printf("B56.x = %d\n", x);
1086 return x;
1091 void test56()
1092 { int i;
1093 B56 b = new B56();
1095 i = b.foo(6);
1096 assert(i == 6);
1097 i = b.foo();
1098 assert(i == 7);
1102 /**************************************/
1104 void test57()
1106 char c;
1107 wchar w;
1108 dchar d;
1110 printf("c = %x, w = %x, d = %x\n", c, w, d);
1111 assert(c == 0xFF);
1112 assert(w == 0xFFFF);
1113 assert(d == 0xFFFF);
1116 /**************************************/
1118 void test58()
1120 static int x;
1122 static class S
1124 static this()
1126 printf ("static constructor\n");
1127 x = 10;
1130 this()
1132 printf ("class constructor\n");
1136 assert(x == 10);
1137 new S;
1140 /**************************************/
1142 struct S61 {
1143 int a, b, c, d;
1146 void rec(int n, S61 t)
1148 if (n > 0) {
1149 t.b++;
1150 rec(n-1,t);
1154 void test61()
1156 S61 F;
1158 rec(100, F);
1161 /**************************************/
1163 class A62
1165 static A62 test(int q=0) {
1166 return null;
1170 A62 foo62()
1172 return A62.test;
1175 void test62()
1177 foo62();
1181 /**************************************/
1183 class A63
1185 private import imports.std11file;
1186 alias imports.std11file.getcwd getcwd;
1189 void test63()
1191 A63 f = new A63();
1192 auto s = f.getcwd();
1193 printf("%.*s\n", cast(int)s.length, s.ptr);
1197 /**************************************/
1199 debug = 3;
1201 void test64()
1203 debug(5)
1205 assert(0);
1207 debug(3)
1209 int x = 3;
1211 assert(x == 3);
1214 /**************************************/
1216 version = 3;
1218 void test65()
1220 version(5)
1222 assert(0);
1224 version(3)
1226 int x = 3;
1228 assert(x == 3);
1231 /**************************************/
1232 // https://issues.dlang.org/show_bug.cgi?id=8809
1234 void test8809()
1236 static class B
1238 char foo() { return 'B'; }
1240 static class C : B
1242 char test1Bx() { return B.foo(); }
1243 char test1Cx() { return C.foo(); }
1244 char test1Dx() { return foo(); }
1245 char test1By() { return this.B.foo(); }
1246 char test1Cy() { return this.C.foo(); } // cannot compile -> OK
1247 char test1Dy() { return this. foo(); }
1248 char test1Bz() { return typeof(super).foo(); }
1249 char test1Cz() { return typeof(this). foo(); }
1250 //char test1Dz();
1252 char test2Bx() { return { return B.foo(); }(); }
1253 char test2Cx() { return { return C.foo(); }(); }
1254 char test2Dx() { return { return foo(); }(); }
1255 char test2By() { return { return this.B.foo(); }(); }
1256 char test2Cy() { return { return this.C.foo(); }(); } // cannot compile -> OK
1257 char test2Dy() { return { return this. foo(); }(); }
1258 char test2Bz() { return { return typeof(super).foo(); }(); }
1259 char test2Cz() { return { return typeof(this). foo(); }(); }
1260 //char test2Dz();
1262 char test3Bx() { return (new class Object { char bar() { return B.foo(); } }).bar(); }
1263 char test3Cx() { return (new class Object { char bar() { return C.foo(); } }).bar(); }
1264 char test3Dx() { return (new class Object { char bar() { return foo(); } }).bar(); }
1266 override char foo() { return 'C'; }
1268 static class D : C
1270 override char foo() { return 'D'; }
1273 C c = new D();
1275 assert(c.test1Bx() == 'B');
1276 assert(c.test1Cx() == 'C');
1277 assert(c.test1Dx() == 'D');
1278 assert(c.test1By() == 'B');
1279 assert(c.test1Cy() == 'C');
1280 assert(c.test1Dy() == 'D');
1281 assert(c.test1Bz() == 'B'); // NG('D') -> OK
1282 assert(c.test1Cz() == 'C');
1283 //assert(c.test1Dz() == 'D');
1285 assert(c.test2Bx() == 'B'); // NG('D') -> OK
1286 assert(c.test2Cx() == 'C'); // NG('D') -> OK
1287 assert(c.test2Dx() == 'D');
1288 assert(c.test2By() == 'B');
1289 assert(c.test2Cy() == 'C');
1290 assert(c.test2Dy() == 'D');
1291 assert(c.test2Bz() == 'B'); // NG('D') -> OK
1292 assert(c.test2Cz() == 'C'); // NG('D') -> OK
1293 //assert(c.test2Dz() == 'D');
1295 assert(c.test3Bx() == 'B'); // NG('D') -> OK
1296 assert(c.test3Cx() == 'C'); // NG('D') -> OK
1297 assert(c.test3Dx() == 'D');
1300 /**************************************/
1301 // https://issues.dlang.org/show_bug.cgi?id=9734
1303 void test9734()
1305 class C {}
1306 class D : C
1308 static bool test(C) { return true; }
1310 void foo()() if (is(typeof(test(super)))) {}
1311 void bar()() if (is(typeof(super) == C)) {}
1313 void baz()() if (is(typeof(super))) {}
1315 auto d = new D();
1316 d.foo();
1317 d.bar();
1318 static assert(!__traits(compiles, baz()));
1321 /**************************************/
1323 int main(string[] argv)
1325 test1();
1326 test2();
1327 test3();
1328 test4();
1329 test6();
1330 test7();
1331 test8();
1332 test9();
1333 test10();
1334 test11();
1335 test12();
1336 test13();
1337 test14();
1338 test15();
1339 test16();
1340 test17();
1341 test18();
1342 test19();
1343 test20();
1344 test21();
1345 test22();
1346 test23();
1347 test24();
1348 test25();
1349 test26();
1350 test27();
1351 test28();
1352 test29();
1353 test30();
1354 test31();
1355 test32();
1356 test33();
1357 test34();
1358 test35();
1359 test36();
1360 test37();
1361 test38();
1362 test39();
1363 test40();
1364 test41();
1365 test42();
1366 test43();
1367 test44();
1368 test45();
1369 test46();
1370 test48();
1371 test49();
1372 test50();
1373 test51();
1374 test52();
1375 test53();
1376 test54();
1377 test55();
1378 test56();
1379 test57();
1380 test58();
1381 test61();
1382 test62();
1383 test63();
1384 test64();
1385 test65();
1386 test8809();
1387 test9734();
1389 printf("Success\n");
1390 return 0;