d: Merge upstream dmd, druntime 4ca4140e58, phobos 454dff14d.
[official-gcc.git] / gcc / testsuite / gdc.test / runnable / testdstress.d
blob269949fb0c0276f03b925d22397724d86dc70bd7
1 // PERMUTE_ARGS:
3 module run.module_01;
5 import core.memory;
6 import core.exception;
7 import core.vararg;
9 extern(C) void* malloc(size_t size);
10 extern(C) int printf(const char*, ...);
12 /* ================================ */
14 struct MyStruct
16 int i;
19 void test1()
21 MyStruct inner()
22 in{
23 assert(1);
24 }out (result){
25 assert(result.i==1);
26 }do{
27 MyStruct s;
28 s.i = 1;
29 return s;
31 assert(inner.i==1);
34 /* ================================ */
36 void foo2()
37 in{
38 assert(0);
40 do{
43 void test2()
45 try{
46 foo2();
47 }catch(Error ie){
48 return;
50 assert(0);
53 /* ================================ */
55 class MyClass3
58 private int g() const {
59 return 1;
62 invariant()
64 assert(g()==1);
68 void test3()
70 MyClass3 c = new MyClass3();
71 assert(c);
74 /* ================================ */
76 class A
78 int x;
80 this(){
81 printf("A.this()\n");
82 x=3;
85 invariant()
87 printf("A.invariant\n");
88 assert(x>2);
92 class B : A
94 int y;
96 this(){
97 printf("B.this()\n");
98 y=5;
101 invariant()
103 printf("B.invariant\n");
104 assert(y>4);
108 void test4()
110 B gc = new B();
113 /* ================================ */
115 class A5
118 int x;
120 this(){
121 printf("A5.this()\n");
122 x=3;
125 invariant()
127 printf("A5.invariant\n");
128 assert(x>2);
132 class C5 : A5 { }
134 class B5 : C5
137 int y;
139 this(){
140 printf("B5.this()\n");
141 y=5;
144 invariant()
146 printf("B5.invariant\n");
147 assert(y>4);
151 void test5()
153 B5 gc = new B5();
156 /* ================================ */
158 void test6()
160 long a;
161 assert(a.max == 0x7FFF_FFFF_FFFF_FFFFL);
162 //assert(a.min == 0xFFFF_FFFF_FFFF_FFFFL);
163 assert(a.min == 0x8000_0000_0000_0000L);
164 assert(a.init == 0);
165 assert(a.sizeof == 8);
168 /* ================================ */
170 int i;
172 void test7()
174 assert(run.module_01.i==0);
175 run.module_01.i++;
176 assert(run.module_01.i==1);
179 /* ================================ */
181 template mix(){
182 int x;
185 mixin .mix;
187 void test8()
191 /* ================================ */
193 struct A9
195 B9 next;
198 alias A9* B9;
200 void test9()
202 B9 n = new A9;
205 /* ================================ */
208 struct TestStruct
210 void add(...)
212 TestStruct other = va_arg!TestStruct(_argptr);
213 foreach(int value; other)
215 foo();
219 void foo()
221 assert(left is null);
222 bar();
225 void bar()
227 assert(left is null);
230 int opApply(int delegate(ref int val) dg)
232 return 0;
235 void* left;
238 void test10()
240 TestStruct t;
241 t.foo();
244 /* ================================ */
246 int status11;
248 void check(int x){
249 status11++;
252 class MyClass11{
253 void test(){
254 assert(status11==0);
255 .check(0);
256 assert(status11==1);
257 check();
258 assert(status11==3);
261 void check(){
262 assert(status11==1);
263 status11+=2;
267 void test11()
269 MyClass11 c = new MyClass11();
270 assert(status11==0);
271 c.test();
272 assert(status11==3);
273 check(0);
274 assert(status11==4);
277 /* ================================ */
279 int status12;
281 class C12{
282 void foo(){
283 status12='N';
286 static void foo(int x){
287 status12=x;
291 void test12()
293 C12 m = new C12();
295 C12.foo('S');
296 assert(status12=='S');
298 m.foo('s');
299 assert(status12=='s');
301 m.foo();
302 assert(status12=='N');
305 /* ================================ */
307 void test13()
309 wstring a="abc";
310 wstring b="efg";
311 wstring c=a~"d"~b;
313 assert(c == "abcdefg");
316 /* ================================ */
318 void test14()
320 dstring a="abc";
321 dstring b="efg";
322 dstring c=a~"d"~b;
324 assert(c == "abcdefg");
327 /* ================================ */
329 class Parent15
331 void test(int i){
334 int opCast(){
335 return 0;
339 class Child15 : Parent15
343 void test15()
345 (new Child15()).test(2);
348 /* ================================ */
350 class Parent16
352 void test(int i){
356 class Child16 : Parent16
358 int opCast(){
359 return 0;
363 void test16()
365 (new Child16()).test=2;
368 /* ================================ */
370 void foo17(){
371 class MyClass{
372 static int x;
376 void foo17(int i){
377 class MyClass{
378 static int x;
382 void test17()
386 /* ================================ */
388 void foo18(){
389 struct MyStruct{
390 static int x;
394 void foo18(int i){
395 struct MyStruct{
396 static int x;
400 void test18()
404 /* ================================ */
406 class Class19 : Throwable
408 this() { super(""); }
411 alias Class19 Alias19;
413 void test19()
415 try{
416 throw new Alias19();
417 }catch(Throwable){
418 return;
420 assert(0);
423 /* ================================ */
425 public static const uint U20 = (cast(uint)(-1)) >>> 2;
427 alias uint myType20;
428 public static const myType20 T20 = (cast(myType20)(-1)) >>> 2;
430 void test20()
432 assert(U20 == 0x3FFFFFFF);
433 assert(T20 == 0x3FFFFFFF);
436 /* ================================ */
438 class C21(T1){
439 alias T1 type1;
442 class C21(T1, T2){
443 alias T1 type1;
444 alias .C21!(T2) type2;
447 void test21()
449 alias C21!(int,long) CT;
450 CT c = new CT();
453 /* ================================ */
455 class AutoClass{
458 void test22()
460 scope AutoClass ac = new AutoClass();
462 with(ac){
466 /* ================================ */
468 int status23;
470 class C23{
471 ~this(){
472 assert(status23==0);
473 status23--;
474 throw new Exception("error msg");
478 void foo23(){
479 assert(status23==0);
480 scope C23 ac = new C23();
483 void test23()
485 try{
486 foo23();
487 }catch(Throwable){
489 assert(status23==-1);
492 /* ================================ */
494 int status24;
496 class C24{
497 this() scope {
498 assert(status24==0);
499 status24+=2;
501 ~this(){
502 assert(status24==2);
503 status24--;
504 throw new Exception("error msg");
508 void check24(){
509 scope C24 ac = new C24();
510 throw new Exception("check error");
513 void test24()
515 assert(status24==0);
516 try{
517 check24();
518 }catch(Throwable){
519 assert(status24==1);
520 status24-=5;
522 assert(status24==-4);
525 /* ================================ */
527 struct S25{
528 S25 opBinary(string op)(int i) if (op == "-") { S25 s; return s; }
531 struct GeomObject{
532 S25 mesh;
533 int xlate;
537 void extractTriangles(GeomObject g)
539 void foobar()
541 g.mesh - g.xlate;
542 //g.mesh.opBinary!("-")(g.xlate);
545 foobar();
548 void test25()
552 /* ================================ */
554 struct S26{
555 int i;
557 void display(){
558 assert(i==10);
561 void someFunc(){
562 // We never call this function
563 void bug(S26[] array){
564 array[0].i = i+1;
567 assert(i==10);
568 display();
569 assert(i==10);
573 void test26()
575 S26 m;
576 m.i = 10;
577 assert(m.i==10);
578 m.someFunc();
579 assert(m.i==10);
582 /* ================================ */
584 template foo27(T:T[],alias S) {
585 string foo(T[] a, T[] b) {
586 return a ~ S ~ b;
590 string comma = ", ";
591 alias foo27!(string,comma).foo catComma;
593 void test27()
595 string a = "Heath";
596 string b = "Regan";
598 assert("Heath, Regan"==catComma(a,b));
601 /* ================================ */
603 void test28()
605 assert((new S28!()).i==int.sizeof);
608 struct S28(){
609 int i=func28(0).sizeof;
612 int func28(...){
613 return 0;
616 /* ================================ */
618 void test29()
620 uint u;
621 u = 1 << 31;
622 assert( u == 0b1000_0000__0000_0000__0000_0000__0000_0000u);
625 /* ================================ */
626 // Test for FinalizeError - it will be thrown if an Exception is thrown
627 // during the class object finalization.
629 int status30;
631 class C30
633 this()
635 status30++;
638 ~this()
640 status30--;
641 throw new Exception("E2");
645 void test30()
649 //scope C30 m = new C30();
650 // It will insert one more `delete m` for the scope destruction, and it will be
651 // called during stack unwinding.
652 // Instead use bare memory chunk on stack to construct dummy class instance.
653 void[__traits(classInstanceSize, C30)] payload =
654 typeid(C30).initializer[];
655 C30 m = cast(C30)payload.ptr;
656 m.__ctor();
658 assert(status30 == 1);
660 destroy(m); // _d_callfinalizer
662 catch (Error e) // FinalizeError
664 assert(status30 == 0);
665 status30--;
668 assert(status30 == -1);
671 /* ================================ */
673 void test31()
675 string str = "\xF0\x9D\x83\x93"; // utf-8 for U+1D0D3
677 int count=0;
678 dchar tmp;
679 foreach(dchar value ; str){
680 tmp=value;
681 count++;
683 assert(count==1);
684 assert(tmp==0x01D0D3);
687 /* ================================ */
689 union MyUnion32
691 int i;
692 byte b;
695 void test32()
697 TypeInfo ti = typeid(MyUnion32*);
698 assert(!(ti is null));
699 assert(ti.tsize==(MyUnion32*).sizeof);
700 assert(ti.toString()=="run.module_01.MyUnion32*");
703 /* ================================ */
705 void test35()
707 try{
708 onOutOfMemoryError();
709 }catch(OutOfMemoryError e){
710 return;
712 assert(0);
715 /* ================================ */
717 void test36()
719 try{
720 onOutOfMemoryError();
721 }catch(OutOfMemoryError e){
722 return;
724 assert(0);
727 /* ================================ */
729 struct S37{
730 int a;
733 const int i37 = 15;
735 const S37 s1 = { i37+1 };
736 const S37 s2 = s1;
738 void test37()
740 assert(s1.a == 16);
741 assert(s2.a == 16);
744 /* ================================ */
746 class Foo38
748 enum MyEnum{
749 VALUE_A=1,
753 class Bar38
755 enum MyEnum{
756 VALUE_B=2,
760 void test38()
762 assert(Foo38.MyEnum.VALUE_A==1);
763 assert(Bar38.MyEnum.VALUE_B==2);
766 /* ================================ */
768 void test39()
770 bool[] bArray;
771 int[] iArray;
773 bArray[]=false;
775 foreach(int c; iArray){
776 assert(0);
780 /* ================================ */
782 bool checked40;
784 class Parent40{
785 int x;
787 void test(){
790 invariant()
792 assert(!checked40);
793 checked40=true;
794 // even number
795 assert((x&1u)==0);
799 class Child40 : Parent40{
802 class GrandChild40 : Child40{
803 this(){
804 x=5;
808 void test40()
810 try{
811 assert(!checked40);
812 GrandChild40 gc = new GrandChild40();
813 }catch(Throwable){
814 assert(checked40);
815 return;
817 assert(0);
820 /* ================================ */
822 int counter41;
824 class C41{
825 this(){
826 printf("this: counter41 = %d\n", counter41);
827 assert(counter41==0);
828 counter41+=2;
832 void test41()
834 C41 c;
835 assert(counter41==0);
836 c = new C41();
837 assert(counter41==2);
840 /* ================================ */
842 struct S42{
843 int y;
844 void* x;
847 void test42()
849 size_t t;
850 t = S42.y.offsetof;
851 assert(t == 0);
852 t = S42.x.offsetof;
853 assert((t % (void*).sizeof) == 0);
856 /* ================================ */
858 int main()
860 test1();
861 test2();
862 test3();
863 test4();
864 test5();
865 test6();
866 test7();
867 test8();
868 test9();
869 test10();
870 test11();
871 test12();
872 test13();
873 test14();
874 test15();
875 test16();
876 test17();
877 test18();
878 test19();
879 test20();
880 test21();
881 test22();
882 test23();
883 test24();
884 test25();
885 test26();
886 test27();
887 test28();
888 test29();
889 test30();
890 test31();
891 test32();
892 test35();
893 test36();
894 test37();
895 test38();
896 test39();
897 test40();
898 test41();
899 test42();
901 printf("Success\n");
902 return 0;