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