isl_map_coalesce: allow wrapping in sets that stick out in different directions
[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_constraint.h>
16
17 static char *srcdir;
18
19 void test_read(struct isl_ctx *ctx)
20 {
21         char filename[PATH_MAX];
22         FILE *input;
23         int n;
24         struct isl_basic_set *bset1, *bset2;
25         const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
26
27         n = snprintf(filename, sizeof(filename),
28                         "%s/test_inputs/set.omega", srcdir);
29         assert(n < sizeof(filename));
30         input = fopen(filename, "r");
31         assert(input);
32
33         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
34         bset2 = isl_basic_set_read_from_str(ctx, str, 0);
35
36         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
37
38         isl_basic_set_free(bset1);
39         isl_basic_set_free(bset2);
40
41         fclose(input);
42 }
43
44 /* Construct the basic set { [i] : 5 <= i <= N } */
45 void test_construction(struct isl_ctx *ctx)
46 {
47         isl_int v;
48         struct isl_dim *dim;
49         struct isl_basic_set *bset;
50         struct isl_constraint *c;
51
52         isl_int_init(v);
53
54         dim = isl_dim_set_alloc(ctx, 1, 1);
55         bset = isl_basic_set_universe(dim);
56
57         c = isl_inequality_alloc(isl_dim_copy(bset->dim));
58         isl_int_set_si(v, -1);
59         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
60         isl_int_set_si(v, 1);
61         isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
62         bset = isl_basic_set_add_constraint(bset, c);
63
64         c = isl_inequality_alloc(isl_dim_copy(bset->dim));
65         isl_int_set_si(v, 1);
66         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
67         isl_int_set_si(v, -5);
68         isl_constraint_set_constant(c, v);
69         bset = isl_basic_set_add_constraint(bset, c);
70
71         isl_basic_set_free(bset);
72
73         isl_int_clear(v);
74 }
75
76 void test_dim(struct isl_ctx *ctx)
77 {
78         isl_map *map1, *map2;
79
80         map1 = isl_map_read_from_str(ctx,
81             "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
82         map1 = isl_map_add(map1, isl_dim_in, 1);
83         map2 = isl_map_read_from_str(ctx,
84             "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
85         assert(isl_map_is_equal(map1, map2));
86         isl_map_free(map2);
87
88         map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
89         map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }", -1);
90         assert(isl_map_is_equal(map1, map2));
91
92         isl_map_free(map1);
93         isl_map_free(map2);
94 }
95
96 void test_div(struct isl_ctx *ctx)
97 {
98         isl_int v;
99         int pos;
100         struct isl_dim *dim;
101         struct isl_div *div;
102         struct isl_basic_set *bset;
103         struct isl_constraint *c;
104
105         isl_int_init(v);
106
107         /* test 1 */
108         dim = isl_dim_set_alloc(ctx, 0, 1);
109         bset = isl_basic_set_universe(dim);
110
111         c = isl_equality_alloc(isl_dim_copy(bset->dim));
112         isl_int_set_si(v, -1);
113         isl_constraint_set_constant(c, v);
114         isl_int_set_si(v, 1);
115         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
116         div = isl_div_alloc(isl_dim_copy(bset->dim));
117         c = isl_constraint_add_div(c, div, &pos);
118         isl_int_set_si(v, 3);
119         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
120         bset = isl_basic_set_add_constraint(bset, c);
121
122         c = isl_equality_alloc(isl_dim_copy(bset->dim));
123         isl_int_set_si(v, 1);
124         isl_constraint_set_constant(c, v);
125         isl_int_set_si(v, -1);
126         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
127         div = isl_div_alloc(isl_dim_copy(bset->dim));
128         c = isl_constraint_add_div(c, div, &pos);
129         isl_int_set_si(v, 3);
130         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
131         bset = isl_basic_set_add_constraint(bset, c);
132
133         assert(bset->n_div == 1);
134         isl_basic_set_free(bset);
135
136         /* test 2 */
137         dim = isl_dim_set_alloc(ctx, 0, 1);
138         bset = isl_basic_set_universe(dim);
139
140         c = isl_equality_alloc(isl_dim_copy(bset->dim));
141         isl_int_set_si(v, 1);
142         isl_constraint_set_constant(c, v);
143         isl_int_set_si(v, -1);
144         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
145         div = isl_div_alloc(isl_dim_copy(bset->dim));
146         c = isl_constraint_add_div(c, div, &pos);
147         isl_int_set_si(v, 3);
148         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
149         bset = isl_basic_set_add_constraint(bset, c);
150
151         c = isl_equality_alloc(isl_dim_copy(bset->dim));
152         isl_int_set_si(v, -1);
153         isl_constraint_set_constant(c, v);
154         isl_int_set_si(v, 1);
155         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
156         div = isl_div_alloc(isl_dim_copy(bset->dim));
157         c = isl_constraint_add_div(c, div, &pos);
158         isl_int_set_si(v, 3);
159         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
160         bset = isl_basic_set_add_constraint(bset, c);
161
162         assert(bset->n_div == 1);
163         isl_basic_set_free(bset);
164
165         /* test 3 */
166         dim = isl_dim_set_alloc(ctx, 0, 1);
167         bset = isl_basic_set_universe(dim);
168
169         c = isl_equality_alloc(isl_dim_copy(bset->dim));
170         isl_int_set_si(v, 1);
171         isl_constraint_set_constant(c, v);
172         isl_int_set_si(v, -1);
173         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
174         div = isl_div_alloc(isl_dim_copy(bset->dim));
175         c = isl_constraint_add_div(c, div, &pos);
176         isl_int_set_si(v, 3);
177         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
178         bset = isl_basic_set_add_constraint(bset, c);
179
180         c = isl_equality_alloc(isl_dim_copy(bset->dim));
181         isl_int_set_si(v, -3);
182         isl_constraint_set_constant(c, v);
183         isl_int_set_si(v, 1);
184         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
185         div = isl_div_alloc(isl_dim_copy(bset->dim));
186         c = isl_constraint_add_div(c, div, &pos);
187         isl_int_set_si(v, 4);
188         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
189         bset = isl_basic_set_add_constraint(bset, c);
190
191         assert(bset->n_div == 1);
192         isl_basic_set_free(bset);
193
194         /* test 4 */
195         dim = isl_dim_set_alloc(ctx, 0, 1);
196         bset = isl_basic_set_universe(dim);
197
198         c = isl_equality_alloc(isl_dim_copy(bset->dim));
199         isl_int_set_si(v, 2);
200         isl_constraint_set_constant(c, v);
201         isl_int_set_si(v, -1);
202         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
203         div = isl_div_alloc(isl_dim_copy(bset->dim));
204         c = isl_constraint_add_div(c, div, &pos);
205         isl_int_set_si(v, 3);
206         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
207         bset = isl_basic_set_add_constraint(bset, c);
208
209         c = isl_equality_alloc(isl_dim_copy(bset->dim));
210         isl_int_set_si(v, -1);
211         isl_constraint_set_constant(c, v);
212         isl_int_set_si(v, 1);
213         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
214         div = isl_div_alloc(isl_dim_copy(bset->dim));
215         c = isl_constraint_add_div(c, div, &pos);
216         isl_int_set_si(v, 6);
217         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
218         bset = isl_basic_set_add_constraint(bset, c);
219
220         assert(isl_basic_set_is_empty(bset));
221         isl_basic_set_free(bset);
222
223         /* test 5 */
224         dim = isl_dim_set_alloc(ctx, 0, 2);
225         bset = isl_basic_set_universe(dim);
226
227         c = isl_equality_alloc(isl_dim_copy(bset->dim));
228         isl_int_set_si(v, -1);
229         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
230         div = isl_div_alloc(isl_dim_copy(bset->dim));
231         c = isl_constraint_add_div(c, div, &pos);
232         isl_int_set_si(v, 3);
233         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
234         bset = isl_basic_set_add_constraint(bset, c);
235
236         c = isl_equality_alloc(isl_dim_copy(bset->dim));
237         isl_int_set_si(v, 1);
238         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
239         isl_int_set_si(v, -3);
240         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
241         bset = isl_basic_set_add_constraint(bset, c);
242
243         assert(bset->n_div == 0);
244         isl_basic_set_free(bset);
245
246         /* test 6 */
247         dim = isl_dim_set_alloc(ctx, 0, 2);
248         bset = isl_basic_set_universe(dim);
249
250         c = isl_equality_alloc(isl_dim_copy(bset->dim));
251         isl_int_set_si(v, -1);
252         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
253         div = isl_div_alloc(isl_dim_copy(bset->dim));
254         c = isl_constraint_add_div(c, div, &pos);
255         isl_int_set_si(v, 6);
256         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
257         bset = isl_basic_set_add_constraint(bset, c);
258
259         c = isl_equality_alloc(isl_dim_copy(bset->dim));
260         isl_int_set_si(v, 1);
261         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
262         isl_int_set_si(v, -3);
263         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
264         bset = isl_basic_set_add_constraint(bset, c);
265
266         assert(bset->n_div == 1);
267         isl_basic_set_free(bset);
268
269         /* test 7 */
270         /* This test is a bit tricky.  We set up an equality
271          *              a + 3b + 3c = 6 e0
272          * Normalization of divs creates _two_ divs
273          *              a = 3 e0
274          *              c - b - e0 = 2 e1
275          * Afterwards e0 is removed again because it has coefficient -1
276          * and we end up with the original equality and div again.
277          * Perhaps we can avoid the introduction of this temporary div.
278          */
279         dim = isl_dim_set_alloc(ctx, 0, 3);
280         bset = isl_basic_set_universe(dim);
281
282         c = isl_equality_alloc(isl_dim_copy(bset->dim));
283         isl_int_set_si(v, -1);
284         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
285         isl_int_set_si(v, -3);
286         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
287         isl_int_set_si(v, -3);
288         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
289         div = isl_div_alloc(isl_dim_copy(bset->dim));
290         c = isl_constraint_add_div(c, div, &pos);
291         isl_int_set_si(v, 6);
292         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
293         bset = isl_basic_set_add_constraint(bset, c);
294
295         /* Test disabled for now */
296         /*
297         assert(bset->n_div == 1);
298         */
299         isl_basic_set_free(bset);
300
301         /* test 8 */
302         dim = isl_dim_set_alloc(ctx, 0, 4);
303         bset = isl_basic_set_universe(dim);
304
305         c = isl_equality_alloc(isl_dim_copy(bset->dim));
306         isl_int_set_si(v, -1);
307         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
308         isl_int_set_si(v, -3);
309         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
310         isl_int_set_si(v, -3);
311         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
312         div = isl_div_alloc(isl_dim_copy(bset->dim));
313         c = isl_constraint_add_div(c, div, &pos);
314         isl_int_set_si(v, 6);
315         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
316         bset = isl_basic_set_add_constraint(bset, c);
317
318         c = isl_equality_alloc(isl_dim_copy(bset->dim));
319         isl_int_set_si(v, -1);
320         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
321         isl_int_set_si(v, 1);
322         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
323         isl_int_set_si(v, 1);
324         isl_constraint_set_constant(c, v);
325         bset = isl_basic_set_add_constraint(bset, c);
326
327         /* Test disabled for now */
328         /*
329         assert(bset->n_div == 1);
330         */
331         isl_basic_set_free(bset);
332
333         /* test 9 */
334         dim = isl_dim_set_alloc(ctx, 0, 2);
335         bset = isl_basic_set_universe(dim);
336
337         c = isl_equality_alloc(isl_dim_copy(bset->dim));
338         isl_int_set_si(v, 1);
339         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
340         isl_int_set_si(v, -1);
341         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
342         div = isl_div_alloc(isl_dim_copy(bset->dim));
343         c = isl_constraint_add_div(c, div, &pos);
344         isl_int_set_si(v, -2);
345         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
346         bset = isl_basic_set_add_constraint(bset, c);
347
348         c = isl_equality_alloc(isl_dim_copy(bset->dim));
349         isl_int_set_si(v, -1);
350         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
351         div = isl_div_alloc(isl_dim_copy(bset->dim));
352         c = isl_constraint_add_div(c, div, &pos);
353         isl_int_set_si(v, 3);
354         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
355         isl_int_set_si(v, 2);
356         isl_constraint_set_constant(c, v);
357         bset = isl_basic_set_add_constraint(bset, c);
358
359         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
360
361         assert(!isl_basic_set_is_empty(bset));
362
363         isl_basic_set_free(bset);
364
365         /* test 10 */
366         dim = isl_dim_set_alloc(ctx, 0, 2);
367         bset = isl_basic_set_universe(dim);
368
369         c = isl_equality_alloc(isl_dim_copy(bset->dim));
370         isl_int_set_si(v, 1);
371         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
372         div = isl_div_alloc(isl_dim_copy(bset->dim));
373         c = isl_constraint_add_div(c, div, &pos);
374         isl_int_set_si(v, -2);
375         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
376         bset = isl_basic_set_add_constraint(bset, c);
377
378         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
379
380         isl_basic_set_free(bset);
381
382         isl_int_clear(v);
383 }
384
385 void test_application_case(struct isl_ctx *ctx, const char *name)
386 {
387         char filename[PATH_MAX];
388         FILE *input;
389         int n;
390         struct isl_basic_set *bset1, *bset2;
391         struct isl_basic_map *bmap;
392
393         n = snprintf(filename, sizeof(filename),
394                         "%s/test_inputs/%s.omega", srcdir, name);
395         assert(n < sizeof(filename));
396         input = fopen(filename, "r");
397         assert(input);
398
399         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
400         bmap = isl_basic_map_read_from_file(ctx, input, 0);
401
402         bset1 = isl_basic_set_apply(bset1, bmap);
403
404         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
405
406         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
407
408         isl_basic_set_free(bset1);
409         isl_basic_set_free(bset2);
410
411         fclose(input);
412 }
413
414 void test_application(struct isl_ctx *ctx)
415 {
416         test_application_case(ctx, "application");
417         test_application_case(ctx, "application2");
418 }
419
420 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
421 {
422         char filename[PATH_MAX];
423         FILE *input;
424         int n;
425         struct isl_basic_set *bset1, *bset2;
426
427         n = snprintf(filename, sizeof(filename),
428                         "%s/test_inputs/%s.polylib", srcdir, name);
429         assert(n < sizeof(filename));
430         input = fopen(filename, "r");
431         assert(input);
432
433         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
434         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
435
436         bset1 = isl_basic_set_affine_hull(bset1);
437
438         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
439
440         isl_basic_set_free(bset1);
441         isl_basic_set_free(bset2);
442
443         fclose(input);
444 }
445
446 void test_affine_hull(struct isl_ctx *ctx)
447 {
448         test_affine_hull_case(ctx, "affine2");
449         test_affine_hull_case(ctx, "affine");
450         test_affine_hull_case(ctx, "affine3");
451 }
452
453 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
454 {
455         char filename[PATH_MAX];
456         FILE *input;
457         int n;
458         struct isl_basic_set *bset1, *bset2;
459         struct isl_set *set;
460
461         n = snprintf(filename, sizeof(filename),
462                         "%s/test_inputs/%s.polylib", srcdir, name);
463         assert(n < sizeof(filename));
464         input = fopen(filename, "r");
465         assert(input);
466
467         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
468         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
469
470         set = isl_basic_set_union(bset1, bset2);
471         bset1 = isl_set_convex_hull(set);
472
473         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
474
475         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
476
477         isl_basic_set_free(bset1);
478         isl_basic_set_free(bset2);
479
480         fclose(input);
481 }
482
483 void test_convex_hull(struct isl_ctx *ctx)
484 {
485         const char *str1, *str2;
486         isl_set *set1, *set2;
487
488         test_convex_hull_case(ctx, "convex0");
489         test_convex_hull_case(ctx, "convex1");
490         test_convex_hull_case(ctx, "convex2");
491         test_convex_hull_case(ctx, "convex3");
492         test_convex_hull_case(ctx, "convex4");
493         test_convex_hull_case(ctx, "convex5");
494         test_convex_hull_case(ctx, "convex6");
495         test_convex_hull_case(ctx, "convex7");
496         test_convex_hull_case(ctx, "convex8");
497         test_convex_hull_case(ctx, "convex9");
498         test_convex_hull_case(ctx, "convex10");
499         test_convex_hull_case(ctx, "convex11");
500         test_convex_hull_case(ctx, "convex12");
501         test_convex_hull_case(ctx, "convex13");
502         test_convex_hull_case(ctx, "convex14");
503         test_convex_hull_case(ctx, "convex15");
504
505         str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
506                "(i0 = 1 and i1 = 0 and i2 = 1) or "
507                "(i0 = 0 and i1 = 0 and i2 = 0) }";
508         str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
509         set1 = isl_set_read_from_str(ctx, str1, -1);
510         set2 = isl_set_read_from_str(ctx, str2, -1);
511         set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
512         assert(isl_set_is_equal(set1, set2));
513         isl_set_free(set1);
514         isl_set_free(set2);
515 }
516
517 void test_gist_case(struct isl_ctx *ctx, const char *name)
518 {
519         char filename[PATH_MAX];
520         FILE *input;
521         int n;
522         struct isl_basic_set *bset1, *bset2;
523
524         n = snprintf(filename, sizeof(filename),
525                         "%s/test_inputs/%s.polylib", srcdir, name);
526         assert(n < sizeof(filename));
527         input = fopen(filename, "r");
528         assert(input);
529
530         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
531         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
532
533         bset1 = isl_basic_set_gist(bset1, bset2);
534
535         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
536
537         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
538
539         isl_basic_set_free(bset1);
540         isl_basic_set_free(bset2);
541
542         fclose(input);
543 }
544
545 void test_gist(struct isl_ctx *ctx)
546 {
547         test_gist_case(ctx, "gist1");
548 }
549
550 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
551 {
552         isl_set *set, *set2;
553
554         set = isl_set_read_from_str(ctx, str, -1);
555         set = isl_set_coalesce(set);
556         set2 = isl_set_read_from_str(ctx, str, -1);
557         assert(isl_set_is_equal(set, set2));
558         if (check_one)
559                 assert(set && set->n == 1);
560         isl_set_free(set);
561         isl_set_free(set2);
562 }
563
564 void test_coalesce(struct isl_ctx *ctx)
565 {
566         const char *str;
567         struct isl_set *set, *set2;
568         struct isl_map *map, *map2;
569
570         set = isl_set_read_from_str(ctx,
571                 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
572                        "y >= x & x >= 2 & 5 >= y }", -1);
573         set = isl_set_coalesce(set);
574         assert(set && set->n == 1);
575         isl_set_free(set);
576
577         set = isl_set_read_from_str(ctx,
578                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
579                        "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
580         set = isl_set_coalesce(set);
581         assert(set && set->n == 1);
582         isl_set_free(set);
583
584         set = isl_set_read_from_str(ctx,
585                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
586                        "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
587         set = isl_set_coalesce(set);
588         assert(set && set->n == 2);
589         isl_set_free(set);
590
591         set = isl_set_read_from_str(ctx,
592                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
593                        "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
594         set = isl_set_coalesce(set);
595         assert(set && set->n == 1);
596         isl_set_free(set);
597
598         set = isl_set_read_from_str(ctx,
599                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
600                        "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
601         set = isl_set_coalesce(set);
602         assert(set && set->n == 2);
603         isl_set_free(set);
604
605         set = isl_set_read_from_str(ctx,
606                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
607                        "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
608         set = isl_set_coalesce(set);
609         assert(set && set->n == 2);
610         isl_set_free(set);
611
612         set = isl_set_read_from_str(ctx,
613                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
614                        "y >= 0 & x = 6 & y <= 6}", -1);
615         set = isl_set_coalesce(set);
616         assert(set && set->n == 1);
617         isl_set_free(set);
618
619         set = isl_set_read_from_str(ctx,
620                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
621                        "y >= 0 & x = 7 & y <= 6}", -1);
622         set = isl_set_coalesce(set);
623         assert(set && set->n == 2);
624         isl_set_free(set);
625
626         set = isl_set_read_from_str(ctx,
627                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
628                        "y >= 0 & x = 6 & y <= 5}", -1);
629         set = isl_set_coalesce(set);
630         assert(set && set->n == 1);
631         set2 = isl_set_read_from_str(ctx,
632                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
633                        "y >= 0 & x = 6 & y <= 5}", -1);
634         assert(isl_set_is_equal(set, set2));
635         isl_set_free(set);
636         isl_set_free(set2);
637
638         set = isl_set_read_from_str(ctx,
639                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
640                        "y >= 0 & x = 6 & y <= 7}", -1);
641         set = isl_set_coalesce(set);
642         assert(set && set->n == 2);
643         isl_set_free(set);
644
645         set = isl_set_read_from_str(ctx,
646                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
647         set = isl_set_coalesce(set);
648         assert(set && set->n == 1);
649         set2 = isl_set_read_from_str(ctx,
650                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
651         assert(isl_set_is_equal(set, set2));
652         isl_set_free(set);
653         isl_set_free(set2);
654
655         set = isl_set_read_from_str(ctx,
656                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
657         set = isl_set_coalesce(set);
658         set2 = isl_set_read_from_str(ctx,
659                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
660         assert(isl_set_is_equal(set, set2));
661         isl_set_free(set);
662         isl_set_free(set2);
663
664         set = isl_set_read_from_str(ctx,
665                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
666                                 "2 <= i and i <= n }", -1);
667         set = isl_set_coalesce(set);
668         assert(set && set->n == 1);
669         set2 = isl_set_read_from_str(ctx,
670                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
671                                 "2 <= i and i <= n }", -1);
672         assert(isl_set_is_equal(set, set2));
673         isl_set_free(set);
674         isl_set_free(set2);
675
676         map = isl_map_read_from_str(ctx,
677                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
678                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
679                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
680                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
681                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
682                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
683                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
684                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
685                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
686                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
687                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
688                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
689                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
690                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
691                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
692                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
693                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
694                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
695         map = isl_map_coalesce(map);
696         map2 = isl_map_read_from_str(ctx,
697                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
698                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
699                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
700                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
701                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
702                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
703                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
704                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
705                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
706                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
707                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
708                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
709                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
710                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
711                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
712                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
713                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
714                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
715         assert(isl_map_is_equal(map, map2));
716         isl_map_free(map);
717         isl_map_free(map2);
718
719         str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
720               "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
721               "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
722               "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
723         map = isl_map_read_from_str(ctx, str, -1);
724         map = isl_map_coalesce(map);
725         map2 = isl_map_read_from_str(ctx, str, -1);
726         assert(isl_map_is_equal(map, map2));
727         isl_map_free(map);
728         isl_map_free(map2);
729
730         str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
731           "[o0, o1, o2, o3, o4, o5, o6] : "
732           "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
733           "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
734           "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
735           "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
736           "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
737           "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
738           "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
739           "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
740           "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
741           "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
742           "o6 >= i3 + i6 - o3 and M >= 0 and "
743           "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
744           "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
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] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
753                 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
754                 "(o0 = 0 and M >= 2 and N >= 3) or "
755                 "(M = 0 and o0 = 0 and N >= 3) }";
756         map = isl_map_read_from_str(ctx, str, -1);
757         map = isl_map_coalesce(map);
758         map2 = isl_map_read_from_str(ctx, str, -1);
759         assert(isl_map_is_equal(map, map2));
760         isl_map_free(map);
761         isl_map_free(map2);
762
763         str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
764                 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
765                 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
766                 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
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         test_coalesce_set(ctx,
775                 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
776                                 "(i1 = M and M >= 1) }", 0);
777         test_coalesce_set(ctx,
778                 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
779         test_coalesce_set(ctx,
780                 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
781                 "(y = 3 and x = 1) }", 1);
782         test_coalesce_set(ctx,
783                 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
784                 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
785                 "i1 <= M and i3 <= M and i4 <= M) or "
786                 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
787                 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
788                 "i4 <= -1 + M) }", 1);
789         test_coalesce_set(ctx,
790                 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
791                 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
792         test_coalesce_set(ctx,
793                 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
794                         "-x - y + 1 >= 0 and -3 <= z <= 3;"
795                 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
796                         "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
797         test_coalesce_set(ctx,
798                 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
799 }
800
801 void test_closure(struct isl_ctx *ctx)
802 {
803         const char *str;
804         isl_set *dom;
805         isl_map *up, *right;
806         isl_map *map, *map2;
807         int exact;
808
809         /* COCOA example 1 */
810         map = isl_map_read_from_str(ctx,
811                 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
812                         "1 <= i and i < n and 1 <= j and j < n or "
813                         "i2 = i + 1 and j2 = j - 1 and "
814                         "1 <= i and i < n and 2 <= j and j <= n }", -1);
815         map = isl_map_power(map, 1, &exact);
816         assert(exact);
817         isl_map_free(map);
818
819         /* COCOA example 1 */
820         map = isl_map_read_from_str(ctx,
821                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
822                         "1 <= i and i < n and 1 <= j and j < n or "
823                         "i2 = i + 1 and j2 = j - 1 and "
824                         "1 <= i and i < n and 2 <= j and j <= n }", -1);
825         map = isl_map_transitive_closure(map, &exact);
826         assert(exact);
827         map2 = isl_map_read_from_str(ctx,
828                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
829                         "1 <= i and i < n and 1 <= j and j <= n and "
830                         "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
831                         "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
832                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
833         assert(isl_map_is_equal(map, map2));
834         isl_map_free(map2);
835         isl_map_free(map);
836
837         map = isl_map_read_from_str(ctx,
838                 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
839                                      " 0 <= y and y <= n }", -1);
840         map = isl_map_transitive_closure(map, &exact);
841         map2 = isl_map_read_from_str(ctx,
842                 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
843                                      " 0 <= y and y <= n }", -1);
844         assert(isl_map_is_equal(map, map2));
845         isl_map_free(map2);
846         isl_map_free(map);
847
848         /* COCOA example 2 */
849         map = isl_map_read_from_str(ctx,
850                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
851                         "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
852                         "i2 = i + 2 and j2 = j - 2 and "
853                         "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
854         map = isl_map_transitive_closure(map, &exact);
855         assert(exact);
856         map2 = isl_map_read_from_str(ctx,
857                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
858                         "1 <= i and i < n - 1 and 1 <= j and j <= n and "
859                         "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
860                         "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
861                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
862         assert(isl_map_is_equal(map, map2));
863         isl_map_free(map);
864         isl_map_free(map2);
865
866         /* COCOA Fig.2 left */
867         map = isl_map_read_from_str(ctx,
868                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
869                         "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
870                         "j <= n or "
871                         "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
872                         "j <= 2 i - 3 and j <= n - 2 or "
873                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
874                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
875         map = isl_map_transitive_closure(map, &exact);
876         assert(exact);
877         isl_map_free(map);
878
879         /* COCOA Fig.2 right */
880         map = isl_map_read_from_str(ctx,
881                 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
882                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
883                         "j <= n or "
884                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
885                         "j <= 2 i - 4 and j <= n - 3 or "
886                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
887                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
888         map = isl_map_power(map, 1, &exact);
889         assert(exact);
890         isl_map_free(map);
891
892         /* COCOA Fig.2 right */
893         map = isl_map_read_from_str(ctx,
894                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
895                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
896                         "j <= n or "
897                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
898                         "j <= 2 i - 4 and j <= n - 3 or "
899                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
900                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
901         map = isl_map_transitive_closure(map, &exact);
902         assert(exact);
903         map2 = isl_map_read_from_str(ctx,
904                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
905                         "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
906                         "j <= n and 3 + i + 2 j <= 3 n and "
907                         "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
908                         "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
909                         "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
910                         "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
911                         "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
912         assert(isl_map_is_equal(map, map2));
913         isl_map_free(map2);
914         isl_map_free(map);
915
916         /* COCOA Fig.1 right */
917         dom = isl_set_read_from_str(ctx,
918                 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
919                         "2 x - 3 y + 3 >= 0 }", -1);
920         right = isl_map_read_from_str(ctx,
921                 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
922         up = isl_map_read_from_str(ctx,
923                 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
924         right = isl_map_intersect_domain(right, isl_set_copy(dom));
925         right = isl_map_intersect_range(right, isl_set_copy(dom));
926         up = isl_map_intersect_domain(up, isl_set_copy(dom));
927         up = isl_map_intersect_range(up, dom);
928         map = isl_map_union(up, right);
929         map = isl_map_transitive_closure(map, &exact);
930         assert(exact);
931         map2 = isl_map_read_from_str(ctx,
932                 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
933                 "  [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }", -1);
934         assert(isl_map_is_equal(map, map2));
935         isl_map_free(map2);
936         isl_map_free(map);
937
938         /* COCOA Theorem 1 counter example */
939         map = isl_map_read_from_str(ctx,
940                 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
941                         "i2 = 1 and j2 = j or "
942                         "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
943         map = isl_map_transitive_closure(map, &exact);
944         assert(exact);
945         isl_map_free(map);
946
947         map = isl_map_read_from_str(ctx,
948                 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
949                         "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
950                         "i2 = i + 1 and 3 <= j2 - j <= 4 and "
951                         "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
952         map = isl_map_transitive_closure(map, &exact);
953         assert(exact);
954         isl_map_free(map);
955
956         /* Kelly et al 1996, fig 12 */
957         map = isl_map_read_from_str(ctx,
958                 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
959                         "1 <= i,j,j+1 <= n or "
960                         "j = n and j2 = 1 and i2 = i + 1 and "
961                         "1 <= i,i+1 <= n }", -1);
962         map = isl_map_transitive_closure(map, &exact);
963         assert(exact);
964         map2 = isl_map_read_from_str(ctx,
965                 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
966                         "1 <= i <= n and i = i2 or "
967                         "1 <= i < i2 <= n and 1 <= j <= n and "
968                         "1 <= j2 <= n }", -1);
969         assert(isl_map_is_equal(map, map2));
970         isl_map_free(map2);
971         isl_map_free(map);
972
973         /* Omega's closure4 */
974         map = isl_map_read_from_str(ctx,
975                 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
976                         "1 <= x,y <= 10 or "
977                         "x2 = x + 1 and y2 = y and "
978                         "1 <= x <= 20 && 5 <= y <= 15 }", -1);
979         map = isl_map_transitive_closure(map, &exact);
980         assert(exact);
981         isl_map_free(map);
982
983         map = isl_map_read_from_str(ctx,
984                 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
985         map = isl_map_transitive_closure(map, &exact);
986         assert(!exact);
987         map2 = isl_map_read_from_str(ctx,
988                 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
989         assert(isl_map_is_equal(map, map2));
990         isl_map_free(map);
991         isl_map_free(map2);
992
993         str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
994             "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
995             "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
996             "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
997         map = isl_map_read_from_str(ctx, str, -1);
998         map = isl_map_transitive_closure(map, &exact);
999         assert(exact);
1000         map2 = isl_map_read_from_str(ctx, str, -1);
1001         assert(isl_map_is_equal(map, map2));
1002         isl_map_free(map);
1003         isl_map_free(map2);
1004
1005         str = "{[0] -> [1]; [2] -> [3]}";
1006         map = isl_map_read_from_str(ctx, str, -1);
1007         map = isl_map_transitive_closure(map, &exact);
1008         assert(exact);
1009         map2 = isl_map_read_from_str(ctx, str, -1);
1010         assert(isl_map_is_equal(map, map2));
1011         isl_map_free(map);
1012         isl_map_free(map2);
1013 }
1014
1015 void test_lexmin(struct isl_ctx *ctx)
1016 {
1017         const char *str;
1018         isl_map *map;
1019
1020         str = "[p0, p1] -> { [] -> [] : "
1021             "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1022             "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1023             "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1024             "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1025             "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1026             "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1027             "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1028             "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1029             "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1030             "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1031             "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1032         map = isl_map_read_from_str(ctx, str, -1);
1033         map = isl_map_lexmin(map);
1034         isl_map_free(map);
1035 }
1036
1037 int main()
1038 {
1039         struct isl_ctx *ctx;
1040
1041         srcdir = getenv("srcdir");
1042         assert(srcdir);
1043
1044         ctx = isl_ctx_alloc();
1045         test_read(ctx);
1046         test_construction(ctx);
1047         test_dim(ctx);
1048         test_div(ctx);
1049         test_application(ctx);
1050         test_affine_hull(ctx);
1051         test_convex_hull(ctx);
1052         test_gist(ctx);
1053         test_coalesce(ctx);
1054         test_closure(ctx);
1055         test_lexmin(ctx);
1056         isl_ctx_free(ctx);
1057         return 0;
1058 }