isl_map_gist_basic_map: remove alignment of context divs with all basic maps
[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 MIT 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_private.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 #include <isl_options_private.h>
24 #include <isl/vertices.h>
25 #include <isl/ast_build.h>
26 #include <isl/val.h>
27
28 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
29
30 static char *srcdir;
31
32 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
33         char *filename;
34         int length;
35         char *pattern = "%s/test_inputs/%s.%s";
36
37         length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
38                 + strlen(suffix) + 1;
39         filename = isl_alloc_array(ctx, char, length);
40
41         if (!filename)
42                 return NULL;
43
44         sprintf(filename, pattern, srcdir, name, suffix);
45
46         return filename;
47 }
48
49 void test_parse_map(isl_ctx *ctx, const char *str)
50 {
51         isl_map *map;
52
53         map = isl_map_read_from_str(ctx, str);
54         assert(map);
55         isl_map_free(map);
56 }
57
58 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
59 {
60         isl_map *map, *map2;
61         int equal;
62
63         map = isl_map_read_from_str(ctx, str);
64         map2 = isl_map_read_from_str(ctx, str2);
65         equal = isl_map_is_equal(map, map2);
66         isl_map_free(map);
67         isl_map_free(map2);
68
69         if (equal < 0)
70                 return -1;
71         if (!equal)
72                 isl_die(ctx, isl_error_unknown, "maps not equal",
73                         return -1);
74
75         return 0;
76 }
77
78 void test_parse_pwqp(isl_ctx *ctx, const char *str)
79 {
80         isl_pw_qpolynomial *pwqp;
81
82         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
83         assert(pwqp);
84         isl_pw_qpolynomial_free(pwqp);
85 }
86
87 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
88 {
89         isl_pw_aff *pwaff;
90
91         pwaff = isl_pw_aff_read_from_str(ctx, str);
92         assert(pwaff);
93         isl_pw_aff_free(pwaff);
94 }
95
96 int test_parse(struct isl_ctx *ctx)
97 {
98         isl_map *map, *map2;
99         const char *str, *str2;
100
101         str = "{ [i] -> [-i] }";
102         map = isl_map_read_from_str(ctx, str);
103         assert(map);
104         isl_map_free(map);
105
106         str = "{ A[i] -> L[([i/3])] }";
107         map = isl_map_read_from_str(ctx, str);
108         assert(map);
109         isl_map_free(map);
110
111         test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
112         test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
113                                 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
114
115         str = "{ [x,y]  : [([x/2]+y)/3] >= 1 }";
116         str2 = "{ [x, y] : 2y >= 6 - x }";
117         if (test_parse_map_equal(ctx, str, str2) < 0)
118                 return -1;
119
120         if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
121                                       "{ [x,y] : x <= y, 2*y + 3 }") < 0)
122                 return -1;
123         str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
124         if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
125                                         str) < 0)
126                 return -1;
127
128         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
129         map = isl_map_read_from_str(ctx, str);
130         str = "{ [new, old] -> [o0, o1] : "
131                "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
132                "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
133                "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
134         map2 = isl_map_read_from_str(ctx, str);
135         assert(isl_map_is_equal(map, map2));
136         isl_map_free(map);
137         isl_map_free(map2);
138
139         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
140         map = isl_map_read_from_str(ctx, str);
141         str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
142         map2 = isl_map_read_from_str(ctx, str);
143         assert(isl_map_is_equal(map, map2));
144         isl_map_free(map);
145         isl_map_free(map2);
146
147         str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
148         str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
149         if (test_parse_map_equal(ctx, str, str2) < 0)
150                 return -1;
151
152         str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
153         str2 = "{ [i,j] -> [min(i,j)] }";
154         if (test_parse_map_equal(ctx, str, str2) < 0)
155                 return -1;
156
157         str = "{ [i,j] : i != j }";
158         str2 = "{ [i,j] : i < j or i > j }";
159         if (test_parse_map_equal(ctx, str, str2) < 0)
160                 return -1;
161
162         str = "{ [i,j] : (i+1)*2 >= j }";
163         str2 = "{ [i, j] : j <= 2 + 2i }";
164         if (test_parse_map_equal(ctx, str, str2) < 0)
165                 return -1;
166
167         str = "{ [i] -> [i > 0 ? 4 : 5] }";
168         str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
169         if (test_parse_map_equal(ctx, str, str2) < 0)
170                 return -1;
171
172         str = "[N=2,M] -> { [i=[(M+N)/4]] }";
173         str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
174         if (test_parse_map_equal(ctx, str, str2) < 0)
175                 return -1;
176
177         str = "{ [x] : x >= 0 }";
178         str2 = "{ [x] : x-0 >= 0 }";
179         if (test_parse_map_equal(ctx, str, str2) < 0)
180                 return -1;
181
182         str = "{ [i] : ((i > 10)) }";
183         str2 = "{ [i] : i >= 11 }";
184         if (test_parse_map_equal(ctx, str, str2) < 0)
185                 return -1;
186
187         str = "{ [i] -> [0] }";
188         str2 = "{ [i] -> [0 * i] }";
189         if (test_parse_map_equal(ctx, str, str2) < 0)
190                 return -1;
191
192         test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
193         test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
194         test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
195         test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
196
197         if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
198                                       "{ [a] -> [b] : true }") < 0)
199                 return -1;
200
201         if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
202                                       "{ [i] : i <= 10 }") < 0)
203                 return -1;
204
205         if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
206                                       "[n] -> { [i] : i <= n }") < 0)
207                 return -1;
208
209         if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
210                 return -1;
211
212         if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
213                                       "{ [i] : exists a : i = 2 a }") < 0)
214                 return -1;
215
216         if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
217                                       "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
218                 return -1;
219
220         return 0;
221 }
222
223 void test_read(struct isl_ctx *ctx)
224 {
225         char *filename;
226         FILE *input;
227         struct isl_basic_set *bset1, *bset2;
228         const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
229
230         filename = get_filename(ctx, "set", "omega");
231         assert(filename);
232         input = fopen(filename, "r");
233         assert(input);
234
235         bset1 = isl_basic_set_read_from_file(ctx, input);
236         bset2 = isl_basic_set_read_from_str(ctx, str);
237
238         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
239
240         isl_basic_set_free(bset1);
241         isl_basic_set_free(bset2);
242         free(filename);
243
244         fclose(input);
245 }
246
247 void test_bounded(struct isl_ctx *ctx)
248 {
249         isl_set *set;
250         int bounded;
251
252         set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
253         bounded = isl_set_is_bounded(set);
254         assert(bounded);
255         isl_set_free(set);
256
257         set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
258         bounded = isl_set_is_bounded(set);
259         assert(!bounded);
260         isl_set_free(set);
261
262         set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
263         bounded = isl_set_is_bounded(set);
264         assert(!bounded);
265         isl_set_free(set);
266 }
267
268 /* Construct the basic set { [i] : 5 <= i <= N } */
269 void test_construction(struct isl_ctx *ctx)
270 {
271         isl_int v;
272         isl_space *dim;
273         isl_local_space *ls;
274         struct isl_basic_set *bset;
275         struct isl_constraint *c;
276
277         isl_int_init(v);
278
279         dim = isl_space_set_alloc(ctx, 1, 1);
280         bset = isl_basic_set_universe(isl_space_copy(dim));
281         ls = isl_local_space_from_space(dim);
282
283         c = isl_inequality_alloc(isl_local_space_copy(ls));
284         isl_int_set_si(v, -1);
285         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
286         isl_int_set_si(v, 1);
287         isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
288         bset = isl_basic_set_add_constraint(bset, c);
289
290         c = isl_inequality_alloc(isl_local_space_copy(ls));
291         isl_int_set_si(v, 1);
292         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
293         isl_int_set_si(v, -5);
294         isl_constraint_set_constant(c, v);
295         bset = isl_basic_set_add_constraint(bset, c);
296
297         isl_local_space_free(ls);
298         isl_basic_set_free(bset);
299
300         isl_int_clear(v);
301 }
302
303 void test_dim(struct isl_ctx *ctx)
304 {
305         const char *str;
306         isl_map *map1, *map2;
307
308         map1 = isl_map_read_from_str(ctx,
309             "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
310         map1 = isl_map_add_dims(map1, isl_dim_in, 1);
311         map2 = isl_map_read_from_str(ctx,
312             "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
313         assert(isl_map_is_equal(map1, map2));
314         isl_map_free(map2);
315
316         map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
317         map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
318         assert(isl_map_is_equal(map1, map2));
319
320         isl_map_free(map1);
321         isl_map_free(map2);
322
323         str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
324         map1 = isl_map_read_from_str(ctx, str);
325         str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
326         map2 = isl_map_read_from_str(ctx, str);
327         map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
328         assert(isl_map_is_equal(map1, map2));
329
330         isl_map_free(map1);
331         isl_map_free(map2);
332 }
333
334 struct {
335         __isl_give isl_val *(*op)(__isl_take isl_val *v);
336         const char *arg;
337         const char *res;
338 } val_un_tests[] = {
339         { &isl_val_neg, "0", "0" },
340         { &isl_val_abs, "0", "0" },
341         { &isl_val_2exp, "0", "1" },
342         { &isl_val_floor, "0", "0" },
343         { &isl_val_ceil, "0", "0" },
344         { &isl_val_neg, "1", "-1" },
345         { &isl_val_neg, "-1", "1" },
346         { &isl_val_neg, "1/2", "-1/2" },
347         { &isl_val_neg, "-1/2", "1/2" },
348         { &isl_val_neg, "infty", "-infty" },
349         { &isl_val_neg, "-infty", "infty" },
350         { &isl_val_neg, "NaN", "NaN" },
351         { &isl_val_abs, "1", "1" },
352         { &isl_val_abs, "-1", "1" },
353         { &isl_val_abs, "1/2", "1/2" },
354         { &isl_val_abs, "-1/2", "1/2" },
355         { &isl_val_abs, "infty", "infty" },
356         { &isl_val_abs, "-infty", "infty" },
357         { &isl_val_abs, "NaN", "NaN" },
358         { &isl_val_floor, "1", "1" },
359         { &isl_val_floor, "-1", "-1" },
360         { &isl_val_floor, "1/2", "0" },
361         { &isl_val_floor, "-1/2", "-1" },
362         { &isl_val_floor, "infty", "infty" },
363         { &isl_val_floor, "-infty", "-infty" },
364         { &isl_val_floor, "NaN", "NaN" },
365         { &isl_val_ceil, "1", "1" },
366         { &isl_val_ceil, "-1", "-1" },
367         { &isl_val_ceil, "1/2", "1" },
368         { &isl_val_ceil, "-1/2", "0" },
369         { &isl_val_ceil, "infty", "infty" },
370         { &isl_val_ceil, "-infty", "-infty" },
371         { &isl_val_ceil, "NaN", "NaN" },
372         { &isl_val_2exp, "-3", "1/8" },
373         { &isl_val_2exp, "-1", "1/2" },
374         { &isl_val_2exp, "1", "2" },
375         { &isl_val_2exp, "2", "4" },
376         { &isl_val_2exp, "3", "8" },
377 };
378
379 /* Perform some basic tests of unary operations on isl_val objects.
380  */
381 static int test_un_val(isl_ctx *ctx)
382 {
383         int i;
384         isl_val *v, *res;
385         __isl_give isl_val *(*fn)(__isl_take isl_val *v);
386         int ok;
387
388         for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
389                 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
390                 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
391                 fn = val_un_tests[i].op;
392                 v = fn(v);
393                 if (isl_val_is_nan(res))
394                         ok = isl_val_is_nan(v);
395                 else
396                         ok = isl_val_eq(v, res);
397                 isl_val_free(v);
398                 isl_val_free(res);
399                 if (ok < 0)
400                         return -1;
401                 if (!ok)
402                         isl_die(ctx, isl_error_unknown,
403                                 "unexpected result", return -1);
404         }
405
406         return 0;
407 }
408
409 struct {
410         __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
411                                 __isl_take isl_val *v2);
412 } val_bin_op[] = {
413         ['+'] = { &isl_val_add },
414         ['-'] = { &isl_val_sub },
415         ['*'] = { &isl_val_mul },
416         ['/'] = { &isl_val_div },
417         ['g'] = { &isl_val_gcd },
418         ['m'] = { &isl_val_min },
419         ['M'] = { &isl_val_max },
420 };
421
422 struct {
423         const char *arg1;
424         unsigned char op;
425         const char *arg2;
426         const char *res;
427 } val_bin_tests[] = {
428         { "0", '+', "0", "0" },
429         { "1", '+', "0", "1" },
430         { "1", '+', "1", "2" },
431         { "1", '-', "1", "0" },
432         { "1", '*', "1", "1" },
433         { "1", '/', "1", "1" },
434         { "2", '*', "3", "6" },
435         { "2", '*', "1/2", "1" },
436         { "2", '*', "1/3", "2/3" },
437         { "2/3", '*', "3/5", "2/5" },
438         { "2/3", '*', "7/5", "14/15" },
439         { "2", '/', "1/2", "4" },
440         { "-2", '/', "-1/2", "4" },
441         { "-2", '/', "1/2", "-4" },
442         { "2", '/', "-1/2", "-4" },
443         { "2", '/', "2", "1" },
444         { "2", '/', "3", "2/3" },
445         { "2/3", '/', "5/3", "2/5" },
446         { "2/3", '/', "5/7", "14/15" },
447         { "0", '/', "0", "NaN" },
448         { "42", '/', "0", "NaN" },
449         { "-42", '/', "0", "NaN" },
450         { "infty", '/', "0", "NaN" },
451         { "-infty", '/', "0", "NaN" },
452         { "NaN", '/', "0", "NaN" },
453         { "0", '/', "NaN", "NaN" },
454         { "42", '/', "NaN", "NaN" },
455         { "-42", '/', "NaN", "NaN" },
456         { "infty", '/', "NaN", "NaN" },
457         { "-infty", '/', "NaN", "NaN" },
458         { "NaN", '/', "NaN", "NaN" },
459         { "0", '/', "infty", "0" },
460         { "42", '/', "infty", "0" },
461         { "-42", '/', "infty", "0" },
462         { "infty", '/', "infty", "NaN" },
463         { "-infty", '/', "infty", "NaN" },
464         { "NaN", '/', "infty", "NaN" },
465         { "0", '/', "-infty", "0" },
466         { "42", '/', "-infty", "0" },
467         { "-42", '/', "-infty", "0" },
468         { "infty", '/', "-infty", "NaN" },
469         { "-infty", '/', "-infty", "NaN" },
470         { "NaN", '/', "-infty", "NaN" },
471         { "1", '-', "1/3", "2/3" },
472         { "1/3", '+', "1/2", "5/6" },
473         { "1/2", '+', "1/2", "1" },
474         { "3/4", '-', "1/4", "1/2" },
475         { "1/2", '-', "1/3", "1/6" },
476         { "infty", '+', "42", "infty" },
477         { "infty", '+', "infty", "infty" },
478         { "42", '+', "infty", "infty" },
479         { "infty", '-', "infty", "NaN" },
480         { "infty", '*', "infty", "infty" },
481         { "infty", '*', "-infty", "-infty" },
482         { "-infty", '*', "infty", "-infty" },
483         { "-infty", '*', "-infty", "infty" },
484         { "0", '*', "infty", "NaN" },
485         { "1", '*', "infty", "infty" },
486         { "infty", '*', "0", "NaN" },
487         { "infty", '*', "42", "infty" },
488         { "42", '-', "infty", "-infty" },
489         { "infty", '+', "-infty", "NaN" },
490         { "4", 'g', "6", "2" },
491         { "5", 'g', "6", "1" },
492         { "42", 'm', "3", "3" },
493         { "42", 'M', "3", "42" },
494         { "3", 'm', "42", "3" },
495         { "3", 'M', "42", "42" },
496         { "42", 'm', "infty", "42" },
497         { "42", 'M', "infty", "infty" },
498         { "42", 'm', "-infty", "-infty" },
499         { "42", 'M', "-infty", "42" },
500         { "42", 'm', "NaN", "NaN" },
501         { "42", 'M', "NaN", "NaN" },
502         { "infty", 'm', "-infty", "-infty" },
503         { "infty", 'M', "-infty", "infty" },
504 };
505
506 /* Perform some basic tests of binary operations on isl_val objects.
507  */
508 static int test_bin_val(isl_ctx *ctx)
509 {
510         int i;
511         isl_val *v1, *v2, *res;
512         __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
513                                 __isl_take isl_val *v2);
514         int ok;
515
516         for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
517                 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
518                 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
519                 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
520                 fn = val_bin_op[val_bin_tests[i].op].fn;
521                 v1 = fn(v1, v2);
522                 if (isl_val_is_nan(res))
523                         ok = isl_val_is_nan(v1);
524                 else
525                         ok = isl_val_eq(v1, res);
526                 isl_val_free(v1);
527                 isl_val_free(res);
528                 if (ok < 0)
529                         return -1;
530                 if (!ok)
531                         isl_die(ctx, isl_error_unknown,
532                                 "unexpected result", return -1);
533         }
534
535         return 0;
536 }
537
538 /* Perform some basic tests on isl_val objects.
539  */
540 static int test_val(isl_ctx *ctx)
541 {
542         if (test_un_val(ctx) < 0)
543                 return -1;
544         if (test_bin_val(ctx) < 0)
545                 return -1;
546         return 0;
547 }
548
549 static int test_div(isl_ctx *ctx)
550 {
551         unsigned n;
552         const char *str;
553         int empty;
554         isl_int v;
555         isl_space *dim;
556         isl_set *set;
557         isl_local_space *ls;
558         struct isl_basic_set *bset;
559         struct isl_constraint *c;
560
561         isl_int_init(v);
562
563         /* test 1 */
564         dim = isl_space_set_alloc(ctx, 0, 3);
565         bset = isl_basic_set_universe(isl_space_copy(dim));
566         ls = isl_local_space_from_space(dim);
567
568         c = isl_equality_alloc(isl_local_space_copy(ls));
569         isl_int_set_si(v, -1);
570         isl_constraint_set_constant(c, v);
571         isl_int_set_si(v, 1);
572         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
573         isl_int_set_si(v, 3);
574         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
575         bset = isl_basic_set_add_constraint(bset, c);
576
577         c = isl_equality_alloc(isl_local_space_copy(ls));
578         isl_int_set_si(v, 1);
579         isl_constraint_set_constant(c, v);
580         isl_int_set_si(v, -1);
581         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
582         isl_int_set_si(v, 3);
583         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
584         bset = isl_basic_set_add_constraint(bset, c);
585
586         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
587
588         assert(bset && bset->n_div == 1);
589         isl_local_space_free(ls);
590         isl_basic_set_free(bset);
591
592         /* test 2 */
593         dim = isl_space_set_alloc(ctx, 0, 3);
594         bset = isl_basic_set_universe(isl_space_copy(dim));
595         ls = isl_local_space_from_space(dim);
596
597         c = isl_equality_alloc(isl_local_space_copy(ls));
598         isl_int_set_si(v, 1);
599         isl_constraint_set_constant(c, v);
600         isl_int_set_si(v, -1);
601         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
602         isl_int_set_si(v, 3);
603         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
604         bset = isl_basic_set_add_constraint(bset, c);
605
606         c = isl_equality_alloc(isl_local_space_copy(ls));
607         isl_int_set_si(v, -1);
608         isl_constraint_set_constant(c, v);
609         isl_int_set_si(v, 1);
610         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
611         isl_int_set_si(v, 3);
612         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
613         bset = isl_basic_set_add_constraint(bset, c);
614
615         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
616
617         assert(bset && bset->n_div == 1);
618         isl_local_space_free(ls);
619         isl_basic_set_free(bset);
620
621         /* test 3 */
622         dim = isl_space_set_alloc(ctx, 0, 3);
623         bset = isl_basic_set_universe(isl_space_copy(dim));
624         ls = isl_local_space_from_space(dim);
625
626         c = isl_equality_alloc(isl_local_space_copy(ls));
627         isl_int_set_si(v, 1);
628         isl_constraint_set_constant(c, v);
629         isl_int_set_si(v, -1);
630         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
631         isl_int_set_si(v, 3);
632         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
633         bset = isl_basic_set_add_constraint(bset, c);
634
635         c = isl_equality_alloc(isl_local_space_copy(ls));
636         isl_int_set_si(v, -3);
637         isl_constraint_set_constant(c, v);
638         isl_int_set_si(v, 1);
639         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
640         isl_int_set_si(v, 4);
641         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
642         bset = isl_basic_set_add_constraint(bset, c);
643
644         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
645
646         assert(bset && bset->n_div == 1);
647         isl_local_space_free(ls);
648         isl_basic_set_free(bset);
649
650         /* test 4 */
651         dim = isl_space_set_alloc(ctx, 0, 3);
652         bset = isl_basic_set_universe(isl_space_copy(dim));
653         ls = isl_local_space_from_space(dim);
654
655         c = isl_equality_alloc(isl_local_space_copy(ls));
656         isl_int_set_si(v, 2);
657         isl_constraint_set_constant(c, v);
658         isl_int_set_si(v, -1);
659         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
660         isl_int_set_si(v, 3);
661         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
662         bset = isl_basic_set_add_constraint(bset, c);
663
664         c = isl_equality_alloc(isl_local_space_copy(ls));
665         isl_int_set_si(v, -1);
666         isl_constraint_set_constant(c, v);
667         isl_int_set_si(v, 1);
668         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
669         isl_int_set_si(v, 6);
670         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
671         bset = isl_basic_set_add_constraint(bset, c);
672
673         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
674
675         assert(isl_basic_set_is_empty(bset));
676         isl_local_space_free(ls);
677         isl_basic_set_free(bset);
678
679         /* test 5 */
680         dim = isl_space_set_alloc(ctx, 0, 3);
681         bset = isl_basic_set_universe(isl_space_copy(dim));
682         ls = isl_local_space_from_space(dim);
683
684         c = isl_equality_alloc(isl_local_space_copy(ls));
685         isl_int_set_si(v, -1);
686         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
687         isl_int_set_si(v, 3);
688         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
689         bset = isl_basic_set_add_constraint(bset, c);
690
691         c = isl_equality_alloc(isl_local_space_copy(ls));
692         isl_int_set_si(v, 1);
693         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
694         isl_int_set_si(v, -3);
695         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
696         bset = isl_basic_set_add_constraint(bset, c);
697
698         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
699
700         assert(bset && bset->n_div == 0);
701         isl_basic_set_free(bset);
702         isl_local_space_free(ls);
703
704         /* test 6 */
705         dim = isl_space_set_alloc(ctx, 0, 3);
706         bset = isl_basic_set_universe(isl_space_copy(dim));
707         ls = isl_local_space_from_space(dim);
708
709         c = isl_equality_alloc(isl_local_space_copy(ls));
710         isl_int_set_si(v, -1);
711         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
712         isl_int_set_si(v, 6);
713         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
714         bset = isl_basic_set_add_constraint(bset, c);
715
716         c = isl_equality_alloc(isl_local_space_copy(ls));
717         isl_int_set_si(v, 1);
718         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
719         isl_int_set_si(v, -3);
720         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
721         bset = isl_basic_set_add_constraint(bset, c);
722
723         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
724
725         assert(bset && bset->n_div == 1);
726         isl_basic_set_free(bset);
727         isl_local_space_free(ls);
728
729         /* test 7 */
730         /* This test is a bit tricky.  We set up an equality
731          *              a + 3b + 3c = 6 e0
732          * Normalization of divs creates _two_ divs
733          *              a = 3 e0
734          *              c - b - e0 = 2 e1
735          * Afterwards e0 is removed again because it has coefficient -1
736          * and we end up with the original equality and div again.
737          * Perhaps we can avoid the introduction of this temporary div.
738          */
739         dim = isl_space_set_alloc(ctx, 0, 4);
740         bset = isl_basic_set_universe(isl_space_copy(dim));
741         ls = isl_local_space_from_space(dim);
742
743         c = isl_equality_alloc(isl_local_space_copy(ls));
744         isl_int_set_si(v, -1);
745         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
746         isl_int_set_si(v, -3);
747         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
748         isl_int_set_si(v, -3);
749         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
750         isl_int_set_si(v, 6);
751         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
752         bset = isl_basic_set_add_constraint(bset, c);
753
754         bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
755
756         /* Test disabled for now */
757         /*
758         assert(bset && bset->n_div == 1);
759         */
760         isl_local_space_free(ls);
761         isl_basic_set_free(bset);
762
763         /* test 8 */
764         dim = isl_space_set_alloc(ctx, 0, 5);
765         bset = isl_basic_set_universe(isl_space_copy(dim));
766         ls = isl_local_space_from_space(dim);
767
768         c = isl_equality_alloc(isl_local_space_copy(ls));
769         isl_int_set_si(v, -1);
770         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
771         isl_int_set_si(v, -3);
772         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
773         isl_int_set_si(v, -3);
774         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
775         isl_int_set_si(v, 6);
776         isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
777         bset = isl_basic_set_add_constraint(bset, c);
778
779         c = isl_equality_alloc(isl_local_space_copy(ls));
780         isl_int_set_si(v, -1);
781         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
782         isl_int_set_si(v, 1);
783         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
784         isl_int_set_si(v, 1);
785         isl_constraint_set_constant(c, v);
786         bset = isl_basic_set_add_constraint(bset, c);
787
788         bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
789
790         /* Test disabled for now */
791         /*
792         assert(bset && bset->n_div == 1);
793         */
794         isl_local_space_free(ls);
795         isl_basic_set_free(bset);
796
797         /* test 9 */
798         dim = isl_space_set_alloc(ctx, 0, 4);
799         bset = isl_basic_set_universe(isl_space_copy(dim));
800         ls = isl_local_space_from_space(dim);
801
802         c = isl_equality_alloc(isl_local_space_copy(ls));
803         isl_int_set_si(v, 1);
804         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
805         isl_int_set_si(v, -1);
806         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
807         isl_int_set_si(v, -2);
808         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
809         bset = isl_basic_set_add_constraint(bset, c);
810
811         c = isl_equality_alloc(isl_local_space_copy(ls));
812         isl_int_set_si(v, -1);
813         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
814         isl_int_set_si(v, 3);
815         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
816         isl_int_set_si(v, 2);
817         isl_constraint_set_constant(c, v);
818         bset = isl_basic_set_add_constraint(bset, c);
819
820         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
821
822         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
823
824         assert(!isl_basic_set_is_empty(bset));
825
826         isl_local_space_free(ls);
827         isl_basic_set_free(bset);
828
829         /* test 10 */
830         dim = isl_space_set_alloc(ctx, 0, 3);
831         bset = isl_basic_set_universe(isl_space_copy(dim));
832         ls = isl_local_space_from_space(dim);
833
834         c = isl_equality_alloc(isl_local_space_copy(ls));
835         isl_int_set_si(v, 1);
836         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
837         isl_int_set_si(v, -2);
838         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
839         bset = isl_basic_set_add_constraint(bset, c);
840
841         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
842
843         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
844
845         isl_local_space_free(ls);
846         isl_basic_set_free(bset);
847
848         isl_int_clear(v);
849
850         str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
851             "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
852         set = isl_set_read_from_str(ctx, str);
853         set = isl_set_compute_divs(set);
854         isl_set_free(set);
855         if (!set)
856                 return -1;
857
858         str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
859         bset = isl_basic_set_read_from_str(ctx, str);
860         n = isl_basic_set_dim(bset, isl_dim_div);
861         isl_basic_set_free(bset);
862         if (!bset)
863                 return -1;
864         if (n != 0)
865                 isl_die(ctx, isl_error_unknown,
866                         "expecting no existentials", return -1);
867
868         str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
869         set = isl_set_read_from_str(ctx, str);
870         set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
871         set = isl_set_fix_si(set, isl_dim_set, 2, -3);
872         empty = isl_set_is_empty(set);
873         isl_set_free(set);
874         if (empty < 0)
875                 return -1;
876         if (!empty)
877                 isl_die(ctx, isl_error_unknown,
878                         "result not as accurate as expected", return -1);
879
880         return 0;
881 }
882
883 void test_application_case(struct isl_ctx *ctx, const char *name)
884 {
885         char *filename;
886         FILE *input;
887         struct isl_basic_set *bset1, *bset2;
888         struct isl_basic_map *bmap;
889
890         filename = get_filename(ctx, name, "omega");
891         assert(filename);
892         input = fopen(filename, "r");
893         assert(input);
894
895         bset1 = isl_basic_set_read_from_file(ctx, input);
896         bmap = isl_basic_map_read_from_file(ctx, input);
897
898         bset1 = isl_basic_set_apply(bset1, bmap);
899
900         bset2 = isl_basic_set_read_from_file(ctx, input);
901
902         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
903
904         isl_basic_set_free(bset1);
905         isl_basic_set_free(bset2);
906         free(filename);
907
908         fclose(input);
909 }
910
911 void test_application(struct isl_ctx *ctx)
912 {
913         test_application_case(ctx, "application");
914         test_application_case(ctx, "application2");
915 }
916
917 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
918 {
919         char *filename;
920         FILE *input;
921         struct isl_basic_set *bset1, *bset2;
922
923         filename = get_filename(ctx, name, "polylib");
924         assert(filename);
925         input = fopen(filename, "r");
926         assert(input);
927
928         bset1 = isl_basic_set_read_from_file(ctx, input);
929         bset2 = isl_basic_set_read_from_file(ctx, input);
930
931         bset1 = isl_basic_set_affine_hull(bset1);
932
933         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
934
935         isl_basic_set_free(bset1);
936         isl_basic_set_free(bset2);
937         free(filename);
938
939         fclose(input);
940 }
941
942 int test_affine_hull(struct isl_ctx *ctx)
943 {
944         const char *str;
945         isl_set *set;
946         isl_basic_set *bset, *bset2;
947         int n;
948         int subset;
949
950         test_affine_hull_case(ctx, "affine2");
951         test_affine_hull_case(ctx, "affine");
952         test_affine_hull_case(ctx, "affine3");
953
954         str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
955                         "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
956                         "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
957                         "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
958         set = isl_set_read_from_str(ctx, str);
959         bset = isl_set_affine_hull(set);
960         n = isl_basic_set_dim(bset, isl_dim_div);
961         isl_basic_set_free(bset);
962         if (n != 0)
963                 isl_die(ctx, isl_error_unknown, "not expecting any divs",
964                         return -1);
965
966         /* Check that isl_map_affine_hull is not confused by
967          * the reordering of divs in isl_map_align_divs.
968          */
969         str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
970                                 "32e0 = b and 32e1 = c); "
971                 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
972         set = isl_set_read_from_str(ctx, str);
973         bset = isl_set_affine_hull(set);
974         isl_basic_set_free(bset);
975         if (!bset)
976                 return -1;
977
978         str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
979                         "32e2 = 31 + 31e0 }";
980         set = isl_set_read_from_str(ctx, str);
981         bset = isl_set_affine_hull(set);
982         str = "{ [a] : exists e : a = 32 e }";
983         bset2 = isl_basic_set_read_from_str(ctx, str);
984         subset = isl_basic_set_is_subset(bset, bset2);
985         isl_basic_set_free(bset);
986         isl_basic_set_free(bset2);
987         if (subset < 0)
988                 return -1;
989         if (!subset)
990                 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
991                         return -1);
992
993         return 0;
994 }
995
996 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
997 {
998         char *filename;
999         FILE *input;
1000         struct isl_basic_set *bset1, *bset2;
1001         struct isl_set *set;
1002
1003         filename = get_filename(ctx, name, "polylib");
1004         assert(filename);
1005         input = fopen(filename, "r");
1006         assert(input);
1007
1008         bset1 = isl_basic_set_read_from_file(ctx, input);
1009         bset2 = isl_basic_set_read_from_file(ctx, input);
1010
1011         set = isl_basic_set_union(bset1, bset2);
1012         bset1 = isl_set_convex_hull(set);
1013
1014         bset2 = isl_basic_set_read_from_file(ctx, input);
1015
1016         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1017
1018         isl_basic_set_free(bset1);
1019         isl_basic_set_free(bset2);
1020         free(filename);
1021
1022         fclose(input);
1023 }
1024
1025 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1026 {
1027         const char *str1, *str2;
1028         isl_set *set1, *set2;
1029         int orig_convex = ctx->opt->convex;
1030         ctx->opt->convex = convex;
1031
1032         test_convex_hull_case(ctx, "convex0");
1033         test_convex_hull_case(ctx, "convex1");
1034         test_convex_hull_case(ctx, "convex2");
1035         test_convex_hull_case(ctx, "convex3");
1036         test_convex_hull_case(ctx, "convex4");
1037         test_convex_hull_case(ctx, "convex5");
1038         test_convex_hull_case(ctx, "convex6");
1039         test_convex_hull_case(ctx, "convex7");
1040         test_convex_hull_case(ctx, "convex8");
1041         test_convex_hull_case(ctx, "convex9");
1042         test_convex_hull_case(ctx, "convex10");
1043         test_convex_hull_case(ctx, "convex11");
1044         test_convex_hull_case(ctx, "convex12");
1045         test_convex_hull_case(ctx, "convex13");
1046         test_convex_hull_case(ctx, "convex14");
1047         test_convex_hull_case(ctx, "convex15");
1048
1049         str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1050                "(i0 = 1 and i1 = 0 and i2 = 1) or "
1051                "(i0 = 0 and i1 = 0 and i2 = 0) }";
1052         str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
1053         set1 = isl_set_read_from_str(ctx, str1);
1054         set2 = isl_set_read_from_str(ctx, str2);
1055         set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1056         assert(isl_set_is_equal(set1, set2));
1057         isl_set_free(set1);
1058         isl_set_free(set2);
1059
1060         ctx->opt->convex = orig_convex;
1061 }
1062
1063 void test_convex_hull(struct isl_ctx *ctx)
1064 {
1065         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1066         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1067 }
1068
1069 void test_gist_case(struct isl_ctx *ctx, const char *name)
1070 {
1071         char *filename;
1072         FILE *input;
1073         struct isl_basic_set *bset1, *bset2;
1074
1075         filename = get_filename(ctx, name, "polylib");
1076         assert(filename);
1077         input = fopen(filename, "r");
1078         assert(input);
1079
1080         bset1 = isl_basic_set_read_from_file(ctx, input);
1081         bset2 = isl_basic_set_read_from_file(ctx, input);
1082
1083         bset1 = isl_basic_set_gist(bset1, bset2);
1084
1085         bset2 = isl_basic_set_read_from_file(ctx, input);
1086
1087         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1088
1089         isl_basic_set_free(bset1);
1090         isl_basic_set_free(bset2);
1091         free(filename);
1092
1093         fclose(input);
1094 }
1095
1096 static int test_gist(struct isl_ctx *ctx)
1097 {
1098         const char *str;
1099         isl_basic_set *bset1, *bset2;
1100         isl_map *map1, *map2;
1101
1102         test_gist_case(ctx, "gist1");
1103
1104         str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1105             "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1106             "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1107             "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1108             "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1109             "16e0 >= 16 + 16p6 + 15p10 and  p2 <= 15 and p3 >= 0 and "
1110             "p3 <= 31 and  p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1111             "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1112             "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1113         bset1 = isl_basic_set_read_from_str(ctx, str);
1114         str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1115             "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1116             "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1117             "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1118             "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1119             "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1120             "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1121             "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1122         bset2 = isl_basic_set_read_from_str(ctx, str);
1123         bset1 = isl_basic_set_gist(bset1, bset2);
1124         assert(bset1 && bset1->n_div == 0);
1125         isl_basic_set_free(bset1);
1126
1127         /* Check that the integer divisions of the second disjunct
1128          * do not spread to the first disjunct.
1129          */
1130         str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1131                 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1132                 "(exists (e0 = [(-1 + t1)/16], "
1133                         "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1134                         "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1135                         "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1136                         "o0 <= 4294967295 and t1 <= -1)) }";
1137         map1 = isl_map_read_from_str(ctx, str);
1138         str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1139         map2 = isl_map_read_from_str(ctx, str);
1140         map1 = isl_map_gist(map1, map2);
1141         if (!map1)
1142                 return -1;
1143         if (map1->n != 1)
1144                 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1145                         isl_map_free(map1); return -1);
1146         if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1147                 isl_die(ctx, isl_error_unknown, "expecting single div",
1148                         isl_map_free(map1); return -1);
1149         isl_map_free(map1);
1150
1151         return 0;
1152 }
1153
1154 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1155 {
1156         isl_set *set, *set2;
1157         int equal;
1158         int one;
1159
1160         set = isl_set_read_from_str(ctx, str);
1161         set = isl_set_coalesce(set);
1162         set2 = isl_set_read_from_str(ctx, str);
1163         equal = isl_set_is_equal(set, set2);
1164         one = set && set->n == 1;
1165         isl_set_free(set);
1166         isl_set_free(set2);
1167
1168         if (equal < 0)
1169                 return -1;
1170         if (!equal)
1171                 isl_die(ctx, isl_error_unknown,
1172                         "coalesced set not equal to input", return -1);
1173         if (check_one && !one)
1174                 isl_die(ctx, isl_error_unknown,
1175                         "coalesced set should not be a union", return -1);
1176
1177         return 0;
1178 }
1179
1180 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1181 {
1182         int r = 0;
1183         int bounded;
1184
1185         bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1186         isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1187
1188         if (test_coalesce_set(ctx,
1189                 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1190                         "-x - y + 1 >= 0 and -3 <= z <= 3;"
1191                 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1192                         "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1193                         "-10 <= y <= 0}", 1) < 0)
1194                 goto error;
1195         if (test_coalesce_set(ctx,
1196                 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1197                 goto error;
1198         if (test_coalesce_set(ctx,
1199                 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1200                 goto error;
1201
1202         if (0) {
1203 error:
1204                 r = -1;
1205         }
1206
1207         isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1208
1209         return r;
1210 }
1211
1212 /* Inputs for coalescing tests.
1213  * "str" is a string representation of the input set.
1214  * "single_disjunct" is set if we expect the result to consist of
1215  *      a single disjunct.
1216  */
1217 struct {
1218         int single_disjunct;
1219         const char *str;
1220 } coalesce_tests[] = {
1221         { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1222                        "y >= x & x >= 2 & 5 >= y }" },
1223         { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1224                        "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1225         { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1226                        "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1227         { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1228                        "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1229         { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1230                        "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1231         { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1232                        "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1233         { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1234         { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1235         { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1236         { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1237         { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1238         { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1239         { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1240         { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1241                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1242                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1243                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1244                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1245                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1246                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1247                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1248                 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1249                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1250                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1251                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1252                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1253                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1254                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1255                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1256                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1257                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1258         { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1259               "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1260               "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1261               "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1262         { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1263           "[o0, o1, o2, o3, o4, o5, o6]] : "
1264           "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1265           "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1266           "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1267           "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1268           "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1269           "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1270           "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1271           "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1272           "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1273           "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1274           "o6 >= i3 + i6 - o3 and M >= 0 and "
1275           "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1276           "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1277         { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1278                 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1279                 "(o0 = 0 and M >= 2 and N >= 3) or "
1280                 "(M = 0 and o0 = 0 and N >= 3) }" },
1281         { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1282             "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1283             "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1284             "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1285         { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1286         { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1287         { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1288                 "(y = 3 and x = 1) }" },
1289         { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1290                 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1291                 "i1 <= M and i3 <= M and i4 <= M) or "
1292                 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1293                 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1294                 "i4 <= -1 + M) }" },
1295         { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1296                 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1297         { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1298         { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1299         { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1300         { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1301         { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1302         { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1303         { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1304         { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1305         { 0, "{ [a, b] : exists e : 2e = a and "
1306                     "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1307         { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1308                         "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1309                         "j >= 1 and j' <= i + j - i' and i >= 1; "
1310                 "[1, 1, 1, 1] }" },
1311         { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1312                  "[i,j] : exists a : j = 3a }" },
1313         { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1314                         "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1315                         "a >= 3) or "
1316                     "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1317                         "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1318         { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1319                                 "c <= 6 + 8a and a >= 3; "
1320                 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1321                                 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1322         { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1323                 "[x,0] : 3 <= x <= 4 }" },
1324         { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1325                 "[x,0] : 4 <= x <= 5 }" },
1326         { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1327                 "[x,0] : 3 <= x <= 5 }" },
1328         { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1329                 "[x,0] : 3 <= x <= 4 }" },
1330         { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1331                         "i1 <= 0; "
1332                 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1333 };
1334
1335 /* Test the functionality of isl_set_coalesce.
1336  * That is, check that the output is always equal to the input
1337  * and in some cases that the result consists of a single disjunct.
1338  */
1339 static int test_coalesce(struct isl_ctx *ctx)
1340 {
1341         int i;
1342
1343         for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1344                 const char *str = coalesce_tests[i].str;
1345                 int check_one = coalesce_tests[i].single_disjunct;
1346                 if (test_coalesce_set(ctx, str, check_one) < 0)
1347                         return -1;
1348         }
1349
1350         if (test_coalesce_unbounded_wrapping(ctx) < 0)
1351                 return -1;
1352
1353         return 0;
1354 }
1355
1356 void test_closure(struct isl_ctx *ctx)
1357 {
1358         const char *str;
1359         isl_set *dom;
1360         isl_map *up, *right;
1361         isl_map *map, *map2;
1362         int exact;
1363
1364         /* COCOA example 1 */
1365         map = isl_map_read_from_str(ctx,
1366                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1367                         "1 <= i and i < n and 1 <= j and j < n or "
1368                         "i2 = i + 1 and j2 = j - 1 and "
1369                         "1 <= i and i < n and 2 <= j and j <= n }");
1370         map = isl_map_power(map, &exact);
1371         assert(exact);
1372         isl_map_free(map);
1373
1374         /* COCOA example 1 */
1375         map = isl_map_read_from_str(ctx,
1376                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1377                         "1 <= i and i < n and 1 <= j and j < n or "
1378                         "i2 = i + 1 and j2 = j - 1 and "
1379                         "1 <= i and i < n and 2 <= j and j <= n }");
1380         map = isl_map_transitive_closure(map, &exact);
1381         assert(exact);
1382         map2 = isl_map_read_from_str(ctx,
1383                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1384                         "1 <= i and i < n and 1 <= j and j <= n and "
1385                         "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1386                         "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1387                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1388         assert(isl_map_is_equal(map, map2));
1389         isl_map_free(map2);
1390         isl_map_free(map);
1391
1392         map = isl_map_read_from_str(ctx,
1393                 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1394                                      " 0 <= y and y <= n }");
1395         map = isl_map_transitive_closure(map, &exact);
1396         map2 = isl_map_read_from_str(ctx,
1397                 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1398                                      " 0 <= y and y <= n }");
1399         assert(isl_map_is_equal(map, map2));
1400         isl_map_free(map2);
1401         isl_map_free(map);
1402
1403         /* COCOA example 2 */
1404         map = isl_map_read_from_str(ctx,
1405                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1406                         "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1407                         "i2 = i + 2 and j2 = j - 2 and "
1408                         "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1409         map = isl_map_transitive_closure(map, &exact);
1410         assert(exact);
1411         map2 = isl_map_read_from_str(ctx,
1412                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1413                         "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1414                         "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1415                         "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1416                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1417         assert(isl_map_is_equal(map, map2));
1418         isl_map_free(map);
1419         isl_map_free(map2);
1420
1421         /* COCOA Fig.2 left */
1422         map = isl_map_read_from_str(ctx,
1423                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1424                         "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1425                         "j <= n or "
1426                         "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1427                         "j <= 2 i - 3 and j <= n - 2 or "
1428                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1429                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1430         map = isl_map_transitive_closure(map, &exact);
1431         assert(exact);
1432         isl_map_free(map);
1433
1434         /* COCOA Fig.2 right */
1435         map = isl_map_read_from_str(ctx,
1436                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1437                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1438                         "j <= n or "
1439                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1440                         "j <= 2 i - 4 and j <= n - 3 or "
1441                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1442                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1443         map = isl_map_power(map, &exact);
1444         assert(exact);
1445         isl_map_free(map);
1446
1447         /* COCOA Fig.2 right */
1448         map = isl_map_read_from_str(ctx,
1449                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1450                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1451                         "j <= n or "
1452                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1453                         "j <= 2 i - 4 and j <= n - 3 or "
1454                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1455                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1456         map = isl_map_transitive_closure(map, &exact);
1457         assert(exact);
1458         map2 = isl_map_read_from_str(ctx,
1459                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1460                         "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1461                         "j <= n and 3 + i + 2 j <= 3 n and "
1462                         "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1463                         "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1464                         "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1465                         "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1466                         "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1467         assert(isl_map_is_equal(map, map2));
1468         isl_map_free(map2);
1469         isl_map_free(map);
1470
1471         /* COCOA Fig.1 right */
1472         dom = isl_set_read_from_str(ctx,
1473                 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1474                         "2 x - 3 y + 3 >= 0 }");
1475         right = isl_map_read_from_str(ctx,
1476                 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1477         up = isl_map_read_from_str(ctx,
1478                 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1479         right = isl_map_intersect_domain(right, isl_set_copy(dom));
1480         right = isl_map_intersect_range(right, isl_set_copy(dom));
1481         up = isl_map_intersect_domain(up, isl_set_copy(dom));
1482         up = isl_map_intersect_range(up, dom);
1483         map = isl_map_union(up, right);
1484         map = isl_map_transitive_closure(map, &exact);
1485         assert(exact);
1486         map2 = isl_map_read_from_str(ctx,
1487                 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1488                 "  [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1489         assert(isl_map_is_equal(map, map2));
1490         isl_map_free(map2);
1491         isl_map_free(map);
1492
1493         /* COCOA Theorem 1 counter example */
1494         map = isl_map_read_from_str(ctx,
1495                 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1496                         "i2 = 1 and j2 = j or "
1497                         "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1498         map = isl_map_transitive_closure(map, &exact);
1499         assert(exact);
1500         isl_map_free(map);
1501
1502         map = isl_map_read_from_str(ctx,
1503                 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1504                         "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1505                         "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1506                         "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1507         map = isl_map_transitive_closure(map, &exact);
1508         assert(exact);
1509         isl_map_free(map);
1510
1511         /* Kelly et al 1996, fig 12 */
1512         map = isl_map_read_from_str(ctx,
1513                 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1514                         "1 <= i,j,j+1 <= n or "
1515                         "j = n and j2 = 1 and i2 = i + 1 and "
1516                         "1 <= i,i+1 <= n }");
1517         map = isl_map_transitive_closure(map, &exact);
1518         assert(exact);
1519         map2 = isl_map_read_from_str(ctx,
1520                 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1521                         "1 <= i <= n and i = i2 or "
1522                         "1 <= i < i2 <= n and 1 <= j <= n and "
1523                         "1 <= j2 <= n }");
1524         assert(isl_map_is_equal(map, map2));
1525         isl_map_free(map2);
1526         isl_map_free(map);
1527
1528         /* Omega's closure4 */
1529         map = isl_map_read_from_str(ctx,
1530                 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1531                         "1 <= x,y <= 10 or "
1532                         "x2 = x + 1 and y2 = y and "
1533                         "1 <= x <= 20 && 5 <= y <= 15 }");
1534         map = isl_map_transitive_closure(map, &exact);
1535         assert(exact);
1536         isl_map_free(map);
1537
1538         map = isl_map_read_from_str(ctx,
1539                 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1540         map = isl_map_transitive_closure(map, &exact);
1541         assert(!exact);
1542         map2 = isl_map_read_from_str(ctx,
1543                 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1544         assert(isl_map_is_equal(map, map2));
1545         isl_map_free(map);
1546         isl_map_free(map2);
1547
1548         str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1549             "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1550             "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1551             "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1552         map = isl_map_read_from_str(ctx, str);
1553         map = isl_map_transitive_closure(map, &exact);
1554         assert(exact);
1555         map2 = isl_map_read_from_str(ctx, str);
1556         assert(isl_map_is_equal(map, map2));
1557         isl_map_free(map);
1558         isl_map_free(map2);
1559
1560         str = "{[0] -> [1]; [2] -> [3]}";
1561         map = isl_map_read_from_str(ctx, str);
1562         map = isl_map_transitive_closure(map, &exact);
1563         assert(exact);
1564         map2 = isl_map_read_from_str(ctx, str);
1565         assert(isl_map_is_equal(map, map2));
1566         isl_map_free(map);
1567         isl_map_free(map2);
1568
1569         str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1570             "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1571             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1572             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1573             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1574             "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1575             "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1576             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1577             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1578             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1579             "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1580             "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1581             "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1582             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1583             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1584             "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1585             "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1586             "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1587             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1588             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1589         map = isl_map_read_from_str(ctx, str);
1590         map = isl_map_transitive_closure(map, NULL);
1591         assert(map);
1592         isl_map_free(map);
1593 }
1594
1595 void test_lex(struct isl_ctx *ctx)
1596 {
1597         isl_space *dim;
1598         isl_map *map;
1599
1600         dim = isl_space_set_alloc(ctx, 0, 0);
1601         map = isl_map_lex_le(dim);
1602         assert(!isl_map_is_empty(map));
1603         isl_map_free(map);
1604 }
1605
1606 static int test_lexmin(struct isl_ctx *ctx)
1607 {
1608         int equal;
1609         const char *str;
1610         isl_basic_map *bmap;
1611         isl_map *map, *map2;
1612         isl_set *set;
1613         isl_set *set2;
1614         isl_pw_multi_aff *pma;
1615
1616         str = "[p0, p1] -> { [] -> [] : "
1617             "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1618             "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1619             "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1620             "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1621             "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1622             "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1623             "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1624             "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1625             "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1626             "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1627             "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1628         map = isl_map_read_from_str(ctx, str);
1629         map = isl_map_lexmin(map);
1630         isl_map_free(map);
1631
1632         str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1633             "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1634         set = isl_set_read_from_str(ctx, str);
1635         set = isl_set_lexmax(set);
1636         str = "[C] -> { [obj,a,b,c] : C = 8 }";
1637         set2 = isl_set_read_from_str(ctx, str);
1638         set = isl_set_intersect(set, set2);
1639         assert(!isl_set_is_empty(set));
1640         isl_set_free(set);
1641
1642         str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1643         map = isl_map_read_from_str(ctx, str);
1644         map = isl_map_lexmin(map);
1645         str = "{ [x] -> [5] : 6 <= x <= 8; "
1646                 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1647         map2 = isl_map_read_from_str(ctx, str);
1648         assert(isl_map_is_equal(map, map2));
1649         isl_map_free(map);
1650         isl_map_free(map2);
1651
1652         str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1653         map = isl_map_read_from_str(ctx, str);
1654         map2 = isl_map_copy(map);
1655         map = isl_map_lexmin(map);
1656         assert(isl_map_is_equal(map, map2));
1657         isl_map_free(map);
1658         isl_map_free(map2);
1659
1660         str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1661         map = isl_map_read_from_str(ctx, str);
1662         map = isl_map_lexmin(map);
1663         str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1664                 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1665                 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1666                 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1667         map2 = isl_map_read_from_str(ctx, str);
1668         assert(isl_map_is_equal(map, map2));
1669         isl_map_free(map);
1670         isl_map_free(map2);
1671
1672         str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1673                                 " 8i' <= i and 8i' >= -7 + i }";
1674         bmap = isl_basic_map_read_from_str(ctx, str);
1675         pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1676         map2 = isl_map_from_pw_multi_aff(pma);
1677         map = isl_map_from_basic_map(bmap);
1678         assert(isl_map_is_equal(map, map2));
1679         isl_map_free(map);
1680         isl_map_free(map2);
1681
1682         str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1683         map = isl_map_read_from_str(ctx, str);
1684         map = isl_map_lexmin(map);
1685         str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1686         map2 = isl_map_read_from_str(ctx, str);
1687         assert(isl_map_is_equal(map, map2));
1688         isl_map_free(map);
1689         isl_map_free(map2);
1690
1691         /* Check that empty pieces are properly combined. */
1692         str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1693                 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1694                 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1695         map = isl_map_read_from_str(ctx, str);
1696         map = isl_map_lexmin(map);
1697         str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1698                 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1699                 "x >= 4 }";
1700         map2 = isl_map_read_from_str(ctx, str);
1701         assert(isl_map_is_equal(map, map2));
1702         isl_map_free(map);
1703         isl_map_free(map2);
1704
1705         str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1706                                 " 8i' <= i and 8i' >= -7 + i }";
1707         set = isl_set_read_from_str(ctx, str);
1708         pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1709         set2 = isl_set_from_pw_multi_aff(pma);
1710         equal = isl_set_is_equal(set, set2);
1711         isl_set_free(set);
1712         isl_set_free(set2);
1713         if (equal < 0)
1714                 return -1;
1715         if (!equal)
1716                 isl_die(ctx, isl_error_unknown,
1717                         "unexpected difference between set and "
1718                         "piecewise affine expression", return -1);
1719
1720         return 0;
1721 }
1722
1723 struct must_may {
1724         isl_map *must;
1725         isl_map *may;
1726 };
1727
1728 static int collect_must_may(__isl_take isl_map *dep, int must,
1729         void *dep_user, void *user)
1730 {
1731         struct must_may *mm = (struct must_may *)user;
1732
1733         if (must)
1734                 mm->must = isl_map_union(mm->must, dep);
1735         else
1736                 mm->may = isl_map_union(mm->may, dep);
1737
1738         return 0;
1739 }
1740
1741 static int common_space(void *first, void *second)
1742 {
1743         int depth = *(int *)first;
1744         return 2 * depth;
1745 }
1746
1747 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1748 {
1749         isl_map *map2;
1750         int equal;
1751
1752         if (!map)
1753                 return -1;
1754
1755         map2 = isl_map_read_from_str(map->ctx, str);
1756         equal = isl_map_is_equal(map, map2);
1757         isl_map_free(map2);
1758
1759         return equal;
1760 }
1761
1762 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1763 {
1764         int equal;
1765
1766         equal = map_is_equal(map, str);
1767         if (equal < 0)
1768                 return -1;
1769         if (!equal)
1770                 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1771                         "result not as expected", return -1);
1772         return 0;
1773 }
1774
1775 void test_dep(struct isl_ctx *ctx)
1776 {
1777         const char *str;
1778         isl_space *dim;
1779         isl_map *map;
1780         isl_access_info *ai;
1781         isl_flow *flow;
1782         int depth;
1783         struct must_may mm;
1784
1785         depth = 3;
1786
1787         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1788         map = isl_map_read_from_str(ctx, str);
1789         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1790
1791         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1792         map = isl_map_read_from_str(ctx, str);
1793         ai = isl_access_info_add_source(ai, map, 1, &depth);
1794
1795         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1796         map = isl_map_read_from_str(ctx, str);
1797         ai = isl_access_info_add_source(ai, map, 1, &depth);
1798
1799         flow = isl_access_info_compute_flow(ai);
1800         dim = isl_space_alloc(ctx, 0, 3, 3);
1801         mm.must = isl_map_empty(isl_space_copy(dim));
1802         mm.may = isl_map_empty(dim);
1803
1804         isl_flow_foreach(flow, collect_must_may, &mm);
1805
1806         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1807               "  [1,10,0] -> [2,5,0] }";
1808         assert(map_is_equal(mm.must, str));
1809         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1810         assert(map_is_equal(mm.may, str));
1811
1812         isl_map_free(mm.must);
1813         isl_map_free(mm.may);
1814         isl_flow_free(flow);
1815
1816
1817         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1818         map = isl_map_read_from_str(ctx, str);
1819         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1820
1821         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1822         map = isl_map_read_from_str(ctx, str);
1823         ai = isl_access_info_add_source(ai, map, 1, &depth);
1824
1825         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1826         map = isl_map_read_from_str(ctx, str);
1827         ai = isl_access_info_add_source(ai, map, 0, &depth);
1828
1829         flow = isl_access_info_compute_flow(ai);
1830         dim = isl_space_alloc(ctx, 0, 3, 3);
1831         mm.must = isl_map_empty(isl_space_copy(dim));
1832         mm.may = isl_map_empty(dim);
1833
1834         isl_flow_foreach(flow, collect_must_may, &mm);
1835
1836         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1837         assert(map_is_equal(mm.must, str));
1838         str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1839         assert(map_is_equal(mm.may, str));
1840
1841         isl_map_free(mm.must);
1842         isl_map_free(mm.may);
1843         isl_flow_free(flow);
1844
1845
1846         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1847         map = isl_map_read_from_str(ctx, str);
1848         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1849
1850         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1851         map = isl_map_read_from_str(ctx, str);
1852         ai = isl_access_info_add_source(ai, map, 0, &depth);
1853
1854         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1855         map = isl_map_read_from_str(ctx, str);
1856         ai = isl_access_info_add_source(ai, map, 0, &depth);
1857
1858         flow = isl_access_info_compute_flow(ai);
1859         dim = isl_space_alloc(ctx, 0, 3, 3);
1860         mm.must = isl_map_empty(isl_space_copy(dim));
1861         mm.may = isl_map_empty(dim);
1862
1863         isl_flow_foreach(flow, collect_must_may, &mm);
1864
1865         str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1866               "  [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1867         assert(map_is_equal(mm.may, str));
1868         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1869         assert(map_is_equal(mm.must, str));
1870
1871         isl_map_free(mm.must);
1872         isl_map_free(mm.may);
1873         isl_flow_free(flow);
1874
1875
1876         str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1877         map = isl_map_read_from_str(ctx, str);
1878         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1879
1880         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1881         map = isl_map_read_from_str(ctx, str);
1882         ai = isl_access_info_add_source(ai, map, 0, &depth);
1883
1884         str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1885         map = isl_map_read_from_str(ctx, str);
1886         ai = isl_access_info_add_source(ai, map, 0, &depth);
1887
1888         flow = isl_access_info_compute_flow(ai);
1889         dim = isl_space_alloc(ctx, 0, 3, 3);
1890         mm.must = isl_map_empty(isl_space_copy(dim));
1891         mm.may = isl_map_empty(dim);
1892
1893         isl_flow_foreach(flow, collect_must_may, &mm);
1894
1895         str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1896               "  [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1897         assert(map_is_equal(mm.may, str));
1898         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1899         assert(map_is_equal(mm.must, str));
1900
1901         isl_map_free(mm.must);
1902         isl_map_free(mm.may);
1903         isl_flow_free(flow);
1904
1905
1906         str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1907         map = isl_map_read_from_str(ctx, str);
1908         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1909
1910         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1911         map = isl_map_read_from_str(ctx, str);
1912         ai = isl_access_info_add_source(ai, map, 0, &depth);
1913
1914         str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1915         map = isl_map_read_from_str(ctx, str);
1916         ai = isl_access_info_add_source(ai, map, 0, &depth);
1917
1918         flow = isl_access_info_compute_flow(ai);
1919         dim = isl_space_alloc(ctx, 0, 3, 3);
1920         mm.must = isl_map_empty(isl_space_copy(dim));
1921         mm.may = isl_map_empty(dim);
1922
1923         isl_flow_foreach(flow, collect_must_may, &mm);
1924
1925         str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1926               "  [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1927         assert(map_is_equal(mm.may, str));
1928         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1929         assert(map_is_equal(mm.must, str));
1930
1931         isl_map_free(mm.must);
1932         isl_map_free(mm.may);
1933         isl_flow_free(flow);
1934
1935
1936         depth = 5;
1937
1938         str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1939         map = isl_map_read_from_str(ctx, str);
1940         ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1941
1942         str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1943         map = isl_map_read_from_str(ctx, str);
1944         ai = isl_access_info_add_source(ai, map, 1, &depth);
1945
1946         flow = isl_access_info_compute_flow(ai);
1947         dim = isl_space_alloc(ctx, 0, 5, 5);
1948         mm.must = isl_map_empty(isl_space_copy(dim));
1949         mm.may = isl_map_empty(dim);
1950
1951         isl_flow_foreach(flow, collect_must_may, &mm);
1952
1953         str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1954         assert(map_is_equal(mm.must, str));
1955         str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1956         assert(map_is_equal(mm.may, str));
1957
1958         isl_map_free(mm.must);
1959         isl_map_free(mm.may);
1960         isl_flow_free(flow);
1961 }
1962
1963 int test_sv(isl_ctx *ctx)
1964 {
1965         const char *str;
1966         isl_map *map;
1967         isl_union_map *umap;
1968         int sv;
1969
1970         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1971         map = isl_map_read_from_str(ctx, str);
1972         sv = isl_map_is_single_valued(map);
1973         isl_map_free(map);
1974         if (sv < 0)
1975                 return -1;
1976         if (!sv)
1977                 isl_die(ctx, isl_error_internal,
1978                         "map not detected as single valued", return -1);
1979
1980         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1981         map = isl_map_read_from_str(ctx, str);
1982         sv = isl_map_is_single_valued(map);
1983         isl_map_free(map);
1984         if (sv < 0)
1985                 return -1;
1986         if (sv)
1987                 isl_die(ctx, isl_error_internal,
1988                         "map detected as single valued", return -1);
1989
1990         str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1991         umap = isl_union_map_read_from_str(ctx, str);
1992         sv = isl_union_map_is_single_valued(umap);
1993         isl_union_map_free(umap);
1994         if (sv < 0)
1995                 return -1;
1996         if (!sv)
1997                 isl_die(ctx, isl_error_internal,
1998                         "map not detected as single valued", return -1);
1999
2000         str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
2001         umap = isl_union_map_read_from_str(ctx, str);
2002         sv = isl_union_map_is_single_valued(umap);
2003         isl_union_map_free(umap);
2004         if (sv < 0)
2005                 return -1;
2006         if (sv)
2007                 isl_die(ctx, isl_error_internal,
2008                         "map detected as single valued", return -1);
2009
2010         return 0;
2011 }
2012
2013 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2014 {
2015         isl_map *map;
2016
2017         map = isl_map_read_from_str(ctx, str);
2018         if (bijective)
2019                 assert(isl_map_is_bijective(map));
2020         else
2021                 assert(!isl_map_is_bijective(map));
2022         isl_map_free(map);
2023 }
2024
2025 void test_bijective(struct isl_ctx *ctx)
2026 {
2027         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2028         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2029         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2030         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2031         test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2032         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2033         test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2034         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2035         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2036         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2037         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2038         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2039         test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2040 }
2041
2042 static int test_pwqp(struct isl_ctx *ctx)
2043 {
2044         const char *str;
2045         isl_set *set;
2046         isl_pw_qpolynomial *pwqp1, *pwqp2;
2047         int equal;
2048
2049         str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2050         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2051
2052         pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2053                                                 isl_dim_in, 1, 1);
2054
2055         str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2056         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2057
2058         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2059
2060         assert(isl_pw_qpolynomial_is_zero(pwqp1));
2061
2062         isl_pw_qpolynomial_free(pwqp1);
2063
2064         str = "{ [i] -> i }";
2065         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2066         str = "{ [k] : exists a : k = 2a }";
2067         set = isl_set_read_from_str(ctx, str);
2068         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2069         str = "{ [i] -> i }";
2070         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2071
2072         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2073
2074         assert(isl_pw_qpolynomial_is_zero(pwqp1));
2075
2076         isl_pw_qpolynomial_free(pwqp1);
2077
2078         str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
2079         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2080         str = "{ [10] }";
2081         set = isl_set_read_from_str(ctx, str);
2082         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2083         str = "{ [i] -> 16 }";
2084         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2085
2086         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2087
2088         assert(isl_pw_qpolynomial_is_zero(pwqp1));
2089
2090         isl_pw_qpolynomial_free(pwqp1);
2091
2092         str = "{ [i] -> ([(i)/2]) }";
2093         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2094         str = "{ [k] : exists a : k = 2a+1 }";
2095         set = isl_set_read_from_str(ctx, str);
2096         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2097         str = "{ [i] -> -1/2 + 1/2 * i }";
2098         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2099
2100         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2101
2102         assert(isl_pw_qpolynomial_is_zero(pwqp1));
2103
2104         isl_pw_qpolynomial_free(pwqp1);
2105
2106         str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2107         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2108         str = "{ [i] -> ([(2 * [i/2])/5]) }";
2109         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2110
2111         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2112
2113         assert(isl_pw_qpolynomial_is_zero(pwqp1));
2114
2115         isl_pw_qpolynomial_free(pwqp1);
2116
2117         str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2118         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2119         str = "{ [x] -> x }";
2120         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2121
2122         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2123
2124         assert(isl_pw_qpolynomial_is_zero(pwqp1));
2125
2126         isl_pw_qpolynomial_free(pwqp1);
2127
2128         str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2129         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2130         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2131         pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2132         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2133         assert(isl_pw_qpolynomial_is_zero(pwqp1));
2134         isl_pw_qpolynomial_free(pwqp1);
2135
2136         str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2137         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2138         str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2139         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2140         set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2141         pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2142         equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2143         isl_pw_qpolynomial_free(pwqp1);
2144         isl_pw_qpolynomial_free(pwqp2);
2145         if (equal < 0)
2146                 return -1;
2147         if (!equal)
2148                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2149
2150         str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2151         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2152         str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2153         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2154         pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2155                                                 isl_val_one(ctx));
2156         equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2157         isl_pw_qpolynomial_free(pwqp1);
2158         isl_pw_qpolynomial_free(pwqp2);
2159         if (equal < 0)
2160                 return -1;
2161         if (!equal)
2162                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2163
2164         return 0;
2165 }
2166
2167 void test_split_periods(isl_ctx *ctx)
2168 {
2169         const char *str;
2170         isl_pw_qpolynomial *pwqp;
2171
2172         str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2173                 "U + 2V + 3 >= 0 and - U -2V  >= 0 and - U + 10 >= 0 and "
2174                 "U  >= 0; [U,V] -> U^2 : U >= 100 }";
2175         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2176
2177         pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2178         assert(pwqp);
2179
2180         isl_pw_qpolynomial_free(pwqp);
2181 }
2182
2183 void test_union(isl_ctx *ctx)
2184 {
2185         const char *str;
2186         isl_union_set *uset1, *uset2;
2187         isl_union_map *umap1, *umap2;
2188
2189         str = "{ [i] : 0 <= i <= 1 }";
2190         uset1 = isl_union_set_read_from_str(ctx, str);
2191         str = "{ [1] -> [0] }";
2192         umap1 = isl_union_map_read_from_str(ctx, str);
2193
2194         umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2195         assert(isl_union_map_is_equal(umap1, umap2));
2196
2197         isl_union_map_free(umap1);
2198         isl_union_map_free(umap2);
2199
2200         str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2201         umap1 = isl_union_map_read_from_str(ctx, str);
2202         str = "{ A[i]; B[i] }";
2203         uset1 = isl_union_set_read_from_str(ctx, str);
2204
2205         uset2 = isl_union_map_domain(umap1);
2206
2207         assert(isl_union_set_is_equal(uset1, uset2));
2208
2209         isl_union_set_free(uset1);
2210         isl_union_set_free(uset2);
2211 }
2212
2213 void test_bound(isl_ctx *ctx)
2214 {
2215         const char *str;
2216         isl_pw_qpolynomial *pwqp;
2217         isl_pw_qpolynomial_fold *pwf;
2218
2219         str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2220         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2221         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2222         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2223         isl_pw_qpolynomial_fold_free(pwf);
2224
2225         str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2226         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2227         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2228         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2229         isl_pw_qpolynomial_fold_free(pwf);
2230 }
2231
2232 void test_lift(isl_ctx *ctx)
2233 {
2234         const char *str;
2235         isl_basic_map *bmap;
2236         isl_basic_set *bset;
2237
2238         str = "{ [i0] : exists e0 : i0 = 4e0 }";
2239         bset = isl_basic_set_read_from_str(ctx, str);
2240         bset = isl_basic_set_lift(bset);
2241         bmap = isl_basic_map_from_range(bset);
2242         bset = isl_basic_map_domain(bmap);
2243         isl_basic_set_free(bset);
2244 }
2245
2246 struct {
2247         const char *set1;
2248         const char *set2;
2249         int subset;
2250 } subset_tests[] = {
2251         { "{ [112, 0] }",
2252           "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2253                 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2254                 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2255         { "{ [65] }",
2256           "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2257                 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2258                 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2259                     "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2260                     "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2261                     "256e0 <= 255i and 256e0 >= -255 + 255i and "
2262                     "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2263                     "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2264                     "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2265         { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2266         { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2267         { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2268         { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2269 };
2270
2271 static int test_subset(isl_ctx *ctx)
2272 {
2273         int i;
2274         isl_set *set1, *set2;
2275         int subset;
2276
2277         for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2278                 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2279                 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2280                 subset = isl_set_is_subset(set1, set2);
2281                 isl_set_free(set1);
2282                 isl_set_free(set2);
2283                 if (subset < 0)
2284                         return -1;
2285                 if (subset != subset_tests[i].subset)
2286                         isl_die(ctx, isl_error_unknown,
2287                                 "incorrect subset result", return -1);
2288         }
2289
2290         return 0;
2291 }
2292
2293 struct {
2294         const char *minuend;
2295         const char *subtrahend;
2296         const char *difference;
2297 } subtract_domain_tests[] = {
2298         { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2299         { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2300         { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2301 };
2302
2303 static int test_subtract(isl_ctx *ctx)
2304 {
2305         int i;
2306         isl_union_map *umap1, *umap2;
2307         isl_union_set *uset;
2308         int equal;
2309
2310         for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2311                 umap1 = isl_union_map_read_from_str(ctx,
2312                                 subtract_domain_tests[i].minuend);
2313                 uset = isl_union_set_read_from_str(ctx,
2314                                 subtract_domain_tests[i].subtrahend);
2315                 umap2 = isl_union_map_read_from_str(ctx,
2316                                 subtract_domain_tests[i].difference);
2317                 umap1 = isl_union_map_subtract_domain(umap1, uset);
2318                 equal = isl_union_map_is_equal(umap1, umap2);
2319                 isl_union_map_free(umap1);
2320                 isl_union_map_free(umap2);
2321                 if (equal < 0)
2322                         return -1;
2323                 if (!equal)
2324                         isl_die(ctx, isl_error_unknown,
2325                                 "incorrect subtract domain result", return -1);
2326         }
2327
2328         return 0;
2329 }
2330
2331 int test_factorize(isl_ctx *ctx)
2332 {
2333         const char *str;
2334         isl_basic_set *bset;
2335         isl_factorizer *f;
2336
2337         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2338             "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2339             "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2340             "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2341             "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2342             "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2343             "3i5 >= -2i0 - i2 + 3i4 }";
2344         bset = isl_basic_set_read_from_str(ctx, str);
2345         f = isl_basic_set_factorizer(bset);
2346         isl_basic_set_free(bset);
2347         isl_factorizer_free(f);
2348         if (!f)
2349                 isl_die(ctx, isl_error_unknown,
2350                         "failed to construct factorizer", return -1);
2351
2352         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2353             "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2354             "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2355             "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2356             "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2357             "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2358             "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2359             "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2360             "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2361             "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2362         bset = isl_basic_set_read_from_str(ctx, str);
2363         f = isl_basic_set_factorizer(bset);
2364         isl_basic_set_free(bset);
2365         isl_factorizer_free(f);
2366         if (!f)
2367                 isl_die(ctx, isl_error_unknown,
2368                         "failed to construct factorizer", return -1);
2369
2370         return 0;
2371 }
2372
2373 static int check_injective(__isl_take isl_map *map, void *user)
2374 {
2375         int *injective = user;
2376
2377         *injective = isl_map_is_injective(map);
2378         isl_map_free(map);
2379
2380         if (*injective < 0 || !*injective)
2381                 return -1;
2382
2383         return 0;
2384 }
2385
2386 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2387         const char *r, const char *s, int tilable, int parallel)
2388 {
2389         int i;
2390         isl_union_set *D;
2391         isl_union_map *W, *R, *S;
2392         isl_union_map *empty;
2393         isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2394         isl_union_map *validity, *proximity;
2395         isl_union_map *schedule;
2396         isl_union_map *test;
2397         isl_union_set *delta;
2398         isl_union_set *domain;
2399         isl_set *delta_set;
2400         isl_set *slice;
2401         isl_set *origin;
2402         isl_schedule *sched;
2403         int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2404
2405         D = isl_union_set_read_from_str(ctx, d);
2406         W = isl_union_map_read_from_str(ctx, w);
2407         R = isl_union_map_read_from_str(ctx, r);
2408         S = isl_union_map_read_from_str(ctx, s);
2409
2410         W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2411         R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2412
2413         empty = isl_union_map_empty(isl_union_map_get_space(S));
2414         isl_union_map_compute_flow(isl_union_map_copy(R),
2415                                    isl_union_map_copy(W), empty,
2416                                    isl_union_map_copy(S),
2417                                    &dep_raw, NULL, NULL, NULL);
2418         isl_union_map_compute_flow(isl_union_map_copy(W),
2419                                    isl_union_map_copy(W),
2420                                    isl_union_map_copy(R),
2421                                    isl_union_map_copy(S),
2422                                    &dep_waw, &dep_war, NULL, NULL);
2423
2424         dep = isl_union_map_union(dep_waw, dep_war);
2425         dep = isl_union_map_union(dep, dep_raw);
2426         validity = isl_union_map_copy(dep);
2427         proximity = isl_union_map_copy(dep);
2428
2429         sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
2430                                                validity, proximity);
2431         schedule = isl_schedule_get_map(sched);
2432         isl_schedule_free(sched);
2433         isl_union_map_free(W);
2434         isl_union_map_free(R);
2435         isl_union_map_free(S);
2436
2437         is_injection = 1;
2438         isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2439
2440         domain = isl_union_map_domain(isl_union_map_copy(schedule));
2441         is_complete = isl_union_set_is_subset(D, domain);
2442         isl_union_set_free(D);
2443         isl_union_set_free(domain);
2444
2445         test = isl_union_map_reverse(isl_union_map_copy(schedule));
2446         test = isl_union_map_apply_range(test, dep);
2447         test = isl_union_map_apply_range(test, schedule);
2448
2449         delta = isl_union_map_deltas(test);
2450         if (isl_union_set_n_set(delta) == 0) {
2451                 is_tilable = 1;
2452                 is_parallel = 1;
2453                 is_nonneg = 1;
2454                 isl_union_set_free(delta);
2455         } else {
2456                 delta_set = isl_set_from_union_set(delta);
2457
2458                 slice = isl_set_universe(isl_set_get_space(delta_set));
2459                 for (i = 0; i < tilable; ++i)
2460                         slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2461                 is_tilable = isl_set_is_subset(delta_set, slice);
2462                 isl_set_free(slice);
2463
2464                 slice = isl_set_universe(isl_set_get_space(delta_set));
2465                 for (i = 0; i < parallel; ++i)
2466                         slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2467                 is_parallel = isl_set_is_subset(delta_set, slice);
2468                 isl_set_free(slice);
2469
2470                 origin = isl_set_universe(isl_set_get_space(delta_set));
2471                 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2472                         origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2473
2474                 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2475                 delta_set = isl_set_lexmin(delta_set);
2476
2477                 is_nonneg = isl_set_is_equal(delta_set, origin);
2478
2479                 isl_set_free(origin);
2480                 isl_set_free(delta_set);
2481         }
2482
2483         if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2484             is_injection < 0 || is_complete < 0)
2485                 return -1;
2486         if (!is_complete)
2487                 isl_die(ctx, isl_error_unknown,
2488                         "generated schedule incomplete", return -1);
2489         if (!is_injection)
2490                 isl_die(ctx, isl_error_unknown,
2491                         "generated schedule not injective on each statement",
2492                         return -1);
2493         if (!is_nonneg)
2494                 isl_die(ctx, isl_error_unknown,
2495                         "negative dependences in generated schedule",
2496                         return -1);
2497         if (!is_tilable)
2498                 isl_die(ctx, isl_error_unknown,
2499                         "generated schedule not as tilable as expected",
2500                         return -1);
2501         if (!is_parallel)
2502                 isl_die(ctx, isl_error_unknown,
2503                         "generated schedule not as parallel as expected",
2504                         return -1);
2505
2506         return 0;
2507 }
2508
2509 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2510         const char *domain, const char *validity, const char *proximity)
2511 {
2512         isl_union_set *dom;
2513         isl_union_map *dep;
2514         isl_union_map *prox;
2515         isl_schedule *schedule;
2516         isl_union_map *sched;
2517
2518         dom = isl_union_set_read_from_str(ctx, domain);
2519         dep = isl_union_map_read_from_str(ctx, validity);
2520         prox = isl_union_map_read_from_str(ctx, proximity);
2521         schedule = isl_union_set_compute_schedule(dom, dep, prox);
2522         sched = isl_schedule_get_map(schedule);
2523         isl_schedule_free(schedule);
2524
2525         return sched;
2526 }
2527
2528 /* Check that a schedule can be constructed on the given domain
2529  * with the given validity and proximity constraints.
2530  */
2531 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2532         const char *validity, const char *proximity)
2533 {
2534         isl_union_map *sched;
2535
2536         sched = compute_schedule(ctx, domain, validity, proximity);
2537         if (!sched)
2538                 return -1;
2539
2540         isl_union_map_free(sched);
2541         return 0;
2542 }
2543
2544 int test_special_schedule(isl_ctx *ctx, const char *domain,
2545         const char *validity, const char *proximity, const char *expected_sched)
2546 {
2547         isl_union_map *sched1, *sched2;
2548         int equal;
2549
2550         sched1 = compute_schedule(ctx, domain, validity, proximity);
2551         sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2552
2553         equal = isl_union_map_is_equal(sched1, sched2);
2554         isl_union_map_free(sched1);
2555         isl_union_map_free(sched2);
2556
2557         if (equal < 0)
2558                 return -1;
2559         if (!equal)
2560                 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2561                         return -1);
2562
2563         return 0;
2564 }
2565
2566 /* Check that the schedule map is properly padded, even after being
2567  * reconstructed from the band forest.
2568  */
2569 static int test_padded_schedule(isl_ctx *ctx)
2570 {
2571         const char *str;
2572         isl_union_set *D;
2573         isl_union_map *validity, *proximity;
2574         isl_schedule *sched;
2575         isl_union_map *map1, *map2;
2576         isl_band_list *list;
2577         int equal;
2578
2579         str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2580         D = isl_union_set_read_from_str(ctx, str);
2581         validity = isl_union_map_empty(isl_union_set_get_space(D));
2582         proximity = isl_union_map_copy(validity);
2583         sched = isl_union_set_compute_schedule(D, validity, proximity);
2584         map1 = isl_schedule_get_map(sched);
2585         list = isl_schedule_get_band_forest(sched);
2586         isl_band_list_free(list);
2587         map2 = isl_schedule_get_map(sched);
2588         isl_schedule_free(sched);
2589         equal = isl_union_map_is_equal(map1, map2);
2590         isl_union_map_free(map1);
2591         isl_union_map_free(map2);
2592
2593         if (equal < 0)
2594                 return -1;
2595         if (!equal)
2596                 isl_die(ctx, isl_error_unknown,
2597                         "reconstructed schedule map not the same as original",
2598                         return -1);
2599
2600         return 0;
2601 }
2602
2603 int test_schedule(isl_ctx *ctx)
2604 {
2605         const char *D, *W, *R, *V, *P, *S;
2606
2607         /* Handle resulting schedule with zero bands. */
2608         if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2609                 return -1;
2610
2611         /* Jacobi */
2612         D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2613         W = "{ S1[t,i] -> a[t,i] }";
2614         R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2615                 "S1[t,i] -> a[t-1,i+1] }";
2616         S = "{ S1[t,i] -> [t,i] }";
2617         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2618                 return -1;
2619
2620         /* Fig. 5 of CC2008 */
2621         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2622                                 "j <= -1 + N }";
2623         W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2624                                 "j >= 2 and j <= -1 + N }";
2625         R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2626                                 "j >= 2 and j <= -1 + N; "
2627                     "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2628                                 "j >= 2 and j <= -1 + N }";
2629         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2630         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2631                 return -1;
2632
2633         D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2634         W = "{ S1[i] -> a[i] }";
2635         R = "{ S2[i] -> a[i+1] }";
2636         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2637         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2638                 return -1;
2639
2640         D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2641         W = "{ S1[i] -> a[i] }";
2642         R = "{ S2[i] -> a[9-i] }";
2643         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2644         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2645                 return -1;
2646
2647         D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2648         W = "{ S1[i] -> a[i] }";
2649         R = "[N] -> { S2[i] -> a[N-1-i] }";
2650         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2651         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2652                 return -1;
2653         
2654         D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2655         W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2656         R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2657         S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2658         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2659                 return -1;
2660
2661         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2662         W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2663         R = "{ S2[i,j] -> a[i-1,j] }";
2664         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2665         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2666                 return -1;
2667
2668         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2669         W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2670         R = "{ S2[i,j] -> a[i,j-1] }";
2671         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2672         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2673                 return -1;
2674
2675         D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2676         W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2677                     "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2678         R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2679         S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2680                     "S_0[] -> [0, 0, 0] }";
2681         if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2682                 return -1;
2683         ctx->opt->schedule_parametric = 0;
2684         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2685                 return -1;
2686         ctx->opt->schedule_parametric = 1;
2687
2688         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2689                     "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2690         W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2691         R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2692                     "S4[i] -> a[i,N] }";
2693         S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2694                 "S4[i] -> [4,i,0] }";
2695         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2696                 return -1;
2697
2698         D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2699         W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2700                                         "j <= N }";
2701         R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2702                                         "j <= N; "
2703                     "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2704                                         "j <= N }";
2705         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2706         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2707                 return -1;
2708
2709         D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2710                     " S_2[t] : t >= 0 and t <= -1 + N; "
2711                     " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2712                                 "i <= -1 + N }";
2713         W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2714                     " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2715                     " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2716                                                 "i >= 0 and i <= -1 + N }";
2717         R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2718                                             "i >= 0 and i <= -1 + N; "
2719                     " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2720         S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2721                     " S_0[t] -> [0, t, 0] }";
2722
2723         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2724                 return -1;
2725         ctx->opt->schedule_parametric = 0;
2726         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2727                 return -1;
2728         ctx->opt->schedule_parametric = 1;
2729
2730         D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2731         S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2732         if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2733                 return -1;
2734
2735         D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2736             "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2737         W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2738                                             "j >= 0 and j <= -1 + N; "
2739                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2740         R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2741                                             "j >= 0 and j <= -1 + N; "
2742                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2743         S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2744         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2745                 return -1;
2746
2747         D = "{ S_0[i] : i >= 0 }";
2748         W = "{ S_0[i] -> a[i] : i >= 0 }";
2749         R = "{ S_0[i] -> a[0] : i >= 0 }";
2750         S = "{ S_0[i] -> [0, i, 0] }";
2751         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2752                 return -1;
2753
2754         D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2755         W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2756         R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2757         S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2758         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2759                 return -1;
2760
2761         D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2762                                 "k <= -1 + n and k >= 0 }";
2763         W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "                                                  "k <= -1 + n and k >= 0 }";
2764         R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2765                                         "k <= -1 + n and k >= 0; "
2766                     "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2767                                         "k <= -1 + n and k >= 0; "
2768                     "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2769                                         "k <= -1 + n and k >= 0 }";
2770         S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2771         ctx->opt->schedule_outer_zero_distance = 1;
2772         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2773                 return -1;
2774         ctx->opt->schedule_outer_zero_distance = 0;
2775
2776         D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2777                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2778                 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2779              "Stmt_for_body24[i0, i1, 1, 0]:"
2780                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2781              "Stmt_for_body7[i0, i1, i2]:"
2782                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2783                 "i2 <= 7 }";
2784
2785         V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2786                 "Stmt_for_body24[1, i1, i2, i3]:"
2787                 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2788                 "i2 >= 1;"
2789              "Stmt_for_body24[0, i1, i2, i3] -> "
2790                 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2791                 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2792                 "i3 >= 0;"
2793               "Stmt_for_body24[0, i1, i2, i3] ->"
2794                 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2795                 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2796               "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2797                 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2798                 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2799                 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2800               "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2801                 "i1 <= 6 and i1 >= 0;"
2802               "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2803               "Stmt_for_body7[i0, i1, i2] -> "
2804                 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2805                 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2806                 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2807               "Stmt_for_body7[i0, i1, i2] -> "
2808                 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2809                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2810                 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2811         P = V;
2812         S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2813                 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2814             "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2815
2816         if (test_special_schedule(ctx, D, V, P, S) < 0)
2817                 return -1;
2818
2819         D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2820         V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2821                                            "j >= 1 and j <= 7;"
2822                 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2823                                              "j >= 1 and j <= 8 }";
2824         P = "{ }";
2825         S = "{ S_0[i, j] -> [i + j, j] }";
2826         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2827         if (test_special_schedule(ctx, D, V, P, S) < 0)
2828                 return -1;
2829         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2830
2831         /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2832         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2833                                  "j >= 0 and j <= -1 + i }";
2834         V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2835                                         "i <= -1 + N and j >= 0;"
2836                      "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2837                                         "i <= -2 + N }";
2838         P = "{ }";
2839         S = "{ S_0[i, j] -> [i, j] }";
2840         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2841         if (test_special_schedule(ctx, D, V, P, S) < 0)
2842                 return -1;
2843         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2844
2845         /* Test both algorithms on a case with only proximity dependences. */
2846         D = "{ S[i,j] : 0 <= i <= 10 }";
2847         V = "{ }";
2848         P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2849         S = "{ S[i, j] -> [j, i] }";
2850         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2851         if (test_special_schedule(ctx, D, V, P, S) < 0)
2852                 return -1;
2853         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2854         if (test_special_schedule(ctx, D, V, P, S) < 0)
2855                 return -1;
2856         
2857         D = "{ A[a]; B[] }";
2858         V = "{}";
2859         P = "{ A[a] -> B[] }";
2860         if (test_has_schedule(ctx, D, V, P) < 0)
2861                 return -1;
2862
2863         if (test_padded_schedule(ctx) < 0)
2864                 return -1;
2865
2866         /* Check that check for progress is not confused by rational
2867          * solution.
2868          */
2869         D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
2870         V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
2871                                                         "i0 <= -2 + N; "
2872                         "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
2873                                 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
2874         P = "{}";
2875         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2876         if (test_has_schedule(ctx, D, V, P) < 0)
2877                 return -1;
2878         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2879
2880         return 0;
2881 }
2882
2883 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2884 {
2885         isl_union_map *umap;
2886         int test;
2887
2888         umap = isl_union_map_read_from_str(ctx, str);
2889         test = isl_union_map_plain_is_injective(umap);
2890         isl_union_map_free(umap);
2891         if (test < 0)
2892                 return -1;
2893         if (test == injective)
2894                 return 0;
2895         if (injective)
2896                 isl_die(ctx, isl_error_unknown,
2897                         "map not detected as injective", return -1);
2898         else
2899                 isl_die(ctx, isl_error_unknown,
2900                         "map detected as injective", return -1);
2901 }
2902
2903 int test_injective(isl_ctx *ctx)
2904 {
2905         const char *str;
2906
2907         if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2908                 return -1;
2909         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2910                 return -1;
2911         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2912                 return -1;
2913         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2914                 return -1;
2915         if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2916                 return -1;
2917         if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2918                 return -1;
2919         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2920                 return -1;
2921         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2922                 return -1;
2923
2924         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2925         if (test_plain_injective(ctx, str, 1))
2926                 return -1;
2927         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2928         if (test_plain_injective(ctx, str, 0))
2929                 return -1;
2930
2931         return 0;
2932 }
2933
2934 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
2935 {
2936         isl_aff *aff2;
2937         int equal;
2938
2939         if (!aff)
2940                 return -1;
2941
2942         aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
2943         equal = isl_aff_plain_is_equal(aff, aff2);
2944         isl_aff_free(aff2);
2945
2946         return equal;
2947 }
2948
2949 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
2950 {
2951         int equal;
2952
2953         equal = aff_plain_is_equal(aff, str);
2954         if (equal < 0)
2955                 return -1;
2956         if (!equal)
2957                 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
2958                         "result not as expected", return -1);
2959         return 0;
2960 }
2961
2962 int test_aff(isl_ctx *ctx)
2963 {
2964         const char *str;
2965         isl_set *set;
2966         isl_space *space;
2967         isl_local_space *ls;
2968         isl_aff *aff;
2969         int zero, equal;
2970
2971         space = isl_space_set_alloc(ctx, 0, 1);
2972         ls = isl_local_space_from_space(space);
2973         aff = isl_aff_zero_on_domain(ls);
2974
2975         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2976         aff = isl_aff_scale_down_ui(aff, 3);
2977         aff = isl_aff_floor(aff);
2978         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2979         aff = isl_aff_scale_down_ui(aff, 2);
2980         aff = isl_aff_floor(aff);
2981         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2982
2983         str = "{ [10] }";
2984         set = isl_set_read_from_str(ctx, str);
2985         aff = isl_aff_gist(aff, set);
2986
2987         aff = isl_aff_add_constant_si(aff, -16);
2988         zero = isl_aff_plain_is_zero(aff);
2989         isl_aff_free(aff);
2990
2991         if (zero < 0)
2992                 return -1;
2993         if (!zero)
2994                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2995
2996         aff = isl_aff_read_from_str(ctx, "{ [-1] }");
2997         aff = isl_aff_scale_down_ui(aff, 64);
2998         aff = isl_aff_floor(aff);
2999         equal = aff_check_plain_equal(aff, "{ [-1] }");
3000         isl_aff_free(aff);
3001         if (equal < 0)
3002                 return -1;
3003
3004         return 0;
3005 }
3006
3007 int test_dim_max(isl_ctx *ctx)
3008 {
3009         int equal;
3010         const char *str;
3011         isl_set *set1, *set2;
3012         isl_set *set;
3013         isl_map *map;
3014         isl_pw_aff *pwaff;
3015
3016         str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3017         set = isl_set_read_from_str(ctx, str);
3018         pwaff = isl_set_dim_max(set, 0);
3019         set1 = isl_set_from_pw_aff(pwaff);
3020         str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3021         set2 = isl_set_read_from_str(ctx, str);
3022         equal = isl_set_is_equal(set1, set2);
3023         isl_set_free(set1);
3024         isl_set_free(set2);
3025         if (equal < 0)
3026                 return -1;
3027         if (!equal)
3028                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3029
3030         str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3031         set = isl_set_read_from_str(ctx, str);
3032         pwaff = isl_set_dim_max(set, 0);
3033         set1 = isl_set_from_pw_aff(pwaff);
3034         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3035         set2 = isl_set_read_from_str(ctx, str);
3036         equal = isl_set_is_equal(set1, set2);
3037         isl_set_free(set1);
3038         isl_set_free(set2);
3039         if (equal < 0)
3040                 return -1;
3041         if (!equal)
3042                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3043
3044         str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3045         set = isl_set_read_from_str(ctx, str);
3046         pwaff = isl_set_dim_max(set, 0);
3047         set1 = isl_set_from_pw_aff(pwaff);
3048         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3049         set2 = isl_set_read_from_str(ctx, str);
3050         equal = isl_set_is_equal(set1, set2);
3051         isl_set_free(set1);
3052         isl_set_free(set2);
3053         if (equal < 0)
3054                 return -1;
3055         if (!equal)
3056                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3057
3058         str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3059                         "0 <= i < N and 0 <= j < M }";
3060         map = isl_map_read_from_str(ctx, str);
3061         set = isl_map_range(map);
3062
3063         pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3064         set1 = isl_set_from_pw_aff(pwaff);
3065         str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3066         set2 = isl_set_read_from_str(ctx, str);
3067         equal = isl_set_is_equal(set1, set2);
3068         isl_set_free(set1);
3069         isl_set_free(set2);
3070
3071         pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3072         set1 = isl_set_from_pw_aff(pwaff);
3073         str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3074         set2 = isl_set_read_from_str(ctx, str);
3075         if (equal >= 0 && equal)
3076                 equal = isl_set_is_equal(set1, set2);
3077         isl_set_free(set1);
3078         isl_set_free(set2);
3079
3080         isl_set_free(set);
3081
3082         if (equal < 0)
3083                 return -1;
3084         if (!equal)
3085                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3086
3087         /* Check that solutions are properly merged. */
3088         str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3089                                 "c <= -1 + n - 4a - 2b and c >= -2b and "
3090                                 "4a >= -4 + n and c >= 0 }";
3091         set = isl_set_read_from_str(ctx, str);
3092         pwaff = isl_set_dim_min(set, 2);
3093         set1 = isl_set_from_pw_aff(pwaff);
3094         str = "[n] -> { [(0)] : n >= 1 }";
3095         set2 = isl_set_read_from_str(ctx, str);
3096         equal = isl_set_is_equal(set1, set2);
3097         isl_set_free(set1);
3098         isl_set_free(set2);
3099
3100         if (equal < 0)
3101                 return -1;
3102         if (!equal)
3103                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3104
3105         return 0;
3106 }
3107
3108 int test_product(isl_ctx *ctx)
3109 {
3110         const char *str;
3111         isl_set *set;
3112         isl_union_set *uset1, *uset2;
3113         int ok;
3114
3115         str = "{ A[i] }";
3116         set = isl_set_read_from_str(ctx, str);
3117         set = isl_set_product(set, isl_set_copy(set));
3118         ok = isl_set_is_wrapping(set);
3119         isl_set_free(set);
3120         if (ok < 0)
3121                 return -1;
3122         if (!ok)
3123                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3124
3125         str = "{ [] }";
3126         uset1 = isl_union_set_read_from_str(ctx, str);
3127         uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3128         str = "{ [[] -> []] }";
3129         uset2 = isl_union_set_read_from_str(ctx, str);
3130         ok = isl_union_set_is_equal(uset1, uset2);
3131         isl_union_set_free(uset1);
3132         isl_union_set_free(uset2);
3133         if (ok < 0)
3134                 return -1;
3135         if (!ok)
3136                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3137
3138         return 0;
3139 }
3140
3141 int test_equal(isl_ctx *ctx)
3142 {
3143         const char *str;
3144         isl_set *set, *set2;
3145         int equal;
3146
3147         str = "{ S_6[i] }";
3148         set = isl_set_read_from_str(ctx, str);
3149         str = "{ S_7[i] }";
3150         set2 = isl_set_read_from_str(ctx, str);
3151         equal = isl_set_is_equal(set, set2);
3152         isl_set_free(set);
3153         isl_set_free(set2);
3154         if (equal < 0)
3155                 return -1;
3156         if (equal)
3157                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3158
3159         return 0;
3160 }
3161
3162 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3163         enum isl_dim_type type, unsigned pos, int fixed)
3164 {
3165         int test;
3166
3167         test = isl_map_plain_is_fixed(map, type, pos, NULL);
3168         isl_map_free(map);
3169         if (test < 0)
3170                 return -1;
3171         if (test == fixed)
3172                 return 0;
3173         if (fixed)
3174                 isl_die(ctx, isl_error_unknown,
3175                         "map not detected as fixed", return -1);
3176         else
3177                 isl_die(ctx, isl_error_unknown,
3178                         "map detected as fixed", return -1);
3179 }
3180
3181 int test_fixed(isl_ctx *ctx)
3182 {
3183         const char *str;
3184         isl_map *map;
3185
3186         str = "{ [i] -> [i] }";
3187         map = isl_map_read_from_str(ctx, str);
3188         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3189                 return -1;
3190         str = "{ [i] -> [1] }";
3191         map = isl_map_read_from_str(ctx, str);
3192         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3193                 return -1;
3194         str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3195         map = isl_map_read_from_str(ctx, str);
3196         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3197                 return -1;
3198         map = isl_map_read_from_str(ctx, str);
3199         map = isl_map_neg(map);
3200         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3201                 return -1;
3202
3203         return 0;
3204 }
3205
3206 int test_vertices(isl_ctx *ctx)
3207 {
3208         const char *str;
3209         isl_basic_set *bset;
3210         isl_vertices *vertices;
3211
3212         str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
3213         bset = isl_basic_set_read_from_str(ctx, str);
3214         vertices = isl_basic_set_compute_vertices(bset);
3215         isl_basic_set_free(bset);
3216         isl_vertices_free(vertices);
3217         if (!vertices)
3218                 return -1;
3219
3220         str = "{ A[t, i] : t = 14 and i = 1 }";
3221         bset = isl_basic_set_read_from_str(ctx, str);
3222         vertices = isl_basic_set_compute_vertices(bset);
3223         isl_basic_set_free(bset);
3224         isl_vertices_free(vertices);
3225         if (!vertices)
3226                 return -1;
3227
3228         return 0;
3229 }
3230
3231 int test_union_pw(isl_ctx *ctx)
3232 {
3233         int equal;
3234         const char *str;
3235         isl_union_set *uset;
3236         isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3237
3238         str = "{ [x] -> x^2 }";
3239         upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3240         upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3241         uset = isl_union_pw_qpolynomial_domain(upwqp1);
3242         upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3243         upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3244         equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3245         isl_union_pw_qpolynomial_free(upwqp1);
3246         isl_union_pw_qpolynomial_free(upwqp2);
3247         if (equal < 0)
3248                 return -1;
3249         if (!equal)
3250                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3251
3252         return 0;
3253 }
3254
3255 int test_output(isl_ctx *ctx)
3256 {
3257         char *s;
3258         const char *str;
3259         isl_pw_aff *pa;
3260         isl_printer *p;
3261         int equal;
3262
3263         str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3264         pa = isl_pw_aff_read_from_str(ctx, str);
3265
3266         p = isl_printer_to_str(ctx);
3267         p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3268         p = isl_printer_print_pw_aff(p, pa);
3269         s = isl_printer_get_str(p);
3270         isl_printer_free(p);
3271         isl_pw_aff_free(pa);
3272         if (!s)
3273                 equal = -1;
3274         else
3275                 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3276         free(s);
3277         if (equal < 0)
3278                 return -1;
3279         if (!equal)
3280                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3281
3282         return 0;
3283 }
3284
3285 int test_sample(isl_ctx *ctx)
3286 {
3287         const char *str;
3288         isl_basic_set *bset1, *bset2;
3289         int empty, subset;
3290
3291         str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3292             "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3293             "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3294             "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3295             "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3296             "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3297             "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3298             "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3299             "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3300             "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3301             "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3302             "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3303                 "d - 1073741821e and "
3304             "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3305             "3j >= 1 - a + b + 2e and "
3306             "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3307             "3i <= 4 - a + 4b - e and "
3308             "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3309             "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3310             "c <= -1 + a and 3i >= -2 - a + 3e and "
3311             "1073741822e <= 1073741823 - a + 1073741822b + c and "
3312             "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3313             "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3314             "1073741823e >= 1 + 1073741823b - d and "
3315             "3i >= 1073741823b + c - 1073741823e - f and "
3316             "3i >= 1 + 2b + e + 3g }";
3317         bset1 = isl_basic_set_read_from_str(ctx, str);
3318         bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3319         empty = isl_basic_set_is_empty(bset2);
3320         subset = isl_basic_set_is_subset(bset2, bset1);
3321         isl_basic_set_free(bset1);
3322         isl_basic_set_free(bset2);
3323         if (empty < 0 || subset < 0)
3324                 return -1;
3325         if (empty)
3326                 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3327         if (!subset)
3328                 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3329
3330         return 0;
3331 }
3332
3333 int test_fixed_power(isl_ctx *ctx)
3334 {
3335         const char *str;
3336         isl_map *map;
3337         isl_int exp;
3338         int equal;
3339
3340         isl_int_init(exp);
3341         str = "{ [i] -> [i + 1] }";
3342         map = isl_map_read_from_str(ctx, str);
3343         isl_int_set_si(exp, 23);
3344         map = isl_map_fixed_power(map, exp);
3345         equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3346         isl_int_clear(exp);
3347         isl_map_free(map);
3348         if (equal < 0)
3349                 return -1;
3350
3351         return 0;
3352 }
3353
3354 int test_slice(isl_ctx *ctx)
3355 {
3356         const char *str;
3357         isl_map *map;
3358         int equal;
3359
3360         str = "{ [i] -> [j] }";
3361         map = isl_map_read_from_str(ctx, str);
3362         map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3363         equal = map_check_equal(map, "{ [i] -> [i] }");
3364         isl_map_free(map);
3365         if (equal < 0)
3366                 return -1;
3367
3368         str = "{ [i] -> [j] }";
3369         map = isl_map_read_from_str(ctx, str);
3370         map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3371         equal = map_check_equal(map, "{ [i] -> [j] }");
3372         isl_map_free(map);
3373         if (equal < 0)
3374                 return -1;
3375
3376         str = "{ [i] -> [j] }";
3377         map = isl_map_read_from_str(ctx, str);
3378         map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3379         equal = map_check_equal(map, "{ [i] -> [-i] }");
3380         isl_map_free(map);
3381         if (equal < 0)
3382                 return -1;
3383
3384         str = "{ [i] -> [j] }";
3385         map = isl_map_read_from_str(ctx, str);
3386         map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3387         equal = map_check_equal(map, "{ [0] -> [j] }");
3388         isl_map_free(map);
3389         if (equal < 0)
3390                 return -1;
3391
3392         str = "{ [i] -> [j] }";
3393         map = isl_map_read_from_str(ctx, str);
3394         map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3395         equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3396         isl_map_free(map);
3397         if (equal < 0)
3398                 return -1;
3399
3400         str = "{ [i] -> [j] }";
3401         map = isl_map_read_from_str(ctx, str);
3402         map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3403         equal = map_check_equal(map, "{ [i] -> [j] : false }");
3404         isl_map_free(map);
3405         if (equal < 0)
3406                 return -1;
3407
3408         return 0;
3409 }
3410
3411 int test_eliminate(isl_ctx *ctx)
3412 {
3413         const char *str;
3414         isl_map *map;
3415         int equal;
3416
3417         str = "{ [i] -> [j] : i = 2j }";
3418         map = isl_map_read_from_str(ctx, str);
3419         map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3420         equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3421         isl_map_free(map);
3422         if (equal < 0)
3423                 return -1;
3424
3425         return 0;
3426 }
3427
3428 /* Check that isl_set_dim_residue_class detects that the values of j
3429  * in the set below are all odd and that it does not detect any spurious
3430  * strides.
3431  */
3432 static int test_residue_class(isl_ctx *ctx)
3433 {
3434         const char *str;
3435         isl_set *set;
3436         isl_int m, r;
3437         int res;
3438
3439         str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3440                 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3441         set = isl_set_read_from_str(ctx, str);
3442         isl_int_init(m);
3443         isl_int_init(r);
3444         res = isl_set_dim_residue_class(set, 1, &m, &r);
3445         if (res >= 0 &&
3446             (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3447                 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3448                         res = -1);
3449         isl_int_clear(r);
3450         isl_int_clear(m);
3451         isl_set_free(set);
3452
3453         return res;
3454 }
3455
3456 int test_align_parameters(isl_ctx *ctx)
3457 {
3458         const char *str;
3459         isl_space *space;
3460         isl_multi_aff *ma1, *ma2;
3461         int equal;
3462
3463         str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3464         ma1 = isl_multi_aff_read_from_str(ctx, str);
3465
3466         space = isl_space_params_alloc(ctx, 1);
3467         space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3468         ma1 = isl_multi_aff_align_params(ma1, space);
3469
3470         str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3471         ma2 = isl_multi_aff_read_from_str(ctx, str);
3472
3473         equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3474
3475         isl_multi_aff_free(ma1);
3476         isl_multi_aff_free(ma2);
3477
3478         if (equal < 0)
3479                 return -1;
3480         if (!equal)
3481                 isl_die(ctx, isl_error_unknown,
3482                         "result not as expected", return -1);
3483
3484         return 0;
3485 }
3486
3487 static int test_list(isl_ctx *ctx)
3488 {
3489         isl_id *a, *b, *c, *d, *id;
3490         isl_id_list *list;
3491         int ok;
3492
3493         a = isl_id_alloc(ctx, "a", NULL);
3494         b = isl_id_alloc(ctx, "b", NULL);
3495         c = isl_id_alloc(ctx, "c", NULL);
3496         d = isl_id_alloc(ctx, "d", NULL);
3497
3498         list = isl_id_list_alloc(ctx, 4);
3499         list = isl_id_list_add(list, a);
3500         list = isl_id_list_add(list, b);
3501         list = isl_id_list_add(list, c);
3502         list = isl_id_list_add(list, d);
3503         list = isl_id_list_drop(list, 1, 1);
3504
3505         if (isl_id_list_n_id(list) != 3) {
3506                 isl_id_list_free(list);
3507                 isl_die(ctx, isl_error_unknown,
3508                         "unexpected number of elements in list", return -1);
3509         }
3510
3511         id = isl_id_list_get_id(list, 0);
3512         ok = id == a;
3513         isl_id_free(id);
3514         id = isl_id_list_get_id(list, 1);
3515         ok = ok && id == c;
3516         isl_id_free(id);
3517         id = isl_id_list_get_id(list, 2);
3518         ok = ok && id == d;
3519         isl_id_free(id);
3520
3521         isl_id_list_free(list);
3522
3523         if (!ok)
3524                 isl_die(ctx, isl_error_unknown,
3525                         "unexpected elements in list", return -1);
3526
3527         return 0;
3528 }
3529
3530 const char *set_conversion_tests[] = {
3531         "[N] -> { [i] : N - 1 <= 2 i <= N }",
3532         "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3533         "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3534         "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3535 };
3536
3537 /* Check that converting from isl_set to isl_pw_multi_aff and back
3538  * to isl_set produces the original isl_set.
3539  */
3540 static int test_set_conversion(isl_ctx *ctx)
3541 {
3542         int i;
3543         const char *str;
3544         isl_set *set1, *set2;
3545         isl_pw_multi_aff *pma;
3546         int equal;
3547
3548         for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3549                 str = set_conversion_tests[i];
3550                 set1 = isl_set_read_from_str(ctx, str);
3551                 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3552                 set2 = isl_set_from_pw_multi_aff(pma);
3553                 equal = isl_set_is_equal(set1, set2);
3554                 isl_set_free(set1);
3555                 isl_set_free(set2);
3556
3557                 if (equal < 0)
3558                         return -1;
3559                 if (!equal)
3560                         isl_die(ctx, isl_error_unknown, "bad conversion",
3561                                 return -1);
3562         }
3563
3564         return 0;
3565 }
3566
3567 /* Check that converting from isl_map to isl_pw_multi_aff and back
3568  * to isl_map produces the original isl_map.
3569  */
3570 static int test_map_conversion(isl_ctx *ctx)
3571 {
3572         const char *str;
3573         isl_map *map1, *map2;
3574         isl_pw_multi_aff *pma;
3575         int equal;
3576
3577         str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3578                 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3579                 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3580                 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3581                 "9e <= -2 - 2a) }";
3582         map1 = isl_map_read_from_str(ctx, str);
3583         pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3584         map2 = isl_map_from_pw_multi_aff(pma);
3585         equal = isl_map_is_equal(map1, map2);
3586         isl_map_free(map1);
3587         isl_map_free(map2);
3588
3589         if (equal < 0)
3590                 return -1;
3591         if (!equal)
3592                 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3593
3594         return 0;
3595 }
3596
3597 static int test_conversion(isl_ctx *ctx)
3598 {
3599         if (test_set_conversion(ctx) < 0)
3600                 return -1;
3601         if (test_map_conversion(ctx) < 0)
3602                 return -1;
3603         return 0;
3604 }
3605
3606 /* Check that isl_basic_map_curry does not modify input.
3607  */
3608 static int test_curry(isl_ctx *ctx)
3609 {
3610         const char *str;
3611         isl_basic_map *bmap1, *bmap2;
3612         int equal;
3613
3614         str = "{ [A[] -> B[]] -> C[] }";
3615         bmap1 = isl_basic_map_read_from_str(ctx, str);
3616         bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3617         equal = isl_basic_map_is_equal(bmap1, bmap2);
3618         isl_basic_map_free(bmap1);
3619         isl_basic_map_free(bmap2);
3620
3621         if (equal < 0)
3622                 return -1;
3623         if (equal)
3624                 isl_die(ctx, isl_error_unknown,
3625                         "curried map should not be equal to original",
3626                         return -1);
3627
3628         return 0;
3629 }
3630
3631 struct {
3632         const char *set;
3633         const char *ma;
3634         const char *res;
3635 } preimage_tests[] = {
3636         { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3637           "{ A[j,i] -> B[i,j] }",
3638           "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3639         { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3640           "{ A[a,b] -> B[a/2,b/6] }",
3641           "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3642         { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3643           "{ A[a,b] -> B[a/2,b/6] }",
3644           "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3645                     "exists i,j : a = 2 i and b = 6 j }" },
3646         { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3647           "[n] -> { : 0 <= n <= 100 }" },
3648         { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3649           "{ A[a] -> B[2a] }",
3650           "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3651         { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3652           "{ A[a] -> B[([a/2])] }",
3653           "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3654         { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3655           "{ A[a] -> B[a,a,a/3] }",
3656           "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3657         { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3658           "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3659 };
3660
3661 static int test_preimage_basic_set(isl_ctx *ctx)
3662 {
3663         int i;
3664         isl_basic_set *bset1, *bset2;
3665         isl_multi_aff *ma;
3666         int equal;
3667
3668         for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3669                 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3670                 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3671                 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3672                 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3673                 equal = isl_basic_set_is_equal(bset1, bset2);
3674                 isl_basic_set_free(bset1);
3675                 isl_basic_set_free(bset2);
3676                 if (equal < 0)
3677                         return -1;
3678                 if (!equal)
3679                         isl_die(ctx, isl_error_unknown, "bad preimage",
3680                                 return -1);
3681         }
3682
3683         return 0;
3684 }
3685
3686 struct {
3687         const char *map;
3688         const char *ma;
3689         const char *res;
3690 } preimage_domain_tests[] = {
3691         { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
3692           "{ A[j,i] -> B[i,j] }",
3693           "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
3694         { "{ B[i] -> C[i]; D[i] -> E[i] }",
3695           "{ A[i] -> B[i + 1] }",
3696           "{ A[i] -> C[i + 1] }" },
3697         { "{ B[i] -> C[i]; B[i] -> E[i] }",
3698           "{ A[i] -> B[i + 1] }",
3699           "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
3700         { "{ B[i] -> C[([i/2])] }",
3701           "{ A[i] -> B[2i] }",
3702           "{ A[i] -> C[i] }" },
3703         { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
3704           "{ A[i] -> B[([i/5]), ([i/7])] }",
3705           "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
3706         { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
3707           "[N] -> { A[] -> B[([N/5])] }",
3708           "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
3709         { "{ B[i] -> C[i] : exists a : i = 5 a }",
3710           "{ A[i] -> B[2i] }",
3711           "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
3712         { "{ B[i] -> C[i] : exists a : i = 2 a; "
3713             "B[i] -> D[i] : exists a : i = 2 a + 1 }",
3714           "{ A[i] -> B[2i] }",
3715           "{ A[i] -> C[2i] }" },
3716 };
3717
3718 static int test_preimage_union_map(isl_ctx *ctx)
3719 {
3720         int i;
3721         isl_union_map *umap1, *umap2;
3722         isl_multi_aff *ma;
3723         int equal;
3724
3725         for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
3726                 umap1 = isl_union_map_read_from_str(ctx,
3727                                                 preimage_domain_tests[i].map);
3728                 ma = isl_multi_aff_read_from_str(ctx,
3729                                                 preimage_domain_tests[i].ma);
3730                 umap2 = isl_union_map_read_from_str(ctx,
3731                                                 preimage_domain_tests[i].res);
3732                 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
3733                 equal = isl_union_map_is_equal(umap1, umap2);
3734                 isl_union_map_free(umap1);
3735                 isl_union_map_free(umap2);
3736                 if (equal < 0)
3737                         return -1;
3738                 if (!equal)
3739                         isl_die(ctx, isl_error_unknown, "bad preimage",
3740                                 return -1);
3741         }
3742
3743         return 0;
3744 }
3745
3746 static int test_preimage(isl_ctx *ctx)
3747 {
3748         if (test_preimage_basic_set(ctx) < 0)
3749                 return -1;
3750         if (test_preimage_union_map(ctx) < 0)
3751                 return -1;
3752
3753         return 0;
3754 }
3755
3756 struct {
3757         const char *ma1;
3758         const char *ma;
3759         const char *res;
3760 } pullback_tests[] = {
3761         { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
3762           "{ A[a,b] -> C[b + 2a] }" },
3763         { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
3764         { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
3765         { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
3766           "{ A[a] -> C[(a)/6] }" },
3767         { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
3768         { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
3769           "{ A[a] -> C[(2a)/3] }" },
3770         { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
3771         { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
3772           "{ A[i,j] -> C[i + j, i + j] }"},
3773         { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
3774         { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
3775           "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
3776 };
3777
3778 static int test_pullback(isl_ctx *ctx)
3779 {
3780         int i;
3781         isl_multi_aff *ma1, *ma2;
3782         isl_multi_aff *ma;
3783         int equal;
3784
3785         for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
3786                 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
3787                 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
3788                 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
3789                 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
3790                 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3791                 isl_multi_aff_free(ma1);
3792                 isl_multi_aff_free(ma2);
3793                 if (equal < 0)
3794                         return -1;
3795                 if (!equal)
3796                         isl_die(ctx, isl_error_unknown, "bad pullback",
3797                                 return -1);
3798         }
3799
3800         return 0;
3801 }
3802
3803 /* Check that negation is printed correctly.
3804  */
3805 static int test_ast(isl_ctx *ctx)
3806 {
3807         isl_ast_expr *expr, *expr1, *expr2, *expr3;
3808         char *str;
3809         int ok;
3810
3811         expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3812         expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3813         expr = isl_ast_expr_add(expr1, expr2);
3814         expr = isl_ast_expr_neg(expr);
3815         str = isl_ast_expr_to_str(expr);
3816         ok = str ? !strcmp(str, "-(A + B)") : -1;
3817         free(str);
3818         isl_ast_expr_free(expr);
3819
3820         if (ok < 0)
3821                 return -1;
3822         if (!ok)
3823                 isl_die(ctx, isl_error_unknown,
3824                         "isl_ast_expr printed incorrectly", return -1);
3825
3826         expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3827         expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3828         expr = isl_ast_expr_add(expr1, expr2);
3829         expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
3830         expr = isl_ast_expr_sub(expr3, expr);
3831         str = isl_ast_expr_to_str(expr);
3832         ok = str ? !strcmp(str, "C - (A + B)") : -1;
3833         free(str);
3834         isl_ast_expr_free(expr);
3835
3836         if (ok < 0)
3837                 return -1;
3838         if (!ok)
3839                 isl_die(ctx, isl_error_unknown,
3840                         "isl_ast_expr printed incorrectly", return -1);
3841
3842         return 0;
3843 }
3844
3845 /* Internal data structure for before_for and after_for callbacks.
3846  *
3847  * depth is the current depth
3848  * before is the number of times before_for has been called
3849  * after is the number of times after_for has been called
3850  */
3851 struct isl_test_codegen_data {
3852         int depth;
3853         int before;
3854         int after;
3855 };
3856
3857 /* This function is called before each for loop in the AST generated
3858  * from test_ast_gen1.
3859  *
3860  * Increment the number of calls and the depth.
3861  * Check that the space returned by isl_ast_build_get_schedule_space
3862  * matches the target space of the schedule returned by
3863  * isl_ast_build_get_schedule.
3864  * Return an isl_id that is checked by the corresponding call
3865  * to after_for.
3866  */
3867 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
3868         void *user)
3869 {
3870         struct isl_test_codegen_data *data = user;
3871         isl_ctx *ctx;
3872         isl_space *space;
3873         isl_union_map *schedule;
3874         isl_union_set *uset;
3875         isl_set *set;
3876         int empty;
3877         char name[] = "d0";
3878
3879         ctx = isl_ast_build_get_ctx(build);
3880
3881         if (data->before >= 3)
3882                 isl_die(ctx, isl_error_unknown,
3883                         "unexpected number of for nodes", return NULL);
3884         if (data->depth >= 2)
3885                 isl_die(ctx, isl_error_unknown,
3886                         "unexpected depth", return NULL);
3887
3888         snprintf(name, sizeof(name), "d%d", data->depth);
3889         data->before++;
3890         data->depth++;
3891
3892         schedule = isl_ast_build_get_schedule(build);
3893         uset = isl_union_map_range(schedule);
3894         if (!uset)
3895                 return NULL;
3896         if (isl_union_set_n_set(uset) != 1) {
3897                 isl_union_set_free(uset);
3898                 isl_die(ctx, isl_error_unknown,
3899                         "expecting single range space", return NULL);
3900         }
3901
3902         space = isl_ast_build_get_schedule_space(build);
3903         set = isl_union_set_extract_set(uset, space);
3904         isl_union_set_free(uset);
3905         empty = isl_set_is_empty(set);
3906         isl_set_free(set);
3907
3908         if (empty < 0)
3909                 return NULL;
3910         if (empty)
3911                 isl_die(ctx, isl_error_unknown,
3912                         "spaces don't match", return NULL);
3913
3914         return isl_id_alloc(ctx, name, NULL);
3915 }
3916
3917 /* This function is called after each for loop in the AST generated
3918  * from test_ast_gen1.
3919  *
3920  * Increment the number of calls and decrement the depth.
3921  * Check that the annotation attached to the node matches
3922  * the isl_id returned by the corresponding call to before_for.
3923  */
3924 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
3925         __isl_keep isl_ast_build *build, void *user)
3926 {
3927         struct isl_test_codegen_data *data = user;
3928         isl_id *id;
3929         const char *name;
3930         int valid;
3931
3932         data->after++;
3933         data->depth--;
3934
3935         if (data->after > data->before)
3936                 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3937                         "mismatch in number of for nodes",
3938                         return isl_ast_node_free(node));
3939
3940         id = isl_ast_node_get_annotation(node);
3941         if (!id)
3942                 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3943                         "missing annotation", return isl_ast_node_free(node));
3944
3945         name = isl_id_get_name(id);
3946         valid = name && atoi(name + 1) == data->depth;
3947         isl_id_free(id);
3948
3949         if (!valid)
3950                 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3951                         "wrong annotation", return isl_ast_node_free(node));
3952
3953         return node;
3954 }
3955
3956 /* Check that the before_each_for and after_each_for callbacks
3957  * are called for each for loop in the generated code,
3958  * that they are called in the right order and that the isl_id
3959  * returned from the before_each_for callback is attached to
3960  * the isl_ast_node passed to the corresponding after_each_for call.
3961  */
3962 static int test_ast_gen1(isl_ctx *ctx)
3963 {
3964         const char *str;
3965         isl_set *set;
3966         isl_union_map *schedule;
3967         isl_ast_build *build;
3968         isl_ast_node *tree;
3969         struct isl_test_codegen_data data;
3970
3971         str = "[N] -> { : N >= 10 }";
3972         set = isl_set_read_from_str(ctx, str);
3973         str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
3974                     "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
3975         schedule = isl_union_map_read_from_str(ctx, str);
3976
3977         data.before = 0;
3978         data.after = 0;
3979         data.depth = 0;
3980         build = isl_ast_build_from_context(set);
3981         build = isl_ast_build_set_before_each_for(build,
3982                         &before_for, &data);
3983         build = isl_ast_build_set_after_each_for(build,
3984                         &after_for, &data);
3985         tree = isl_ast_build_ast_from_schedule(build, schedule);
3986         isl_ast_build_free(build);
3987         if (!tree)
3988                 return -1;
3989
3990         isl_ast_node_free(tree);
3991
3992         if (data.before != 3 || data.after != 3)
3993                 isl_die(ctx, isl_error_unknown,
3994                         "unexpected number of for nodes", return -1);
3995
3996         return 0;
3997 }
3998
3999 /* Check that the AST generator handles domains that are integrally disjoint
4000  * but not ratinoally disjoint.
4001  */
4002 static int test_ast_gen2(isl_ctx *ctx)
4003 {
4004         const char *str;
4005         isl_set *set;
4006         isl_union_map *schedule;
4007         isl_union_map *options;
4008         isl_ast_build *build;
4009         isl_ast_node *tree;
4010
4011         str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4012         schedule = isl_union_map_read_from_str(ctx, str);
4013         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4014         build = isl_ast_build_from_context(set);
4015
4016         str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4017         options = isl_union_map_read_from_str(ctx, str);
4018         build = isl_ast_build_set_options(build, options);
4019         tree = isl_ast_build_ast_from_schedule(build, schedule);
4020         isl_ast_build_free(build);
4021         if (!tree)
4022                 return -1;
4023         isl_ast_node_free(tree);
4024
4025         return 0;
4026 }
4027
4028 /* Increment *user on each call.
4029  */
4030 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4031         __isl_keep isl_ast_build *build, void *user)
4032 {
4033         int *n = user;
4034
4035         (*n)++;
4036
4037         return node;
4038 }
4039
4040 /* Test that unrolling tries to minimize the number of instances.
4041  * In particular, for the schedule given below, make sure it generates
4042  * 3 nodes (rather than 101).
4043  */
4044 static int test_ast_gen3(isl_ctx *ctx)
4045 {
4046         const char *str;
4047         isl_set *set;
4048         isl_union_map *schedule;
4049         isl_union_map *options;
4050         isl_ast_build *build;
4051         isl_ast_node *tree;
4052         int n_domain = 0;
4053
4054         str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4055         schedule = isl_union_map_read_from_str(ctx, str);
4056         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4057
4058         str = "{ [i] -> unroll[0] }";
4059         options = isl_union_map_read_from_str(ctx, str);
4060
4061         build = isl_ast_build_from_context(set);
4062         build = isl_ast_build_set_options(build, options);
4063         build = isl_ast_build_set_at_each_domain(build,
4064                         &count_domains, &n_domain);
4065         tree = isl_ast_build_ast_from_schedule(build, schedule);
4066         isl_ast_build_free(build);
4067         if (!tree)
4068                 return -1;
4069
4070         isl_ast_node_free(tree);
4071
4072         if (n_domain != 3)
4073                 isl_die(ctx, isl_error_unknown,
4074                         "unexpected number of for nodes", return -1);
4075
4076         return 0;
4077 }
4078
4079 /* Check that if the ast_build_exploit_nested_bounds options is set,
4080  * we do not get an outer if node in the generated AST,
4081  * while we do get such an outer if node if the options is not set.
4082  */
4083 static int test_ast_gen4(isl_ctx *ctx)
4084 {
4085         const char *str;
4086         isl_set *set;
4087         isl_union_map *schedule;
4088         isl_ast_build *build;
4089         isl_ast_node *tree;
4090         enum isl_ast_node_type type;
4091         int enb;
4092
4093         enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4094         str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4095
4096         isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4097
4098         schedule = isl_union_map_read_from_str(ctx, str);
4099         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4100         build = isl_ast_build_from_context(set);
4101         tree = isl_ast_build_ast_from_schedule(build, schedule);
4102         isl_ast_build_free(build);
4103         if (!tree)
4104                 return -1;
4105
4106         type = isl_ast_node_get_type(tree);
4107         isl_ast_node_free(tree);
4108
4109         if (type == isl_ast_node_if)
4110                 isl_die(ctx, isl_error_unknown,
4111                         "not expecting if node", return -1);
4112
4113         isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4114
4115         schedule = isl_union_map_read_from_str(ctx, str);
4116         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4117         build = isl_ast_build_from_context(set);
4118         tree = isl_ast_build_ast_from_schedule(build, schedule);
4119         isl_ast_build_free(build);
4120         if (!tree)
4121                 return -1;
4122
4123         type = isl_ast_node_get_type(tree);
4124         isl_ast_node_free(tree);
4125
4126         if (type != isl_ast_node_if)
4127                 isl_die(ctx, isl_error_unknown,
4128                         "expecting if node", return -1);
4129
4130         isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4131
4132         return 0;
4133 }
4134
4135 /* This function is called for each leaf in the AST generated
4136  * from test_ast_gen5.
4137  *
4138  * We finalize the AST generation by extending the outer schedule
4139  * with a zero-dimensional schedule.  If this results in any for loops,
4140  * then this means that we did not pass along enough information
4141  * about the outer schedule to the inner AST generation.
4142  */
4143 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4144         void *user)
4145 {
4146         isl_union_map *schedule, *extra;
4147         isl_ast_node *tree;
4148
4149         schedule = isl_ast_build_get_schedule(build);
4150         extra = isl_union_map_copy(schedule);
4151         extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4152         schedule = isl_union_map_range_product(schedule, extra);
4153         tree = isl_ast_build_ast_from_schedule(build, schedule);
4154         isl_ast_build_free(build);
4155
4156         if (!tree)
4157                 return NULL;
4158
4159         if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4160                 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4161                         "code should not contain any for loop",
4162                         return isl_ast_node_free(tree));
4163
4164         return tree;
4165 }
4166
4167 /* Check that we do not lose any information when going back and
4168  * forth between internal and external schedule.
4169  *
4170  * In particular, we create an AST where we unroll the only
4171  * non-constant dimension in the schedule.  We therefore do
4172  * not expect any for loops in the AST.  However, older versions
4173  * of isl would not pass along enough information about the outer
4174  * schedule when performing an inner code generation from a create_leaf
4175  * callback, resulting in the inner code generation producing a for loop.
4176  */
4177 static int test_ast_gen5(isl_ctx *ctx)
4178 {
4179         const char *str;
4180         isl_set *set;
4181         isl_union_map *schedule, *options;
4182         isl_ast_build *build;
4183         isl_ast_node *tree;
4184
4185         str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4186         schedule = isl_union_map_read_from_str(ctx, str);
4187
4188         str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4189                                 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4190         options = isl_union_map_read_from_str(ctx, str);
4191
4192         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4193         build = isl_ast_build_from_context(set);
4194         build = isl_ast_build_set_options(build, options);
4195         build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4196         tree = isl_ast_build_ast_from_schedule(build, schedule);
4197         isl_ast_build_free(build);
4198         isl_ast_node_free(tree);
4199         if (!tree)
4200                 return -1;
4201
4202         return 0;
4203 }
4204
4205 static int test_ast_gen(isl_ctx *ctx)
4206 {
4207         if (test_ast_gen1(ctx) < 0)
4208                 return -1;
4209         if (test_ast_gen2(ctx) < 0)
4210                 return -1;
4211         if (test_ast_gen3(ctx) < 0)
4212                 return -1;
4213         if (test_ast_gen4(ctx) < 0)
4214                 return -1;
4215         if (test_ast_gen5(ctx) < 0)
4216                 return -1;
4217         return 0;
4218 }
4219
4220 /* Check if dropping output dimensions from an isl_pw_multi_aff
4221  * works properly.
4222  */
4223 static int test_pw_multi_aff(isl_ctx *ctx)
4224 {
4225         const char *str;
4226         isl_pw_multi_aff *pma1, *pma2;
4227         int equal;
4228
4229         str = "{ [i,j] -> [i+j, 4i-j] }";
4230         pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4231         str = "{ [i,j] -> [4i-j] }";
4232         pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4233
4234         pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4235
4236         equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4237
4238         isl_pw_multi_aff_free(pma1);
4239         isl_pw_multi_aff_free(pma2);
4240         if (equal < 0)
4241                 return -1;
4242         if (!equal)
4243                 isl_die(ctx, isl_error_unknown,
4244                         "expressions not equal", return -1);
4245
4246         return 0;
4247 }
4248
4249 /* This is a regression test for a bug where isl_basic_map_simplify
4250  * would end up in an infinite loop.  In particular, we construct
4251  * an empty basic set that is not obviously empty.
4252  * isl_basic_set_is_empty marks the basic set as empty.
4253  * After projecting out i3, the variable can be dropped completely,
4254  * but isl_basic_map_simplify refrains from doing so if the basic set
4255  * is empty and would end up in an infinite loop if it didn't test
4256  * explicitly for empty basic maps in the outer loop.
4257  */
4258 static int test_simplify(isl_ctx *ctx)
4259 {
4260         const char *str;
4261         isl_basic_set *bset;
4262         int empty;
4263
4264         str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4265                 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4266                 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4267                 "i3 >= i2 }";
4268         bset = isl_basic_set_read_from_str(ctx, str);
4269         empty = isl_basic_set_is_empty(bset);
4270         bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4271         isl_basic_set_free(bset);
4272         if (!bset)
4273                 return -1;
4274         if (!empty)
4275                 isl_die(ctx, isl_error_unknown,
4276                         "basic set should be empty", return -1);
4277
4278         return 0;
4279 }
4280
4281 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4282  * with gbr context would fail to disable the use of the shifted tableau
4283  * when transferring equalities for the input to the context, resulting
4284  * in invalid sample values.
4285  */
4286 static int test_partial_lexmin(isl_ctx *ctx)
4287 {
4288         const char *str;
4289         isl_basic_set *bset;
4290         isl_basic_map *bmap;
4291         isl_map *map;
4292
4293         str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4294         bmap = isl_basic_map_read_from_str(ctx, str);
4295         str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4296         bset = isl_basic_set_read_from_str(ctx, str);
4297         map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4298         isl_map_free(map);
4299
4300         if (!map)
4301                 return -1;
4302
4303         return 0;
4304 }
4305
4306 /* Check that the variable compression performed on the existentially
4307  * quantified variables inside isl_basic_set_compute_divs is not confused
4308  * by the implicit equalities among the parameters.
4309  */
4310 static int test_compute_divs(isl_ctx *ctx)
4311 {
4312         const char *str;
4313         isl_basic_set *bset;
4314         isl_set *set;
4315
4316         str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4317                 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4318                 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4319                 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4320         bset = isl_basic_set_read_from_str(ctx, str);
4321         set = isl_basic_set_compute_divs(bset);
4322         isl_set_free(set);
4323         if (!set)
4324                 return -1;
4325
4326         return 0;
4327 }
4328
4329 struct {
4330         const char *name;
4331         int (*fn)(isl_ctx *ctx);
4332 } tests [] = {
4333         { "val", &test_val },
4334         { "compute divs", &test_compute_divs },
4335         { "partial lexmin", &test_partial_lexmin },
4336         { "simplify", &test_simplify },
4337         { "curry", &test_curry },
4338         { "piecewise multi affine expressions", &test_pw_multi_aff },
4339         { "conversion", &test_conversion },
4340         { "list", &test_list },
4341         { "align parameters", &test_align_parameters },
4342         { "preimage", &test_preimage },
4343         { "pullback", &test_pullback },
4344         { "AST", &test_ast },
4345         { "AST generation", &test_ast_gen },
4346         { "eliminate", &test_eliminate },
4347         { "residue class", &test_residue_class },
4348         { "div", &test_div },
4349         { "slice", &test_slice },
4350         { "fixed power", &test_fixed_power },
4351         { "sample", &test_sample },
4352         { "output", &test_output },
4353         { "vertices", &test_vertices },
4354         { "fixed", &test_fixed },
4355         { "equal", &test_equal },
4356         { "product", &test_product },
4357         { "dim_max", &test_dim_max },
4358         { "affine", &test_aff },
4359         { "injective", &test_injective },
4360         { "schedule", &test_schedule },
4361         { "union_pw", &test_union_pw },
4362         { "parse", &test_parse },
4363         { "single-valued", &test_sv },
4364         { "affine hull", &test_affine_hull },
4365         { "coalesce", &test_coalesce },
4366         { "factorize", &test_factorize },
4367         { "subset", &test_subset },
4368         { "subtract", &test_subtract },
4369         { "lexmin", &test_lexmin },
4370         { "gist", &test_gist },
4371         { "piecewise quasi-polynomials", &test_pwqp },
4372 };
4373
4374 int main()
4375 {
4376         int i;
4377         struct isl_ctx *ctx;
4378
4379         srcdir = getenv("srcdir");
4380         assert(srcdir);
4381
4382         ctx = isl_ctx_alloc();
4383         for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4384                 printf("%s\n", tests[i].name);
4385                 if (tests[i].fn(ctx) < 0)
4386                         goto error;
4387         }
4388         test_lift(ctx);
4389         test_bound(ctx);
4390         test_union(ctx);
4391         test_split_periods(ctx);
4392         test_lex(ctx);
4393         test_bijective(ctx);
4394         test_dep(ctx);
4395         test_read(ctx);
4396         test_bounded(ctx);
4397         test_construction(ctx);
4398         test_dim(ctx);
4399         test_application(ctx);
4400         test_convex_hull(ctx);
4401         test_closure(ctx);
4402         isl_ctx_free(ctx);
4403         return 0;
4404 error:
4405         isl_ctx_free(ctx);
4406         return -1;
4407 }