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