1 /* Test Box::Box(const Congruence_System&).
2 Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3 Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4
5 This file is part of the Parma Polyhedra Library (PPL).
6
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23
24 #include "ppl_test.hh"
25
26 namespace {
27
28 // Universe Box constructed from empty congruences
29 bool
test01()30 test01() {
31 Congruence_System cgs;
32 TBox box(cgs);
33
34 Rational_Box known_result(0);
35
36 bool ok = check_result(box, known_result);
37
38 print_constraints(box, "*** box ***");
39
40 return ok;
41 }
42
43 // Box constructed from empty congruences and add_congruences()
44 bool
test02()45 test02() {
46 Variable A(0);
47 Variable B(1);
48 Variable C(2);
49 Variable D(3);
50
51 Congruence_System cgs;
52 cgs.insert(0*D %= 0);
53 TBox box(cgs);
54
55 Rational_Box known_result(4);
56 known_result.add_congruences(cgs);
57
58 bool ok = check_result(box, known_result);
59
60 print_constraints(box, "*** box ***");
61
62 return ok;
63 }
64
65 // Box constructed from non-empty congruences
66 bool
test03()67 test03() {
68 Variable A(0);
69 Variable B(1);
70 Variable C(2);
71
72 Congruence_System cgs;
73 cgs.insert((C %= 7) / 0);
74
75 TBox box(cgs);
76
77 Rational_Box known_result(3);
78 known_result.add_constraint(C == 7);
79
80 bool ok = check_result(box, known_result);
81
82 print_constraints(box, "*** box ***");
83
84 return ok;
85 }
86
87 // add_recycled_congruences()
88 bool
test04()89 test04() {
90 Variable A(0);
91 Variable B(1);
92 Variable C(2);
93
94 Congruence_System cgs;
95 cgs.insert((A %= 7) / 0);
96
97 TBox box(3);
98 box.add_recycled_congruences(cgs);
99 bool ok = !box.is_empty();
100
101 Rational_Box known_result(3);
102 known_result.add_constraint(A == 7);
103
104 ok = ok && check_result(box, known_result);
105
106 print_constraints(box, "*** box ***");
107
108 return ok;
109 }
110
111 // Box constructed from non-empty congruences; congruences().
112 bool
test05()113 test05() {
114 Variable A(0);
115 Variable B(1);
116 Variable C(2);
117
118 Congruence_System cgs;
119 cgs.insert((A + 0*C %= 7) / 0);
120
121 TBox box(cgs);
122
123 TBox box1(box.congruences());
124
125 Rational_Box known_result(3);
126 known_result.add_constraint(A == 7);
127
128 bool ok = check_result(box1, known_result);
129
130 print_constraints(box, "*** box ***");
131 print_constraints(box1, "*** box1(box.congruences()) ***");
132
133 return ok;
134 }
135
136 // Box constructed from non-empty congruences.
137 bool
test06()138 test06() {
139 Variable A(0);
140 Variable B(1);
141 Variable C(2);
142
143 Congruence_System cgs;
144 cgs.insert((A %= 7) / 0);
145 cgs.insert((B %= 3) / 0);
146 // This inconsistent equality is not ignored when refining.
147 cgs.insert((A + B %= 0) / 0);
148 cgs.insert(C %= 7);
149
150 TBox box(cgs.space_dimension(), UNIVERSE);
151 box.refine_with_congruences(cgs);
152
153 Rational_Box known_result(3, EMPTY);
154
155 bool ok = check_result(box, known_result);
156
157 print_constraints(box, "*** box ***");
158
159 return ok;
160 }
161
162 // add_congruences() for inconsistent equality congruences
163 bool
test07()164 test07() {
165 Variable A(0);
166 Variable B(1);
167 Variable C(2);
168
169 Congruence_System cgs;
170 cgs.insert((A %= 7) / 0);
171 cgs.insert((B %= 3) / 0);
172 cgs.insert((A %= 0) / 0);
173
174 TBox box(3);
175 box.add_congruences(cgs);
176 bool ok = box.is_empty();
177
178 Rational_Box known_result(3, EMPTY);
179
180 ok = ok && check_result(box, known_result);
181
182 print_constraints(box, "*** box ***");
183
184 return ok;
185 }
186
187 // add_congruence()
188 bool
test08()189 test08() {
190 Variable A(0);
191
192 TBox box(1);
193 box.add_congruence((A %= 7) / 0);
194 bool ok = !box.is_empty();
195 box.add_congruence((A %= 2) / 0);
196 ok = ok && box.is_empty();
197
198 Rational_Box known_result(1, EMPTY);
199
200 ok = ok && check_result(box, known_result);
201
202 print_constraints(box, "*** box ***");
203 print_constraints(known_result, "*** known_result ***");
204
205 return ok;
206 }
207
208 // Non-empty Box; minimized_congruences().
209 bool
test09()210 test09() {
211 Variable A(0);
212 Variable B(1);
213 Variable C(2);
214
215 Congruence_System cgs;
216 cgs.insert((A %= 7) / 0);
217 cgs.insert((C %= 3) / 0);
218
219 TBox box(cgs);
220 TBox box1(box.minimized_congruences());
221
222 Rational_Box known_result(3);
223 known_result.add_constraint(A == 7);
224 known_result.add_constraint(C == 3);
225
226 bool ok = check_result(box1, known_result);
227
228 print_constraints(box1, "*** box1(box.congruences()) ***");
229
230 return ok;
231 }
232
233 // Empty Box; minimized_congruences().
234 bool
test10()235 test10() {
236 Variable A(0);
237 Variable B(1);
238 Variable C(2);
239
240 Congruence_System cgs;
241 cgs.insert((A %= 7) / 0);
242 cgs.insert((B %= 3) / 0);
243 cgs.insert((A %= 0) / 0);
244
245 TBox box(3);
246 box.add_congruences(cgs);
247
248 TBox box1(box.minimized_congruences());
249
250 Rational_Box known_result(3, EMPTY);
251
252 bool ok = check_result(box1, known_result);
253
254 print_constraints(box1, "*** box1(box.congruences()) ***");
255
256 return ok;
257 }
258
259 // Zero dimension universe; congruences()
260 bool
test11()261 test11() {
262 TBox box(0);
263 TBox box1(box.congruences());
264
265 Rational_Box known_result(0);
266
267 bool ok = check_result(box1, known_result);
268
269 print_constraints(box1, "*** box1(box.congruences()) ***");
270
271 return ok;
272 }
273
274 // Zero dimension empty; congruences()
275 bool
test12()276 test12() {
277 TBox box(0, EMPTY);
278 TBox box1(box.congruences());
279
280 Rational_Box known_result(0, EMPTY);
281
282 bool ok = check_result(box1, known_result);
283
284 print_constraints(box1, "*** box1(box.congruences()) ***");
285
286 return ok;
287 }
288
289 bool
test13()290 test13() {
291 Variable x(0);
292 Variable y(1);
293 Variable z(2);
294
295 TBox box1(2);
296
297 try {
298 box1.add_congruence((x %= 0) / 0);
299 // This is an invalid use of method
300 // Box::add_congruence: it is illegal
301 // to add a congruence with bigger dimension.
302 box1.add_congruence(y - x + z %= 0);
303 }
304 catch (std::invalid_argument& e) {
305 nout << "std::invalid_argument: " << e.what() << endl;
306 return true;
307 }
308 catch (...) {
309 }
310 return false;
311 }
312
313 bool
test14()314 test14() {
315 Variable x(0);
316 Variable y(1);
317
318 TBox box(1);
319
320 try {
321 // This is an invalid use of the method
322 // Box::add_congruences(cs): it is illegal to
323 // add a system of congruences that is dimensional incompatible
324 // with the box.
325 Congruence_System cgs;
326 cgs.insert(x - y %= 0);
327 box.add_congruences(cgs);
328 }
329 catch (std::invalid_argument& e) {
330 nout << "std::invalid_argument: " << e.what() << endl;
331 return true;
332 }
333 catch (...) {
334 }
335 return false;
336 }
337
338 bool
test15()339 test15() {
340 Variable y(1);
341
342 TBox box(1);
343
344 try {
345 // This is an invalid use of the method
346 // Box::add_congruence(c): it is illegal to insert a
347 // congruence that contains a variable that is not in the space
348 // of the box.
349 box.add_congruence((y %= 0) / 0);
350 }
351 catch (std::invalid_argument& e) {
352 nout << "std::invalid_argument: " << e.what() << endl;
353 return true;
354 }
355 catch (...) {
356 }
357 return false;
358 }
359
360 } // namespace
361
362 BEGIN_MAIN
363 DO_TEST(test01);
364 DO_TEST(test02);
365 DO_TEST(test03);
366 DO_TEST(test04);
367 DO_TEST(test05);
368 DO_TEST(test06);
369 DO_TEST(test07);
370 DO_TEST(test08);
371 DO_TEST(test09);
372 DO_TEST(test10);
373 DO_TEST(test11);
374 DO_TEST(test12);
375 DO_TEST(test13);
376 DO_TEST(test14);
377 DO_TEST(test15);
378 END_MAIN
379