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