isl_set_convex_hull: optionally use Fourier-Motzkin based algorithm
[platform/upstream/isl.git] / isl_test.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #include <assert.h>
11 #include <stdio.h>
12 #include <limits.h>
13 #include <isl/ctx.h>
14 #include <isl/set.h>
15 #include <isl/flow.h>
16 #include <isl/constraint.h>
17 #include <isl/polynomial.h>
18 #include <isl/union_map.h>
19 #include <isl_map_private.h>
20
21 static char *srcdir;
22
23 void test_parse_map(isl_ctx *ctx, const char *str)
24 {
25         isl_map *map;
26
27         map = isl_map_read_from_str(ctx, str, -1);
28         assert(map);
29         isl_map_free(map);
30 }
31
32 void test_parse_pwqp(isl_ctx *ctx, const char *str)
33 {
34         isl_pw_qpolynomial *pwqp;
35
36         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
37         assert(pwqp);
38         isl_pw_qpolynomial_free(pwqp);
39 }
40
41 void test_parse(struct isl_ctx *ctx)
42 {
43         isl_map *map, *map2;
44         const char *str;
45
46         str = "{ [i] -> [-i] }";
47         map = isl_map_read_from_str(ctx, str, -1);
48         assert(map);
49         isl_map_free(map);
50
51         str = "{ A[i] -> L[([i/3])] }";
52         map = isl_map_read_from_str(ctx, str, -1);
53         assert(map);
54         isl_map_free(map);
55
56         test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
57
58         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
59         map = isl_map_read_from_str(ctx, str, -1);
60         str = "{ [new, old] -> [o0, o1] : "
61                "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
62                "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
63                "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
64         map2 = isl_map_read_from_str(ctx, str, -1);
65         assert(isl_map_is_equal(map, map2));
66         isl_map_free(map);
67         isl_map_free(map2);
68
69         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
70         map = isl_map_read_from_str(ctx, str, -1);
71         str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
72         map2 = isl_map_read_from_str(ctx, str, -1);
73         assert(isl_map_is_equal(map, map2));
74         isl_map_free(map);
75         isl_map_free(map2);
76
77         test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
78 }
79
80 void test_read(struct isl_ctx *ctx)
81 {
82         char filename[PATH_MAX];
83         FILE *input;
84         int n;
85         struct isl_basic_set *bset1, *bset2;
86         const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
87
88         n = snprintf(filename, sizeof(filename),
89                         "%s/test_inputs/set.omega", srcdir);
90         assert(n < sizeof(filename));
91         input = fopen(filename, "r");
92         assert(input);
93
94         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
95         bset2 = isl_basic_set_read_from_str(ctx, str, 0);
96
97         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
98
99         isl_basic_set_free(bset1);
100         isl_basic_set_free(bset2);
101
102         fclose(input);
103 }
104
105 void test_bounded(struct isl_ctx *ctx)
106 {
107         isl_set *set;
108         int bounded;
109
110         set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }", -1);
111         bounded = isl_set_is_bounded(set);
112         assert(bounded);
113         isl_set_free(set);
114
115         set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }", -1);
116         bounded = isl_set_is_bounded(set);
117         assert(!bounded);
118         isl_set_free(set);
119
120         set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }", -1);
121         bounded = isl_set_is_bounded(set);
122         assert(!bounded);
123         isl_set_free(set);
124 }
125
126 /* Construct the basic set { [i] : 5 <= i <= N } */
127 void test_construction(struct isl_ctx *ctx)
128 {
129         isl_int v;
130         struct isl_dim *dim;
131         struct isl_basic_set *bset;
132         struct isl_constraint *c;
133
134         isl_int_init(v);
135
136         dim = isl_dim_set_alloc(ctx, 1, 1);
137         bset = isl_basic_set_universe(dim);
138
139         c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
140         isl_int_set_si(v, -1);
141         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
142         isl_int_set_si(v, 1);
143         isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
144         bset = isl_basic_set_add_constraint(bset, c);
145
146         c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
147         isl_int_set_si(v, 1);
148         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
149         isl_int_set_si(v, -5);
150         isl_constraint_set_constant(c, v);
151         bset = isl_basic_set_add_constraint(bset, c);
152
153         isl_basic_set_free(bset);
154
155         isl_int_clear(v);
156 }
157
158 void test_dim(struct isl_ctx *ctx)
159 {
160         const char *str;
161         isl_map *map1, *map2;
162
163         map1 = isl_map_read_from_str(ctx,
164             "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
165         map1 = isl_map_add_dims(map1, isl_dim_in, 1);
166         map2 = isl_map_read_from_str(ctx,
167             "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
168         assert(isl_map_is_equal(map1, map2));
169         isl_map_free(map2);
170
171         map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
172         map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }", -1);
173         assert(isl_map_is_equal(map1, map2));
174
175         isl_map_free(map1);
176         isl_map_free(map2);
177
178         str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
179         map1 = isl_map_read_from_str(ctx, str, -1);
180         str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
181         map2 = isl_map_read_from_str(ctx, str, -1);
182         map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
183         assert(isl_map_is_equal(map1, map2));
184
185         isl_map_free(map1);
186         isl_map_free(map2);
187 }
188
189 void test_div(struct isl_ctx *ctx)
190 {
191         isl_int v;
192         int pos;
193         struct isl_dim *dim;
194         struct isl_div *div;
195         struct isl_basic_set *bset;
196         struct isl_constraint *c;
197
198         isl_int_init(v);
199
200         /* test 1 */
201         dim = isl_dim_set_alloc(ctx, 0, 1);
202         bset = isl_basic_set_universe(dim);
203
204         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
205         isl_int_set_si(v, -1);
206         isl_constraint_set_constant(c, v);
207         isl_int_set_si(v, 1);
208         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
209         div = isl_div_alloc(isl_basic_set_get_dim(bset));
210         c = isl_constraint_add_div(c, div, &pos);
211         isl_int_set_si(v, 3);
212         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
213         bset = isl_basic_set_add_constraint(bset, c);
214
215         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
216         isl_int_set_si(v, 1);
217         isl_constraint_set_constant(c, v);
218         isl_int_set_si(v, -1);
219         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
220         div = isl_div_alloc(isl_basic_set_get_dim(bset));
221         c = isl_constraint_add_div(c, div, &pos);
222         isl_int_set_si(v, 3);
223         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
224         bset = isl_basic_set_add_constraint(bset, c);
225
226         assert(bset && bset->n_div == 1);
227         isl_basic_set_free(bset);
228
229         /* test 2 */
230         dim = isl_dim_set_alloc(ctx, 0, 1);
231         bset = isl_basic_set_universe(dim);
232
233         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
234         isl_int_set_si(v, 1);
235         isl_constraint_set_constant(c, v);
236         isl_int_set_si(v, -1);
237         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
238         div = isl_div_alloc(isl_basic_set_get_dim(bset));
239         c = isl_constraint_add_div(c, div, &pos);
240         isl_int_set_si(v, 3);
241         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
242         bset = isl_basic_set_add_constraint(bset, c);
243
244         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
245         isl_int_set_si(v, -1);
246         isl_constraint_set_constant(c, v);
247         isl_int_set_si(v, 1);
248         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
249         div = isl_div_alloc(isl_basic_set_get_dim(bset));
250         c = isl_constraint_add_div(c, div, &pos);
251         isl_int_set_si(v, 3);
252         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
253         bset = isl_basic_set_add_constraint(bset, c);
254
255         assert(bset && bset->n_div == 1);
256         isl_basic_set_free(bset);
257
258         /* test 3 */
259         dim = isl_dim_set_alloc(ctx, 0, 1);
260         bset = isl_basic_set_universe(dim);
261
262         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
263         isl_int_set_si(v, 1);
264         isl_constraint_set_constant(c, v);
265         isl_int_set_si(v, -1);
266         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
267         div = isl_div_alloc(isl_basic_set_get_dim(bset));
268         c = isl_constraint_add_div(c, div, &pos);
269         isl_int_set_si(v, 3);
270         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
271         bset = isl_basic_set_add_constraint(bset, c);
272
273         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
274         isl_int_set_si(v, -3);
275         isl_constraint_set_constant(c, v);
276         isl_int_set_si(v, 1);
277         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
278         div = isl_div_alloc(isl_basic_set_get_dim(bset));
279         c = isl_constraint_add_div(c, div, &pos);
280         isl_int_set_si(v, 4);
281         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
282         bset = isl_basic_set_add_constraint(bset, c);
283
284         assert(bset && bset->n_div == 1);
285         isl_basic_set_free(bset);
286
287         /* test 4 */
288         dim = isl_dim_set_alloc(ctx, 0, 1);
289         bset = isl_basic_set_universe(dim);
290
291         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
292         isl_int_set_si(v, 2);
293         isl_constraint_set_constant(c, v);
294         isl_int_set_si(v, -1);
295         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
296         div = isl_div_alloc(isl_basic_set_get_dim(bset));
297         c = isl_constraint_add_div(c, div, &pos);
298         isl_int_set_si(v, 3);
299         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
300         bset = isl_basic_set_add_constraint(bset, c);
301
302         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
303         isl_int_set_si(v, -1);
304         isl_constraint_set_constant(c, v);
305         isl_int_set_si(v, 1);
306         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
307         div = isl_div_alloc(isl_basic_set_get_dim(bset));
308         c = isl_constraint_add_div(c, div, &pos);
309         isl_int_set_si(v, 6);
310         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
311         bset = isl_basic_set_add_constraint(bset, c);
312
313         assert(isl_basic_set_is_empty(bset));
314         isl_basic_set_free(bset);
315
316         /* test 5 */
317         dim = isl_dim_set_alloc(ctx, 0, 2);
318         bset = isl_basic_set_universe(dim);
319
320         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
321         isl_int_set_si(v, -1);
322         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
323         div = isl_div_alloc(isl_basic_set_get_dim(bset));
324         c = isl_constraint_add_div(c, div, &pos);
325         isl_int_set_si(v, 3);
326         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
327         bset = isl_basic_set_add_constraint(bset, c);
328
329         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
330         isl_int_set_si(v, 1);
331         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
332         isl_int_set_si(v, -3);
333         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
334         bset = isl_basic_set_add_constraint(bset, c);
335
336         assert(bset && bset->n_div == 0);
337         isl_basic_set_free(bset);
338
339         /* test 6 */
340         dim = isl_dim_set_alloc(ctx, 0, 2);
341         bset = isl_basic_set_universe(dim);
342
343         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
344         isl_int_set_si(v, -1);
345         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
346         div = isl_div_alloc(isl_basic_set_get_dim(bset));
347         c = isl_constraint_add_div(c, div, &pos);
348         isl_int_set_si(v, 6);
349         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
350         bset = isl_basic_set_add_constraint(bset, c);
351
352         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
353         isl_int_set_si(v, 1);
354         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
355         isl_int_set_si(v, -3);
356         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
357         bset = isl_basic_set_add_constraint(bset, c);
358
359         assert(bset && bset->n_div == 1);
360         isl_basic_set_free(bset);
361
362         /* test 7 */
363         /* This test is a bit tricky.  We set up an equality
364          *              a + 3b + 3c = 6 e0
365          * Normalization of divs creates _two_ divs
366          *              a = 3 e0
367          *              c - b - e0 = 2 e1
368          * Afterwards e0 is removed again because it has coefficient -1
369          * and we end up with the original equality and div again.
370          * Perhaps we can avoid the introduction of this temporary div.
371          */
372         dim = isl_dim_set_alloc(ctx, 0, 3);
373         bset = isl_basic_set_universe(dim);
374
375         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
376         isl_int_set_si(v, -1);
377         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
378         isl_int_set_si(v, -3);
379         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
380         isl_int_set_si(v, -3);
381         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
382         div = isl_div_alloc(isl_basic_set_get_dim(bset));
383         c = isl_constraint_add_div(c, div, &pos);
384         isl_int_set_si(v, 6);
385         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
386         bset = isl_basic_set_add_constraint(bset, c);
387
388         /* Test disabled for now */
389         /*
390         assert(bset && bset->n_div == 1);
391         */
392         isl_basic_set_free(bset);
393
394         /* test 8 */
395         dim = isl_dim_set_alloc(ctx, 0, 4);
396         bset = isl_basic_set_universe(dim);
397
398         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
399         isl_int_set_si(v, -1);
400         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
401         isl_int_set_si(v, -3);
402         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
403         isl_int_set_si(v, -3);
404         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
405         div = isl_div_alloc(isl_basic_set_get_dim(bset));
406         c = isl_constraint_add_div(c, div, &pos);
407         isl_int_set_si(v, 6);
408         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
409         bset = isl_basic_set_add_constraint(bset, c);
410
411         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
412         isl_int_set_si(v, -1);
413         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
414         isl_int_set_si(v, 1);
415         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
416         isl_int_set_si(v, 1);
417         isl_constraint_set_constant(c, v);
418         bset = isl_basic_set_add_constraint(bset, c);
419
420         /* Test disabled for now */
421         /*
422         assert(bset && bset->n_div == 1);
423         */
424         isl_basic_set_free(bset);
425
426         /* test 9 */
427         dim = isl_dim_set_alloc(ctx, 0, 2);
428         bset = isl_basic_set_universe(dim);
429
430         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
431         isl_int_set_si(v, 1);
432         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
433         isl_int_set_si(v, -1);
434         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
435         div = isl_div_alloc(isl_basic_set_get_dim(bset));
436         c = isl_constraint_add_div(c, div, &pos);
437         isl_int_set_si(v, -2);
438         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
439         bset = isl_basic_set_add_constraint(bset, c);
440
441         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
442         isl_int_set_si(v, -1);
443         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
444         div = isl_div_alloc(isl_basic_set_get_dim(bset));
445         c = isl_constraint_add_div(c, div, &pos);
446         isl_int_set_si(v, 3);
447         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
448         isl_int_set_si(v, 2);
449         isl_constraint_set_constant(c, v);
450         bset = isl_basic_set_add_constraint(bset, c);
451
452         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
453
454         assert(!isl_basic_set_is_empty(bset));
455
456         isl_basic_set_free(bset);
457
458         /* test 10 */
459         dim = isl_dim_set_alloc(ctx, 0, 2);
460         bset = isl_basic_set_universe(dim);
461
462         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
463         isl_int_set_si(v, 1);
464         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
465         div = isl_div_alloc(isl_basic_set_get_dim(bset));
466         c = isl_constraint_add_div(c, div, &pos);
467         isl_int_set_si(v, -2);
468         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
469         bset = isl_basic_set_add_constraint(bset, c);
470
471         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
472
473         isl_basic_set_free(bset);
474
475         isl_int_clear(v);
476 }
477
478 void test_application_case(struct isl_ctx *ctx, const char *name)
479 {
480         char filename[PATH_MAX];
481         FILE *input;
482         int n;
483         struct isl_basic_set *bset1, *bset2;
484         struct isl_basic_map *bmap;
485
486         n = snprintf(filename, sizeof(filename),
487                         "%s/test_inputs/%s.omega", srcdir, name);
488         assert(n < sizeof(filename));
489         input = fopen(filename, "r");
490         assert(input);
491
492         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
493         bmap = isl_basic_map_read_from_file(ctx, input, 0);
494
495         bset1 = isl_basic_set_apply(bset1, bmap);
496
497         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
498
499         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
500
501         isl_basic_set_free(bset1);
502         isl_basic_set_free(bset2);
503
504         fclose(input);
505 }
506
507 void test_application(struct isl_ctx *ctx)
508 {
509         test_application_case(ctx, "application");
510         test_application_case(ctx, "application2");
511 }
512
513 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
514 {
515         char filename[PATH_MAX];
516         FILE *input;
517         int n;
518         struct isl_basic_set *bset1, *bset2;
519
520         n = snprintf(filename, sizeof(filename),
521                         "%s/test_inputs/%s.polylib", srcdir, name);
522         assert(n < sizeof(filename));
523         input = fopen(filename, "r");
524         assert(input);
525
526         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
527         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
528
529         bset1 = isl_basic_set_affine_hull(bset1);
530
531         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
532
533         isl_basic_set_free(bset1);
534         isl_basic_set_free(bset2);
535
536         fclose(input);
537 }
538
539 void test_affine_hull(struct isl_ctx *ctx)
540 {
541         test_affine_hull_case(ctx, "affine2");
542         test_affine_hull_case(ctx, "affine");
543         test_affine_hull_case(ctx, "affine3");
544 }
545
546 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
547 {
548         char filename[PATH_MAX];
549         FILE *input;
550         int n;
551         struct isl_basic_set *bset1, *bset2;
552         struct isl_set *set;
553
554         n = snprintf(filename, sizeof(filename),
555                         "%s/test_inputs/%s.polylib", srcdir, name);
556         assert(n < sizeof(filename));
557         input = fopen(filename, "r");
558         assert(input);
559
560         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
561         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
562
563         set = isl_basic_set_union(bset1, bset2);
564         bset1 = isl_set_convex_hull(set);
565
566         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
567
568         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
569
570         isl_basic_set_free(bset1);
571         isl_basic_set_free(bset2);
572
573         fclose(input);
574 }
575
576 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
577 {
578         const char *str1, *str2;
579         isl_set *set1, *set2;
580         int orig_convex = ctx->opt->convex;
581         ctx->opt->convex = convex;
582
583         test_convex_hull_case(ctx, "convex0");
584         test_convex_hull_case(ctx, "convex1");
585         test_convex_hull_case(ctx, "convex2");
586         test_convex_hull_case(ctx, "convex3");
587         test_convex_hull_case(ctx, "convex4");
588         test_convex_hull_case(ctx, "convex5");
589         test_convex_hull_case(ctx, "convex6");
590         test_convex_hull_case(ctx, "convex7");
591         test_convex_hull_case(ctx, "convex8");
592         test_convex_hull_case(ctx, "convex9");
593         test_convex_hull_case(ctx, "convex10");
594         test_convex_hull_case(ctx, "convex11");
595         test_convex_hull_case(ctx, "convex12");
596         test_convex_hull_case(ctx, "convex13");
597         test_convex_hull_case(ctx, "convex14");
598         test_convex_hull_case(ctx, "convex15");
599
600         str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
601                "(i0 = 1 and i1 = 0 and i2 = 1) or "
602                "(i0 = 0 and i1 = 0 and i2 = 0) }";
603         str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
604         set1 = isl_set_read_from_str(ctx, str1, -1);
605         set2 = isl_set_read_from_str(ctx, str2, -1);
606         set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
607         assert(isl_set_is_equal(set1, set2));
608         isl_set_free(set1);
609         isl_set_free(set2);
610
611         ctx->opt->convex = orig_convex;
612 }
613
614 void test_convex_hull(struct isl_ctx *ctx)
615 {
616         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
617         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
618 }
619
620 void test_gist_case(struct isl_ctx *ctx, const char *name)
621 {
622         char filename[PATH_MAX];
623         FILE *input;
624         int n;
625         struct isl_basic_set *bset1, *bset2;
626
627         n = snprintf(filename, sizeof(filename),
628                         "%s/test_inputs/%s.polylib", srcdir, name);
629         assert(n < sizeof(filename));
630         input = fopen(filename, "r");
631         assert(input);
632
633         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
634         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
635
636         bset1 = isl_basic_set_gist(bset1, bset2);
637
638         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
639
640         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
641
642         isl_basic_set_free(bset1);
643         isl_basic_set_free(bset2);
644
645         fclose(input);
646 }
647
648 void test_gist(struct isl_ctx *ctx)
649 {
650         test_gist_case(ctx, "gist1");
651 }
652
653 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
654 {
655         isl_set *set, *set2;
656
657         set = isl_set_read_from_str(ctx, str, -1);
658         set = isl_set_coalesce(set);
659         set2 = isl_set_read_from_str(ctx, str, -1);
660         assert(isl_set_is_equal(set, set2));
661         if (check_one)
662                 assert(set && set->n == 1);
663         isl_set_free(set);
664         isl_set_free(set2);
665 }
666
667 void test_coalesce(struct isl_ctx *ctx)
668 {
669         const char *str;
670         struct isl_set *set, *set2;
671         struct isl_map *map, *map2;
672
673         set = isl_set_read_from_str(ctx,
674                 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
675                        "y >= x & x >= 2 & 5 >= y }", -1);
676         set = isl_set_coalesce(set);
677         assert(set && set->n == 1);
678         isl_set_free(set);
679
680         set = isl_set_read_from_str(ctx,
681                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
682                        "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
683         set = isl_set_coalesce(set);
684         assert(set && set->n == 1);
685         isl_set_free(set);
686
687         set = isl_set_read_from_str(ctx,
688                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
689                        "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
690         set = isl_set_coalesce(set);
691         assert(set && set->n == 2);
692         isl_set_free(set);
693
694         set = isl_set_read_from_str(ctx,
695                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
696                        "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
697         set = isl_set_coalesce(set);
698         assert(set && set->n == 1);
699         isl_set_free(set);
700
701         set = isl_set_read_from_str(ctx,
702                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
703                        "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
704         set = isl_set_coalesce(set);
705         assert(set && set->n == 2);
706         isl_set_free(set);
707
708         set = isl_set_read_from_str(ctx,
709                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
710                        "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
711         set = isl_set_coalesce(set);
712         assert(set && set->n == 2);
713         isl_set_free(set);
714
715         set = isl_set_read_from_str(ctx,
716                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
717                        "y >= 0 & x = 6 & y <= 6}", -1);
718         set = isl_set_coalesce(set);
719         assert(set && set->n == 1);
720         isl_set_free(set);
721
722         set = isl_set_read_from_str(ctx,
723                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
724                        "y >= 0 & x = 7 & y <= 6}", -1);
725         set = isl_set_coalesce(set);
726         assert(set && set->n == 2);
727         isl_set_free(set);
728
729         set = isl_set_read_from_str(ctx,
730                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
731                        "y >= 0 & x = 6 & y <= 5}", -1);
732         set = isl_set_coalesce(set);
733         assert(set && set->n == 1);
734         set2 = isl_set_read_from_str(ctx,
735                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
736                        "y >= 0 & x = 6 & y <= 5}", -1);
737         assert(isl_set_is_equal(set, set2));
738         isl_set_free(set);
739         isl_set_free(set2);
740
741         set = isl_set_read_from_str(ctx,
742                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
743                        "y >= 0 & x = 6 & y <= 7}", -1);
744         set = isl_set_coalesce(set);
745         assert(set && set->n == 2);
746         isl_set_free(set);
747
748         set = isl_set_read_from_str(ctx,
749                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
750         set = isl_set_coalesce(set);
751         assert(set && set->n == 1);
752         set2 = isl_set_read_from_str(ctx,
753                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
754         assert(isl_set_is_equal(set, set2));
755         isl_set_free(set);
756         isl_set_free(set2);
757
758         set = isl_set_read_from_str(ctx,
759                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
760         set = isl_set_coalesce(set);
761         set2 = isl_set_read_from_str(ctx,
762                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
763         assert(isl_set_is_equal(set, set2));
764         isl_set_free(set);
765         isl_set_free(set2);
766
767         set = isl_set_read_from_str(ctx,
768                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
769                                 "2 <= i and i <= n }", -1);
770         set = isl_set_coalesce(set);
771         assert(set && set->n == 1);
772         set2 = isl_set_read_from_str(ctx,
773                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
774                                 "2 <= i and i <= n }", -1);
775         assert(isl_set_is_equal(set, set2));
776         isl_set_free(set);
777         isl_set_free(set2);
778
779         map = isl_map_read_from_str(ctx,
780                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
781                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
782                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
783                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
784                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
785                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
786                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
787                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
788                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
789                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
790                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
791                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
792                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
793                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
794                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
795                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
796                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
797                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
798         map = isl_map_coalesce(map);
799         map2 = isl_map_read_from_str(ctx,
800                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
801                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
802                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
803                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
804                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
805                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
806                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
807                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
808                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
809                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
810                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
811                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
812                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
813                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
814                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
815                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
816                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
817                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
818         assert(isl_map_is_equal(map, map2));
819         isl_map_free(map);
820         isl_map_free(map2);
821
822         str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
823               "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
824               "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
825               "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
826         map = isl_map_read_from_str(ctx, str, -1);
827         map = isl_map_coalesce(map);
828         map2 = isl_map_read_from_str(ctx, str, -1);
829         assert(isl_map_is_equal(map, map2));
830         isl_map_free(map);
831         isl_map_free(map2);
832
833         str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
834           "[o0, o1, o2, o3, o4, o5, o6] : "
835           "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
836           "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
837           "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
838           "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
839           "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
840           "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
841           "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
842           "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
843           "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
844           "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
845           "o6 >= i3 + i6 - o3 and M >= 0 and "
846           "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
847           "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
848         map = isl_map_read_from_str(ctx, str, -1);
849         map = isl_map_coalesce(map);
850         map2 = isl_map_read_from_str(ctx, str, -1);
851         assert(isl_map_is_equal(map, map2));
852         isl_map_free(map);
853         isl_map_free(map2);
854
855         str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
856                 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
857                 "(o0 = 0 and M >= 2 and N >= 3) or "
858                 "(M = 0 and o0 = 0 and N >= 3) }";
859         map = isl_map_read_from_str(ctx, str, -1);
860         map = isl_map_coalesce(map);
861         map2 = isl_map_read_from_str(ctx, str, -1);
862         assert(isl_map_is_equal(map, map2));
863         isl_map_free(map);
864         isl_map_free(map2);
865
866         str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
867                 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
868                 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
869                 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
870         map = isl_map_read_from_str(ctx, str, -1);
871         map = isl_map_coalesce(map);
872         map2 = isl_map_read_from_str(ctx, str, -1);
873         assert(isl_map_is_equal(map, map2));
874         isl_map_free(map);
875         isl_map_free(map2);
876
877         test_coalesce_set(ctx,
878                 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
879                                 "(i1 = M and M >= 1) }", 0);
880         test_coalesce_set(ctx,
881                 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
882         test_coalesce_set(ctx,
883                 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
884                 "(y = 3 and x = 1) }", 1);
885         test_coalesce_set(ctx,
886                 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
887                 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
888                 "i1 <= M and i3 <= M and i4 <= M) or "
889                 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
890                 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
891                 "i4 <= -1 + M) }", 1);
892         test_coalesce_set(ctx,
893                 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
894                 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
895         test_coalesce_set(ctx,
896                 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
897                         "-x - y + 1 >= 0 and -3 <= z <= 3;"
898                 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
899                         "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
900         test_coalesce_set(ctx,
901                 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
902         test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
903         test_coalesce_set(ctx,
904                 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
905         test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
906 }
907
908 void test_closure(struct isl_ctx *ctx)
909 {
910         const char *str;
911         isl_set *dom;
912         isl_map *up, *right;
913         isl_map *map, *map2;
914         int exact;
915
916         /* COCOA example 1 */
917         map = isl_map_read_from_str(ctx,
918                 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
919                         "1 <= i and i < n and 1 <= j and j < n or "
920                         "i2 = i + 1 and j2 = j - 1 and "
921                         "1 <= i and i < n and 2 <= j and j <= n }", -1);
922         map = isl_map_power(map, 1, &exact);
923         assert(exact);
924         isl_map_free(map);
925
926         /* COCOA example 1 */
927         map = isl_map_read_from_str(ctx,
928                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
929                         "1 <= i and i < n and 1 <= j and j < n or "
930                         "i2 = i + 1 and j2 = j - 1 and "
931                         "1 <= i and i < n and 2 <= j and j <= n }", -1);
932         map = isl_map_transitive_closure(map, &exact);
933         assert(exact);
934         map2 = isl_map_read_from_str(ctx,
935                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
936                         "1 <= i and i < n and 1 <= j and j <= n and "
937                         "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
938                         "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
939                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
940         assert(isl_map_is_equal(map, map2));
941         isl_map_free(map2);
942         isl_map_free(map);
943
944         map = isl_map_read_from_str(ctx,
945                 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
946                                      " 0 <= y and y <= n }", -1);
947         map = isl_map_transitive_closure(map, &exact);
948         map2 = isl_map_read_from_str(ctx,
949                 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
950                                      " 0 <= y and y <= n }", -1);
951         assert(isl_map_is_equal(map, map2));
952         isl_map_free(map2);
953         isl_map_free(map);
954
955         /* COCOA example 2 */
956         map = isl_map_read_from_str(ctx,
957                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
958                         "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
959                         "i2 = i + 2 and j2 = j - 2 and "
960                         "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
961         map = isl_map_transitive_closure(map, &exact);
962         assert(exact);
963         map2 = isl_map_read_from_str(ctx,
964                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
965                         "1 <= i and i < n - 1 and 1 <= j and j <= n and "
966                         "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
967                         "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
968                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
969         assert(isl_map_is_equal(map, map2));
970         isl_map_free(map);
971         isl_map_free(map2);
972
973         /* COCOA Fig.2 left */
974         map = isl_map_read_from_str(ctx,
975                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
976                         "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
977                         "j <= n or "
978                         "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
979                         "j <= 2 i - 3 and j <= n - 2 or "
980                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
981                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
982         map = isl_map_transitive_closure(map, &exact);
983         assert(exact);
984         isl_map_free(map);
985
986         /* COCOA Fig.2 right */
987         map = isl_map_read_from_str(ctx,
988                 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
989                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
990                         "j <= n or "
991                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
992                         "j <= 2 i - 4 and j <= n - 3 or "
993                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
994                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
995         map = isl_map_power(map, 1, &exact);
996         assert(exact);
997         isl_map_free(map);
998
999         /* COCOA Fig.2 right */
1000         map = isl_map_read_from_str(ctx,
1001                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1002                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1003                         "j <= n or "
1004                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1005                         "j <= 2 i - 4 and j <= n - 3 or "
1006                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1007                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
1008         map = isl_map_transitive_closure(map, &exact);
1009         assert(exact);
1010         map2 = isl_map_read_from_str(ctx,
1011                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1012                         "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1013                         "j <= n and 3 + i + 2 j <= 3 n and "
1014                         "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1015                         "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1016                         "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1017                         "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1018                         "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
1019         assert(isl_map_is_equal(map, map2));
1020         isl_map_free(map2);
1021         isl_map_free(map);
1022
1023         /* COCOA Fig.1 right */
1024         dom = isl_set_read_from_str(ctx,
1025                 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1026                         "2 x - 3 y + 3 >= 0 }", -1);
1027         right = isl_map_read_from_str(ctx,
1028                 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
1029         up = isl_map_read_from_str(ctx,
1030                 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
1031         right = isl_map_intersect_domain(right, isl_set_copy(dom));
1032         right = isl_map_intersect_range(right, isl_set_copy(dom));
1033         up = isl_map_intersect_domain(up, isl_set_copy(dom));
1034         up = isl_map_intersect_range(up, dom);
1035         map = isl_map_union(up, right);
1036         map = isl_map_transitive_closure(map, &exact);
1037         assert(exact);
1038         map2 = isl_map_read_from_str(ctx,
1039                 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1040                 "  [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }", -1);
1041         assert(isl_map_is_equal(map, map2));
1042         isl_map_free(map2);
1043         isl_map_free(map);
1044
1045         /* COCOA Theorem 1 counter example */
1046         map = isl_map_read_from_str(ctx,
1047                 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1048                         "i2 = 1 and j2 = j or "
1049                         "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
1050         map = isl_map_transitive_closure(map, &exact);
1051         assert(exact);
1052         isl_map_free(map);
1053
1054         map = isl_map_read_from_str(ctx,
1055                 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1056                         "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1057                         "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1058                         "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
1059         map = isl_map_transitive_closure(map, &exact);
1060         assert(exact);
1061         isl_map_free(map);
1062
1063         /* Kelly et al 1996, fig 12 */
1064         map = isl_map_read_from_str(ctx,
1065                 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1066                         "1 <= i,j,j+1 <= n or "
1067                         "j = n and j2 = 1 and i2 = i + 1 and "
1068                         "1 <= i,i+1 <= n }", -1);
1069         map = isl_map_transitive_closure(map, &exact);
1070         assert(exact);
1071         map2 = isl_map_read_from_str(ctx,
1072                 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1073                         "1 <= i <= n and i = i2 or "
1074                         "1 <= i < i2 <= n and 1 <= j <= n and "
1075                         "1 <= j2 <= n }", -1);
1076         assert(isl_map_is_equal(map, map2));
1077         isl_map_free(map2);
1078         isl_map_free(map);
1079
1080         /* Omega's closure4 */
1081         map = isl_map_read_from_str(ctx,
1082                 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1083                         "1 <= x,y <= 10 or "
1084                         "x2 = x + 1 and y2 = y and "
1085                         "1 <= x <= 20 && 5 <= y <= 15 }", -1);
1086         map = isl_map_transitive_closure(map, &exact);
1087         assert(exact);
1088         isl_map_free(map);
1089
1090         map = isl_map_read_from_str(ctx,
1091                 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
1092         map = isl_map_transitive_closure(map, &exact);
1093         assert(!exact);
1094         map2 = isl_map_read_from_str(ctx,
1095                 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
1096         assert(isl_map_is_equal(map, map2));
1097         isl_map_free(map);
1098         isl_map_free(map2);
1099
1100         str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1101             "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1102             "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1103             "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1104         map = isl_map_read_from_str(ctx, str, -1);
1105         map = isl_map_transitive_closure(map, &exact);
1106         assert(exact);
1107         map2 = isl_map_read_from_str(ctx, str, -1);
1108         assert(isl_map_is_equal(map, map2));
1109         isl_map_free(map);
1110         isl_map_free(map2);
1111
1112         str = "{[0] -> [1]; [2] -> [3]}";
1113         map = isl_map_read_from_str(ctx, str, -1);
1114         map = isl_map_transitive_closure(map, &exact);
1115         assert(exact);
1116         map2 = isl_map_read_from_str(ctx, str, -1);
1117         assert(isl_map_is_equal(map, map2));
1118         isl_map_free(map);
1119         isl_map_free(map2);
1120 }
1121
1122 void test_lex(struct isl_ctx *ctx)
1123 {
1124         isl_dim *dim;
1125         isl_map *map;
1126
1127         dim = isl_dim_alloc(ctx, 0, 0, 0);
1128         map = isl_map_lex_le(dim);
1129         assert(!isl_map_is_empty(map));
1130         isl_map_free(map);
1131 }
1132
1133 void test_lexmin(struct isl_ctx *ctx)
1134 {
1135         const char *str;
1136         isl_map *map, *map2;
1137         isl_set *set;
1138         isl_set *set2;
1139
1140         str = "[p0, p1] -> { [] -> [] : "
1141             "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1142             "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1143             "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1144             "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1145             "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1146             "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1147             "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1148             "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1149             "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1150             "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1151             "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1152         map = isl_map_read_from_str(ctx, str, -1);
1153         map = isl_map_lexmin(map);
1154         isl_map_free(map);
1155
1156         str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1157             "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1158         set = isl_set_read_from_str(ctx, str, -1);
1159         set = isl_set_lexmax(set);
1160         str = "[C] -> { [obj,a,b,c] : C = 8 }";
1161         set2 = isl_set_read_from_str(ctx, str, -1);
1162         set = isl_set_intersect(set, set2);
1163         assert(!isl_set_is_empty(set));
1164         isl_set_free(set);
1165
1166         str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1167         map = isl_map_read_from_str(ctx, str, -1);
1168         map = isl_map_lexmin(map);
1169         str = "{ [x] -> [5] : 6 <= x <= 8; "
1170                 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1171         map2 = isl_map_read_from_str(ctx, str, -1);
1172         assert(isl_map_is_equal(map, map2));
1173         isl_map_free(map);
1174         isl_map_free(map2);
1175
1176         str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1177         map = isl_map_read_from_str(ctx, str, -1);
1178         map2 = isl_map_copy(map);
1179         map = isl_map_lexmin(map);
1180         assert(isl_map_is_equal(map, map2));
1181         isl_map_free(map);
1182         isl_map_free(map2);
1183
1184         str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1185         map = isl_map_read_from_str(ctx, str, -1);
1186         map = isl_map_lexmin(map);
1187         str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1188                 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1189                 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1190                 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1191         map2 = isl_map_read_from_str(ctx, str, -1);
1192         assert(isl_map_is_equal(map, map2));
1193         isl_map_free(map);
1194         isl_map_free(map2);
1195 }
1196
1197 struct must_may {
1198         isl_map *must;
1199         isl_map *may;
1200 };
1201
1202 static int collect_must_may(__isl_take isl_map *dep, int must,
1203         void *dep_user, void *user)
1204 {
1205         struct must_may *mm = (struct must_may *)user;
1206
1207         if (must)
1208                 mm->must = isl_map_union(mm->must, dep);
1209         else
1210                 mm->may = isl_map_union(mm->may, dep);
1211
1212         return 0;
1213 }
1214
1215 static int common_space(void *first, void *second)
1216 {
1217         int depth = *(int *)first;
1218         return 2 * depth;
1219 }
1220
1221 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1222 {
1223         isl_map *map2;
1224         int equal;
1225
1226         if (!map)
1227                 return -1;
1228
1229         map2 = isl_map_read_from_str(map->ctx, str, -1);
1230         equal = isl_map_is_equal(map, map2);
1231         isl_map_free(map2);
1232
1233         return equal;
1234 }
1235
1236 void test_dep(struct isl_ctx *ctx)
1237 {
1238         const char *str;
1239         isl_dim *dim;
1240         isl_map *map;
1241         isl_access_info *ai;
1242         isl_flow *flow;
1243         int depth;
1244         struct must_may mm;
1245
1246         depth = 3;
1247
1248         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1249         map = isl_map_read_from_str(ctx, str, -1);
1250         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1251
1252         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1253         map = isl_map_read_from_str(ctx, str, -1);
1254         ai = isl_access_info_add_source(ai, map, 1, &depth);
1255
1256         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1257         map = isl_map_read_from_str(ctx, str, -1);
1258         ai = isl_access_info_add_source(ai, map, 1, &depth);
1259
1260         flow = isl_access_info_compute_flow(ai);
1261         dim = isl_dim_alloc(ctx, 0, 3, 3);
1262         mm.must = isl_map_empty(isl_dim_copy(dim));
1263         mm.may = isl_map_empty(dim);
1264
1265         isl_flow_foreach(flow, collect_must_may, &mm);
1266
1267         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1268               "  [1,10,0] -> [2,5,0] }";
1269         assert(map_is_equal(mm.must, str));
1270         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1271         assert(map_is_equal(mm.may, str));
1272
1273         isl_map_free(mm.must);
1274         isl_map_free(mm.may);
1275         isl_flow_free(flow);
1276
1277
1278         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1279         map = isl_map_read_from_str(ctx, str, -1);
1280         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1281
1282         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1283         map = isl_map_read_from_str(ctx, str, -1);
1284         ai = isl_access_info_add_source(ai, map, 1, &depth);
1285
1286         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1287         map = isl_map_read_from_str(ctx, str, -1);
1288         ai = isl_access_info_add_source(ai, map, 0, &depth);
1289
1290         flow = isl_access_info_compute_flow(ai);
1291         dim = isl_dim_alloc(ctx, 0, 3, 3);
1292         mm.must = isl_map_empty(isl_dim_copy(dim));
1293         mm.may = isl_map_empty(dim);
1294
1295         isl_flow_foreach(flow, collect_must_may, &mm);
1296
1297         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1298         assert(map_is_equal(mm.must, str));
1299         str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1300         assert(map_is_equal(mm.may, str));
1301
1302         isl_map_free(mm.must);
1303         isl_map_free(mm.may);
1304         isl_flow_free(flow);
1305
1306
1307         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1308         map = isl_map_read_from_str(ctx, str, -1);
1309         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1310
1311         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1312         map = isl_map_read_from_str(ctx, str, -1);
1313         ai = isl_access_info_add_source(ai, map, 0, &depth);
1314
1315         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1316         map = isl_map_read_from_str(ctx, str, -1);
1317         ai = isl_access_info_add_source(ai, map, 0, &depth);
1318
1319         flow = isl_access_info_compute_flow(ai);
1320         dim = isl_dim_alloc(ctx, 0, 3, 3);
1321         mm.must = isl_map_empty(isl_dim_copy(dim));
1322         mm.may = isl_map_empty(dim);
1323
1324         isl_flow_foreach(flow, collect_must_may, &mm);
1325
1326         str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1327               "  [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1328         assert(map_is_equal(mm.may, str));
1329         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1330         assert(map_is_equal(mm.must, str));
1331
1332         isl_map_free(mm.must);
1333         isl_map_free(mm.may);
1334         isl_flow_free(flow);
1335
1336
1337         str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1338         map = isl_map_read_from_str(ctx, str, -1);
1339         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1340
1341         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1342         map = isl_map_read_from_str(ctx, str, -1);
1343         ai = isl_access_info_add_source(ai, map, 0, &depth);
1344
1345         str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1346         map = isl_map_read_from_str(ctx, str, -1);
1347         ai = isl_access_info_add_source(ai, map, 0, &depth);
1348
1349         flow = isl_access_info_compute_flow(ai);
1350         dim = isl_dim_alloc(ctx, 0, 3, 3);
1351         mm.must = isl_map_empty(isl_dim_copy(dim));
1352         mm.may = isl_map_empty(dim);
1353
1354         isl_flow_foreach(flow, collect_must_may, &mm);
1355
1356         str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1357               "  [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1358         assert(map_is_equal(mm.may, str));
1359         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1360         assert(map_is_equal(mm.must, str));
1361
1362         isl_map_free(mm.must);
1363         isl_map_free(mm.may);
1364         isl_flow_free(flow);
1365
1366
1367         str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1368         map = isl_map_read_from_str(ctx, str, -1);
1369         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1370
1371         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1372         map = isl_map_read_from_str(ctx, str, -1);
1373         ai = isl_access_info_add_source(ai, map, 0, &depth);
1374
1375         str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1376         map = isl_map_read_from_str(ctx, str, -1);
1377         ai = isl_access_info_add_source(ai, map, 0, &depth);
1378
1379         flow = isl_access_info_compute_flow(ai);
1380         dim = isl_dim_alloc(ctx, 0, 3, 3);
1381         mm.must = isl_map_empty(isl_dim_copy(dim));
1382         mm.may = isl_map_empty(dim);
1383
1384         isl_flow_foreach(flow, collect_must_may, &mm);
1385
1386         str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1387               "  [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1388         assert(map_is_equal(mm.may, str));
1389         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1390         assert(map_is_equal(mm.must, str));
1391
1392         isl_map_free(mm.must);
1393         isl_map_free(mm.may);
1394         isl_flow_free(flow);
1395
1396
1397         depth = 5;
1398
1399         str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1400         map = isl_map_read_from_str(ctx, str, -1);
1401         ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1402
1403         str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1404         map = isl_map_read_from_str(ctx, str, -1);
1405         ai = isl_access_info_add_source(ai, map, 1, &depth);
1406
1407         flow = isl_access_info_compute_flow(ai);
1408         dim = isl_dim_alloc(ctx, 0, 5, 5);
1409         mm.must = isl_map_empty(isl_dim_copy(dim));
1410         mm.may = isl_map_empty(dim);
1411
1412         isl_flow_foreach(flow, collect_must_may, &mm);
1413
1414         str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1415         assert(map_is_equal(mm.must, str));
1416         str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1417         assert(map_is_equal(mm.may, str));
1418
1419         isl_map_free(mm.must);
1420         isl_map_free(mm.may);
1421         isl_flow_free(flow);
1422 }
1423
1424 void test_sv(struct isl_ctx *ctx)
1425 {
1426         const char *str;
1427         isl_map *map;
1428
1429         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1430         map = isl_map_read_from_str(ctx, str, -1);
1431         assert(isl_map_is_single_valued(map));
1432         isl_map_free(map);
1433
1434         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1435         map = isl_map_read_from_str(ctx, str, -1);
1436         assert(!isl_map_is_single_valued(map));
1437         isl_map_free(map);
1438 }
1439
1440 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1441 {
1442         isl_map *map;
1443
1444         map = isl_map_read_from_str(ctx, str, -1);
1445         if (bijective)
1446                 assert(isl_map_is_bijective(map));
1447         else
1448                 assert(!isl_map_is_bijective(map));
1449         isl_map_free(map);
1450 }
1451
1452 void test_bijective(struct isl_ctx *ctx)
1453 {
1454         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1455         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1456         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1457         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1458         test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1459         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1460         test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1461         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1462         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1463         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1464         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1465         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1466         test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1467 }
1468
1469 void test_pwqp(struct isl_ctx *ctx)
1470 {
1471         const char *str;
1472         isl_set *set;
1473         isl_pw_qpolynomial *pwqp1, *pwqp2;
1474
1475         str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1476         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1477
1478         pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1479                                                 isl_dim_set, 1, 1);
1480
1481         str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1482         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1483
1484         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1485
1486         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1487
1488         isl_pw_qpolynomial_free(pwqp1);
1489
1490         str = "{ [i] -> i }";
1491         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1492         str = "{ [k] : exists a : k = 2a }";
1493         set = isl_set_read_from_str(ctx, str, 0);
1494         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1495         str = "{ [i] -> i }";
1496         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1497
1498         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1499
1500         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1501
1502         isl_pw_qpolynomial_free(pwqp1);
1503
1504         str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1505         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1506         str = "{ [10] }";
1507         set = isl_set_read_from_str(ctx, str, 0);
1508         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1509         str = "{ [i] -> 16 }";
1510         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1511
1512         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1513
1514         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1515
1516         isl_pw_qpolynomial_free(pwqp1);
1517
1518         str = "{ [i] -> ([(i)/2]) }";
1519         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1520         str = "{ [k] : exists a : k = 2a+1 }";
1521         set = isl_set_read_from_str(ctx, str, 0);
1522         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1523         str = "{ [i] -> -1/2 + 1/2 * i }";
1524         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1525
1526         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1527
1528         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1529
1530         isl_pw_qpolynomial_free(pwqp1);
1531
1532         str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1533         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1534         str = "{ [i] -> ([(2 * [i/2])/5]) }";
1535         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1536
1537         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1538
1539         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1540
1541         isl_pw_qpolynomial_free(pwqp1);
1542
1543         str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1544         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1545         str = "{ [x] -> x }";
1546         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1547
1548         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1549
1550         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1551
1552         isl_pw_qpolynomial_free(pwqp1);
1553 }
1554
1555 void test_split_periods(isl_ctx *ctx)
1556 {
1557         const char *str;
1558         isl_pw_qpolynomial *pwqp;
1559
1560         str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1561                 "U + 2V + 3 >= 0 and - U -2V  >= 0 and - U + 10 >= 0 and "
1562                 "U  >= 0; [U,V] -> U^2 : U >= 100 }";
1563         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1564
1565         pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1566         assert(pwqp);
1567
1568         isl_pw_qpolynomial_free(pwqp);
1569 }
1570
1571 void test_union(isl_ctx *ctx)
1572 {
1573         const char *str;
1574         isl_union_set *uset1, *uset2;
1575         isl_union_map *umap1, *umap2;
1576
1577         str = "{ [i] : 0 <= i <= 1 }";
1578         uset1 = isl_union_set_read_from_str(ctx, str);
1579         str = "{ [1] -> [0] }";
1580         umap1 = isl_union_map_read_from_str(ctx, str);
1581
1582         umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1583         assert(isl_union_map_is_equal(umap1, umap2));
1584
1585         isl_union_map_free(umap1);
1586         isl_union_map_free(umap2);
1587
1588         str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1589         umap1 = isl_union_map_read_from_str(ctx, str);
1590         str = "{ A[i]; B[i] }";
1591         uset1 = isl_union_set_read_from_str(ctx, str);
1592
1593         uset2 = isl_union_map_domain(umap1);
1594
1595         assert(isl_union_set_is_equal(uset1, uset2));
1596
1597         isl_union_set_free(uset1);
1598         isl_union_set_free(uset2);
1599 }
1600
1601 void test_bound(isl_ctx *ctx)
1602 {
1603         const char *str;
1604         isl_pw_qpolynomial *pwqp;
1605         isl_pw_qpolynomial_fold *pwf;
1606
1607         str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1608         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1609         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1610         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 4);
1611         isl_pw_qpolynomial_fold_free(pwf);
1612 }
1613
1614 void test_lift(isl_ctx *ctx)
1615 {
1616         const char *str;
1617         isl_basic_map *bmap;
1618         isl_basic_set *bset;
1619
1620         str = "{ [i0] : exists e0 : i0 = 4e0 }";
1621         bset = isl_basic_set_read_from_str(ctx, str, 0);
1622         bset = isl_basic_set_lift(bset);
1623         bmap = isl_basic_map_from_range(bset);
1624         bset = isl_basic_map_domain(bmap);
1625         isl_basic_set_free(bset);
1626 }
1627
1628 void test_subset(isl_ctx *ctx)
1629 {
1630         const char *str;
1631         isl_set *set1, *set2;
1632
1633         str = "{ [112, 0] }";
1634         set1 = isl_set_read_from_str(ctx, str, 0);
1635         str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1636                 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1637                 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1638         set2 = isl_set_read_from_str(ctx, str, 0);
1639         assert(isl_set_is_subset(set1, set2));
1640         isl_set_free(set1);
1641         isl_set_free(set2);
1642 }
1643
1644 int main()
1645 {
1646         struct isl_ctx *ctx;
1647
1648         srcdir = getenv("srcdir");
1649         assert(srcdir);
1650
1651         ctx = isl_ctx_alloc();
1652         test_subset(ctx);
1653         test_lift(ctx);
1654         test_bound(ctx);
1655         test_union(ctx);
1656         test_split_periods(ctx);
1657         test_parse(ctx);
1658         test_pwqp(ctx);
1659         test_lex(ctx);
1660         test_sv(ctx);
1661         test_bijective(ctx);
1662         test_dep(ctx);
1663         test_read(ctx);
1664         test_bounded(ctx);
1665         test_construction(ctx);
1666         test_dim(ctx);
1667         test_div(ctx);
1668         test_application(ctx);
1669         test_affine_hull(ctx);
1670         test_convex_hull(ctx);
1671         test_gist(ctx);
1672         test_coalesce(ctx);
1673         test_closure(ctx);
1674         test_lexmin(ctx);
1675         isl_ctx_free(ctx);
1676         return 0;
1677 }