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