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