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