1 /* Test BD_Shape::difference_assign().
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   Variable A(0);
31   Variable B(1);
32 
33   TBD_Shape bds1(2);
34   bds1.add_constraint(A >= 0);
35   bds1.add_constraint(A <= -2);
36   bds1.add_constraint(B == 0);
37 
38   TBD_Shape bds2(2);
39   bds2.add_constraint(A >= 0);
40   bds2.add_constraint(A <= 2);
41   bds2.add_constraint(B >= 0);
42   bds2.add_constraint(B <= 2);
43 
44   print_constraints(bds1, "*** bds1 ***");
45   print_constraints(bds2, "*** ph2 ***");
46 
47   bds1.difference_assign(bds2);
48 
49   BD_Shape<mpq_class> known_result(2, EMPTY);
50 
51   bool ok = check_result(bds1, known_result);
52 
53   print_constraints(bds1, "*** after bds1.difference_assign(ph2) ***");
54 
55   return ok;
56 }
57 
58 bool
test02()59 test02() {
60   TBD_Shape bds1;
61   TBD_Shape bds2;
62 
63   print_constraints(bds1, "*** bds1 ***");
64   print_constraints(bds2, "*** bds2 ***");
65 
66   bds1.difference_assign(bds2);
67 
68   Constraint_System cs;
69   cs.insert(Linear_Expression(-4) >= 0);
70   BD_Shape<mpq_class> known_result(cs);
71 
72   bool ok = check_result(bds1, known_result);
73 
74   print_constraints(bds1, "*** after bds1.difference_assign(bds2) ***");
75 
76   return ok;
77 }
78 
79 bool
test03()80 test03() {
81   Variable x(0);
82   Variable y(1);
83 
84   TBD_Shape bds1(2);
85   bds1.add_constraint(x <= 2);
86   bds1.add_constraint(x >= 0);
87   bds1.add_constraint(y <= 5);
88   bds1.add_constraint(y >= 2);
89 
90   TBD_Shape bds2(2);
91   bds2.add_constraint(x <= 3);
92   bds2.add_constraint(x >= 1);
93   bds2.add_constraint(y <= 4);
94   bds2.add_constraint(y >= 1);
95 
96   print_constraints(bds1, "*** bds1 ***");
97   print_constraints(bds2, "*** bds2 ***");
98 
99   bds1.difference_assign(bds2);
100 
101   BD_Shape<mpq_class> known_result(2);
102   known_result.add_constraint(x >= 0);
103   known_result.add_constraint(x <= 2);
104   known_result.add_constraint(y <= 5);
105   known_result.add_constraint(y >= 2);
106   known_result.add_constraint(y - x >= 1);
107 
108   bool ok = check_result(bds1, known_result);
109 
110   print_constraints(bds1, "*** after bds1.difference_assign(bds2) ***");
111 
112   return ok;
113 }
114 
115 bool
test04()116 test04() {
117   Variable x(0);
118   Variable y(1);
119 
120   TBD_Shape bds1(2);
121   bds1.add_constraint(x <= 8);
122   bds1.add_constraint(x >= 0);
123   bds1.add_constraint(y <= 7);
124   bds1.add_constraint(y >= 2);
125 
126   TBD_Shape bds2(2);
127   bds2.add_constraint(x <= 3);
128   bds2.add_constraint(x >= 1);
129   bds2.add_constraint(y <= 0);
130   bds2.add_constraint(y >= 1);
131 
132   print_constraints(bds1, "*** bds1 ***");
133   print_constraints(bds2, "*** bds2 ***");
134 
135   BD_Shape<mpq_class> known_result(bds1);
136 
137   bds1.difference_assign(bds2);
138 
139   bool ok = check_result(bds1, known_result);
140 
141   print_constraints(bds1, "*** after bds1.difference_assign(bds2) ***");
142 
143   return ok;
144 }
145 
146 bool
test05()147 test05() {
148   Variable x(0);
149   Variable y(1);
150 
151   TBD_Shape bds1(2);
152   bds1.add_constraint(x <= 8);
153   bds1.add_constraint(x >= 0);
154   bds1.add_constraint(y <= 7);
155   bds1.add_constraint(y >= 2);
156 
157   TBD_Shape bds2(2);
158   bds2.add_constraint(x <= 9);
159   bds2.add_constraint(x >= 0);
160   bds2.add_constraint(y <= 8);
161   bds2.add_constraint(y >= 1);
162 
163   print_constraints(bds1, "*** bds1 ***");
164   print_constraints(bds2, "*** bds2 ***");
165 
166   bds1.difference_assign(bds2);
167 
168   BD_Shape<mpq_class> known_result(2, EMPTY);
169 
170   bool ok = check_result(bds1, known_result);
171 
172   print_constraints(bds1, "*** after bds1.difference_assign(bds2) ***");
173 
174   return ok;
175 }
176 
177 bool
test06()178 test06() {
179   Variable x(0);
180   Variable y(1);
181   Variable z(2);
182 
183   TBD_Shape bds1(3);
184   bds1.add_constraint(x <= 8);
185   bds1.add_constraint(y <= 7);
186   bds1.add_constraint(y >= 1);
187   bds1.add_constraint(z <= 2);
188 
189   TBD_Shape bds2(3);
190   bds2.add_constraint(x == 8);
191   bds2.add_constraint(y <= 2);
192   bds2.add_constraint(y >= 1);
193 
194   print_constraints(bds1, "*** bds1 ***");
195   print_constraints(bds2, "*** bds2 ***");
196 
197   bds1.difference_assign(bds2);
198 
199   BD_Shape<mpq_class> known_result(3);
200   known_result.add_constraint(x <= 8);
201   known_result.add_constraint(y <= 7);
202   known_result.add_constraint(y >= 1);
203   known_result.add_constraint(z <= 2);
204 
205   bool ok = check_result(bds1, known_result);
206 
207   print_constraints(bds1, "*** after bds1.difference_assign(bds2) ***");
208 
209   return ok;
210 }
211 
212 bool
test07()213 test07() {
214   Variable A(0);
215   Variable B(1);
216 
217   TBD_Shape bds1(2);
218   bds1.add_constraint(A >= 0);
219   bds1.add_constraint(A <= 4);
220   bds1.add_constraint(B >= 0);
221   bds1.add_constraint(B <= 2);
222 
223   TBD_Shape bds2(2);
224   bds2.add_constraint(A >= 2);
225   bds2.add_constraint(A <= 4);
226   bds2.add_constraint(B >= 0);
227   bds2.add_constraint(B <= 2);
228 
229   print_constraints(bds1, "*** bds1 ***");
230   print_constraints(bds2, "*** bds2 ***");
231 
232   BD_Shape<mpq_class> known_result(2);
233   known_result.add_constraint(A >= 0);
234   known_result.add_constraint(A <= 2);
235   known_result.add_constraint(B >= 0);
236   known_result.add_constraint(B <= 2);
237 
238   bds1.difference_assign(bds2);
239 
240   bool ok = check_result(bds1, known_result);
241 
242   print_constraints(bds1, "*** after bds1.difference_assign(bds2) ***");
243   print_constraints(known_result, "*** known_result ***");
244 
245   return ok;
246 }
247 
248 bool
test08()249 test08() {
250   TBD_Shape bds1(3);
251   TBD_Shape bds2(5);
252 
253   try {
254     // This is an incorrect use of method
255     // BD_Shape::difference_assign(bds2): it is impossible to apply
256     // this method to two polyhedra of different dimensions.
257     bds1.difference_assign(bds2);
258   }
259   catch (std::invalid_argument& e) {
260     nout << "std::invalid_argument: " << endl;
261     return true;
262   }
263   catch (...) {
264   }
265   return false;
266 }
267 
268 } // namespace
269 
270 BEGIN_MAIN
271   DO_TEST(test01);
272   DO_TEST(test02);
273   DO_TEST(test03);
274   DO_TEST(test04);
275   DO_TEST(test05);
276   DO_TEST(test06);
277   DO_TEST(test07);
278   DO_TEST(test08);
279 END_MAIN
280