Debian Codegraph @

gcc/testsuite/gdc.test/runnable/testcontracts.d

raw
1 /* PERMUTE_ARGS: -inline -g -O
2 */
3 extern(C) int printf(const char*, ...);
4
5 /*******************************************/
6
7 class A
8 {
9 int x = 7;
10
11 int foo(int i)
12 in
13 {
14 printf("A.foo.in %d\n", i);
15 assert(i == 2);
16 assert(x == 7);
17 printf("A.foo.in pass\n");
18 }
19 out (result)
20 {
21 assert(result & 1);
22 assert(x == 7);
23 }
24 do
25 {
26 return i;
27 }
28 }
29
30 class B : A
31 {
32 override int foo(int i)
33 in
34 {
35 float f;
36 printf("B.foo.in %d\n", i);
37 assert(i == 4);
38 assert(x == 7);
39 f = f + i;
40 }
41 out (result)
42 {
43 assert(result < 8);
44 assert(x == 7);
45 }
46 do
47 {
48 return i - 1;
49 }
50 }
51
52 void test1()
53 {
54 auto b = new B();
55 b.foo(2);
56 b.foo(4);
57 }
58
59 /*******************************************/
60
61 class A2
62 {
63 int x = 7;
64
65 int foo(int i)
66 in
67 {
68 printf("A2.foo.in %d\n", i);
69 assert(i == 2);
70 assert(x == 7);
71 printf("A2.foo.in pass\n");
72 }
73 out (result)
74 {
75 assert(result & 1);
76 assert(x == 7);
77 }
78 do
79 {
80 return i;
81 }
82 }
83
84 class B2 : A2
85 {
86 override int foo(int i)
87 in
88 {
89 float f;
90 printf("B2.foo.in %d\n", i);
91 assert(i == 4);
92 assert(x == 7);
93 f = f + i;
94 }
95 out (result)
96 {
97 assert(result < 8);
98 assert(x == 7);
99 }
100 do
101 {
102 return i - 1;
103 }
104 }
105
106 class C : B2
107 {
108 override int foo(int i)
109 in
110 {
111 float f;
112 printf("C.foo.in %d\n", i);
113 assert(i == 6);
114 assert(x == 7);
115 f = f + i;
116 }
117 out (result)
118 {
119 assert(result == 1 || result == 3 || result == 5);
120 assert(x == 7);
121 }
122 do
123 {
124 return i - 1;
125 }
126 }
127
128 void test2()
129 {
130 auto c = new C();
131 c.foo(2);
132 c.foo(4);
133 c.foo(6);
134 }
135
136 /*******************************************/
137
138 void fun(int x)
139 in {
140 if (x < 0) throw new Exception("a");
141 }
142 do {
143 }
144
145 void test3()
146 {
147 fun(1);
148 }
149
150 /*******************************************/
151
152 interface Stack {
153 int pop()
154 // in { printf("pop.in\n"); }
155 out(result) {
156 printf("pop.out\n");
157 assert(result == 3);
158 }
159 }
160
161 class CC : Stack
162 {
163 int pop()
164 //out (result) { printf("CC.pop.out\n"); } do
165 {
166 printf("CC.pop.in\n");
167 return 3;
168 }
169 }
170
171 void test4()
172 {
173 auto cc = new CC();
174 cc.pop();
175 }
176
177 /*******************************************/
178
179 int mul100(int n)
180 out(result)
181 {
182 assert(result == 500);
183 }
184 do
185 {
186 return n * 100;
187 }
188
189 void test5()
190 {
191 mul100(5);
192 }
193
194 /*******************************************/
195 // https://issues.dlang.org/show_bug.cgi?id=3273
196
197 // original case
198 struct Bug3273
199 {
200 ~this() {}
201 invariant() {}
202 }
203
204 // simplest case
205 ref int func3273()
206 out(r)
207 {
208 // Regression check of https://issues.dlang.org/show_bug.cgi?id=3390
209 static assert(!__traits(compiles, r = 1));
210 }
211 do
212 {
213 static int dummy;
214 return dummy;
215 }
216
217 void test6()
218 {
219 func3273() = 1;
220 assert(func3273() == 1);
221 }
222
223 /*******************************************/
224
225 /+
226 // https://issues.dlang.org/show_bug.cgi?id=3722
227
228 class Bug3722A
229 {
230 void fun() {}
231 }
232 class Bug3722B : Bug3722A
233 {
234 override void fun() in { assert(false); } do {}
235 }
236
237 void test6()
238 {
239 auto x = new Bug3722B();
240 x.fun();
241 }
242 +/
243
244 /*******************************************/
245
246 auto test7foo()
247 in{
248 ++cnt;
249 }do{
250 ++cnt;
251 return "str";
252 }
253
254 void test7()
255 {
256 cnt = 0;
257 assert(test7foo() == "str");
258 assert(cnt == 2);
259 }
260
261 /*******************************************/
262
263 auto foo8()
264 out(r){
265 ++cnt;
266 assert(r == 10);
267 }do{
268 ++cnt;
269 return 10;
270 }
271
272 auto bar8()
273 out{
274 ++cnt;
275 }do{
276 ++cnt;
277 }
278
279 void test8()
280 {
281 cnt = 0;
282 assert(foo8() == 10);
283 assert(cnt == 2);
284
285 cnt = 0;
286 bar8();
287 assert(cnt == 2);
288 }
289
290 /*******************************************/
291 // from fail317
292
293 void test9()
294 {
295 {
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
300
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
305 }
306 {
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
311
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
316 }
317 }
318
319 /*******************************************/
320
321 auto test10() do { return 3; }
322 auto test11()() do { return 3; }
323
324 auto test12()
325 {
326 auto test10() do { return 3; }
327 auto test11()() do { return 3; }
328 return 3;
329 }
330
331
332 void test13()
333 {
334 int function() fp13;
335 }
336
337 /*******************************************/
338 // https://issues.dlang.org/show_bug.cgi?id=4785
339
340 int cnt;
341
342 auto foo4785()
343 in{
344 int r;
345 ++cnt;
346 }
347 out(r){
348 assert(r == 10);
349 ++cnt;
350 }do{
351 ++cnt;
352 int r = 10;
353 return r;
354 }
355 void test4785()
356 {
357 cnt = 0;
358 assert(foo4785() == 10);
359 assert(cnt == 3);
360 }
361
362 /*******************************************/
363 // https://issues.dlang.org/show_bug.cgi?id=5039
364
365 class C5039 {
366 int x;
367
368 invariant() {
369 assert( x < int.max );
370 }
371
372 auto foo() {
373 return x;
374 }
375 }
376
377 /*******************************************/
378 // https://issues.dlang.org/show_bug.cgi?id=5204
379
380 interface IFoo5204
381 {
382 IFoo5204 bar()
383 out {}
384 }
385 class Foo5204 : IFoo5204
386 {
387 Foo5204 bar() { return null; }
388 }
389
390 /*******************************************/
391 // https://issues.dlang.org/show_bug.cgi?id=6417
392
393 class Bug6417
394 {
395 void bar()
396 in
397 {
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");
403 };
404 dg();
405 }
406 out
407 {
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");
413 };
414 dg();
415 }
416 do {}
417 }
418
419 void test6417()
420 {
421 (new Bug6417).bar();
422 }
423
424 /*******************************************/
425 // https://issues.dlang.org/show_bug.cgi?id=6549
426
427 class C6549
428 {
429 static int ocount = 0;
430 static int icount = 0;
431
432 abstract int foo(int)
433 in { ++icount; }
434 out { ++ocount; }
435 }
436
437 class CD6549 : C6549
438 {
439 override int foo(int)
440 in { assert(false); }
441 do { return 10; }
442 }
443
444 abstract class D6549
445 {
446 static int icount = 0;
447 static int ocount = 0;
448
449 int foo(int)
450 in { ++icount; }
451 out { ++ocount; }
452 }
453
454 class DD6549 : D6549
455 {
456 override int foo(int)
457 in { assert(false); }
458 do { return 10; }
459 }
460
461 void test6549()
462 {
463 auto c = new CD6549;
464 c.foo(10);
465 assert(C6549.icount == 1);
466 assert(C6549.ocount == 1);
467
468 auto d = new DD6549;
469 d.foo(10);
470 assert(D6549.icount == 1);
471 assert(D6549.ocount == 1);
472 }
473
474 /*******************************************/
475 // https://issues.dlang.org/show_bug.cgi?id=7218
476
477 void test7218()
478 {
479 {
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
484 }
485 {
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
490 }
491 }
492
493 /*******************************************/
494 // https://issues.dlang.org/show_bug.cgi?id=7335
495
496 class A7335
497 {
498 int mValue = 10;
499
500 void setValue(int newValue)
501 in { }
502 out { assert(mValue == 3); }
503 do
504 {
505 mValue = newValue;
506 }
507 }
508
509 class B7335 : A7335
510 {
511 override void setValue(int newValue)
512 in { assert(false); }
513 out { assert(mValue == 3); }
514 do
515 {
516 mValue = newValue;
517 }
518 }
519
520 class C7335 : A7335
521 {
522 override void setValue(int newValue)
523 in { int a = newValue; }
524 out { assert(mValue == 3); }
525 do
526 {
527 mValue = newValue;
528 }
529 }
530
531 void test7335()
532 {
533 A7335 aObject = new B7335();
534 aObject.setValue(3);
535
536 A7335 bObject = new C7335();
537 bObject.setValue(3); // <<<<< will crash because undefined mValue in the
538 // A7335.setValue().out-block.
539 }
540
541 /*******************************************/
542 // https://issues.dlang.org/show_bug.cgi?id=7517
543
544 void test7517()
545 {
546 static string result;
547
548 interface I
549 {
550 static I self;
551
552 void setEnable()
553 in
554 {
555 assert(self is this);
556 result ~= "I.setEnable.in/";
557 assert(!enabled);
558 }
559 out
560 {
561 assert(self is this);
562 result ~= "I.setEnable.out/";
563 assert( enabled);
564 }
565
566 void setDisable()
567 in
568 {
569 assert(self is this);
570 result ~= "I.setDisable.in/";
571 assert( enabled);
572 }
573 out
574 {
575 assert(self is this);
576 result ~= "I.setDisable.out/";
577 assert(!enabled);
578 }
579
580 @property bool enabled() const;
581 }
582
583 class C : I
584 {
585 static C self;
586
587 void setEnable()
588 in {} // supply in-contract to invoke I.setEnable.in
589 do
590 {
591 assert(self is this);
592 result ~= "C.setEnable/";
593 _enabled = true;
594 }
595
596 void setDisable()
597 {
598 assert(self is this);
599 result ~= "C.setDisable/";
600 _enabled = false;
601 }
602
603 @property bool enabled() const
604 {
605 assert(self is this);
606 result ~= "C.enabled/";
607 return _enabled;
608 }
609
610 bool _enabled;
611 }
612
613 C c = C.self = new C;
614 I i = I.self = c;
615
616 result = null;
617 i.setEnable();
618 assert(result == "I.setEnable.in/C.enabled/C.setEnable/I.setEnable.out/C.enabled/");
619
620 result = null;
621 i.setDisable();
622 assert(result == "C.setDisable/I.setDisable.out/C.enabled/");
623 }
624
625 /*******************************************/
626 // https://issues.dlang.org/show_bug.cgi?id=7699
627
628 class P7699
629 {
630 void f(int n) in {
631 assert (n);
632 } do { }
633 }
634 class D7699 : P7699
635 {
636 override void f(int n) in { } do { }
637 }
638
639 /*******************************************/
640 // https://issues.dlang.org/show_bug.cgi?id=7883
641
642 // Segmentation fault
643 class AA7883
644 {
645 int foo()
646 out (r1) { }
647 do { return 1; }
648 }
649
650 class BA7883 : AA7883
651 {
652 override int foo()
653 out (r2) { }
654 do { return 1; }
655 }
656
657 class CA7883 : BA7883
658 {
659 override int foo()
660 do { return 1; }
661 }
662
663 // Error: undefined identifier r2, did you mean variable r3?
664 class AB7883
665 {
666 int foo()
667 out (r1) { }
668 do { return 1; }
669 }
670
671 class BB7883 : AB7883
672 {
673 override int foo()
674 out (r2) { }
675 do { return 1; }
676
677 }
678
679 class CB7883 : BB7883
680 {
681 override int foo()
682 out (r3) { }
683 do { return 1; }
684 }
685
686 // Error: undefined identifier r3, did you mean variable r4?
687 class AC7883
688 {
689 int foo()
690 out (r1) { }
691 do { return 1; }
692 }
693
694 class BC7883 : AC7883
695 {
696 override int foo()
697 out (r2) { }
698 do { return 1; }
699 }
700
701 class CC7883 : BC7883
702 {
703 override int foo()
704 out (r3) { }
705 do { return 1; }
706 }
707
708 class DC7883 : CC7883
709 {
710 override int foo()
711 out (r4) { }
712 do { return 1; }
713 }
714
715 /*******************************************/
716 // https://issues.dlang.org/show_bug.cgi?id=7892
717
718 struct S7892
719 {
720 @disable this();
721 this(int x) {}
722 }
723
724 S7892 f7892()
725 out (result) {} // case 1
726 do
727 {
728 return S7892(1);
729 }
730
731 interface I7892
732 {
733 S7892 f();
734 }
735 class C7892
736 {
737 invariant() {} // case 2
738
739 S7892 f()
740 {
741 return S7892(1);
742 }
743 }
744
745 /*******************************************/
746 // https://issues.dlang.org/show_bug.cgi?id=8066
747
748 struct CLCommandQueue
749 {
750 invariant() {}
751
752 //private:
753 int enqueueNativeKernel()
754 {
755 assert(0, "implement me");
756 }
757 }
758
759 /*******************************************/
760 // https://issues.dlang.org/show_bug.cgi?id=8073
761
762 struct Container8073
763 {
764 int opApply (int delegate(ref int) dg) { return 0; }
765 }
766
767 class Bug8073
768 {
769 static int test;
770 int foo()
771 out(r) { test = 7; } do
772 {
773 Container8073 ww;
774 foreach( xxx ; ww ) { }
775 return 7;
776 }
777
778 ref int bar()
779 out { } do
780 {
781 Container8073 ww;
782 foreach( xxx ; ww ) { }
783 test = 7;
784 return test;
785 }
786 }
787 void test8073()
788 {
789 auto c = new Bug8073();
790 assert(c.foo() == 7);
791 assert(c.test == 7);
792
793 auto p = &c.bar();
794 assert(p == &c.test);
795 assert(*p == 7);
796 }
797
798 /*******************************************/
799 // https://issues.dlang.org/show_bug.cgi?id=8093
800
801 void test8093()
802 {
803 static int g = 10;
804 static int* p;
805
806 enum fdo = q{
807 static struct S {
808 int opApply(scope int delegate(ref int) dg) { return dg(g); }
809 }
810 S s;
811 foreach (ref e; s)
812 return g;
813 assert(0);
814 };
815
816 ref int foo_ref1() out(r) { assert(&r is &g && r == 10); }
817 do { mixin(fdo); }
818
819 ref int foo_ref2()
820 do { mixin(fdo); }
821
822 { auto q = &foo_ref1(); assert(q is &g && *q == 10); }
823 { auto q = &foo_ref2(); assert(q is &g && *q == 10); }
824
825 int foo_val1() out(r) { assert(&r !is &g && r == 10); }
826 do { mixin(fdo); }
827
828 int foo_val2()
829 do { mixin(fdo); }
830
831 { auto n = foo_val1(); assert(&n !is &g && n == 10); }
832 { auto n = foo_val2(); assert(&n !is &g && n == 10); }
833 }
834
835 /*******************************************/
836 // https://issues.dlang.org/show_bug.cgi?id=9383
837
838 class A9383
839 {
840 static void delegate() dg;
841 static int val;
842
843 void failInBase() { assert(typeid(this) is typeid(A9383)); }
844
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]
853
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]
862 }
863
864 class B9383 : A9383
865 {
866 static int val;
867
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
876
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
885 }
886
887 void test9383()
888 {
889 auto a = new A9383();
890 auto b = new B9383();
891
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
900
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
909
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]
914
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]
919 }
920
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.
924
925 class A15524
926 {
927 static void delegate() dg;
928 static string val;
929
930 void failInBase() { assert(typeid(this) is typeid(A15524)); }
931
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]
940
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]
949 }
950
951 class B15524 : A15524
952 {
953 static string val;
954
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
963
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
972 }
973
974 void test15524()
975 {
976 auto a = new A15524();
977 auto b = new B15524();
978
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
987
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
996
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]
1001
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]
1006 }
1007
1008 void test15524a()
1009 {
1010 auto t1 = new Test15524a();
1011 t1.infos["first"] = 0; //t1.add("first");
1012 t1.add("second");
1013
1014 auto t2 = new Test15524b();
1015 t2.add("second");
1016 }
1017
1018 class Test15524a
1019 {
1020 int[string] infos;
1021
1022 void add(string key)
1023 in
1024 {
1025 assert(key !in infos); // @@@ crash here at second
1026 }
1027 do
1028 {
1029 auto item = new class
1030 {
1031 void notCalled()
1032 {
1033 infos[key] = 0;
1034 // affects, key parameter is made a closure variable.
1035 }
1036 };
1037 }
1038 }
1039
1040 class Test15524b
1041 {
1042 void add(string key)
1043 in
1044 {
1045 assert(key == "second"); // @@@ fails
1046 }
1047 do
1048 {
1049 static void delegate() dg;
1050 dg = { auto x = key; };
1051 // affects, key parameter is made a closure variable.
1052 }
1053 }
1054
1055 /*******************************************/
1056 // https://issues.dlang.org/show_bug.cgi?id=10479
1057
1058 class B10479
1059 {
1060 B10479 foo()
1061 out { } do { return null; }
1062 }
1063
1064 class D10479 : B10479
1065 {
1066 override D10479 foo() { return null; }
1067 }
1068
1069 /*******************************************/
1070 // https://issues.dlang.org/show_bug.cgi?id=10596
1071
1072 class Foo10596
1073 {
1074 auto bar()
1075 out (result) { }
1076 do { return 0; }
1077 }
1078
1079 /*******************************************/
1080 // https://issues.dlang.org/show_bug.cgi?id=10721
1081
1082 class Foo10721
1083 {
1084 this()
1085 out { }
1086 do { }
1087
1088 ~this()
1089 out { }
1090 do { }
1091 }
1092
1093 struct Bar10721
1094 {
1095 this(this)
1096 out { }
1097 do { }
1098 }
1099
1100 /*******************************************/
1101 // https://issues.dlang.org/show_bug.cgi?id=10981
1102
1103 class C10981
1104 {
1105 void foo(int i) pure
1106 in { assert(i); }
1107 out { assert(i); }
1108 do {}
1109 }
1110
1111 /*******************************************/
1112 // https://issues.dlang.org/show_bug.cgi?id=14779
1113
1114 class C14779
1115 {
1116 final void foo(int v)
1117 in { assert(v == 0); }
1118 out { assert(v == 0); }
1119 do
1120 {
1121 }
1122 }
1123
1124 void test14779()
1125 {
1126 auto c = new C14779();
1127 c.foo(0);
1128 }
1129
1130 /*******************************************/
1131 // https://issues.dlang.org/show_bug.cgi?id=15984
1132
1133 I15984 i15984;
1134 C15984 c15984;
1135
1136 void check15984(T)(const char* s, T this_, int i)
1137 {
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);
1143 }
1144
1145 interface I15984
1146 {
1147 void f1(int i)
1148 in { check15984("I.f1.i", this, i); assert(0); }
1149 out { check15984("I.f1.o", this, i); }
1150
1151 int[3] f2(int i)
1152 in { check15984("I.f2.i", this, i); assert(0); }
1153 out { check15984("I.f2.o", this, i); }
1154
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(); }
1158
1159 void f4(out int i, lazy int j)
1160 in { }
1161 out { }
1162 }
1163
1164 class C15984 : I15984
1165 {
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); }
1170
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]; }
1175
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); }
1180
1181 void f4(out int i, lazy int j)
1182 in { assert(0); }
1183 do { i = 10; }
1184 }
1185
1186 void test15984()
1187 {
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);
1201 }
1202
1203 /*******************************************/
1204
1205 //******************************************/
1206 // DIP 1009
1207
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",)
1213 {
1214 return -x;
1215 }
1216
1217 int dip1009_2(int x)
1218 in (x > 0)
1219 out (r; r < 0)
1220 {
1221 return -x;
1222 }
1223
1224 int dip1009_3(int x)
1225 in (x > 0,)
1226 out (r; r < 0,)
1227 do
1228 {
1229 return -x;
1230 }
1231
1232 void dip1009_4(int x)
1233 in (x > 0)
1234 out (; x > 1)
1235 {
1236 x += 1;
1237 }
1238
1239 interface DIP1009_5
1240 {
1241 void dip1009_5(int x)
1242 in (x > 0)
1243 out (; x > 1);
1244 }
1245
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)
1253 {
1254 return x+y;
1255 }
1256
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)
1263 {
1264 x += 2;
1265 return x;
1266 }
1267
1268 class DIP1009_8
1269 {
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; }
1276 }
1277
1278 /*******************************************/
1279
1280 int main()
1281 {
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();
1311
1312 printf("Success\n");
1313 return 0;
1314 }