barvinok.cc: split off part dealing with existential variables
[barvinok.git] / testlib.cc
blob24b1446756bd60448c0ac355453d67949f50450d
1 #include <sstream>
2 #include <barvinok/set.h>
3 #include <barvinok/options.h>
4 #include <barvinok/evalue.h>
5 #include <barvinok/util.h>
6 #include "conversion.h"
7 #include "evalue_read.h"
8 #include "dpoly.h"
9 #include "lattice_point.h"
10 #include "tcounter.h"
11 #include "bernoulli.h"
13 using std::cout;
14 using std::cerr;
15 using std::endl;
17 template <typename T>
18 void set_from_string(T& v, const char *s)
20 std::istringstream str(s);
21 str >> v;
24 int test_evalue(struct barvinok_options *options)
26 unsigned nvar, nparam;
27 char **all_vars;
28 evalue *poly1, poly2;
30 poly1 = evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
31 NULL, &all_vars, &nvar, &nparam,
32 options->MaxRays);
33 Free_ParamNames(all_vars, nvar+nparam);
35 value_init(poly2.d);
36 evalue_copy(&poly2, poly1);
37 evalue_negate(poly1);
38 eadd(&poly2, poly1);
39 reduce_evalue(poly1);
40 assert(EVALUE_IS_ZERO(*poly1));
41 free_evalue_refs(poly1);
42 free_evalue_refs(&poly2);
43 free(poly1);
46 int test_split_periods(struct barvinok_options *options)
48 unsigned nvar, nparam;
49 char **all_vars;
50 evalue *e;
52 e = evalue_read_from_str("U + 2V + 3 >= 0\n- U -2V >= 0\n- U 10 >= 0\n"
53 "U >= 0\n\n({( 1/3 * U + ( 2/3 * V + 0 ))})",
54 NULL, &all_vars, &nvar, &nparam,
55 options->MaxRays);
56 Free_ParamNames(all_vars, nvar+nparam);
58 evalue_split_periods(e, 2, options->MaxRays);
59 assert(value_zero_p(e->d));
60 assert(e->x.p->type == partition);
61 assert(e->x.p->size == 4);
62 assert(value_zero_p(e->x.p->arr[1].d));
63 assert(e->x.p->arr[1].x.p->type == polynomial);
64 assert(value_zero_p(e->x.p->arr[3].d));
65 assert(e->x.p->arr[3].x.p->type == polynomial);
66 free_evalue_refs(e);
67 free(e);
70 int test_specialization(struct barvinok_options *options)
72 Value v;
73 value_init(v);
74 mpq_t count;
75 mpq_init(count);
77 value_set_si(v, 5);
78 dpoly n(2, v);
79 assert(value_cmp_si(n.coeff->p[0], 1) == 0);
80 assert(value_cmp_si(n.coeff->p[1], 5) == 0);
81 assert(value_cmp_si(n.coeff->p[2], 10) == 0);
83 value_set_si(v, 1);
84 dpoly d(2, v, 1);
85 value_set_si(v, 2);
86 dpoly d2(2, v, 1);
87 d *= d2;
88 assert(value_cmp_si(d.coeff->p[0], 2) == 0);
89 assert(value_cmp_si(d.coeff->p[1], 1) == 0);
90 assert(value_cmp_si(d.coeff->p[2], 0) == 0);
92 n.div(d, count, 1);
93 mpq_canonicalize(count);
94 assert(value_cmp_si(mpq_numref(count), 31) == 0);
95 assert(value_cmp_si(mpq_denref(count), 8) == 0);
97 value_set_si(v, -2);
98 dpoly n2(2, v);
99 assert(value_cmp_si(n2.coeff->p[0], 1) == 0);
100 assert(value_cmp_si(n2.coeff->p[1], -2) == 0);
101 assert(value_cmp_si(n2.coeff->p[2], 3) == 0);
103 n2.div(d, count, 1);
104 mpq_canonicalize(count);
105 assert(value_cmp_si(mpq_numref(count), 6) == 0);
106 assert(value_cmp_si(mpq_denref(count), 1) == 0);
108 value_clear(v);
109 mpq_clear(count);
112 int test_lattice_points(struct barvinok_options *options)
114 Param_Vertices V;
115 mat_ZZ tmp;
116 set_from_string(tmp, "[[0 0 0 0 4][0 0 0 0 4][-1 0 1 0 4]]");
117 V.Vertex = zz2matrix(tmp);
118 vec_ZZ lambda;
119 set_from_string(lambda, "[3 5 7]");
120 mat_ZZ rays;
121 set_from_string(rays, "[[0 1 0][4 0 1][0 0 -1]]");
122 term_info num;
123 evalue *point[4];
125 unsigned nvar, nparam;
126 char **all_vars;
127 point[0] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
128 "( 7 * {( 1/4 * a + ( 3/4 * c + 3/4 ) ) } + -21/4 ) ) )",
129 "a,b,c", &all_vars, &nvar, &nparam, options->MaxRays);
130 Free_ParamNames(all_vars, nvar+nparam);
131 point[1] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
132 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/2 ) ) } + -1/2 ) ) )",
133 "a,b,c", &all_vars, &nvar, &nparam, options->MaxRays);
134 Free_ParamNames(all_vars, nvar+nparam);
135 point[2] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
136 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/4 ) ) } + 17/4 ) ) )",
137 "a,b,c", &all_vars, &nvar, &nparam, options->MaxRays);
138 Free_ParamNames(all_vars, nvar+nparam);
139 point[3] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
140 "( 7 * {( 1/4 * a + ( 3/4 * c + 0 ) ) } + 9 ) ) )",
141 "a,b,c", &all_vars, &nvar, &nparam, options->MaxRays);
142 Free_ParamNames(all_vars, nvar+nparam);
144 lattice_point(&V, rays, lambda, &num, 4, NULL, options);
145 Matrix_Free(V.Vertex);
147 for (int i = 0; i < 4; ++i) {
148 assert(eequal(num.E[i], point[i]));
149 free_evalue_refs(point[i]);
150 free(point[i]);
151 free_evalue_refs(num.E[i]);
152 delete num.E[i];
154 delete [] num.E;
157 int test_todd(struct barvinok_options *options)
159 tcounter t(2, options->max_index);
160 assert(value_cmp_si(t.todd.coeff->p[0], 1) == 0);
161 assert(value_cmp_si(t.todd.coeff->p[1], -3) == 0);
162 assert(value_cmp_si(t.todd.coeff->p[2], 3) == 0);
163 assert(value_cmp_si(t.todd_denom->p[0], 1) == 0);
164 assert(value_cmp_si(t.todd_denom->p[1], 6) == 0);
165 assert(value_cmp_si(t.todd_denom->p[2], 36) == 0);
167 vec_ZZ lambda;
168 set_from_string(lambda, "[1 -1]");
169 zz2values(lambda, t.lambda->p);
171 mat_ZZ rays;
172 set_from_string(rays, "[[-1 0][-1 1]]");
174 QQ one(1, 1);
176 vec_ZZ v;
177 set_from_string(v, "[2 0 1]");
178 Vector *vertex = Vector_Alloc(3);
179 zz2values(v, vertex->p);
181 t.handle(rays, vertex->p, one, 1, NULL, options);
182 assert(value_cmp_si(mpq_numref(t.count), 71) == 0);
183 assert(value_cmp_si(mpq_denref(t.count), 24) == 0);
185 set_from_string(rays, "[[0 -1][1 -1]]");
186 set_from_string(v, "[0 2 1]");
187 zz2values(v, vertex->p);
189 t.handle(rays, vertex->p, one, 1, NULL, options);
190 assert(value_cmp_si(mpq_numref(t.count), 71) == 0);
191 assert(value_cmp_si(mpq_denref(t.count), 12) == 0);
193 set_from_string(rays, "[[1 0][0 1]]");
194 set_from_string(v, "[0 0 1]");
195 zz2values(v, vertex->p);
197 t.handle(rays, vertex->p, one, 1, NULL, options);
198 assert(value_cmp_si(mpq_numref(t.count), 6) == 0);
199 assert(value_cmp_si(mpq_denref(t.count), 1) == 0);
201 Vector_Free(vertex);
204 int test_bernoulli(struct barvinok_options *options)
206 struct bernoulli *bernoulli;
207 bernoulli = bernoulli_compute(2);
208 bernoulli = bernoulli_compute(4);
209 bernoulli = bernoulli_compute(8);
210 assert(value_cmp_si(bernoulli->b_num->p[6], 1) == 0);
211 assert(value_cmp_si(bernoulli->b_den->p[6], 42) == 0);
212 assert(value_cmp_si(bernoulli->sum[3]->p[0], 0) == 0);
213 assert(value_cmp_si(bernoulli->sum[3]->p[1], 0) == 0);
214 assert(value_cmp_si(bernoulli->sum[3]->p[2], 1) == 0);
215 assert(value_cmp_si(bernoulli->sum[3]->p[3], -2) == 0);
216 assert(value_cmp_si(bernoulli->sum[3]->p[4], 1) == 0);
217 assert(value_cmp_si(bernoulli->sum[3]->p[5], 4) == 0);
219 unsigned nvar, nparam;
220 char **all_vars;
221 evalue *base, *sum1, *sum2;
222 base = evalue_read_from_str("(1 * n + 1)", NULL, &all_vars, &nvar, &nparam,
223 options->MaxRays);
225 sum1 = evalue_polynomial(bernoulli->sum[3], base);
226 Free_ParamNames(all_vars, nvar+nparam);
228 sum2 = evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
229 NULL, &all_vars, &nvar, &nparam,
230 options->MaxRays);
231 Free_ParamNames(all_vars, nvar+nparam);
232 assert(eequal(sum1, sum2));
233 free_evalue_refs(base);
234 free_evalue_refs(sum1);
235 free_evalue_refs(sum2);
236 free(base);
237 free(sum1);
238 free(sum2);
241 int test_bernoulli_sum(struct barvinok_options *options)
243 unsigned nvar, nparam;
244 char **all_vars;
245 evalue *e, *sum1, *sum2;
246 e = evalue_read_from_str("i + -1 >= 0\n -i + n >= 0\n\n 1 + (-1 *i) + i^2",
247 "i", &all_vars, &nvar, &nparam,
248 options->MaxRays);
249 Free_ParamNames(all_vars, nvar+nparam);
251 sum1 = Bernoulli_sum_evalue(e, 1, options);
252 sum2 = evalue_read_from_str("n -1 >= 0\n\n (1/3 * n^3 + 2/3 * n)",
253 NULL, &all_vars, &nvar, &nparam,
254 options->MaxRays);
255 Free_ParamNames(all_vars, nvar+nparam);
256 evalue_negate(sum1);
257 eadd(sum2, sum1);
258 reduce_evalue(sum1);
259 assert(EVALUE_IS_ZERO(*sum1));
260 free_evalue_refs(e);
261 free_evalue_refs(sum1);
262 free(e);
263 free(sum1);
265 e = evalue_read_from_str("-i + -1 >= 0\n i + n >= 0\n\n 1 + i + i^2",
266 "i", &all_vars, &nvar, &nparam,
267 options->MaxRays);
268 Free_ParamNames(all_vars, nvar+nparam);
269 sum1 = Bernoulli_sum_evalue(e, 1, options);
270 evalue_negate(sum1);
271 eadd(sum2, sum1);
272 reduce_evalue(sum1);
273 assert(EVALUE_IS_ZERO(*sum1));
274 free_evalue_refs(e);
275 free_evalue_refs(sum1);
276 free(e);
277 free(sum1);
279 free_evalue_refs(sum2);
280 free(sum2);
282 e = evalue_read_from_str("i + 4 >= 0\n -i + n >= 0\n\n i",
283 "i", &all_vars, &nvar, &nparam,
284 options->MaxRays);
285 Free_ParamNames(all_vars, nvar+nparam);
286 sum1 = Bernoulli_sum_evalue(e, 1, options);
287 sum2 = evalue_read_from_str("n + 0 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))\n"
288 "n + 4 >= 0\n -n -1 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))",
289 NULL, &all_vars, &nvar, &nparam,
290 options->MaxRays);
291 Free_ParamNames(all_vars, nvar+nparam);
292 evalue_negate(sum1);
293 eadd(sum2, sum1);
294 reduce_evalue(sum1);
295 assert(EVALUE_IS_ZERO(*sum1));
296 free_evalue_refs(e);
297 free_evalue_refs(sum1);
298 free(e);
299 free(sum1);
300 free_evalue_refs(sum2);
301 free(sum2);
303 e = evalue_read_from_str("i -5 >= 0\n -i + n >= 0\n j -1 >= 0\n i -j >= 0\n"
304 "k -1 >= 0\n j -k >= 0\n\n1",
305 "i,j,k", &all_vars, &nvar, &nparam,
306 options->MaxRays);
307 Free_ParamNames(all_vars, nvar+nparam);
308 sum1 = Bernoulli_sum_evalue(e, 3, options);
309 sum2 = evalue_read_from_str("n -5 >= 0\n\n"
310 "1/6 * n^3 + 1/2 * n^2 + 1/3 * n + -20",
311 NULL, &all_vars, &nvar, &nparam,
312 options->MaxRays);
313 Free_ParamNames(all_vars, nvar+nparam);
314 evalue_negate(sum1);
315 eadd(sum2, sum1);
316 reduce_evalue(sum1);
317 assert(EVALUE_IS_ZERO(*sum1));
318 free_evalue_refs(e);
319 free_evalue_refs(sum1);
320 free(e);
321 free(sum1);
322 free_evalue_refs(sum2);
323 free(sum2);
326 int main(int argc, char **argv)
328 struct barvinok_options *options = barvinok_options_new_with_defaults();
329 test_evalue(options);
330 test_split_periods(options);
331 test_specialization(options);
332 test_lattice_points(options);
333 test_todd(options);
334 test_bernoulli(options);
335 test_bernoulli_sum(options);
336 barvinok_options_free(options);