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