isl_stream_read_map: accept modulo expressions
[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(struct isl_ctx *ctx)
577 {
578         const char *str1, *str2;
579         isl_set *set1, *set2;
580
581         test_convex_hull_case(ctx, "convex0");
582         test_convex_hull_case(ctx, "convex1");
583         test_convex_hull_case(ctx, "convex2");
584         test_convex_hull_case(ctx, "convex3");
585         test_convex_hull_case(ctx, "convex4");
586         test_convex_hull_case(ctx, "convex5");
587         test_convex_hull_case(ctx, "convex6");
588         test_convex_hull_case(ctx, "convex7");
589         test_convex_hull_case(ctx, "convex8");
590         test_convex_hull_case(ctx, "convex9");
591         test_convex_hull_case(ctx, "convex10");
592         test_convex_hull_case(ctx, "convex11");
593         test_convex_hull_case(ctx, "convex12");
594         test_convex_hull_case(ctx, "convex13");
595         test_convex_hull_case(ctx, "convex14");
596         test_convex_hull_case(ctx, "convex15");
597
598         str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
599                "(i0 = 1 and i1 = 0 and i2 = 1) or "
600                "(i0 = 0 and i1 = 0 and i2 = 0) }";
601         str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
602         set1 = isl_set_read_from_str(ctx, str1, -1);
603         set2 = isl_set_read_from_str(ctx, str2, -1);
604         set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
605         assert(isl_set_is_equal(set1, set2));
606         isl_set_free(set1);
607         isl_set_free(set2);
608 }
609
610 void test_gist_case(struct isl_ctx *ctx, const char *name)
611 {
612         char filename[PATH_MAX];
613         FILE *input;
614         int n;
615         struct isl_basic_set *bset1, *bset2;
616
617         n = snprintf(filename, sizeof(filename),
618                         "%s/test_inputs/%s.polylib", srcdir, name);
619         assert(n < sizeof(filename));
620         input = fopen(filename, "r");
621         assert(input);
622
623         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
624         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
625
626         bset1 = isl_basic_set_gist(bset1, bset2);
627
628         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
629
630         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
631
632         isl_basic_set_free(bset1);
633         isl_basic_set_free(bset2);
634
635         fclose(input);
636 }
637
638 void test_gist(struct isl_ctx *ctx)
639 {
640         test_gist_case(ctx, "gist1");
641 }
642
643 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
644 {
645         isl_set *set, *set2;
646
647         set = isl_set_read_from_str(ctx, str, -1);
648         set = isl_set_coalesce(set);
649         set2 = isl_set_read_from_str(ctx, str, -1);
650         assert(isl_set_is_equal(set, set2));
651         if (check_one)
652                 assert(set && set->n == 1);
653         isl_set_free(set);
654         isl_set_free(set2);
655 }
656
657 void test_coalesce(struct isl_ctx *ctx)
658 {
659         const char *str;
660         struct isl_set *set, *set2;
661         struct isl_map *map, *map2;
662
663         set = isl_set_read_from_str(ctx,
664                 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
665                        "y >= x & x >= 2 & 5 >= y }", -1);
666         set = isl_set_coalesce(set);
667         assert(set && set->n == 1);
668         isl_set_free(set);
669
670         set = isl_set_read_from_str(ctx,
671                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
672                        "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
673         set = isl_set_coalesce(set);
674         assert(set && set->n == 1);
675         isl_set_free(set);
676
677         set = isl_set_read_from_str(ctx,
678                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
679                        "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
680         set = isl_set_coalesce(set);
681         assert(set && set->n == 2);
682         isl_set_free(set);
683
684         set = isl_set_read_from_str(ctx,
685                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
686                        "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
687         set = isl_set_coalesce(set);
688         assert(set && set->n == 1);
689         isl_set_free(set);
690
691         set = isl_set_read_from_str(ctx,
692                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
693                        "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
694         set = isl_set_coalesce(set);
695         assert(set && set->n == 2);
696         isl_set_free(set);
697
698         set = isl_set_read_from_str(ctx,
699                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
700                        "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
701         set = isl_set_coalesce(set);
702         assert(set && set->n == 2);
703         isl_set_free(set);
704
705         set = isl_set_read_from_str(ctx,
706                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
707                        "y >= 0 & x = 6 & y <= 6}", -1);
708         set = isl_set_coalesce(set);
709         assert(set && set->n == 1);
710         isl_set_free(set);
711
712         set = isl_set_read_from_str(ctx,
713                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
714                        "y >= 0 & x = 7 & y <= 6}", -1);
715         set = isl_set_coalesce(set);
716         assert(set && set->n == 2);
717         isl_set_free(set);
718
719         set = isl_set_read_from_str(ctx,
720                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
721                        "y >= 0 & x = 6 & y <= 5}", -1);
722         set = isl_set_coalesce(set);
723         assert(set && set->n == 1);
724         set2 = isl_set_read_from_str(ctx,
725                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
726                        "y >= 0 & x = 6 & y <= 5}", -1);
727         assert(isl_set_is_equal(set, set2));
728         isl_set_free(set);
729         isl_set_free(set2);
730
731         set = isl_set_read_from_str(ctx,
732                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
733                        "y >= 0 & x = 6 & y <= 7}", -1);
734         set = isl_set_coalesce(set);
735         assert(set && set->n == 2);
736         isl_set_free(set);
737
738         set = isl_set_read_from_str(ctx,
739                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
740         set = isl_set_coalesce(set);
741         assert(set && set->n == 1);
742         set2 = isl_set_read_from_str(ctx,
743                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
744         assert(isl_set_is_equal(set, set2));
745         isl_set_free(set);
746         isl_set_free(set2);
747
748         set = isl_set_read_from_str(ctx,
749                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
750         set = isl_set_coalesce(set);
751         set2 = isl_set_read_from_str(ctx,
752                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
753         assert(isl_set_is_equal(set, set2));
754         isl_set_free(set);
755         isl_set_free(set2);
756
757         set = isl_set_read_from_str(ctx,
758                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
759                                 "2 <= i and i <= n }", -1);
760         set = isl_set_coalesce(set);
761         assert(set && set->n == 1);
762         set2 = isl_set_read_from_str(ctx,
763                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
764                                 "2 <= i and i <= n }", -1);
765         assert(isl_set_is_equal(set, set2));
766         isl_set_free(set);
767         isl_set_free(set2);
768
769         map = isl_map_read_from_str(ctx,
770                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
771                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
772                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
773                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
774                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
775                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
776                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
777                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
778                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
779                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
780                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
781                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
782                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
783                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
784                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
785                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
786                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
787                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
788         map = isl_map_coalesce(map);
789         map2 = isl_map_read_from_str(ctx,
790                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
791                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
792                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
793                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
794                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
795                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
796                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
797                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
798                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
799                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
800                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
801                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
802                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
803                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
804                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
805                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
806                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
807                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
808         assert(isl_map_is_equal(map, map2));
809         isl_map_free(map);
810         isl_map_free(map2);
811
812         str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
813               "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
814               "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
815               "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
816         map = isl_map_read_from_str(ctx, str, -1);
817         map = isl_map_coalesce(map);
818         map2 = isl_map_read_from_str(ctx, str, -1);
819         assert(isl_map_is_equal(map, map2));
820         isl_map_free(map);
821         isl_map_free(map2);
822
823         str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
824           "[o0, o1, o2, o3, o4, o5, o6] : "
825           "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
826           "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
827           "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
828           "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
829           "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
830           "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
831           "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
832           "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
833           "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
834           "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
835           "o6 >= i3 + i6 - o3 and M >= 0 and "
836           "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
837           "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
838         map = isl_map_read_from_str(ctx, str, -1);
839         map = isl_map_coalesce(map);
840         map2 = isl_map_read_from_str(ctx, str, -1);
841         assert(isl_map_is_equal(map, map2));
842         isl_map_free(map);
843         isl_map_free(map2);
844
845         str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
846                 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
847                 "(o0 = 0 and M >= 2 and N >= 3) or "
848                 "(M = 0 and o0 = 0 and N >= 3) }";
849         map = isl_map_read_from_str(ctx, str, -1);
850         map = isl_map_coalesce(map);
851         map2 = isl_map_read_from_str(ctx, str, -1);
852         assert(isl_map_is_equal(map, map2));
853         isl_map_free(map);
854         isl_map_free(map2);
855
856         str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
857                 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
858                 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
859                 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
860         map = isl_map_read_from_str(ctx, str, -1);
861         map = isl_map_coalesce(map);
862         map2 = isl_map_read_from_str(ctx, str, -1);
863         assert(isl_map_is_equal(map, map2));
864         isl_map_free(map);
865         isl_map_free(map2);
866
867         test_coalesce_set(ctx,
868                 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
869                                 "(i1 = M and M >= 1) }", 0);
870         test_coalesce_set(ctx,
871                 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
872         test_coalesce_set(ctx,
873                 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
874                 "(y = 3 and x = 1) }", 1);
875         test_coalesce_set(ctx,
876                 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
877                 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
878                 "i1 <= M and i3 <= M and i4 <= M) or "
879                 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
880                 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
881                 "i4 <= -1 + M) }", 1);
882         test_coalesce_set(ctx,
883                 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
884                 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
885         test_coalesce_set(ctx,
886                 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
887                         "-x - y + 1 >= 0 and -3 <= z <= 3;"
888                 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
889                         "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
890         test_coalesce_set(ctx,
891                 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
892         test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
893         test_coalesce_set(ctx,
894                 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
895         test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
896 }
897
898 void test_closure(struct isl_ctx *ctx)
899 {
900         const char *str;
901         isl_set *dom;
902         isl_map *up, *right;
903         isl_map *map, *map2;
904         int exact;
905
906         /* COCOA example 1 */
907         map = isl_map_read_from_str(ctx,
908                 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
909                         "1 <= i and i < n and 1 <= j and j < n or "
910                         "i2 = i + 1 and j2 = j - 1 and "
911                         "1 <= i and i < n and 2 <= j and j <= n }", -1);
912         map = isl_map_power(map, 1, &exact);
913         assert(exact);
914         isl_map_free(map);
915
916         /* COCOA example 1 */
917         map = isl_map_read_from_str(ctx,
918                 "[n] -> { [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_transitive_closure(map, &exact);
923         assert(exact);
924         map2 = isl_map_read_from_str(ctx,
925                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
926                         "1 <= i and i < n and 1 <= j and j <= n and "
927                         "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
928                         "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
929                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
930         assert(isl_map_is_equal(map, map2));
931         isl_map_free(map2);
932         isl_map_free(map);
933
934         map = isl_map_read_from_str(ctx,
935                 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
936                                      " 0 <= y and y <= n }", -1);
937         map = isl_map_transitive_closure(map, &exact);
938         map2 = isl_map_read_from_str(ctx,
939                 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
940                                      " 0 <= y and y <= n }", -1);
941         assert(isl_map_is_equal(map, map2));
942         isl_map_free(map2);
943         isl_map_free(map);
944
945         /* COCOA example 2 */
946         map = isl_map_read_from_str(ctx,
947                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
948                         "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
949                         "i2 = i + 2 and j2 = j - 2 and "
950                         "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
951         map = isl_map_transitive_closure(map, &exact);
952         assert(exact);
953         map2 = isl_map_read_from_str(ctx,
954                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
955                         "1 <= i and i < n - 1 and 1 <= j and j <= n and "
956                         "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
957                         "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
958                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
959         assert(isl_map_is_equal(map, map2));
960         isl_map_free(map);
961         isl_map_free(map2);
962
963         /* COCOA Fig.2 left */
964         map = isl_map_read_from_str(ctx,
965                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
966                         "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
967                         "j <= n or "
968                         "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
969                         "j <= 2 i - 3 and j <= n - 2 or "
970                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
971                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
972         map = isl_map_transitive_closure(map, &exact);
973         assert(exact);
974         isl_map_free(map);
975
976         /* COCOA Fig.2 right */
977         map = isl_map_read_from_str(ctx,
978                 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
979                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
980                         "j <= n or "
981                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
982                         "j <= 2 i - 4 and j <= n - 3 or "
983                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
984                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
985         map = isl_map_power(map, 1, &exact);
986         assert(exact);
987         isl_map_free(map);
988
989         /* COCOA Fig.2 right */
990         map = isl_map_read_from_str(ctx,
991                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
992                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
993                         "j <= n or "
994                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
995                         "j <= 2 i - 4 and j <= n - 3 or "
996                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
997                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
998         map = isl_map_transitive_closure(map, &exact);
999         assert(exact);
1000         map2 = isl_map_read_from_str(ctx,
1001                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1002                         "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1003                         "j <= n and 3 + i + 2 j <= 3 n and "
1004                         "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1005                         "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1006                         "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1007                         "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1008                         "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
1009         assert(isl_map_is_equal(map, map2));
1010         isl_map_free(map2);
1011         isl_map_free(map);
1012
1013         /* COCOA Fig.1 right */
1014         dom = isl_set_read_from_str(ctx,
1015                 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1016                         "2 x - 3 y + 3 >= 0 }", -1);
1017         right = isl_map_read_from_str(ctx,
1018                 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
1019         up = isl_map_read_from_str(ctx,
1020                 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
1021         right = isl_map_intersect_domain(right, isl_set_copy(dom));
1022         right = isl_map_intersect_range(right, isl_set_copy(dom));
1023         up = isl_map_intersect_domain(up, isl_set_copy(dom));
1024         up = isl_map_intersect_range(up, dom);
1025         map = isl_map_union(up, right);
1026         map = isl_map_transitive_closure(map, &exact);
1027         assert(exact);
1028         map2 = isl_map_read_from_str(ctx,
1029                 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1030                 "  [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }", -1);
1031         assert(isl_map_is_equal(map, map2));
1032         isl_map_free(map2);
1033         isl_map_free(map);
1034
1035         /* COCOA Theorem 1 counter example */
1036         map = isl_map_read_from_str(ctx,
1037                 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1038                         "i2 = 1 and j2 = j or "
1039                         "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
1040         map = isl_map_transitive_closure(map, &exact);
1041         assert(exact);
1042         isl_map_free(map);
1043
1044         map = isl_map_read_from_str(ctx,
1045                 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1046                         "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1047                         "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1048                         "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
1049         map = isl_map_transitive_closure(map, &exact);
1050         assert(exact);
1051         isl_map_free(map);
1052
1053         /* Kelly et al 1996, fig 12 */
1054         map = isl_map_read_from_str(ctx,
1055                 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1056                         "1 <= i,j,j+1 <= n or "
1057                         "j = n and j2 = 1 and i2 = i + 1 and "
1058                         "1 <= i,i+1 <= n }", -1);
1059         map = isl_map_transitive_closure(map, &exact);
1060         assert(exact);
1061         map2 = isl_map_read_from_str(ctx,
1062                 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1063                         "1 <= i <= n and i = i2 or "
1064                         "1 <= i < i2 <= n and 1 <= j <= n and "
1065                         "1 <= j2 <= n }", -1);
1066         assert(isl_map_is_equal(map, map2));
1067         isl_map_free(map2);
1068         isl_map_free(map);
1069
1070         /* Omega's closure4 */
1071         map = isl_map_read_from_str(ctx,
1072                 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1073                         "1 <= x,y <= 10 or "
1074                         "x2 = x + 1 and y2 = y and "
1075                         "1 <= x <= 20 && 5 <= y <= 15 }", -1);
1076         map = isl_map_transitive_closure(map, &exact);
1077         assert(exact);
1078         isl_map_free(map);
1079
1080         map = isl_map_read_from_str(ctx,
1081                 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
1082         map = isl_map_transitive_closure(map, &exact);
1083         assert(!exact);
1084         map2 = isl_map_read_from_str(ctx,
1085                 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
1086         assert(isl_map_is_equal(map, map2));
1087         isl_map_free(map);
1088         isl_map_free(map2);
1089
1090         str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1091             "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1092             "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1093             "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1094         map = isl_map_read_from_str(ctx, str, -1);
1095         map = isl_map_transitive_closure(map, &exact);
1096         assert(exact);
1097         map2 = isl_map_read_from_str(ctx, str, -1);
1098         assert(isl_map_is_equal(map, map2));
1099         isl_map_free(map);
1100         isl_map_free(map2);
1101
1102         str = "{[0] -> [1]; [2] -> [3]}";
1103         map = isl_map_read_from_str(ctx, str, -1);
1104         map = isl_map_transitive_closure(map, &exact);
1105         assert(exact);
1106         map2 = isl_map_read_from_str(ctx, str, -1);
1107         assert(isl_map_is_equal(map, map2));
1108         isl_map_free(map);
1109         isl_map_free(map2);
1110 }
1111
1112 void test_lex(struct isl_ctx *ctx)
1113 {
1114         isl_dim *dim;
1115         isl_map *map;
1116
1117         dim = isl_dim_alloc(ctx, 0, 0, 0);
1118         map = isl_map_lex_le(dim);
1119         assert(!isl_map_is_empty(map));
1120         isl_map_free(map);
1121 }
1122
1123 void test_lexmin(struct isl_ctx *ctx)
1124 {
1125         const char *str;
1126         isl_map *map, *map2;
1127         isl_set *set;
1128         isl_set *set2;
1129
1130         str = "[p0, p1] -> { [] -> [] : "
1131             "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1132             "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1133             "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1134             "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1135             "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1136             "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1137             "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1138             "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1139             "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1140             "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1141             "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1142         map = isl_map_read_from_str(ctx, str, -1);
1143         map = isl_map_lexmin(map);
1144         isl_map_free(map);
1145
1146         str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1147             "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1148         set = isl_set_read_from_str(ctx, str, -1);
1149         set = isl_set_lexmax(set);
1150         str = "[C] -> { [obj,a,b,c] : C = 8 }";
1151         set2 = isl_set_read_from_str(ctx, str, -1);
1152         set = isl_set_intersect(set, set2);
1153         assert(!isl_set_is_empty(set));
1154         isl_set_free(set);
1155
1156         str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1157         map = isl_map_read_from_str(ctx, str, -1);
1158         map = isl_map_lexmin(map);
1159         str = "{ [x] -> [5] : 6 <= x <= 8; "
1160                 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1161         map2 = isl_map_read_from_str(ctx, str, -1);
1162         assert(isl_map_is_equal(map, map2));
1163         isl_map_free(map);
1164         isl_map_free(map2);
1165
1166         str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1167         map = isl_map_read_from_str(ctx, str, -1);
1168         map2 = isl_map_copy(map);
1169         map = isl_map_lexmin(map);
1170         assert(isl_map_is_equal(map, map2));
1171         isl_map_free(map);
1172         isl_map_free(map2);
1173
1174         str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1175         map = isl_map_read_from_str(ctx, str, -1);
1176         map = isl_map_lexmin(map);
1177         str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1178                 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1179                 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1180                 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1181         map2 = isl_map_read_from_str(ctx, str, -1);
1182         assert(isl_map_is_equal(map, map2));
1183         isl_map_free(map);
1184         isl_map_free(map2);
1185 }
1186
1187 struct must_may {
1188         isl_map *must;
1189         isl_map *may;
1190 };
1191
1192 static int collect_must_may(__isl_take isl_map *dep, int must,
1193         void *dep_user, void *user)
1194 {
1195         struct must_may *mm = (struct must_may *)user;
1196
1197         if (must)
1198                 mm->must = isl_map_union(mm->must, dep);
1199         else
1200                 mm->may = isl_map_union(mm->may, dep);
1201
1202         return 0;
1203 }
1204
1205 static int common_space(void *first, void *second)
1206 {
1207         int depth = *(int *)first;
1208         return 2 * depth;
1209 }
1210
1211 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1212 {
1213         isl_map *map2;
1214         int equal;
1215
1216         if (!map)
1217                 return -1;
1218
1219         map2 = isl_map_read_from_str(map->ctx, str, -1);
1220         equal = isl_map_is_equal(map, map2);
1221         isl_map_free(map2);
1222
1223         return equal;
1224 }
1225
1226 void test_dep(struct isl_ctx *ctx)
1227 {
1228         const char *str;
1229         isl_dim *dim;
1230         isl_map *map;
1231         isl_access_info *ai;
1232         isl_flow *flow;
1233         int depth;
1234         struct must_may mm;
1235
1236         depth = 3;
1237
1238         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1239         map = isl_map_read_from_str(ctx, str, -1);
1240         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1241
1242         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1243         map = isl_map_read_from_str(ctx, str, -1);
1244         ai = isl_access_info_add_source(ai, map, 1, &depth);
1245
1246         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1247         map = isl_map_read_from_str(ctx, str, -1);
1248         ai = isl_access_info_add_source(ai, map, 1, &depth);
1249
1250         flow = isl_access_info_compute_flow(ai);
1251         dim = isl_dim_alloc(ctx, 0, 3, 3);
1252         mm.must = isl_map_empty(isl_dim_copy(dim));
1253         mm.may = isl_map_empty(dim);
1254
1255         isl_flow_foreach(flow, collect_must_may, &mm);
1256
1257         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1258               "  [1,10,0] -> [2,5,0] }";
1259         assert(map_is_equal(mm.must, str));
1260         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1261         assert(map_is_equal(mm.may, str));
1262
1263         isl_map_free(mm.must);
1264         isl_map_free(mm.may);
1265         isl_flow_free(flow);
1266
1267
1268         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1269         map = isl_map_read_from_str(ctx, str, -1);
1270         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1271
1272         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1273         map = isl_map_read_from_str(ctx, str, -1);
1274         ai = isl_access_info_add_source(ai, map, 1, &depth);
1275
1276         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1277         map = isl_map_read_from_str(ctx, str, -1);
1278         ai = isl_access_info_add_source(ai, map, 0, &depth);
1279
1280         flow = isl_access_info_compute_flow(ai);
1281         dim = isl_dim_alloc(ctx, 0, 3, 3);
1282         mm.must = isl_map_empty(isl_dim_copy(dim));
1283         mm.may = isl_map_empty(dim);
1284
1285         isl_flow_foreach(flow, collect_must_may, &mm);
1286
1287         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1288         assert(map_is_equal(mm.must, str));
1289         str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1290         assert(map_is_equal(mm.may, str));
1291
1292         isl_map_free(mm.must);
1293         isl_map_free(mm.may);
1294         isl_flow_free(flow);
1295
1296
1297         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1298         map = isl_map_read_from_str(ctx, str, -1);
1299         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1300
1301         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1302         map = isl_map_read_from_str(ctx, str, -1);
1303         ai = isl_access_info_add_source(ai, map, 0, &depth);
1304
1305         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1306         map = isl_map_read_from_str(ctx, str, -1);
1307         ai = isl_access_info_add_source(ai, map, 0, &depth);
1308
1309         flow = isl_access_info_compute_flow(ai);
1310         dim = isl_dim_alloc(ctx, 0, 3, 3);
1311         mm.must = isl_map_empty(isl_dim_copy(dim));
1312         mm.may = isl_map_empty(dim);
1313
1314         isl_flow_foreach(flow, collect_must_may, &mm);
1315
1316         str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1317               "  [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1318         assert(map_is_equal(mm.may, str));
1319         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1320         assert(map_is_equal(mm.must, str));
1321
1322         isl_map_free(mm.must);
1323         isl_map_free(mm.may);
1324         isl_flow_free(flow);
1325
1326
1327         str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1328         map = isl_map_read_from_str(ctx, str, -1);
1329         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1330
1331         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1332         map = isl_map_read_from_str(ctx, str, -1);
1333         ai = isl_access_info_add_source(ai, map, 0, &depth);
1334
1335         str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1336         map = isl_map_read_from_str(ctx, str, -1);
1337         ai = isl_access_info_add_source(ai, map, 0, &depth);
1338
1339         flow = isl_access_info_compute_flow(ai);
1340         dim = isl_dim_alloc(ctx, 0, 3, 3);
1341         mm.must = isl_map_empty(isl_dim_copy(dim));
1342         mm.may = isl_map_empty(dim);
1343
1344         isl_flow_foreach(flow, collect_must_may, &mm);
1345
1346         str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1347               "  [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1348         assert(map_is_equal(mm.may, str));
1349         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1350         assert(map_is_equal(mm.must, str));
1351
1352         isl_map_free(mm.must);
1353         isl_map_free(mm.may);
1354         isl_flow_free(flow);
1355
1356
1357         str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1358         map = isl_map_read_from_str(ctx, str, -1);
1359         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1360
1361         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1362         map = isl_map_read_from_str(ctx, str, -1);
1363         ai = isl_access_info_add_source(ai, map, 0, &depth);
1364
1365         str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1366         map = isl_map_read_from_str(ctx, str, -1);
1367         ai = isl_access_info_add_source(ai, map, 0, &depth);
1368
1369         flow = isl_access_info_compute_flow(ai);
1370         dim = isl_dim_alloc(ctx, 0, 3, 3);
1371         mm.must = isl_map_empty(isl_dim_copy(dim));
1372         mm.may = isl_map_empty(dim);
1373
1374         isl_flow_foreach(flow, collect_must_may, &mm);
1375
1376         str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1377               "  [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1378         assert(map_is_equal(mm.may, str));
1379         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1380         assert(map_is_equal(mm.must, str));
1381
1382         isl_map_free(mm.must);
1383         isl_map_free(mm.may);
1384         isl_flow_free(flow);
1385
1386
1387         depth = 5;
1388
1389         str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1390         map = isl_map_read_from_str(ctx, str, -1);
1391         ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1392
1393         str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1394         map = isl_map_read_from_str(ctx, str, -1);
1395         ai = isl_access_info_add_source(ai, map, 1, &depth);
1396
1397         flow = isl_access_info_compute_flow(ai);
1398         dim = isl_dim_alloc(ctx, 0, 5, 5);
1399         mm.must = isl_map_empty(isl_dim_copy(dim));
1400         mm.may = isl_map_empty(dim);
1401
1402         isl_flow_foreach(flow, collect_must_may, &mm);
1403
1404         str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1405         assert(map_is_equal(mm.must, str));
1406         str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1407         assert(map_is_equal(mm.may, str));
1408
1409         isl_map_free(mm.must);
1410         isl_map_free(mm.may);
1411         isl_flow_free(flow);
1412 }
1413
1414 void test_sv(struct isl_ctx *ctx)
1415 {
1416         const char *str;
1417         isl_map *map;
1418
1419         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1420         map = isl_map_read_from_str(ctx, str, -1);
1421         assert(isl_map_is_single_valued(map));
1422         isl_map_free(map);
1423
1424         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1425         map = isl_map_read_from_str(ctx, str, -1);
1426         assert(!isl_map_is_single_valued(map));
1427         isl_map_free(map);
1428 }
1429
1430 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1431 {
1432         isl_map *map;
1433
1434         map = isl_map_read_from_str(ctx, str, -1);
1435         if (bijective)
1436                 assert(isl_map_is_bijective(map));
1437         else
1438                 assert(!isl_map_is_bijective(map));
1439         isl_map_free(map);
1440 }
1441
1442 void test_bijective(struct isl_ctx *ctx)
1443 {
1444         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1445         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1446         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1447         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1448         test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1449         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1450         test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1451         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1452         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1453         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1454         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1455         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1456         test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1457 }
1458
1459 void test_pwqp(struct isl_ctx *ctx)
1460 {
1461         const char *str;
1462         isl_set *set;
1463         isl_pw_qpolynomial *pwqp1, *pwqp2;
1464
1465         str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1466         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1467
1468         pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1469                                                 isl_dim_set, 1, 1);
1470
1471         str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1472         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1473
1474         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1475
1476         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1477
1478         isl_pw_qpolynomial_free(pwqp1);
1479
1480         str = "{ [i] -> i }";
1481         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1482         str = "{ [k] : exists a : k = 2a }";
1483         set = isl_set_read_from_str(ctx, str, 0);
1484         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1485         str = "{ [i] -> i }";
1486         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1487
1488         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1489
1490         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1491
1492         isl_pw_qpolynomial_free(pwqp1);
1493
1494         str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1495         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1496         str = "{ [10] }";
1497         set = isl_set_read_from_str(ctx, str, 0);
1498         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1499         str = "{ [i] -> 16 }";
1500         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1501
1502         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1503
1504         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1505
1506         isl_pw_qpolynomial_free(pwqp1);
1507
1508         str = "{ [i] -> ([(i)/2]) }";
1509         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1510         str = "{ [k] : exists a : k = 2a+1 }";
1511         set = isl_set_read_from_str(ctx, str, 0);
1512         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1513         str = "{ [i] -> -1/2 + 1/2 * i }";
1514         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1515
1516         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1517
1518         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1519
1520         isl_pw_qpolynomial_free(pwqp1);
1521
1522         str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1523         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1524         str = "{ [i] -> ([(2 * [i/2])/5]) }";
1525         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1526
1527         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1528
1529         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1530
1531         isl_pw_qpolynomial_free(pwqp1);
1532
1533         str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1534         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1535         str = "{ [x] -> x }";
1536         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1537
1538         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1539
1540         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1541
1542         isl_pw_qpolynomial_free(pwqp1);
1543 }
1544
1545 void test_split_periods(isl_ctx *ctx)
1546 {
1547         const char *str;
1548         isl_pw_qpolynomial *pwqp;
1549
1550         str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1551                 "U + 2V + 3 >= 0 and - U -2V  >= 0 and - U + 10 >= 0 and "
1552                 "U  >= 0; [U,V] -> U^2 : U >= 100 }";
1553         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1554
1555         pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1556         assert(pwqp);
1557
1558         isl_pw_qpolynomial_free(pwqp);
1559 }
1560
1561 void test_union(isl_ctx *ctx)
1562 {
1563         const char *str;
1564         isl_union_set *uset1, *uset2;
1565         isl_union_map *umap1, *umap2;
1566
1567         str = "{ [i] : 0 <= i <= 1 }";
1568         uset1 = isl_union_set_read_from_str(ctx, str);
1569         str = "{ [1] -> [0] }";
1570         umap1 = isl_union_map_read_from_str(ctx, str);
1571
1572         umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1573         assert(isl_union_map_is_equal(umap1, umap2));
1574
1575         isl_union_map_free(umap1);
1576         isl_union_map_free(umap2);
1577
1578         str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1579         umap1 = isl_union_map_read_from_str(ctx, str);
1580         str = "{ A[i]; B[i] }";
1581         uset1 = isl_union_set_read_from_str(ctx, str);
1582
1583         uset2 = isl_union_map_domain(umap1);
1584
1585         assert(isl_union_set_is_equal(uset1, uset2));
1586
1587         isl_union_set_free(uset1);
1588         isl_union_set_free(uset2);
1589 }
1590
1591 void test_bound(isl_ctx *ctx)
1592 {
1593         const char *str;
1594         isl_pw_qpolynomial *pwqp;
1595         isl_pw_qpolynomial_fold *pwf;
1596
1597         str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1598         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1599         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1600         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 4);
1601         isl_pw_qpolynomial_fold_free(pwf);
1602 }
1603
1604 void test_lift(isl_ctx *ctx)
1605 {
1606         const char *str;
1607         isl_basic_map *bmap;
1608         isl_basic_set *bset;
1609
1610         str = "{ [i0] : exists e0 : i0 = 4e0 }";
1611         bset = isl_basic_set_read_from_str(ctx, str, 0);
1612         bset = isl_basic_set_lift(bset);
1613         bmap = isl_basic_map_from_range(bset);
1614         bset = isl_basic_map_domain(bmap);
1615         isl_basic_set_free(bset);
1616 }
1617
1618 void test_subset(isl_ctx *ctx)
1619 {
1620         const char *str;
1621         isl_set *set1, *set2;
1622
1623         str = "{ [112, 0] }";
1624         set1 = isl_set_read_from_str(ctx, str, 0);
1625         str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1626                 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1627                 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1628         set2 = isl_set_read_from_str(ctx, str, 0);
1629         assert(isl_set_is_subset(set1, set2));
1630         isl_set_free(set1);
1631         isl_set_free(set2);
1632 }
1633
1634 int main()
1635 {
1636         struct isl_ctx *ctx;
1637
1638         srcdir = getenv("srcdir");
1639         assert(srcdir);
1640
1641         ctx = isl_ctx_alloc();
1642         test_subset(ctx);
1643         test_lift(ctx);
1644         test_bound(ctx);
1645         test_union(ctx);
1646         test_split_periods(ctx);
1647         test_parse(ctx);
1648         test_pwqp(ctx);
1649         test_lex(ctx);
1650         test_sv(ctx);
1651         test_bijective(ctx);
1652         test_dep(ctx);
1653         test_read(ctx);
1654         test_bounded(ctx);
1655         test_construction(ctx);
1656         test_dim(ctx);
1657         test_div(ctx);
1658         test_application(ctx);
1659         test_affine_hull(ctx);
1660         test_convex_hull(ctx);
1661         test_gist(ctx);
1662         test_coalesce(ctx);
1663         test_closure(ctx);
1664         test_lexmin(ctx);
1665         isl_ctx_free(ctx);
1666         return 0;
1667 }