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