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