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