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