1 /* Test Box::ascii_dump() and Box::ascii_load().
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 #include "files.hh"
27 #include <string>
28 #include <fstream>
29
30 using std::string;
31 using std::fstream;
32 using std::ios_base;
33
34 namespace {
35
36 const char* my_file = "ascii_dump_load1.dat";
37
38 bool
test01()39 test01() {
40 Variable A(0);
41 Variable B(1);
42
43 TBox box(2);
44 box.add_constraint(A >= 0);
45 box.add_constraint(B >= 0);
46
47 fstream f;
48 open(f, my_file, ios_base::out);
49 box.ascii_dump(f);
50 close(f);
51
52 open(f, my_file, ios_base::in | ios_base::out);
53 string str;
54 do
55 f >> str;
56 while (str != "-EUP");
57 f.seekp(0, ios_base::cur);
58 f << " A";
59 close(f);
60
61 open(f, my_file, ios_base::in);
62 TBox box2;
63 bool ok = !box2.ascii_load(f);
64 close(f);
65
66 return ok;
67 }
68
69 bool
test02()70 test02() {
71 Variable A(0);
72 Variable B(1);
73
74 TBox box(2);
75 box.add_constraint(A >= 0);
76 box.add_constraint(B >= 1);
77
78 fstream f;
79 open(f, my_file, ios_base::out);
80 box.ascii_dump(f);
81 close(f);
82
83 open(f, my_file, ios_base::in | ios_base::out);
84 string str;
85 do
86 f >> str;
87 while (str != "-EM");
88 f.seekp(0, ios_base::cur);
89 f << "A";
90 close(f);
91
92 open(f, my_file, ios_base::in);
93 TBox box2;
94 bool ok = !box2.ascii_load(f);
95 close(f);
96
97 return ok;
98 }
99
100 bool
test03()101 test03() {
102 Variable A(0);
103 Variable B(1);
104
105 TBox box(2);
106 box.add_constraint(A >= 0);
107 box.add_constraint(B >= 2);
108
109 fstream f;
110 open(f, my_file, ios_base::out);
111 box.ascii_dump(f);
112 close(f);
113
114 open(f, my_file, ios_base::in | ios_base::out);
115 string str;
116 do
117 f >> str;
118 while (str != "-UN");
119 f.seekp(0, ios_base::cur);
120 f << "A";
121 close(f);
122
123 open(f, my_file, ios_base::in);
124 TBox box2;
125 bool ok = !box2.ascii_load(f);
126 close(f);
127
128 return ok;
129 }
130
131 bool
test04()132 test04() {
133 Variable A(0);
134 Variable B(1);
135
136 TBox box(2);
137 box.add_constraint(A >= -10);
138 box.add_constraint(A <= 10);
139 box.add_constraint(B >= 3);
140 box.add_constraint(B <= 6);
141
142 fstream f;
143 open(f, my_file, ios_base::out);
144 box.ascii_dump(f);
145 close(f);
146
147 open(f, my_file, ios_base::in | ios_base::out);
148 string str;
149 do
150 f >> str;
151 while (str != "lower");
152 f.seekp(0, ios_base::cur);
153 f << "Z(";
154 close(f);
155
156 open(f, my_file, ios_base::in);
157 TBox box2;
158 bool ok = !box2.ascii_load(f);
159 close(f);
160
161 return ok;
162 }
163
164 bool
test05()165 test05() {
166 Variable A(0);
167 Variable B(1);
168
169 TBox box1(3);
170 box1.add_constraint(A >= 2);
171 box1.add_constraint(B >= 0);
172
173 fstream f;
174 open(f, my_file, ios_base::out);
175 box1.ascii_dump(f);
176 close(f);
177
178 open(f, my_file, ios_base::in);
179 TBox box2;
180 bool ok = box2.ascii_load(f);
181 close(f);
182
183 if (!ok) {
184 nout << "ascii_load() failed" << endl;
185 return false;
186 }
187
188 ok = (box1 == box2);
189
190 print_constraints(box1, "*** box1 ***");
191 print_constraints(box2, "*** box2 ***");
192
193 return ok;
194 }
195
196 bool
test06()197 test06() {
198 Variable A(0);
199 Variable B(1);
200 Variable C(2);
201
202 Constraint_System cs;
203 cs.insert(3*C == 5);
204 TBox box1(cs);
205
206 print_constraints(box1, "*** box1(cs) ***");
207
208 TBox box1_copy(box1);
209
210 box1.difference_assign(box1_copy);
211
212 print_constraints(box1, "*** box1.difference_assign(box1_copy) ***");
213
214 box1.concatenate_assign(box1_copy);
215
216 print_constraints(box1, "*** box1.concatenate_assign(box1_copy) ***");
217
218 nout << "box1.space_dimension() = " << box1.space_dimension() << endl;
219
220 fstream f;
221 open(f, my_file, ios_base::out);
222 box1.ascii_dump(f);
223 close(f);
224
225 open(f, my_file, ios_base::in);
226 TBox box2;
227 bool ok = box2.ascii_load(f);
228 close(f);
229
230 if (!ok) {
231 nout << "ascii_load() failed" << endl;
232 return false;
233 }
234
235 ok = (box1 == box2);
236
237 nout << "box2.space_dimension() = " << box2.space_dimension() << endl;
238
239 print_constraints(box1, "*** box1 ***");
240 print_constraints(box2, "*** box2 ***");
241
242 return ok;
243 }
244
245 } // namespace
246
247 BEGIN_MAIN
248 DO_TEST(test01);
249 DO_TEST(test02);
250 DO_TEST(test03);
251 DO_TEST(test04);
252 DO_TEST(test05);
253 DO_TEST(test06);
254 END_MAIN
255