1 // RUNNABLE_PHOBOS_TEST
2 // PERMUTE_ARGS:
3 
4 module dstress.run.module_01;
5 
6 import core.memory;
7 import core.exception;
8 import core.vararg;
9 
10 extern(C) void* malloc(size_t size);
11 
12 /* ================================ */
13 
14 struct MyStruct
15 {
16         int i;
17 }
18 
test1()19 void test1()
20 {
21         MyStruct inner()
22         in{
23                 assert(1);
24         }out (result){
25                 assert(result.i==1);
26         }body{
27                 MyStruct s;
28                 s.i = 1;
29                 return s;
30         }
31         assert(inner.i==1);
32 }
33 
34 /* ================================ */
35 
foo2()36 void foo2()
37 in{
38         assert(0);
39 }
40 body{
41 }
42 
test2()43 void test2()
44 {
45         try{
46                 foo2();
47         }catch(Error ie){
48                 return;
49         }
50         assert(0);
51 }
52 
53 /* ================================ */
54 
55 class MyClass3
56 {
57 
g()58         private int g() const {
59                 return 1;
60         }
61 
invariant()62         invariant()
63         {
64                 assert(g()==1);
65         }
66 }
67 
test3()68 void test3()
69 {
70         MyClass3 c = new MyClass3();
71         assert(c);
72 }
73 
74 /* ================================ */
75 
76 class A
77 {
78         int x;
79 
this()80         this(){
81             printf("A.this()\n");
82             x=3;
83         }
84 
invariant()85         invariant()
86         {
87             printf("A.invariant\n");
88             assert(x>2);
89         }
90 }
91 
92 class B : A
93 {
94         int y;
95 
this()96         this(){
97             printf("B.this()\n");
98             y=5;
99         }
100 
invariant()101         invariant()
102         {
103             printf("B.invariant\n");
104             assert(y>4);
105         }
106 }
107 
test4()108 void test4()
109 {
110         B gc = new B();
111 }
112 
113 /* ================================ */
114 
115 class A5
116 {
117 
118         int x;
119 
this()120         this(){
121             printf("A5.this()\n");
122             x=3;
123         }
124 
invariant()125         invariant()
126         {
127             printf("A5.invariant\n");
128             assert(x>2);
129         }
130 }
131 
132 class C5 : A5 { }
133 
134 class B5 : C5
135 {
136 
137         int y;
138 
this()139         this(){
140             printf("B5.this()\n");
141             y=5;
142         }
143 
invariant()144         invariant()
145         {
146             printf("B5.invariant\n");
147             assert(y>4);
148         }
149 }
150 
test5()151 void test5()
152 {
153         B5 gc = new B5();
154 }
155 
156 /* ================================ */
157 
test6()158 void test6()
159 {
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);
166 }
167 
168 /* ================================ */
169 
170 int i;
171 
test7()172 void test7()
173 {
174         assert(dstress.run.module_01.i==0);
175         dstress.run.module_01.i++;
176         assert(dstress.run.module_01.i==1);
177 }
178 
179 /* ================================ */
180 
mix()181 template mix(){
182         int x;
183 }
184 
185 mixin .mix;
186 
test8()187 void test8()
188 {
189 }
190 
191 /* ================================ */
192 
193 struct A9
194 {
195     B9 next;
196 }
197 
198 alias A9* B9;
199 
test9()200 void test9()
201 {
202     B9 n = new A9;
203 }
204 
205 /* ================================ */
206 
207 
208 struct TestStruct
209 {
addTestStruct210         void add(...)
211         {
212                 TestStruct other = va_arg!TestStruct(_argptr);
213                 foreach(int value; other)
214                 {
215                         foo();
216                 }
217         }
218 
fooTestStruct219         void foo()
220         {
221                 assert(left is null);
222                 bar();
223         }
224 
barTestStruct225         void bar()
226         {
227                 assert(left is null);
228         }
229 
opApplyTestStruct230         int opApply(int delegate(ref int val) dg)
231         {
232                 return 0;
233         }
234 
235         void* left;
236 }
237 
test10()238 void test10()
239 {
240         TestStruct t;
241         t.foo();
242 }
243 
244 /* ================================ */
245 
246 int status11;
247 
check(int x)248 void check(int x){
249         status11++;
250 }
251 
252 class MyClass11{
test()253         void test(){
254                 assert(status11==0);
255                 .check(0);
256                 assert(status11==1);
257                 check();
258                 assert(status11==3);
259         }
260 
check()261         void check(){
262                 assert(status11==1);
263                 status11+=2;
264         }
265 }
266 
test11()267 void test11()
268 {
269         MyClass11 c = new MyClass11();
270         assert(status11==0);
271         c.test();
272         assert(status11==3);
273         check(0);
274         assert(status11==4);
275 }
276 
277 /* ================================ */
278 
279 int status12;
280 
281 class C12{
foo()282         void foo(){
283                 status12='N';
284         }
285 
foo(int x)286         static void foo(int x){
287                 status12=x;
288         }
289 }
290 
test12()291 void test12()
292 {
293         C12 m = new C12();
294 
295         C12.foo('S');
296         assert(status12=='S');
297 
298         m.foo('s');
299         assert(status12=='s');
300 
301         m.foo();
302         assert(status12=='N');
303 }
304 
305 /* ================================ */
306 
test13()307 void test13()
308 {
309         wstring a="abc";
310         wstring b="efg";
311         wstring c=a~"d"~b;
312 
313         assert(c == "abcdefg");
314 }
315 
316 /* ================================ */
317 
test14()318 void test14()
319 {
320         dstring a="abc";
321         dstring b="efg";
322         dstring c=a~"d"~b;
323 
324         assert(c == "abcdefg");
325 }
326 
327 /* ================================ */
328 
329 class Parent15
330 {
test(int i)331         void test(int i){
332         }
333 
opCast()334         int opCast(){
335                 return 0;
336         }
337 }
338 
339 class Child15 : Parent15
340 {
341 }
342 
test15()343 void test15()
344 {
345         (new Child15()).test(2);
346 }
347 
348 /* ================================ */
349 
350 class Parent16
351 {
test(int i)352         void test(int i){
353         }
354 }
355 
356 class Child16 : Parent16
357 {
opCast()358         int opCast(){
359                 return 0;
360         }
361 }
362 
test16()363 void test16()
364 {
365         (new Child16()).test=2;
366 }
367 
368 /* ================================ */
369 
foo17()370 void foo17(){
371         class MyClass{
372                 static int x;
373         }
374 }
375 
foo17(int i)376 void foo17(int i){
377         class MyClass{
378                 static int x;
379         }
380 }
381 
test17()382 void test17()
383 {
384 }
385 
386 /* ================================ */
387 
foo18()388 void foo18(){
389         struct MyStruct{
390                 static int x;
391         }
392 }
393 
394 void foo18(int i){
395         struct MyStruct{
396                 static int x;
397         }
398 }
399 
400 void test18()
401 {
402 }
403 
404 /* ================================ */
405 
406 class Class19 : Throwable
407 {
408     this() { super(""); }
409 }
410 
411 alias Class19 Alias19;
412 
413 void test19()
414 {
415         try{
416                 throw new Alias19();
417         }catch{
418                 return;
419         }
420         assert(0);
421 }
422 
423 /* ================================ */
424 
425 public static const uint U20 = (cast(uint)(-1)) >>> 2;
426 
427 alias uint myType20;
428 public static const myType20 T20 = (cast(myType20)(-1)) >>> 2;
429 
430 void test20()
431 {
432         assert(U20 == 0x3FFFFFFF);
433         assert(T20 == 0x3FFFFFFF);
434 }
435 
436 /* ================================ */
437 
438 class C21(T1){
439         alias T1 type1;
440 }
441 
442 class C21(T1, T2){
443         alias T1 type1;
444         alias .C21!(T2) type2;
445 }
446 
447 void test21()
448 {
449         alias C21!(int,long) CT;
450         CT c = new CT();
451 }
452 
453 /* ================================ */
454 
455 scope class AutoClass{
456 }
457 
458 void test22()
459 {
460         scope AutoClass ac = new AutoClass();
461 
462         with(ac){
463         }
464 }
465 
466 /* ================================ */
467 
468 int status23;
469 
470 scope class C23{
471         ~this(){
472                 assert(status23==0);
473                 status23--;
474                 throw new Exception("error msg");
475         }
476 }
477 
478 void foo23(){
479         assert(status23==0);
480         scope C23 ac = new C23();
481 }
482 
483 void test23()
484 {
485         try{
486                 foo23();
487         }catch{
488         }
489         assert(status23==-1);
490 }
491 
492 /* ================================ */
493 
494 int status24;
495 
496 scope class C24{
497         this(){
498                 assert(status24==0);
499                 status24+=2;
500         }
501         ~this(){
502                 assert(status24==2);
503                 status24--;
504                 throw new Exception("error msg");
505         }
506 }
507 
508 void check24(){
509         scope C24 ac = new C24();
510         throw new Exception("check error");
511 }
512 
513 void test24()
514 {
515         assert(status24==0);
516         try{
517                 check24();
518         }catch{
519                 assert(status24==1);
520                 status24-=5;
521         }
522         assert(status24==-4);
523 }
524 
525 /* ================================ */
526 
527 struct S25{
528         S25 opSub(int i) { S25 s; return s; }
529 }
530 
531 struct GeomObject{
532         S25     mesh;
533         int     xlate;
534 }
535 
536 
537 void extractTriangles(GeomObject g)
538 {
539     void foobar()
540     {
541         g.mesh - g.xlate;
542         //g.mesh.opSub(g.xlate);
543     }
544 
545     foobar();
546 }
547 
548 void test25()
549 {
550 }
551 
552 /* ================================ */
553 
554 struct S26{
555         int i;
556 
557         void display(){
558                 assert(i==10);
559         }
560 
561         void someFunc(){
562                 // We never call this function
563                 void bug(S26[] array){
564                         array[0].i = i+1;
565                 }
566 
567                 assert(i==10);
568                 display();
569                 assert(i==10);
570         }
571 }
572 
573 void test26()
574 {
575         S26 m;
576         m.i = 10;
577         assert(m.i==10);
578         m.someFunc();
579         assert(m.i==10);
580 }
581 
582 /* ================================ */
583 
584 template foo27(T:T[],alias S) {
585         string foo(T[] a, T[] b) {
586                 return a ~ S ~ b;
587         }
588 }
589 
590 string comma = ", ";
591 alias foo27!(string,comma).foo catComma;
592 
593 void test27()
594 {
595         string a = "Heath";
596         string b = "Regan";
597 
598         assert("Heath, Regan"==catComma(a,b));
599 }
600 
601 /* ================================ */
602 
603 void test28()
604 {
605         assert((new S28!()).i==int.sizeof);
606 }
607 
608 struct S28(){
609         int i=func28(0).sizeof;
610 }
611 
612 int func28(...){
613         return 0;
614 }
615 
616 /* ================================ */
617 
618 void test29()
619 {
620         uint u;
621         u = 1 << 31;
622         assert( u == 0b1000_0000__0000_0000__0000_0000__0000_0000u);
623 }
624 
625 /* ================================ */
626 // Test for FinalizeError - it will be thrown if an Exception is thrown
627 // during the class object finalization.
628 
629 int status30;
630 
631 class C30
632 {
633     this()
634     {
635         status30++;
636     }
637 
638     ~this()
639     {
640         status30--;
641         throw new Exception("E2");
642     }
643 }
644 
645 void test30()
646 {
647     try
648     {
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();
657 
658         assert(status30 == 1);
659 
660         delete m;   // _d_callfinalizer
661     }
662     catch (Error e) // FinalizeError
663     {
664         assert(status30 == 0);
665         status30--;
666     }
667 
668     assert(status30 == -1);
669 }
670 
671 /* ================================ */
672 
673 void test31()
674 {
675         string str = x"F0 9D 83 93"; // utf-8 for U+1D0D3
676 
677         int count=0;
678         dchar tmp;
679         foreach(dchar value ; str){
680                 tmp=value;
681                 count++;
682         }
683         assert(count==1);
684         assert(tmp==0x01D0D3);
685 }
686 
687 /* ================================ */
688 
689 import std.stdio;
690 
691 union MyUnion32
692 {
693         int i;
694         byte b;
695 }
696 
697 void test32()
698 {
699         TypeInfo ti = typeid(MyUnion32*);
700         assert(!(ti is null));
701         writefln("%s %d %d", ti.toString(), ti.tsize, (MyUnion32*).sizeof);
702         assert(ti.tsize==(MyUnion32*).sizeof);
703         assert(ti.toString()=="dstress.run.module_01.MyUnion32*");
704 }
705 
706 /* ================================ */
707 
708 void test33()
709 {
710         creal a=1.3L+9.7Li;
711         assert(a.re == 1.3L);
712         assert(a.im == 9.7L);
713 }
714 
715 /* ================================ */
716 
717 void test34()
718 {
719         creal c = 2.7L + 0i;
720         assert(c.re==2.7L);
721         assert(c.im==0.0L);
722 }
723 
724 /* ================================ */
725 
726 void test35()
727 {
728         try{
729                 onOutOfMemoryError();
730         }catch(OutOfMemoryError e){
731                 return;
732         }
733         assert(0);
734 }
735 
736 /* ================================ */
737 
738 void test36()
739 {
740         try{
741                 onOutOfMemoryError();
742         }catch(OutOfMemoryError e){
743                 return;
744         }
745         assert(0);
746 }
747 
748 /* ================================ */
749 
750 struct S37{
751         int a;
752 }
753 
754 const int i37 = 15;
755 
756 const S37 s1 = { i37+1 };
757 const S37 s2 = s1;
758 
759 void test37()
760 {
761         assert(s1.a == 16);
762         assert(s2.a == 16);
763 }
764 
765 /* ================================ */
766 
767 class Foo38
768 {
769         enum MyEnum{
770                 VALUE_A=1,
771         }
772 }
773 
774 class Bar38
775 {
776         enum MyEnum{
777                 VALUE_B=2,
778         }
779 }
780 
781 void test38()
782 {
783         assert(Foo38.MyEnum.VALUE_A==1);
784         assert(Bar38.MyEnum.VALUE_B==2);
785 }
786 
787 /* ================================ */
788 
789 void test39()
790 {
791         bool[] bArray;
792         int[] iArray;
793 
794         bArray[]=false;
795 
796         foreach(int c; iArray){
797                 assert(0);
798         }
799 }
800 
801 /* ================================ */
802 
803 bool checked40;
804 
805 class Parent40{
806         int x;
807 
808         void test(){
809         }
810 
811         invariant()
812         {
813                 assert(!checked40);
814                 checked40=true;
815                 // even number
816                 assert((x&1u)==0);
817         }
818 }
819 
820 class Child40 : Parent40{
821 }
822 
823 class GrandChild40 : Child40{
824         this(){
825                 x=5;
826         }
827 }
828 
829 void test40()
830 {
831         try{
832                 assert(!checked40);
833                 GrandChild40 gc = new GrandChild40();
834         }catch{
835                 assert(checked40);
836                 return;
837         }
838         assert(0);
839 }
840 
841 /* ================================ */
842 
843 int counter41;
844 
845 class C41{
846         this(){
847                 printf("this: counter41 = %d\n", counter41);
848                 assert(counter41==1);
849                 counter41+=2;
850         }
851 
852         new(size_t size){
853                 printf("new: size = %d\n", size);
854                 assert(counter41==0);
855                 counter41++;
856                 return malloc(size);
857         }
858 }
859 
860 void test41()
861 {
862         C41 c;
863         assert(counter41==0);
864         c = new C41();
865         assert(counter41==3);
866 }
867 
868 /* ================================ */
869 
870 struct S42{
871         int y;
872         void* x;
873 }
874 
875 void test42()
876 {
877         size_t t;
878         t = S42.y.offsetof;
879         assert(t == 0);
880         t = S42.x.offsetof;
881         assert((t % (void*).sizeof) == 0);
882 }
883 
884 /* ================================ */
885 
886 int main()
887 {
888     test1();
889     test2();
890     test3();
891     test4();
892     test5();
893     test6();
894     test7();
895     test8();
896     test9();
897     test10();
898     test11();
899     test12();
900     test13();
901     test14();
902     test15();
903     test16();
904     test17();
905     test18();
906     test19();
907     test20();
908     test21();
909     test22();
910     test23();
911     test24();
912     test25();
913     test26();
914     test27();
915     test28();
916     test29();
917     test30();
918     test31();
919     test32();
920     test33();
921     test34();
922     test35();
923     test36();
924     test37();
925     test38();
926     test39();
927     test40();
928     test41();
929     test42();
930 
931     printf("Success\n");
932     return 0;
933 }
934