1 /* Test Grid::bounded_affine_image().
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 // Non-empty grid.
29 bool
test01()30 test01() {
31   Variable A(0);
32   Variable B(1);
33   Variable C(2);
34 
35   Grid gr(3);
36   gr.add_congruence((A ==  0) / 0);
37   gr.add_congruence((B ==  0) / 0);
38   gr.add_congruence((C == -2) / 0);
39 
40   print_congruences(gr, "*** gr ***");
41 
42   gr.bounded_affine_preimage(A, 7-B, B+3);
43 
44   Grid known_gr(3, EMPTY);
45   known_gr.add_grid_generator(grid_point(-2*C));
46   known_gr.add_grid_generator(grid_line(A));
47 
48   bool ok = (gr == known_gr);
49 
50   print_congruences(gr, "*** gr.bounded_affine_preimage(A, 7-B, B+3) ***");
51 
52   return ok;
53 }
54 
55 // Empty grid.
56 bool
test02()57 test02() {
58   Variable A(0);
59   Variable B(1);
60   Variable C(2);
61 
62   Grid gr(3);
63   gr.add_congruence((A ==  0) / 0);
64   gr.add_congruence((A ==  1) / 0);
65   gr.add_congruence((C == -2) / 0);
66 
67   print_congruences(gr, "*** gr ***");
68 
69   gr.bounded_affine_preimage(A, 7-B, B+3);
70 
71   Grid known_gr(3, EMPTY);
72 
73   bool ok = (gr == known_gr);
74 
75   print_congruences(gr, "*** gr.bounded_affine_preimage(A, 7-B, B+3) ***");
76 
77   return ok;
78 }
79 
80 bool
test03()81 test03() {
82   Variable A(0);
83   Variable B(1);
84 
85   Grid gr(2);
86   gr.add_congruence(A - B %= 0);
87 
88   try {
89     // This is an incorrect use of function
90     // Grid::bounded_affine_preimage(v, lb_expr, ub_expr, d):
91     // any call with a denominator equal to zero is illegal.
92     Coefficient d = 0;
93     gr.bounded_affine_preimage(B, A - 7, B + 2, d);
94   }
95   catch (std::invalid_argument& e) {
96     nout << "invalid_argument: " << e.what() << endl << endl;
97     return true;
98   }
99   catch (...) {
100   }
101   return false;
102 }
103 
104 bool
test04()105 test04() {
106   Variable A(0);
107   Variable B(1);
108 
109   Grid gr(1);
110   gr.add_congruence(A %= 0);
111 
112   try {
113     // This is an incorrect use of function
114     // Grid::bounded_affine_preimage(v, lb_expr, ub_expr, d):
115     // it is illegal to use a variable in the lower bounding expression
116     // that does not appear in the grid.
117     gr.bounded_affine_preimage(A, B, A + 7);
118   }
119   catch (std::invalid_argument& e) {
120     nout << "invalid_argument: " << e.what() << endl << endl;
121     return true;
122   }
123   catch (...) {
124   }
125   return false;
126 }
127 
128 bool
test05()129 test05() {
130   Variable A(0);
131   Variable B(1);
132 
133   Grid gr(1);
134   gr.add_congruence(A %= 0);
135 
136   try {
137     // This is an incorrect use of function
138     // Grid::bounded_affine_preimage(v, lb_expr, ub_expr, d):
139     // it is illegal to use a variable in the upper bounding expression
140     // that does not appear in the grid.
141     gr.bounded_affine_preimage(A, A + 7, B);
142   }
143   catch (std::invalid_argument& e) {
144     nout << "invalid_argument: " << e.what() << endl << endl;
145     return true;
146   }
147   catch (...) {
148   }
149   return false;
150 }
151 
152 bool
test06()153 test06() {
154   Variable A(0);
155   Variable B(1);
156 
157   Grid gr(1);
158   gr.add_congruence(A %= 1);
159 
160   try {
161     // This is an incorrect use of function
162     // Grid::bounded_affine_preimage(v, lb_expr, ub_expr, d):
163     // it is illegal to bound a variable not occurring in the
164     // vector space embedding the grid.
165     gr.bounded_affine_preimage(B, A - 7, 2*A - 2);
166   }
167   catch (std::invalid_argument& e) {
168     nout << "invalid_argument: " << e.what() << endl << endl;
169     return true;
170   }
171   catch (...) {
172   }
173   return false;
174 }
175 
176 } // namespace
177 
178 BEGIN_MAIN
179   DO_TEST(test01);
180   DO_TEST(test02);
181   DO_TEST(test03);
182   DO_TEST(test04);
183   DO_TEST(test05);
184   DO_TEST(test06);
185 END_MAIN
186