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