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