d: Merge upstream dmd, druntime 26f049fb26, phobos 330d6a4fd.
[official-gcc.git] / gcc / testsuite / gdc.test / runnable / testcontracts.d
blob63d18fcc6270240595be8a16063399e04190c022
1 /* PERMUTE_ARGS: -inline -g -O
2 */
3 extern(C) int printf(const char*, ...);
5 /*******************************************/
7 class A
9 int x = 7;
11 int foo(int i)
14 printf("A.foo.in %d\n", i);
15 assert(i == 2);
16 assert(x == 7);
17 printf("A.foo.in pass\n");
19 out (result)
21 assert(result & 1);
22 assert(x == 7);
26 return i;
30 class B : A
32 override int foo(int i)
35 float f;
36 printf("B.foo.in %d\n", i);
37 assert(i == 4);
38 assert(x == 7);
39 f = f + i;
41 out (result)
43 assert(result < 8);
44 assert(x == 7);
48 return i - 1;
52 void test1()
54 auto b = new B();
55 b.foo(2);
56 b.foo(4);
59 /*******************************************/
61 class A2
63 int x = 7;
65 int foo(int i)
68 printf("A2.foo.in %d\n", i);
69 assert(i == 2);
70 assert(x == 7);
71 printf("A2.foo.in pass\n");
73 out (result)
75 assert(result & 1);
76 assert(x == 7);
80 return i;
84 class B2 : A2
86 override int foo(int i)
89 float f;
90 printf("B2.foo.in %d\n", i);
91 assert(i == 4);
92 assert(x == 7);
93 f = f + i;
95 out (result)
97 assert(result < 8);
98 assert(x == 7);
102 return i - 1;
106 class C : B2
108 override int foo(int i)
111 float f;
112 printf("C.foo.in %d\n", i);
113 assert(i == 6);
114 assert(x == 7);
115 f = f + i;
117 out (result)
119 assert(result == 1 || result == 3 || result == 5);
120 assert(x == 7);
124 return i - 1;
128 void test2()
130 auto c = new C();
131 c.foo(2);
132 c.foo(4);
133 c.foo(6);
136 /*******************************************/
138 void fun(int x)
139 in {
140 if (x < 0) throw new Exception("a");
142 do {
145 void test3()
147 fun(1);
150 /*******************************************/
152 interface Stack {
153 int pop()
154 // in { printf("pop.in\n"); }
155 out(result) {
156 printf("pop.out\n");
157 assert(result == 3);
161 class CC : Stack
163 int pop()
164 //out (result) { printf("CC.pop.out\n"); } do
166 printf("CC.pop.in\n");
167 return 3;
171 void test4()
173 auto cc = new CC();
174 cc.pop();
177 /*******************************************/
179 int mul100(int n)
180 out(result)
182 assert(result == 500);
186 return n * 100;
189 void test5()
191 mul100(5);
194 /*******************************************/
195 // https://issues.dlang.org/show_bug.cgi?id=3273
197 // original case
198 struct Bug3273
200 ~this() {}
201 invariant() {}
204 // simplest case
205 ref int func3273()
206 out(r)
208 // Regression check of https://issues.dlang.org/show_bug.cgi?id=3390
209 static assert(!__traits(compiles, r = 1));
213 static int dummy;
214 return dummy;
217 void test6()
219 func3273() = 1;
220 assert(func3273() == 1);
223 /*******************************************/
226 // https://issues.dlang.org/show_bug.cgi?id=3722
228 class Bug3722A
230 void fun() {}
232 class Bug3722B : Bug3722A
234 override void fun() in { assert(false); } do {}
237 void test6()
239 auto x = new Bug3722B();
240 x.fun();
244 /*******************************************/
246 auto test7foo()
248 ++cnt;
249 }do{
250 ++cnt;
251 return "str";
254 void test7()
256 cnt = 0;
257 assert(test7foo() == "str");
258 assert(cnt == 2);
261 /*******************************************/
263 auto foo8()
264 out(r){
265 ++cnt;
266 assert(r == 10);
267 }do{
268 ++cnt;
269 return 10;
272 auto bar8()
273 out{
274 ++cnt;
275 }do{
276 ++cnt;
279 void test8()
281 cnt = 0;
282 assert(foo8() == 10);
283 assert(cnt == 2);
285 cnt = 0;
286 bar8();
287 assert(cnt == 2);
290 /*******************************************/
291 // from fail317
293 void test9()
296 auto f1 = function() do { }; // fine
297 auto f2 = function() in { } do { }; // fine
298 auto f3 = function() out { } do { }; // error
299 auto f4 = function() in { } out { } do { }; // error
301 auto d1 = delegate() do { }; // fine
302 auto d2 = delegate() in { } do { }; // fine
303 auto d3 = delegate() out { } do { }; // error
304 auto d4 = delegate() in { } out { } do { }; // error
307 auto f1 = function() body { }; // fine
308 auto f2 = function() in { } body { }; // fine
309 auto f3 = function() out { } body { }; // error
310 auto f4 = function() in { } out { } body { }; // error
312 auto d1 = delegate() body { }; // fine
313 auto d2 = delegate() in { } body { }; // fine
314 auto d3 = delegate() out { } body { }; // error
315 auto d4 = delegate() in { } out { } body { }; // error
319 /*******************************************/
321 auto test10() do { return 3; }
322 auto test11()() do { return 3; }
324 auto test12()
326 auto test10() do { return 3; }
327 auto test11()() do { return 3; }
328 return 3;
332 void test13()
334 int function() fp13;
337 /*******************************************/
338 // https://issues.dlang.org/show_bug.cgi?id=4785
340 int cnt;
342 auto foo4785()
344 int r;
345 ++cnt;
347 out(r){
348 assert(r == 10);
349 ++cnt;
350 }do{
351 ++cnt;
352 int r = 10;
353 return r;
355 void test4785()
357 cnt = 0;
358 assert(foo4785() == 10);
359 assert(cnt == 3);
362 /*******************************************/
363 // https://issues.dlang.org/show_bug.cgi?id=5039
365 class C5039 {
366 int x;
368 invariant() {
369 assert( x < int.max );
372 auto foo() {
373 return x;
377 /*******************************************/
378 // https://issues.dlang.org/show_bug.cgi?id=5204
380 interface IFoo5204
382 IFoo5204 bar()
383 out {}
385 class Foo5204 : IFoo5204
387 Foo5204 bar() { return null; }
390 /*******************************************/
391 // https://issues.dlang.org/show_bug.cgi?id=6417
393 class Bug6417
395 void bar()
398 int i = 14;
399 assert(i == 14);
400 auto dg = (){
401 //printf("in: i = %d\n", i);
402 assert(i == 14, "in contract failure");
404 dg();
408 int j = 10;
409 assert(j == 10);
410 auto dg = (){
411 //printf("out: j = %d\n", j);
412 assert(j == 10, "out contract failure");
414 dg();
416 do {}
419 void test6417()
421 (new Bug6417).bar();
424 /*******************************************/
425 // https://issues.dlang.org/show_bug.cgi?id=6549
427 class C6549
429 static int ocount = 0;
430 static int icount = 0;
432 abstract int foo(int)
433 in { ++icount; }
434 out { ++ocount; }
437 class CD6549 : C6549
439 override int foo(int)
440 in { assert(false); }
441 do { return 10; }
444 abstract class D6549
446 static int icount = 0;
447 static int ocount = 0;
449 int foo(int)
450 in { ++icount; }
451 out { ++ocount; }
454 class DD6549 : D6549
456 override int foo(int)
457 in { assert(false); }
458 do { return 10; }
461 void test6549()
463 auto c = new CD6549;
464 c.foo(10);
465 assert(C6549.icount == 1);
466 assert(C6549.ocount == 1);
468 auto d = new DD6549;
469 d.foo(10);
470 assert(D6549.icount == 1);
471 assert(D6549.ocount == 1);
474 /*******************************************/
475 // https://issues.dlang.org/show_bug.cgi?id=7218
477 void test7218()
480 size_t foo() in{} out{} do{ return 0; } // OK
481 size_t bar() in{}/*out{}*/do{ return 0; } // OK
482 size_t hoo()/*in{}*/out{} do{ return 0; } // NG1
483 size_t baz()/*in{} out{}*/do{ return 0; } // NG2
486 size_t goo() in{} out{} body{ return 0; } // OK
487 size_t gar() in{}/*out{}*/body{ return 0; } // OK
488 size_t gob()/*in{}*/out{} body{ return 0; } // NG1
489 size_t gaz()/*in{} out{}*/body{ return 0; } // NG2
493 /*******************************************/
494 // https://issues.dlang.org/show_bug.cgi?id=7335
496 class A7335
498 int mValue = 10;
500 void setValue(int newValue)
501 in { }
502 out { assert(mValue == 3); }
505 mValue = newValue;
509 class B7335 : A7335
511 override void setValue(int newValue)
512 in { assert(false); }
513 out { assert(mValue == 3); }
516 mValue = newValue;
520 class C7335 : A7335
522 override void setValue(int newValue)
523 in { int a = newValue; }
524 out { assert(mValue == 3); }
527 mValue = newValue;
531 void test7335()
533 A7335 aObject = new B7335();
534 aObject.setValue(3);
536 A7335 bObject = new C7335();
537 bObject.setValue(3); // <<<<< will crash because undefined mValue in the
538 // A7335.setValue().out-block.
541 /*******************************************/
542 // https://issues.dlang.org/show_bug.cgi?id=7517
544 void test7517()
546 static string result;
548 interface I
550 static I self;
552 void setEnable()
555 assert(self is this);
556 result ~= "I.setEnable.in/";
557 assert(!enabled);
561 assert(self is this);
562 result ~= "I.setEnable.out/";
563 assert( enabled);
566 void setDisable()
569 assert(self is this);
570 result ~= "I.setDisable.in/";
571 assert( enabled);
575 assert(self is this);
576 result ~= "I.setDisable.out/";
577 assert(!enabled);
580 @property bool enabled() const;
583 class C : I
585 static C self;
587 void setEnable()
588 in {} // supply in-contract to invoke I.setEnable.in
591 assert(self is this);
592 result ~= "C.setEnable/";
593 _enabled = true;
596 void setDisable()
598 assert(self is this);
599 result ~= "C.setDisable/";
600 _enabled = false;
603 @property bool enabled() const
605 assert(self is this);
606 result ~= "C.enabled/";
607 return _enabled;
610 bool _enabled;
613 C c = C.self = new C;
614 I i = I.self = c;
616 result = null;
617 i.setEnable();
618 assert(result == "I.setEnable.in/C.enabled/C.setEnable/I.setEnable.out/C.enabled/");
620 result = null;
621 i.setDisable();
622 assert(result == "C.setDisable/I.setDisable.out/C.enabled/");
625 /*******************************************/
626 // https://issues.dlang.org/show_bug.cgi?id=7699
628 class P7699
630 void f(int n) in {
631 assert (n);
632 } do { }
634 class D7699 : P7699
636 override void f(int n) in { } do { }
639 /*******************************************/
640 // https://issues.dlang.org/show_bug.cgi?id=7883
642 // Segmentation fault
643 class AA7883
645 int foo()
646 out (r1) { }
647 do { return 1; }
650 class BA7883 : AA7883
652 override int foo()
653 out (r2) { }
654 do { return 1; }
657 class CA7883 : BA7883
659 override int foo()
660 do { return 1; }
663 // Error: undefined identifier r2, did you mean variable r3?
664 class AB7883
666 int foo()
667 out (r1) { }
668 do { return 1; }
671 class BB7883 : AB7883
673 override int foo()
674 out (r2) { }
675 do { return 1; }
679 class CB7883 : BB7883
681 override int foo()
682 out (r3) { }
683 do { return 1; }
686 // Error: undefined identifier r3, did you mean variable r4?
687 class AC7883
689 int foo()
690 out (r1) { }
691 do { return 1; }
694 class BC7883 : AC7883
696 override int foo()
697 out (r2) { }
698 do { return 1; }
701 class CC7883 : BC7883
703 override int foo()
704 out (r3) { }
705 do { return 1; }
708 class DC7883 : CC7883
710 override int foo()
711 out (r4) { }
712 do { return 1; }
715 /*******************************************/
716 // https://issues.dlang.org/show_bug.cgi?id=7892
718 struct S7892
720 @disable this();
721 this(int x) {}
724 S7892 f7892()
725 out (result) {} // case 1
728 return S7892(1);
731 interface I7892
733 S7892 f();
735 class C7892
737 invariant() {} // case 2
739 S7892 f()
741 return S7892(1);
745 /*******************************************/
746 // https://issues.dlang.org/show_bug.cgi?id=8066
748 struct CLCommandQueue
750 invariant() {}
752 //private:
753 int enqueueNativeKernel()
755 assert(0, "implement me");
759 /*******************************************/
760 // https://issues.dlang.org/show_bug.cgi?id=8073
762 struct Container8073
764 int opApply (int delegate(ref int) dg) { return 0; }
767 class Bug8073
769 static int test;
770 int foo()
771 out(r) { test = 7; } do
773 Container8073 ww;
774 foreach( xxx ; ww ) { }
775 return 7;
778 ref int bar()
779 out { } do
781 Container8073 ww;
782 foreach( xxx ; ww ) { }
783 test = 7;
784 return test;
787 void test8073()
789 auto c = new Bug8073();
790 assert(c.foo() == 7);
791 assert(c.test == 7);
793 auto p = &c.bar();
794 assert(p == &c.test);
795 assert(*p == 7);
798 /*******************************************/
799 // https://issues.dlang.org/show_bug.cgi?id=8093
801 void test8093()
803 static int g = 10;
804 static int* p;
806 enum fdo = q{
807 static struct S {
808 int opApply(scope int delegate(ref int) dg) { return dg(g); }
810 S s;
811 foreach (ref e; s)
812 return g;
813 assert(0);
816 ref int foo_ref1() out(r) { assert(&r is &g && r == 10); }
817 do { mixin(fdo); }
819 ref int foo_ref2()
820 do { mixin(fdo); }
822 { auto q = &foo_ref1(); assert(q is &g && *q == 10); }
823 { auto q = &foo_ref2(); assert(q is &g && *q == 10); }
825 int foo_val1() out(r) { assert(&r !is &g && r == 10); }
826 do { mixin(fdo); }
828 int foo_val2()
829 do { mixin(fdo); }
831 { auto n = foo_val1(); assert(&n !is &g && n == 10); }
832 { auto n = foo_val2(); assert(&n !is &g && n == 10); }
835 /*******************************************/
836 // https://issues.dlang.org/show_bug.cgi?id=9383
838 class A9383
840 static void delegate() dg;
841 static int val;
843 void failInBase() { assert(typeid(this) is typeid(A9383)); }
845 // in-contract tests
846 void foo1(int i) in { A9383.val = i; failInBase; } do { } // no closure
847 void foo2(int i) in { A9383.val = i; failInBase; } do { int x; dg = { ++x; }; } // closure [local]
848 void foo3(int i) in { A9383.val = i; failInBase; } do { dg = { ++i; }; } // closure [parameter]
849 void foo4(int i) in { A9383.val = i; failInBase; } do { } // no closure
850 void foo5(int i) in { A9383.val = i; failInBase; } do { } // no closure
851 void foo6(int i) in { A9383.val = i; failInBase; } do { int x; dg = { ++x; }; } // closure [local]
852 void foo7(int i) in { A9383.val = i; failInBase; } do { dg = { ++i; }; } // closure [parameter]
854 // out-contract tests
855 void bar1(int i) out { A9383.val = i; } do { } // no closure
856 void bar2(int i) out { A9383.val = i; } do { int x; dg = { ++x; }; } // closure [local]
857 void bar3(int i) out { A9383.val = i; } do { dg = { ++i; }; } // closure [parameter]
858 void bar4(int i) out { A9383.val = i; } do { } // no closure
859 void bar5(int i) out { A9383.val = i; } do { } // no closure
860 void bar6(int i) out { A9383.val = i; } do { int x; dg = { ++x; }; } // closure [local]
861 void bar7(int i) out { A9383.val = i; } do { dg = { ++i; }; } // closure [parameter]
864 class B9383 : A9383
866 static int val;
868 // in-contract tests
869 override void foo1(int i) in { B9383.val = i; } do { } // -> no closure
870 override void foo2(int i) in { B9383.val = i; } do { int x; dg = { ++x; }; } // -> closure [local] appears
871 override void foo3(int i) in { B9383.val = i; } do { dg = { ++i; }; } // -> closure [parameter]
872 override void foo4(int i) in { B9383.val = i; } do { int x; dg = { ++x; }; } // -> closure [local] appears
873 override void foo5(int i) in { B9383.val = i; } do { dg = { ++i; }; } // -> closure [parameter] appears
874 override void foo6(int i) in { B9383.val = i; } do { } // -> closure [local] disappears
875 override void foo7(int i) in { B9383.val = i; } do { } // -> closure [parameter] disappears
877 // out-contract tests
878 override void bar1(int i) out { B9383.val = i; } do { } // -> no closure
879 override void bar2(int i) out { B9383.val = i; } do { int x; dg = { ++x; }; } // -> closure [local] appears
880 override void bar3(int i) out { B9383.val = i; } do { dg = { ++i; }; } // -> closure [parameter]
881 override void bar4(int i) out { B9383.val = i; } do { int x; dg = { ++x; }; } // -> closure [local] appears
882 override void bar5(int i) out { B9383.val = i; } do { dg = { ++i; }; } // -> closure [parameter] appears
883 override void bar6(int i) out { B9383.val = i; } do { } // -> closure [local] disappears
884 override void bar7(int i) out { B9383.val = i; } do { } // -> closure [parameter] disappears
887 void test9383()
889 auto a = new A9383();
890 auto b = new B9383();
892 // base class in-contract is used from derived class. // base derived
893 b.foo1(101); assert(A9383.val == 101 && B9383.val == 101); // no closure -> no closure
894 b.foo2(102); assert(A9383.val == 102 && B9383.val == 102); // closure [local] -> closure [local] appears
895 b.foo3(103); assert(A9383.val == 103 && B9383.val == 103); // closure [parameter] -> closure [parameter]
896 b.foo4(104); assert(A9383.val == 104 && B9383.val == 104); // no closure -> closure [local] appears
897 b.foo5(105); assert(A9383.val == 105 && B9383.val == 105); // no closure -> closure [parameter] appears
898 b.foo6(106); assert(A9383.val == 106 && B9383.val == 106); // closure [local] -> closure [local] disappears
899 b.foo7(107); assert(A9383.val == 107 && B9383.val == 107); // closure [parameter] -> closure [parameter] disappears
901 // base class out-contract is used from derived class. // base derived
902 b.bar1(101); assert(A9383.val == 101 && B9383.val == 101); // no closure -> no closure
903 b.bar2(102); assert(A9383.val == 102 && B9383.val == 102); // closure [local] -> closure [local] appears
904 b.bar3(103); assert(A9383.val == 103 && B9383.val == 103); // closure [parameter] -> closure [parameter]
905 b.bar4(104); assert(A9383.val == 104 && B9383.val == 104); // no closure -> closure [local] appears
906 b.bar5(105); assert(A9383.val == 105 && B9383.val == 105); // no closure -> closure [parameter] appears
907 b.bar6(106); assert(A9383.val == 106 && B9383.val == 106); // closure [local] -> closure [local] disappears
908 b.bar7(107); assert(A9383.val == 107 && B9383.val == 107); // closure [parameter] -> closure [parameter] disappears
910 // in-contract in base class.
911 a.foo1(101); assert(A9383.val == 101); // no closure
912 a.foo2(102); assert(A9383.val == 102); // closure [local]
913 a.foo3(103); assert(A9383.val == 103); // closure [parameter]
915 // out-contract in base class.
916 a.bar1(101); assert(A9383.val == 101); // no closure
917 a.bar2(102); assert(A9383.val == 102); // closure [local]
918 a.bar3(103); assert(A9383.val == 103); // closure [parameter]
921 /*******************************************/
922 // https://issues.dlang.org/show_bug.cgi?id=15524
923 // Different from https://issues.dlang.org/show_bug.cgi?id=9383 cases, closed variable size is bigger than REGSIZE.
925 class A15524
927 static void delegate() dg;
928 static string val;
930 void failInBase() { assert(typeid(this) is typeid(A15524)); }
932 // in-contract tests
933 void foo1(string s) in { A15524.val = s; failInBase; } do { } // no closure
934 void foo2(string s) in { A15524.val = s; failInBase; } do { string x; dg = { x = null; }; } // closure [local]
935 void foo3(string s) in { A15524.val = s; failInBase; } do { dg = { s = null; }; } // closure [parameter]
936 void foo4(string s) in { A15524.val = s; failInBase; } do { } // no closure
937 void foo5(string s) in { A15524.val = s; failInBase; } do { } // no closure
938 void foo6(string s) in { A15524.val = s; failInBase; } do { string x; dg = { x = null; }; } // closure [local]
939 void foo7(string s) in { A15524.val = s; failInBase; } do { dg = { s = null; }; } // closure [parameter]
941 // out-contract tests
942 void bar1(string s) out { A15524.val = s; } do { } // no closure
943 void bar2(string s) out { A15524.val = s; } do { string x; dg = { x = null; }; } // closure [local]
944 void bar3(string s) out { A15524.val = s; } do { dg = { s = null; }; } // closure [parameter]
945 void bar4(string s) out { A15524.val = s; } do { } // no closure
946 void bar5(string s) out { A15524.val = s; } do { } // no closure
947 void bar6(string s) out { A15524.val = s; } do { string x; dg = { x = null; }; } // closure [local]
948 void bar7(string s) out { A15524.val = s; } do { dg = { s = null; }; } // closure [parameter]
951 class B15524 : A15524
953 static string val;
955 // in-contract tests
956 override void foo1(string s) in { B15524.val = s; } do { } // -> no closure
957 override void foo2(string s) in { B15524.val = s; } do { string x; dg = { x = null; }; } // -> closure [local] appears
958 override void foo3(string s) in { B15524.val = s; } do { dg = { s = null; }; } // -> closure [parameter]
959 override void foo4(string s) in { B15524.val = s; } do { string x; dg = { x = null; }; } // -> closure [local] appears
960 override void foo5(string s) in { B15524.val = s; } do { dg = { s = null; }; } // -> closure [parameter] appears
961 override void foo6(string s) in { B15524.val = s; } do { } // -> closure [local] disappears
962 override void foo7(string s) in { B15524.val = s; } do { } // -> closure [parameter] disappears
964 // out-contract tests
965 override void bar1(string s) out { B15524.val = s; } do { } // -> no closure
966 override void bar2(string s) out { B15524.val = s; } do { string x; dg = { x = null; }; } // -> closure [local] appears
967 override void bar3(string s) out { B15524.val = s; } do { dg = { s = null; }; } // -> closure [parameter]
968 override void bar4(string s) out { B15524.val = s; } do { string x; dg = { x = null; }; } // -> closure [local] appears
969 override void bar5(string s) out { B15524.val = s; } do { dg = { s = null; }; } // -> closure [parameter] appears
970 override void bar6(string s) out { B15524.val = s; } do { } // -> closure [local] disappears
971 override void bar7(string s) out { B15524.val = s; } do { } // -> closure [parameter] disappears
974 void test15524()
976 auto a = new A15524();
977 auto b = new B15524();
979 // base class in-contract is used from derived class. // base derived
980 b.foo1("1"); assert(A15524.val == "1" && B15524.val == "1"); // no closure -> no closure
981 b.foo2("2"); assert(A15524.val == "2" && B15524.val == "2"); // closure [local] -> closure [local] appears
982 b.foo3("3"); assert(A15524.val == "3" && B15524.val == "3"); // closure [parameter] -> closure [parameter]
983 b.foo4("4"); assert(A15524.val == "4" && B15524.val == "4"); // no closure -> closure [local] appears
984 b.foo5("5"); assert(A15524.val == "5" && B15524.val == "5"); // no closure -> closure [parameter] appears
985 b.foo6("6"); assert(A15524.val == "6" && B15524.val == "6"); // closure [local] -> closure [local] disappears
986 b.foo7("7"); assert(A15524.val == "7" && B15524.val == "7"); // closure [parameter] -> closure [parameter] disappears
988 // base class out-contract is used from derived class. // base derived
989 b.bar1("1"); assert(A15524.val == "1" && B15524.val == "1"); // no closure -> no closure
990 b.bar2("2"); assert(A15524.val == "2" && B15524.val == "2"); // closure [local] -> closure [local] appears
991 b.bar3("3"); assert(A15524.val == "3" && B15524.val == "3"); // closure [parameter] -> closure [parameter]
992 b.bar4("4"); assert(A15524.val == "4" && B15524.val == "4"); // no closure -> closure [local] appears
993 b.bar5("5"); assert(A15524.val == "5" && B15524.val == "5"); // no closure -> closure [parameter] appears
994 b.bar6("6"); assert(A15524.val == "6" && B15524.val == "6"); // closure [local] -> closure [local] disappears
995 b.bar7("7"); assert(A15524.val == "7" && B15524.val == "7"); // closure [parameter] -> closure [parameter] disappears
997 // in-contract in base class.
998 a.foo1("1"); assert(A15524.val == "1"); // no closure
999 a.foo2("2"); assert(A15524.val == "2"); // closure [local]
1000 a.foo3("3"); assert(A15524.val == "3"); // closure [parameter]
1002 // out-contract in base class.
1003 a.bar1("1"); assert(A15524.val == "1"); // no closure
1004 a.bar2("2"); assert(A15524.val == "2"); // closure [local]
1005 a.bar3("3"); assert(A15524.val == "3"); // closure [parameter]
1008 void test15524a()
1010 auto t1 = new Test15524a();
1011 t1.infos["first"] = 0; //t1.add("first");
1012 t1.add("second");
1014 auto t2 = new Test15524b();
1015 t2.add("second");
1018 class Test15524a
1020 int[string] infos;
1022 void add(string key)
1025 assert(key !in infos); // @@@ crash here at second
1029 auto item = new class
1031 void notCalled()
1033 infos[key] = 0;
1034 // affects, key parameter is made a closure variable.
1040 class Test15524b
1042 void add(string key)
1045 assert(key == "second"); // @@@ fails
1049 static void delegate() dg;
1050 dg = { auto x = key; };
1051 // affects, key parameter is made a closure variable.
1055 /*******************************************/
1056 // https://issues.dlang.org/show_bug.cgi?id=10479
1058 class B10479
1060 B10479 foo()
1061 out { } do { return null; }
1064 class D10479 : B10479
1066 override D10479 foo() { return null; }
1069 /*******************************************/
1070 // https://issues.dlang.org/show_bug.cgi?id=10596
1072 class Foo10596
1074 auto bar()
1075 out (result) { }
1076 do { return 0; }
1079 /*******************************************/
1080 // https://issues.dlang.org/show_bug.cgi?id=10721
1082 class Foo10721
1084 this()
1085 out { }
1086 do { }
1088 ~this()
1089 out { }
1090 do { }
1093 struct Bar10721
1095 this(this)
1096 out { }
1097 do { }
1100 /*******************************************/
1101 // https://issues.dlang.org/show_bug.cgi?id=10981
1103 class C10981
1105 void foo(int i) pure
1106 in { assert(i); }
1107 out { assert(i); }
1108 do {}
1111 /*******************************************/
1112 // https://issues.dlang.org/show_bug.cgi?id=14779
1114 class C14779
1116 final void foo(int v)
1117 in { assert(v == 0); }
1118 out { assert(v == 0); }
1124 void test14779()
1126 auto c = new C14779();
1127 c.foo(0);
1130 /*******************************************/
1131 // https://issues.dlang.org/show_bug.cgi?id=15984
1133 I15984 i15984;
1134 C15984 c15984;
1136 void check15984(T)(const char* s, T this_, int i)
1138 printf("%s this = %p, i = %d\n", s, this_, i);
1139 static if (is(T == I15984)) assert(this_ is i15984);
1140 else static if (is(T == C15984)) assert(this_ is c15984);
1141 else static assert(0);
1142 assert(i == 5);
1145 interface I15984
1147 void f1(int i)
1148 in { check15984("I.f1.i", this, i); assert(0); }
1149 out { check15984("I.f1.o", this, i); }
1151 int[3] f2(int i)
1152 in { check15984("I.f2.i", this, i); assert(0); }
1153 out { check15984("I.f2.o", this, i); }
1155 void f3(int i)
1156 in { void nested() { check15984("I.f3.i", this, i); } nested(); assert(0); }
1157 out { void nested() { check15984("I.f3.o", this, i); } nested(); }
1159 void f4(out int i, lazy int j)
1160 in { }
1161 out { }
1164 class C15984 : I15984
1166 void f1(int i)
1167 in { check15984("C.f1.i", this, i); }
1168 out { check15984("C.f1.o", this, i); }
1169 do { check15984("C.f1 ", this, i); }
1171 int[3] f2(int i)
1172 in { check15984("C.f2.i", this, i); }
1173 out { check15984("C.f2.o", this, i); }
1174 do { check15984("C.f2 ", this, i); return [0,0,0]; }
1176 void f3(int i)
1177 in { void nested() { check15984("C.f3.i", this, i); } nested(); }
1178 out { void nested() { check15984("C.f3.o", this, i); } nested(); }
1179 do { check15984("C.f3 ", this, i); }
1181 void f4(out int i, lazy int j)
1182 in { assert(0); }
1183 do { i = 10; }
1186 void test15984()
1188 c15984 = new C15984;
1189 i15984 = c15984;
1190 printf("i = %p\n", i15984);
1191 printf("c = %p\n", c15984);
1192 printf("====\n");
1193 i15984.f1(5);
1194 printf("====\n");
1195 i15984.f2(5);
1196 printf("====\n");
1197 i15984.f3(5);
1198 int i;
1199 i15984.f4(i, 1);
1200 assert(i == 10);
1203 /*******************************************/
1205 //******************************************/
1206 // DIP 1009
1208 int dip1009_1(int x)
1209 in (x > 0, "x must be positive!")
1210 out (r; r < 0, "r must be negative!")
1211 in (true, "cover trailing comma case",)
1212 out (; true, "cover trailing comma case",)
1214 return -x;
1217 int dip1009_2(int x)
1218 in (x > 0)
1219 out (r; r < 0)
1221 return -x;
1224 int dip1009_3(int x)
1225 in (x > 0,)
1226 out (r; r < 0,)
1229 return -x;
1232 void dip1009_4(int x)
1233 in (x > 0)
1234 out (; x > 1)
1236 x += 1;
1239 interface DIP1009_5
1241 void dip1009_5(int x)
1242 in (x > 0)
1243 out (; x > 1);
1246 int dip1009_6(int x, int y)
1247 in (x > 0)
1248 out (r; r > 1)
1249 out (; x > 0)
1250 in (y > 0)
1251 in (x + y > 1)
1252 out (r; r > 1)
1254 return x+y;
1257 int dip1009_7(int x)
1258 in (x > 0)
1259 in { assert(x > 1); }
1260 out { assert(x > 2); }
1261 out (; x > 3)
1262 out (r; r > 3)
1264 x += 2;
1265 return x;
1268 class DIP1009_8
1270 private int x = 4;
1271 invariant (x > 0, "x must stay positive");
1272 invariant (x > 1, "x must be greater than one",);
1273 invariant (x > 2);
1274 invariant (x > 3,);
1275 void foo(){ x = 5; }
1278 /*******************************************/
1280 int main()
1282 test1();
1283 test2();
1284 test3();
1285 test4();
1286 test5();
1287 // test6();
1288 test7();
1289 test8();
1290 test9();
1291 test4785();
1292 test6417();
1293 test6549();
1294 test7218();
1295 test7335();
1296 test7517();
1297 test8073();
1298 test8093();
1299 test9383();
1300 test15524();
1301 test15524a();
1302 test14779();
1303 test15984();
1304 dip1009_1(1);
1305 dip1009_2(1);
1306 dip1009_3(1);
1307 dip1009_4(1);
1308 dip1009_6(1, 1);
1309 dip1009_7(3);
1310 new DIP1009_8().foo();
1312 printf("Success\n");
1313 return 0;