1 /* Test Grid::add_grid_generator().
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 // grid1.cc also tests add_grid_generator.
27 
28 // One dimension.
29 bool
test01()30 test01() {
31   Variable A(0);
32 
33   Grid gr(1, EMPTY);
34 
35   print_congruences(gr, "*** gr ***");
36 
37   gr.add_grid_generator(grid_point(-A));
38 
39   Grid known_gr(1);
40   known_gr.add_congruence((A == -1) / 0);
41 
42   bool ok = (gr == known_gr);
43 
44   print_congruences(gr, "*** gr.add_grid_generator(grid_point(-A)) ***");
45 
46   return ok;
47 }
48 
49 // Two dimensions.
50 bool
test02()51 test02() {
52   Variable A(0);
53   Variable B(1);
54 
55   Grid gr(2, EMPTY);
56 
57   print_congruences(gr, "*** gr ***");
58   gr.add_grid_generator(grid_point(A + B));
59 
60   Grid known_gr(2);
61   known_gr.add_congruence((A == 1) / 0);
62   known_gr.add_congruence((B == 1) / 0);
63 
64   bool ok = (gr == known_gr);
65 
66   print_congruences(gr, "*** gr.add_grid_generator(grid_point(A + B)) ***");
67 
68   return ok;
69 }
70 
71 // Add many generators to grid of two dimensions.
72 
73 bool
test03()74 test03() {
75   Variable A(0);
76   Variable B(1);
77 
78   Grid gr(2, EMPTY);
79 
80   print_congruences(gr, "*** gr ***");
81 
82   gr.add_grid_generator(grid_point());
83   gr.add_grid_generator(grid_point(A + 2*B));
84   gr.add_grid_generator(grid_point(A + B));
85   gr.add_grid_generator(grid_point(2*A + 2*B));
86   gr.add_grid_generator(grid_line(A));
87 
88   Grid known_gr(2);
89   known_gr.add_congruence(B %= 0);
90 
91   bool ok = (gr == known_gr);
92 
93   print_congruences(gr, "*** gr.add_grid_generator(...) ***");
94 
95   return ok;
96 }
97 
98 // Add generators where one has a divisor.
99 bool
test04()100 test04() {
101   Variable A(0);
102   Variable B(1);
103 
104   Grid_Generator_System gs;
105   gs.insert(grid_point(7*A, 4));
106   gs.insert(grid_line(A - B));
107 
108   Grid gr(2, EMPTY);
109 
110   print_congruences(gr, "*** gr ***");
111 
112   for (Grid_Generator_System::const_iterator i = gs.begin(),
113          gs_end = gs.end(); i != gs_end; ++i)
114     gr.add_grid_generator(*i);
115 
116   Grid known_gr(2);
117   known_gr.add_congruence((4*A + 4*B == 7) / 0);
118 
119   bool ok = (gr == known_gr);
120 
121   print_congruences(gr, "*** gr.add_grid_generator(*i) ***");
122 
123   return ok;
124 }
125 
126 // Add generators to a grid of a higher space dimension.
127 bool
test05()128 test05() {
129   Variable A(0);
130   Variable B(1);
131   Variable C(2);
132   Variable D(3);
133 
134   Grid gr(4, EMPTY);
135   print_congruences(gr, "*** gr ***");
136 
137   gr.add_grid_generator(grid_point(7*A, 3));
138   print_congruences(gr, "*** gr.add_grid_generator(grid_point(7*A, 3)) ***");
139   gr.add_grid_generator(grid_line(A - B));
140 
141   Grid known_gr(4);
142 
143   known_gr.add_congruence((3*A + 3*B == 7) / 0);
144   known_gr.add_congruence((C == 0) / 0);
145   known_gr.add_congruence((D == 0) / 0);
146 
147   bool ok = (gr == known_gr);
148 
149   print_congruences(gr, "*** gr.add_grid_generator(grid_line(A - B)) ***");
150 
151   return ok;
152 }
153 
154 // Add a generator to a universe grid.
155 bool
test06()156 test06() {
157   Variable A(0);
158   Variable B(1);
159   Variable C(2);
160   Variable D(3);
161 
162   Grid gr(4);
163 
164   print_congruences(gr, "*** gr ***");
165 
166   gr.add_grid_generator(grid_point(12*A + 7*D));
167 
168   Grid known_gr(4);
169 
170   bool ok = (gr == known_gr);
171 
172   print_congruences(gr,
173                     "*** gr.add_grid_generator(grid_point(12*A + 7*D)) ***");
174 
175   return ok;
176 }
177 
178 // adding a generator with a divisor to a grid of many generators.
179 bool
test07()180 test07() {
181   Variable A(0);
182   Variable B(1);
183 
184   Grid gr(2, EMPTY);
185 
186   gr.add_grid_generator(grid_point());
187   gr.add_grid_generator(grid_point(A));
188 
189   print_congruences(gr, "*** gr ***");
190 
191   // Minimize the grid.
192 
193   gr.add_grid_generator(grid_point(B, 3));
194 
195   Grid known_gr(2);
196   known_gr.add_congruence(A %= 0);
197   known_gr.add_congruence(3*B %= 0);
198 
199   bool ok = (gr == known_gr);
200 
201   print_congruences(gr,
202                     "*** gr.add_grid_generator(grid_point(B, 3)) ***");
203 
204   return ok;
205 }
206 
207 // Space dimension exception.
208 bool
test08()209 test08() {
210   Variable A(0);
211   Variable C(2);
212 
213   Grid gr(2);
214 
215   try {
216     gr.add_grid_generator(grid_point(A + C));
217   }
218   catch (const std::invalid_argument& e) {
219     nout << "invalid_argument: " << e.what() << endl;
220     return true;
221   }
222   catch (...) {
223   }
224   return false;
225 }
226 
227 // Zero dimensions empty.
228 bool
test09()229 test09() {
230   Grid gr(0, EMPTY);
231 
232   print_congruences(gr, "*** gr ***");
233 
234   gr.add_grid_generator(grid_point());
235 
236   Grid known_gr(0);
237 
238   bool ok = (gr == known_gr);
239 
240   print_congruences(gr, "*** gr.add_grid_generator(grid_point()) ***");
241 
242   return ok;
243 }
244 
245 // Zero dimension universe.
246 bool
test10()247 test10() {
248   Grid gr(0);
249 
250   print_congruences(gr, "*** gr ***");
251 
252   gr.add_grid_generator(grid_point());
253 
254   Grid known_gr(0);
255 
256   bool ok = (gr == known_gr);
257 
258   print_congruences(gr, "*** gr.add_grid_generator(grid_point()) ***");
259 
260   return ok;
261 }
262 
263 // Space dimension exception.
264 bool
test11()265 test11() {
266   Variable A(0);
267 
268   Grid gr(2, EMPTY);
269 
270   try {
271     gr.add_grid_generator(grid_line(A));
272   }
273   catch (const std::invalid_argument& e) {
274     nout << "invalid_argument: " << e.what() << endl;
275     return true;
276   }
277   catch (...) {
278   }
279   return false;
280 }
281 
282 // Try add parameter to empty grid.
283 bool
test12()284 test12() {
285   Grid gr(2, EMPTY);
286 
287   try {
288     gr.add_grid_generator(parameter());
289   }
290   catch (const std::invalid_argument& e) {
291     nout << "invalid_argument: " << e.what() << endl;
292     return true;
293   }
294   catch (...) {
295   }
296   return false;
297 }
298 
299 // Try add parameter to zero dimension empty grid.
300 bool
test13()301 test13() {
302   Grid gr(0, EMPTY);
303 
304   try {
305     gr.add_grid_generator(parameter());
306   }
307   catch (const std::invalid_argument& e) {
308     nout << "invalid_argument: " << e.what() << endl;
309     return true;
310   }
311   catch (...) {
312   }
313   return false;
314 }
315 
316 BEGIN_MAIN
317   DO_TEST(test01);
318   DO_TEST(test02);
319   DO_TEST(test03);
320   DO_TEST(test04);
321   DO_TEST(test05);
322   DO_TEST(test06);
323   DO_TEST(test07);
324   DO_TEST(test08);
325   DO_TEST(test09);
326   DO_TEST(test10);
327   DO_TEST(test11);
328   DO_TEST(test12);
329   DO_TEST(test13);
330 END_MAIN
331