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