1 /* Test Polyhedron::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 x(0);
31   Variable y(1);
32 
33   Generator_System gs1;
34   gs1.insert(point(0*x + 0*y));
35   gs1.insert(point(4*x + 0*y));
36   gs1.insert(point(2*x + 2*y));
37 
38   C_Polyhedron ph1(gs1);
39 
40   print_generators(ph1, "*** ph1 ***");
41 
42   Generator_System gs2;
43   gs2.insert(point(0*x + 3*y));
44   gs2.insert(point(4*x + 3*y));
45   gs2.insert(point(2*x + 1*y));
46 
47   C_Polyhedron ph2(gs2);
48 
49   print_generators(ph2, "*** ph2 ***");
50 
51   C_Polyhedron computed_result = ph1;
52 
53   computed_result.difference_assign(ph2);
54 
55   Generator_System gs_known_result;
56   gs_known_result.insert(point());
57   gs_known_result.insert(point(3*x + 3*y, 2));
58   gs_known_result.insert(point(4*x));
59   gs_known_result.insert(point(5*x + 3*y, 2));
60 
61   C_Polyhedron known_result(gs_known_result);
62 
63   C_Polyhedron ph3(2);
64   ph3.add_constraint(2*y >= 3);
65 
66   known_result.difference_assign(ph3);
67 
68   bool ok = (computed_result == known_result);
69 
70   print_generators(computed_result, "*** after difference_assign ***");
71   print_generators(known_result, "*** known_result ***");
72 
73   return ok;
74 }
75 
76 bool
test02()77 test02() {
78   C_Polyhedron ph1;
79   C_Polyhedron ph2;
80 
81   print_constraints(ph1, "*** ph1 ***");
82   print_constraints(ph2, "*** ph2 ***");
83 
84   Constraint_System cs;
85   cs.insert(Linear_Expression(-4) >= 0);
86   C_Polyhedron known_result(cs);
87 
88   ph1.difference_assign(ph2);
89 
90   bool ok = (ph1 == known_result);
91 
92   print_constraints(ph1, "*** after ph1.difference_assign(ph2) ***");
93 
94   return ok;
95 }
96 
97 bool
test03()98 test03() {
99   Variable A(0);
100   Variable B(1);
101 
102   C_Polyhedron ph1(2);
103   ph1.add_constraint(A >= 0);
104   ph1.add_constraint(A <= 2);
105   ph1.add_constraint(B == 0);
106 
107   C_Polyhedron ph2(2);
108   ph1.add_constraint(A >= 0);
109   ph1.add_constraint(A <= 2);
110   ph1.add_constraint(B >= 0);
111   ph1.add_constraint(B <= 2);
112 
113   print_constraints(ph1, "*** ph1 ***");
114   print_constraints(ph2, "*** ph2 ***");
115 
116   C_Polyhedron known_result(2, EMPTY);
117 
118   ph1.difference_assign(ph2);
119 
120   bool ok = (ph1 == known_result);
121 
122   print_constraints(ph1, "*** after ph1.difference_assign(ph2) ***");
123 
124   return ok;
125 }
126 
127 bool
test04()128 test04() {
129   Variable A(0);
130 
131   C_Polyhedron ph1(1);
132   ph1.add_constraint(A >= 0);
133   ph1.add_constraint(A <= 7);
134   C_Polyhedron ph2(1);
135   ph2.add_constraint(A == 5);
136 
137   print_constraints(ph1, "*** ph1 ***");
138   print_constraints(ph2, "*** ph2 ***");
139 
140   C_Polyhedron known_result(ph1);
141 
142   ph1.difference_assign(ph2);
143 
144   bool ok = (ph1 == known_result);
145 
146   print_constraints(ph1, "*** after ph1.difference_assign(ph2) ***");
147 
148   return ok;
149 }
150 
151 bool
test05()152 test05() {
153   Variable A(0);
154   Variable B(1);
155 
156   C_Polyhedron ph1(2, EMPTY);
157   C_Polyhedron ph2(2);
158   ph2.add_constraint(A == B);
159 
160   print_constraints(ph1, "*** ph1 ***");
161   print_constraints(ph2, "*** ph2 ***");
162 
163   C_Polyhedron known_result(ph1);
164 
165   ph1.difference_assign(ph2);
166 
167   bool ok = (ph1 == known_result);
168 
169   print_constraints(ph1, "*** after ph1.difference_assign(ph2) ***");
170 
171   return ok;
172 }
173 
174 bool
test06()175 test06() {
176   Variable A(0);
177   Variable B(1);
178 
179   C_Polyhedron ph1(2);
180   ph1.add_constraint(A + B == 0);
181   C_Polyhedron ph2(2, EMPTY);
182 
183   print_constraints(ph1, "*** ph1 ***");
184   print_constraints(ph2, "*** ph2 ***");
185 
186   C_Polyhedron known_result(ph1);
187 
188   ph1.difference_assign(ph2);
189 
190   bool ok = (ph1 == known_result);
191 
192   print_constraints(ph1, "*** after ph1.difference_assign(ph2) ***");
193 
194   return ok;
195 }
196 
197 } // namespace
198 
199 BEGIN_MAIN
200   DO_TEST(test01);
201   DO_TEST(test02);
202   DO_TEST(test03);
203   DO_TEST(test04);
204   DO_TEST(test05);
205   DO_TEST(test06);
206 END_MAIN
207