]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/runnable/inner.d
Add D front-end, libphobos library, and D2 testsuite.
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / inner.d
1
2 import std.stdio;
3
4 /*******************************************************/
5
6 class Outer
7 {
8 int o;
9 static int os = 3;
10
11 static class StaticInner
12 {
13 int si;
14
15 int foo()
16 {
17 return si + os;
18 }
19 }
20
21 class Inner
22 {
23 int i;
24
25 int foo(int m)
26 {
27 return i * o + m;
28 }
29
30 this(int k)
31 {
32 i = k + 7;
33 os = 6;
34 }
35 }
36
37 Inner bar()
38 {
39 return new Inner(17);
40 }
41 }
42
43 void test1()
44 {
45 Outer p = new Outer();
46
47 assert(p.o == 0);
48 assert(p.os == 3);
49 p.o = 9;
50
51 Outer.StaticInner s = new Outer.StaticInner;
52 s.si = 10;
53 assert(s.foo() == 13);
54
55 Outer.Inner q = p.bar();
56 assert(q.i == 24);
57 assert(q.foo(8) == 24*9+8);
58 }
59
60 /*******************************************************/
61
62 class C1
63 {
64 int c1;
65
66 this()
67 {
68 c1 = 2;
69 }
70
71 class C2
72 {
73 class C3
74 {
75 int c3;
76
77 this(int n)
78 {
79 c3 = n + c1 + c2;
80 }
81 }
82
83 int c2;
84
85 C3 foo()
86 {
87 return new C3(8);
88 }
89
90 this(int k)
91 {
92 c2 = k + 7;
93 }
94 }
95
96 C2 bar()
97 {
98 return new C2(17);
99 }
100 }
101
102 void test2()
103 {
104 C1 p = new C1();
105 assert(p.c1 == 2);
106
107 C1.C2 q = p.bar();
108 assert(q.c2 == 24);
109
110 C1.C2.C3 r = q.foo();
111 assert(r.c3 == 2+24+8);
112 }
113
114 /*******************************************************/
115
116 class C3
117 {
118 int c1;
119
120 this()
121 {
122 c1 = 2;
123 }
124
125 class B { }
126
127 class C2 : B
128 {
129 int c2;
130
131 this(int k)
132 {
133 c2 = c1 + k + 7;
134 }
135 }
136
137 C2 bar()
138 {
139 return new C2(17);
140 }
141 }
142
143 void test3()
144 {
145 C3 q = new C3();
146 assert(q.c1 == 2);
147 C3.C2 p = q.bar();
148 assert(p.c2 == 26);
149 }
150
151
152 /*******************************************************/
153
154 void test4()
155 {
156 int m = 3;
157
158 class C
159 {
160 void foo()
161 {
162 assert(m == 3);
163 }
164 }
165
166 void bar()
167 {
168 assert(m == 3);
169 }
170
171 bar();
172 C c = new C;
173 c.foo();
174 }
175
176 /*******************************************************/
177
178 void test5()
179 {
180 int m = 3;
181
182 void bar()
183 {
184 assert(m == 3);
185 }
186
187 class C
188 {
189 void foo()
190 {
191 assert(m == 3);
192 bar();
193 }
194 }
195
196 bar();
197 C c = new C;
198 c.foo();
199 }
200
201
202 /*******************************************************/
203
204 void test6()
205 {
206 int m = 3;
207
208 void bar()
209 {
210 assert(m == 3);
211 }
212
213 void abc()
214 {
215 class C
216 {
217 void foo()
218 {
219 assert(m == 3);
220 bar();
221 }
222 }
223
224 bar();
225 C c = new C;
226 c.foo();
227 }
228
229 abc();
230 }
231
232 /*******************************************************/
233
234 void test7()
235 {
236 int m = 3;
237
238 void bar()
239 {
240 assert(m == 3);
241 }
242
243 void ghi()
244 {
245 void abc()
246 {
247 class C
248 {
249 void foo()
250 {
251 assert(m == 3);
252 bar();
253 }
254 }
255
256 bar();
257 C c = new C;
258 c.foo();
259 }
260
261 abc();
262 }
263
264 ghi();
265 }
266
267 /*******************************************************/
268
269 void test8()
270 {
271 int m = 3;
272
273 void bar()
274 {
275 assert(m == 3);
276 }
277
278 void ghi()
279 {
280 void abc()
281 {
282 class C
283 {
284 void foo()
285 {
286 void fooey()
287 {
288 assert(m == 3);
289 bar();
290 }
291
292 fooey();
293 }
294 }
295
296 bar();
297 C c = new C;
298 c.foo();
299 }
300
301 abc();
302 }
303
304 ghi();
305 }
306
307 /*******************************************************/
308
309 class C9
310 {
311 int m;
312
313 void ccc()
314 {
315 assert(m == 3);
316 }
317
318 this()
319 {
320 m = 3;
321 }
322
323 class D
324 {
325 int n;
326
327 this() { n = 4; }
328
329 void abc()
330 {
331 assert(m == 3);
332 ccc();
333 }
334 }
335
336 D def()
337 {
338 return new D;
339 }
340 }
341
342
343 void test9()
344 {
345 C9 c = new C9;
346 assert(c.m == 3);
347 c.ccc();
348
349 C9.D d = c.def();
350 assert(d.n == 4);
351 d.abc();
352 }
353
354 /*******************************************************/
355
356 void test10()
357 {
358 int m = 3;
359
360 class C
361 {
362 void foo()
363 {
364 assert(m == 3);
365 }
366 }
367
368 void abc()
369 {
370 C c = new C;
371 c.foo();
372 }
373
374 abc();
375 }
376
377 /*******************************************************/
378
379 void test11()
380 {
381 int m = 3;
382
383 class C
384 {
385 void foo()
386 {
387 assert(m == 3);
388 }
389 }
390
391 void abc()
392 {
393 void def()
394 {
395 C c = new C;
396 c.foo();
397 }
398
399 def();
400 }
401
402 abc();
403 }
404
405 /*******************************************************/
406
407 void test12()
408 {
409 int m = 3;
410
411 class C
412 {
413 void foo()
414 {
415 assert(m == 3);
416 }
417
418 void abc()
419 {
420 assert(m == 3);
421 C c = new C;
422 c.foo();
423 }
424 }
425
426 C d = new C;
427 d.abc();
428 }
429
430 /*******************************************************/
431
432 void test13()
433 {
434 int m = 3;
435
436 class C
437 {
438 void foo()
439 {
440 assert(m == 3);
441 }
442
443 void abc()
444 {
445 void def()
446 {
447 assert(m == 3);
448 C c = new C;
449 c.foo();
450 }
451
452 def();
453 }
454 }
455
456 C d = new C;
457 d.abc();
458 }
459
460 /*******************************************************/
461
462 class C14
463 {
464 void foo()
465 {
466 assert(0);
467 }
468 }
469
470 void test14()
471 {
472 int m = 3;
473
474 C14 c = new class C14
475 {
476 override void foo()
477 {
478 printf("in inner class\n");
479 assert(m == 3);
480 }
481 };
482
483 c.foo();
484 }
485
486 /*******************************************************/
487
488 class C15
489 {
490 void foo()
491 {
492 assert(0);
493 }
494 }
495
496 void test15()
497 {
498 int m = 3;
499
500 C15 c = new class(1,2) C15
501 {
502 this(int i, int j)
503 {
504 printf("in inner class ctor\n");
505 assert(i == 1);
506 assert(j == 2);
507 }
508
509 override void foo()
510 {
511 printf("in inner class\n");
512 assert(m == 3);
513 }
514 };
515
516 c.foo();
517 }
518
519 /*******************************************************/
520
521 class C16
522 {
523 int w = 3;
524
525 void iterator()
526 {
527 class Foo
528 {
529 void current()
530 {
531 assert(w == 3);
532 }
533 }
534
535 Foo f = new Foo();
536 f.current();
537 }
538 }
539
540 void test16()
541 {
542 C16 s = new C16();
543
544 s.iterator();
545 }
546
547 /*******************************************************/
548
549 class Owner
550 {
551
552 this()
553 {
554 n = new Nested(this);
555 }
556
557 class Nested
558 {
559 this(Owner owner)
560 {
561 m = owner;
562 }
563
564 Owner foo()
565 {
566 return m;
567 }
568
569 Owner m;
570 }
571
572 Nested n;
573 }
574
575 class OwnerDerived : Owner
576 {
577 }
578
579 void test17()
580 {
581 OwnerDerived o = new OwnerDerived();
582
583 assert(o.n.foo() is o);
584 }
585
586 /*******************************************************/
587
588 class Foo18
589 {
590 class Bar
591 {
592 void doSayHello()
593 {
594 writefln("Betty");
595 sayHello();
596 }
597 }
598 Bar bar;
599
600 void sayHello()
601 {
602 writefln("Hello");
603 }
604 }
605
606 class Foo182 : Foo18
607 {
608 this()
609 {
610 bar = new Bar();
611 }
612 }
613
614 void test18()
615 {
616 Foo182 foo = new Foo182();
617 writefln("This should print Hello:");
618
619 foo.bar.doSayHello();
620 }
621
622 /*******************************************************/
623
624 class Foo19
625 {
626 class Bar
627 {
628 void doSayHello()
629 {
630 writefln("Betty");
631 sayHello();
632 }
633 }
634 Bar bar;
635
636 void sayHello()
637 {
638 writefln("Hello");
639 }
640
641 this()
642 {
643 bar = new Bar();
644 }
645 }
646
647 class Foo192 : Foo19
648 {
649 }
650
651 void test19()
652 {
653 Foo192 foo = new Foo192();
654 writefln("This should print Hello:");
655
656 foo.bar.doSayHello();
657 }
658
659 /*******************************************************/
660
661 class Outer20
662 {
663 int a;
664
665 class Inner
666 {
667 int foo()
668 {
669 return a;
670 }
671 }
672 }
673
674 void test20()
675 {
676 Outer20 o = new Outer20;
677 o.a = 3;
678 Outer20.Inner oi = o.new Inner;
679 assert(oi.foo() == 3);
680 }
681
682 /*******************************************************/
683
684 class Foo21{}
685
686 static if (is(typeof(new class Foo21{}))) {}
687
688 void test21()
689 {
690 }
691
692 /*******************************************************/
693
694 class Outer22
695 {
696 class Inner
697 {
698 Outer22 foo()
699 {
700 return this.outer;
701 }
702 }
703
704 void bar()
705 {
706 Inner i = new Inner;
707 assert(this == i.foo());
708 }
709 }
710
711 void test22()
712 {
713 Outer22 o = new Outer22;
714 o.bar();
715 }
716
717 /*******************************************************/
718
719 class Adapter23
720 {
721 void func() { }
722 }
723
724 class Foo23
725 {
726 class AnonAdapter : Adapter23
727 {
728 }
729
730 void bar()
731 {
732 Adapter23 a = cast( Adapter23 )( new AnonAdapter() );
733 }
734 }
735
736 void test23()
737 {
738 Foo23 f = new Foo23();
739 f.bar();
740 Adapter23 a = cast( Adapter23 )( f.new AnonAdapter() );
741 auto aa = f.new AnonAdapter();
742 Adapter23 ab = cast( Adapter23 )(aa);
743
744 }
745
746 /*******************************************************/
747
748 class I24
749 {
750 public abstract void callI();
751 }
752
753 C24 c24;
754
755 class C24
756 {
757 private int index;
758 void foo()
759 {
760 printf( "ok, this = %p\n", this);
761 assert(this == c24);
762 }
763 I24 bar()
764 {
765 auto i = new class() I24
766 {
767 override public void callI()
768 {
769 foo();
770 }
771 };
772 printf("bar.this = %p\n", this);
773 printf(" i.this = %p\n", (cast(void**)i)[2]);
774 assert(*cast(void**)&c24 == (cast(void**)i)[2]);
775 return i;
776 }
777 }
778
779 void test24()
780 {
781 c24 = new C24;
782 printf("c = %p\n", c24);
783 auto i = c24.bar();
784 i.callI();
785 }
786
787 /*******************************************************/
788
789 struct S7426
790 {
791 static struct Inner
792 {
793 int x;
794 alias typeof(Inner.tupleof) T;
795 }
796 }
797
798 /*******************************************************/
799 // 14046
800
801 class A14046
802 {
803 class NestedA { }
804 }
805
806 class B14046 : A14046
807 {
808 int field;
809
810 class NestedB : NestedA
811 {
812 void foo()
813 {
814 this.outer.field = 1; // ok <- disallowed
815
816 //(cast(B14046)this.outer).field = 1; // workaround
817 }
818 }
819 }
820
821 void test14046()
822 {
823 auto b = new B14046();
824 auto nb = b.new NestedB();
825 assert(b.field == 0);
826 nb.foo();
827 assert(b.field == 1);
828 }
829
830 /*******************************************************/
831 // 15839
832
833 class AnimatedProgress15839(bool makeClosure)
834 {
835 static AnimatedProgress15839 saveThis;
836
837 interface Runnable {}
838
839 static class GC
840 {
841 this(AnimatedProgress15839 ap)
842 {
843 assert(ap is saveThis);
844 }
845 }
846
847 void start()
848 {
849 assert(this is saveThis);
850
851 static if (makeClosure) int a;
852
853 auto r = new class Runnable
854 {
855 void run()
856 {
857 static assert(is(typeof(this.outer) == AnimatedProgress15839));
858 assert(this.outer is saveThis);
859
860 GC gc = new GC(this.outer);
861
862 static if (makeClosure) int b = a;
863 }
864 };
865 r.run();
866 }
867 }
868
869 void test15839()
870 {
871 auto ap1 = new AnimatedProgress15839!false();
872 ap1.saveThis = ap1;
873 ap1.start();
874
875 auto ap2 = new AnimatedProgress15839!true();
876 ap2.saveThis = ap2;
877 ap2.start();
878 }
879
880 /*******************************************************/
881
882 int main()
883 {
884
885 test1();
886 test2();
887 test3();
888 test4();
889 test5();
890 test6();
891 test7();
892 test8();
893 test9();
894 test10();
895
896 test11();
897 test12();
898 test13();
899
900 test14();
901 test15();
902 test16();
903 test17();
904 test18();
905 test19();
906 test20();
907
908 test21();
909 test22();
910 test23();
911 test24();
912
913 test14046();
914 test15839();
915
916 printf("Success\n");
917 return 0;
918 }