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