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