Debian Codegraph @

gcc/testsuite/g++.dg/contracts/contracts-pre2.C

raw
1 // basic test to ensure pre contracts work for free functions
2 // { dg-do run }
3 // { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
4 #include <cstdio>
5
6 namespace nullary
7 {
8 int x = 10;
9 int y = 10;
10
11 void fun()
12 [[ pre: x < 0 ]]
13 {
14 printf("fun::x: %d\n", x);
15 }
16
17 void fun2()
18 [[ pre: x < 0 ]]
19 [[ pre: y < 0 ]]
20 {
21 printf("fun2::x: %d fun2::y: %d\n", x, y);
22 }
23
24 void funend()
25 [[ pre: x < 0 ]];
26 }
27
28 namespace nonvoid
29 {
30 int x = 10;
31 double y = 10.5;
32
33 struct S
34 {
35 bool z;
36 };
37
38 void vfun()
39 [[ pre: x < 0 ]]
40 {
41 printf("vfun::x: %d\n", x);
42 }
43
44 int fun()
45 [[ pre: x < 0 ]]
46 {
47 printf("fun::x: %d\n", x);
48 return x;
49 }
50
51 double fun2()
52 [[ pre: x < 0 ]]
53 [[ pre: y < 0 ]]
54 {
55 printf("fun2::x: %d fun2::y: %f\n", x, y);
56 return y;
57 }
58
59 S funend()
60 [[ pre: x < 0 ]];
61 }
62
63 namespace nonnullary
64 {
65 int x = 10;
66 double y = 10.5;
67
68 struct S
69 {
70 bool z;
71 };
72
73 void vfun(int m, double n)
74 [[ pre: x < 0 ]]
75 [[ pre: m < 0 ]]
76 {
77 printf("vfun::x: %d\n", x);
78 }
79
80 int fun(int m, double n)
81 [[ pre: x < 0 ]]
82 {
83 printf("fun::x: %d\n", x);
84 return x;
85 }
86
87 double fun2(int m, double n)
88 [[ pre: x < 0 ]]
89 [[ pre: y < 0 ]]
90 [[ pre: m < 0 ]]
91 [[ pre: n < 0 ]]
92 {
93 printf("fun2::x: %d fun2::y: %f\n", x, y);
94 return y;
95 }
96
97 S funend(int m, double n)
98 [[ pre: x < 0 ]]
99 [[ pre: m < 0 ]];
100 }
101
102 int main(int, char**) {
103 // nullary void
104 {
105 nullary::fun();
106 nullary::fun2();
107 nullary::funend();
108 }
109
110 // nullary non void
111 {
112 nonvoid::vfun();
113
114 int f = 13;
115 f = nonvoid::fun();
116 printf("main::f: %d\n", f);
117 double d = 13.37;
118 d = nonvoid::fun2();
119 printf("main::d: %f\n", d);
120 nonvoid::S s = nonvoid::funend();
121 printf("main::s.z: %d\n", s.z ? 1 : 0);
122 }
123
124 // non-nullary non-void
125 {
126 int x = 11;
127 double y = 11.5;
128
129 nonnullary::vfun(x, y);
130
131 int f = 13;
132 f = nonnullary::fun(x, y);
133 printf("main::f: %d\n", f);
134 double d = 13.37;
135 d = nonnullary::fun2(x, y);
136 printf("main::d: %f\n", d);
137 nonnullary::S s = nonnullary::funend(x, y);
138 printf("main::s.z: %d\n", s.z ? 1 : 0);
139 }
140 return 0;
141 }
142
143 namespace nullary
144 {
145 void funend()
146 [[ pre: x < 0 ]]
147 {
148 printf("funend::x: %d\n", x);
149 }
150 }
151
152 namespace nonvoid
153 {
154 S funend()
155 [[ pre: x < 0 ]]
156 {
157 printf("funend::x: %d\n", x);
158 S s;
159 s.z = true;
160 return s;
161 }
162 }
163
164 namespace nonnullary
165 {
166 S funend(int m, double n)
167 [[ pre: x < 0 ]]
168 [[ pre: m < 0 ]]
169 {
170 printf("funend::x: %d\n", x);
171 S s;
172 s.z = true;
173 return s;
174 }
175 }
176
177 // { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
178 // { dg-output "contract violation in function nullary::fun at .*.C:12: .*(\n|\r\n|\r)" }
179 // { dg-output "fun::x: 10(\n|\r\n|\r)" }
180 // { dg-output "contract violation in function nullary::fun2 at .*.C:18: .*(\n|\r\n|\r)" }
181 // { dg-output "contract violation in function nullary::fun2 at .*.C:19: .*(\n|\r\n|\r)" }
182 // { dg-output "fun2::x: 10 fun2::y: 10(\n|\r\n|\r)" }
183 // { dg-output "contract violation in function nullary::funend at .*.C:146: .*(\n|\r\n|\r)" }
184 // { dg-output "funend::x: 10(\n|\r\n|\r)" }
185 // { dg-output "contract violation in function nonvoid::vfun at .*.C:39: .*(\n|\r\n|\r)" }
186 // { dg-output "vfun::x: 10(\n|\r\n|\r)" }
187 // { dg-output "contract violation in function nonvoid::fun at .*.C:45: .*(\n|\r\n|\r)" }
188 // { dg-output "fun::x: 10(\n|\r\n|\r)" }
189 // { dg-output "main::f: 10(\n|\r\n|\r)" }
190 // { dg-output "contract violation in function nonvoid::fun2 at .*.C:52: .*(\n|\r\n|\r)" }
191 // { dg-output "contract violation in function nonvoid::fun2 at .*.C:53: .*(\n|\r\n|\r)" }
192 // { dg-output "fun2::x: 10 fun2::y: 10.500000(\n|\r\n|\r)" }
193 // { dg-output "main::d: 10.500000(\n|\r\n|\r)" }
194 // { dg-output "contract violation in function nonvoid::funend at .*.C:155: .*(\n|\r\n|\r)" }
195 // { dg-output "funend::x: 10(\n|\r\n|\r)" }
196 // { dg-output "main::s.z: 1(\n|\r\n|\r)" }
197 // { dg-output "contract violation in function nonnullary::vfun at .*.C:74: .*(\n|\r\n|\r)" }
198 // { dg-output "contract violation in function nonnullary::vfun at .*.C:75: .*(\n|\r\n|\r)" }
199 // { dg-output "vfun::x: 10(\n|\r\n|\r)" }
200 // { dg-output "contract violation in function nonnullary::fun at .*.C:81: .*(\n|\r\n|\r)" }
201 // { dg-output "fun::x: 10(\n|\r\n|\r)" }
202 // { dg-output "main::f: 10(\n|\r\n|\r)" }
203 // { dg-output "contract violation in function nonnullary::fun2 at .*.C:88: .*(\n|\r\n|\r)" }
204 // { dg-output "contract violation in function nonnullary::fun2 at .*.C:89: .*(\n|\r\n|\r)" }
205 // { dg-output "contract violation in function nonnullary::fun2 at .*.C:90: .*(\n|\r\n|\r)" }
206 // { dg-output "contract violation in function nonnullary::fun2 at .*.C:91: .*(\n|\r\n|\r)" }
207 // { dg-output "fun2::x: 10 fun2::y: 10.500000(\n|\r\n|\r)" }
208 // { dg-output "main::d: 10.500000(\n|\r\n|\r)" }
209 // { dg-output "contract violation in function nonnullary::funend at .*.C:167: .*(\n|\r\n|\r)" }
210 // { dg-output "contract violation in function nonnullary::funend at .*.C:168: .*(\n|\r\n|\r)" }
211 // { dg-output "funend::x: 10(\n|\r\n|\r)" }
212 // { dg-output "main::s.z: 1(\n|\r\n|\r)" }
213