1 /* Test that the right exceptions are thrown in case of incorrect uses.
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 bool
test01()29 test01() {
30   Constraint_System cs;
31   Variable x(0);
32   Variables_Set params(x);
33 
34   try {
35     // This is an incorrect use of the constructor:
36     // the parameters in `params' should be space dimensions
37     // that are valid for the PIP_Problem we are going to build.
38     PIP_Problem pip(0, cs.begin(), cs.end(), params);
39   }
40   catch (std::invalid_argument& e) {
41     nout << "invalid_argument: " << e.what() << endl << endl;
42     return true;
43   }
44   catch (...) {
45   }
46   return false;
47 }
48 
49 bool
test02()50 test02() {
51   Constraint_System cs;
52 
53   try {
54     // This is an incorrect use of the constructor:
55     // invalid space dimension required.
56     PIP_Problem pip(1 + PIP_Problem::max_space_dimension(),
57                     cs.begin(), cs.end(), Variables_Set());
58   }
59   catch (std::length_error& e) {
60     nout << "length_error: " << e.what() << endl << endl;
61     return true;
62   }
63   catch (...) {
64   }
65   return false;
66 }
67 
68 bool
test03()69 test03() {
70   try {
71     // This is an incorrect use of the constructor:
72     // invalid space dimension required.
73     PIP_Problem pip(1 + PIP_Problem::max_space_dimension());
74   }
75   catch (std::length_error& e) {
76     nout << "length_error: " << e.what() << endl << endl;
77     return true;
78   }
79   catch (...) {
80   }
81   return false;
82 }
83 
84 bool
test04()85 test04() {
86   PIP_Problem pip;
87   try {
88     // Adding too many space dimensions.
89     pip.add_space_dimensions_and_embed(1, PIP_Problem::max_space_dimension());
90   }
91   catch (std::length_error& e) {
92     nout << "length_error: " << e.what() << endl << endl;
93     return true;
94   }
95   catch (...) {
96   }
97   return false;
98 }
99 
100 bool
test05()101 test05() {
102   Variable X(0);
103   Variable Y(1);
104   Variable Z(2);
105   Constraint_System cs;
106   cs.insert(X == 0);
107   cs.insert(Y == 1);
108   cs.insert(Z <= 2);
109 
110   try {
111     // This is an incorrect use of the constructor:
112     // the space dimensions of the constraints should not be greater
113     // than the space dimension of the PIP problem.
114     PIP_Problem pip(2, cs.begin(), cs.end(), Variables_Set());
115   }
116   catch (std::invalid_argument& e) {
117     nout << "invalid_argument: " << e.what() << endl << endl;
118     return true;
119   }
120   catch (...) {
121   }
122   return false;
123 }
124 
125 bool
test06()126 test06() {
127   PIP_Problem pip;
128   try {
129     // Adding invalid parameter space dimensions.
130     Variable m(4);
131     pip.add_to_parameter_space_dimensions(Variables_Set(m));
132   }
133   catch (std::invalid_argument& e) {
134     nout << "invalid_argument: " << e.what() << endl << endl;
135     return true;
136   }
137   catch (...) {
138   }
139   return false;
140 }
141 
142 bool
test07()143 test07() {
144   PIP_Problem pip;
145   try {
146     // Adding space dimension incompatible constraint.
147     Variable x(2);
148     pip.add_constraint(x >= 0);
149   }
150   catch (std::invalid_argument& e) {
151     nout << "invalid_argument: " << e.what() << endl << endl;
152     return true;
153   }
154   catch (...) {
155   }
156   return false;
157 }
158 
159 bool
test08()160 test08() {
161   PIP_Problem pip;
162   try {
163     // Setting an invalid control parameter value.
164     pip.set_control_parameter(PIP_Problem::CONTROL_PARAMETER_VALUE_SIZE);
165   }
166   catch (std::invalid_argument& e) {
167     nout << "invalid_argument: " << e.what() << endl << endl;
168     return true;
169   }
170   catch (...) {
171   }
172   return false;
173 }
174 
175 bool
test09()176 test09() {
177   Variable X1(0);
178   Variable X2(1);
179   Variable I0(2);
180   Variable J0(3);
181   Variable N(4);
182   Variables_Set params(I0, N);
183 
184   Constraint_System cs;
185   cs.insert(-X1 + N - 1 >= 0);
186   cs.insert(X1 - X2 >= 0);
187   cs.insert(X1 + I0 == N);
188   cs.insert(X2 + J0 - N - 1 >= 0);
189   cs.insert(I0 >= 1);
190   cs.insert(N >= 1);
191 
192   PIP_Problem pip(cs.space_dimension(), cs.begin(), cs.end(), params);
193   (void) pip.is_satisfiable();
194   // Adding 2 additional space dimensions.
195   pip.add_space_dimensions_and_embed(2, 0);
196 
197   try {
198     // Trying to mark a problem variable as a parameter.
199     pip.add_to_parameter_space_dimensions(Variables_Set(X1));
200   }
201   catch (std::invalid_argument& e) {
202     nout << "invalid_argument: " << e.what() << endl << endl;
203     return true;
204   }
205   catch (...) {
206   }
207   return false;
208 }
209 
210 bool
test10()211 test10() {
212   PIP_Problem pip;
213 
214   try {
215     // Trying to set an invalid big parameter dimension.
216     pip.set_big_parameter_dimension(1);
217   }
218   catch (std::invalid_argument& e) {
219     nout << "invalid_argument: " << e.what() << endl << endl;
220     return true;
221   }
222   catch (...) {
223   }
224   return false;
225 }
226 
227 bool
test11()228 test11() {
229   Variable X1(0);
230   Variable X2(1);
231   Variable I0(2);
232   Variable J0(3);
233   Variable N(4);
234   Variables_Set params(I0, N);
235 
236   Constraint_System cs;
237   cs.insert(-X1 + N - 1 >= 0);
238   cs.insert(X1 - X2 >= 0);
239   cs.insert(X1 + I0 == N);
240   cs.insert(X2 + J0 - N - 1 >= 0);
241   cs.insert(I0 >= 1);
242   cs.insert(N >= 1);
243 
244   PIP_Problem pip(cs.space_dimension(), cs.begin(), cs.end(), params);
245   (void) pip.is_satisfiable();
246   // Adding 2 additional space dimensions.
247   pip.add_space_dimensions_and_embed(2, 0);
248 
249   try {
250     // Trying to set an invalid big parameter dimension.
251     pip.set_big_parameter_dimension(3);
252   }
253   catch (std::invalid_argument& e) {
254     nout << "invalid_argument: " << e.what() << endl << endl;
255     return true;
256   }
257   catch (...) {
258   }
259   return false;
260 }
261 
262 bool
test12()263 test12() {
264   typedef PIP_Tree_Node::Artificial_Parameter Art_Param;
265   Variable A(0);
266 
267   try {
268     // Trying to set an invalid (zero) denominator.
269     Art_Param ap(3*A + 8, 0);
270   }
271   catch (std::invalid_argument& e) {
272     nout << "invalid_argument: " << e.what() << endl << endl;
273     return true;
274   }
275   catch (...) {
276   }
277   return false;
278 }
279 
280 bool
test13()281 test13() {
282   PIP_Problem pip;
283   try {
284     // Printing the solution before trying to solve the problem.
285     pip.print_solution(nout);
286   }
287   catch (std::logic_error& e) {
288     nout << "logic_error: " << e.what() << endl << endl;
289     return true;
290   }
291   catch (...) {
292   }
293   return false;
294 }
295 
296 bool
test14()297 test14() {
298   Variable i(0);
299   Variable j(1);
300   Variable n(2);
301   Variable m(3);
302   Variables_Set params(n, m);
303 
304   Constraint_System cs;
305   cs.insert(3*j >= -2*i+8);
306   cs.insert(j <= 4*i - 4);
307   cs.insert(j <= m);
308   cs.insert(i <= n);
309 
310   PIP_Problem pip(cs.space_dimension(), cs.begin(), cs.end(), params);
311   (void) pip.solve();
312 
313   const PIP_Decision_Node* root = pip.solution()->as_decision();
314   const PIP_Decision_Node* t_child = root->child_node(true)->as_decision();
315   const PIP_Solution_Node* t_t_child = t_child->child_node(true)->as_solution();
316 
317   try {
318     // It is illegal to ask for the parametric value of a parameter.
319     (void) t_t_child->parametric_values(n);
320   }
321   catch (std::invalid_argument& e) {
322     nout << "invalid_argument: " << e.what() << endl << endl;
323     return true;
324   }
325   catch (...) {
326   }
327   return false;
328 }
329 
330 bool
test15()331 test15() {
332   Variable i(0);
333   Variable j(1);
334   Variable n(2);
335   Variable m(3);
336   Variables_Set params(n, m);
337 
338   Constraint_System cs;
339   cs.insert(3*j >= -2*i+8);
340   cs.insert(j <= 4*i - 4);
341   cs.insert(j <= m);
342   cs.insert(i <= n);
343 
344   PIP_Problem pip(cs.space_dimension(), cs.begin(), cs.end(), params);
345   (void) pip.solve();
346 
347   const PIP_Decision_Node* root = pip.solution()->as_decision();
348   const PIP_Decision_Node* t_child = root->child_node(true)->as_decision();
349   const PIP_Solution_Node* t_t_child = t_child->child_node(true)->as_solution();
350 
351   try {
352     // It is illegal to ask for the parametric value of a variable
353     // having space dimension greater than that o fthe problem.
354     (void) t_t_child->parametric_values(Variable(4));
355   }
356   catch (std::invalid_argument& e) {
357     nout << "invalid_argument: " << e.what() << endl << endl;
358     return true;
359   }
360   catch (...) {
361   }
362   return false;
363 }
364 
365 } // namespace
366 
367 BEGIN_MAIN
368   DO_TEST(test01);
369   DO_TEST(test02);
370   DO_TEST(test03);
371   DO_TEST(test04);
372   DO_TEST(test05);
373   DO_TEST(test06);
374   DO_TEST(test07);
375   DO_TEST(test08);
376   DO_TEST(test09);
377   DO_TEST(test10);
378   DO_TEST(test11);
379   DO_TEST(test12);
380   DO_TEST(test13);
381   DO_TEST_F8(test14);
382   DO_TEST_F8(test15);
383 END_MAIN
384