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