Merge branch 'maint'
[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         /* Check that empty pieces are properly combined. */
1441         str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1442                 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1443                 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1444         map = isl_map_read_from_str(ctx, str);
1445         map = isl_map_lexmin(map);
1446         str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1447                 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1448                 "x >= 4 }";
1449         map2 = isl_map_read_from_str(ctx, str);
1450         assert(isl_map_is_equal(map, map2));
1451         isl_map_free(map);
1452         isl_map_free(map2);
1453
1454         str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1455                                 " 8i' <= i and 8i' >= -7 + i }";
1456         set = isl_set_read_from_str(ctx, str);
1457         pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1458         set2 = isl_set_from_pw_multi_aff(pma);
1459         equal = isl_set_is_equal(set, set2);
1460         isl_set_free(set);
1461         isl_set_free(set2);
1462         if (equal < 0)
1463                 return -1;
1464         if (!equal)
1465                 isl_die(ctx, isl_error_unknown,
1466                         "unexpected difference between set and "
1467                         "piecewise affine expression", return -1);
1468
1469         return 0;
1470 }
1471
1472 struct must_may {
1473         isl_map *must;
1474         isl_map *may;
1475 };
1476
1477 static int collect_must_may(__isl_take isl_map *dep, int must,
1478         void *dep_user, void *user)
1479 {
1480         struct must_may *mm = (struct must_may *)user;
1481
1482         if (must)
1483                 mm->must = isl_map_union(mm->must, dep);
1484         else
1485                 mm->may = isl_map_union(mm->may, dep);
1486
1487         return 0;
1488 }
1489
1490 static int common_space(void *first, void *second)
1491 {
1492         int depth = *(int *)first;
1493         return 2 * depth;
1494 }
1495
1496 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1497 {
1498         isl_map *map2;
1499         int equal;
1500
1501         if (!map)
1502                 return -1;
1503
1504         map2 = isl_map_read_from_str(map->ctx, str);
1505         equal = isl_map_is_equal(map, map2);
1506         isl_map_free(map2);
1507
1508         return equal;
1509 }
1510
1511 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1512 {
1513         int equal;
1514
1515         equal = map_is_equal(map, str);
1516         if (equal < 0)
1517                 return -1;
1518         if (!equal)
1519                 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1520                         "result not as expected", return -1);
1521         return 0;
1522 }
1523
1524 void test_dep(struct isl_ctx *ctx)
1525 {
1526         const char *str;
1527         isl_space *dim;
1528         isl_map *map;
1529         isl_access_info *ai;
1530         isl_flow *flow;
1531         int depth;
1532         struct must_may mm;
1533
1534         depth = 3;
1535
1536         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1537         map = isl_map_read_from_str(ctx, str);
1538         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1539
1540         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1541         map = isl_map_read_from_str(ctx, str);
1542         ai = isl_access_info_add_source(ai, map, 1, &depth);
1543
1544         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1545         map = isl_map_read_from_str(ctx, str);
1546         ai = isl_access_info_add_source(ai, map, 1, &depth);
1547
1548         flow = isl_access_info_compute_flow(ai);
1549         dim = isl_space_alloc(ctx, 0, 3, 3);
1550         mm.must = isl_map_empty(isl_space_copy(dim));
1551         mm.may = isl_map_empty(dim);
1552
1553         isl_flow_foreach(flow, collect_must_may, &mm);
1554
1555         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1556               "  [1,10,0] -> [2,5,0] }";
1557         assert(map_is_equal(mm.must, str));
1558         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1559         assert(map_is_equal(mm.may, str));
1560
1561         isl_map_free(mm.must);
1562         isl_map_free(mm.may);
1563         isl_flow_free(flow);
1564
1565
1566         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1567         map = isl_map_read_from_str(ctx, str);
1568         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1569
1570         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1571         map = isl_map_read_from_str(ctx, str);
1572         ai = isl_access_info_add_source(ai, map, 1, &depth);
1573
1574         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1575         map = isl_map_read_from_str(ctx, str);
1576         ai = isl_access_info_add_source(ai, map, 0, &depth);
1577
1578         flow = isl_access_info_compute_flow(ai);
1579         dim = isl_space_alloc(ctx, 0, 3, 3);
1580         mm.must = isl_map_empty(isl_space_copy(dim));
1581         mm.may = isl_map_empty(dim);
1582
1583         isl_flow_foreach(flow, collect_must_may, &mm);
1584
1585         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1586         assert(map_is_equal(mm.must, str));
1587         str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1588         assert(map_is_equal(mm.may, str));
1589
1590         isl_map_free(mm.must);
1591         isl_map_free(mm.may);
1592         isl_flow_free(flow);
1593
1594
1595         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1596         map = isl_map_read_from_str(ctx, str);
1597         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1598
1599         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1600         map = isl_map_read_from_str(ctx, str);
1601         ai = isl_access_info_add_source(ai, map, 0, &depth);
1602
1603         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1604         map = isl_map_read_from_str(ctx, str);
1605         ai = isl_access_info_add_source(ai, map, 0, &depth);
1606
1607         flow = isl_access_info_compute_flow(ai);
1608         dim = isl_space_alloc(ctx, 0, 3, 3);
1609         mm.must = isl_map_empty(isl_space_copy(dim));
1610         mm.may = isl_map_empty(dim);
1611
1612         isl_flow_foreach(flow, collect_must_may, &mm);
1613
1614         str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1615               "  [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1616         assert(map_is_equal(mm.may, str));
1617         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1618         assert(map_is_equal(mm.must, str));
1619
1620         isl_map_free(mm.must);
1621         isl_map_free(mm.may);
1622         isl_flow_free(flow);
1623
1624
1625         str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1626         map = isl_map_read_from_str(ctx, str);
1627         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1628
1629         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1630         map = isl_map_read_from_str(ctx, str);
1631         ai = isl_access_info_add_source(ai, map, 0, &depth);
1632
1633         str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1634         map = isl_map_read_from_str(ctx, str);
1635         ai = isl_access_info_add_source(ai, map, 0, &depth);
1636
1637         flow = isl_access_info_compute_flow(ai);
1638         dim = isl_space_alloc(ctx, 0, 3, 3);
1639         mm.must = isl_map_empty(isl_space_copy(dim));
1640         mm.may = isl_map_empty(dim);
1641
1642         isl_flow_foreach(flow, collect_must_may, &mm);
1643
1644         str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1645               "  [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1646         assert(map_is_equal(mm.may, str));
1647         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1648         assert(map_is_equal(mm.must, str));
1649
1650         isl_map_free(mm.must);
1651         isl_map_free(mm.may);
1652         isl_flow_free(flow);
1653
1654
1655         str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1656         map = isl_map_read_from_str(ctx, str);
1657         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1658
1659         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1660         map = isl_map_read_from_str(ctx, str);
1661         ai = isl_access_info_add_source(ai, map, 0, &depth);
1662
1663         str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1664         map = isl_map_read_from_str(ctx, str);
1665         ai = isl_access_info_add_source(ai, map, 0, &depth);
1666
1667         flow = isl_access_info_compute_flow(ai);
1668         dim = isl_space_alloc(ctx, 0, 3, 3);
1669         mm.must = isl_map_empty(isl_space_copy(dim));
1670         mm.may = isl_map_empty(dim);
1671
1672         isl_flow_foreach(flow, collect_must_may, &mm);
1673
1674         str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1675               "  [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1676         assert(map_is_equal(mm.may, str));
1677         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1678         assert(map_is_equal(mm.must, str));
1679
1680         isl_map_free(mm.must);
1681         isl_map_free(mm.may);
1682         isl_flow_free(flow);
1683
1684
1685         depth = 5;
1686
1687         str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1688         map = isl_map_read_from_str(ctx, str);
1689         ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1690
1691         str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1692         map = isl_map_read_from_str(ctx, str);
1693         ai = isl_access_info_add_source(ai, map, 1, &depth);
1694
1695         flow = isl_access_info_compute_flow(ai);
1696         dim = isl_space_alloc(ctx, 0, 5, 5);
1697         mm.must = isl_map_empty(isl_space_copy(dim));
1698         mm.may = isl_map_empty(dim);
1699
1700         isl_flow_foreach(flow, collect_must_may, &mm);
1701
1702         str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1703         assert(map_is_equal(mm.must, str));
1704         str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1705         assert(map_is_equal(mm.may, str));
1706
1707         isl_map_free(mm.must);
1708         isl_map_free(mm.may);
1709         isl_flow_free(flow);
1710 }
1711
1712 int test_sv(isl_ctx *ctx)
1713 {
1714         const char *str;
1715         isl_map *map;
1716         isl_union_map *umap;
1717         int sv;
1718
1719         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1720         map = isl_map_read_from_str(ctx, str);
1721         sv = isl_map_is_single_valued(map);
1722         isl_map_free(map);
1723         if (sv < 0)
1724                 return -1;
1725         if (!sv)
1726                 isl_die(ctx, isl_error_internal,
1727                         "map not detected as single valued", return -1);
1728
1729         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1730         map = isl_map_read_from_str(ctx, str);
1731         sv = isl_map_is_single_valued(map);
1732         isl_map_free(map);
1733         if (sv < 0)
1734                 return -1;
1735         if (sv)
1736                 isl_die(ctx, isl_error_internal,
1737                         "map detected as single valued", return -1);
1738
1739         str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1740         umap = isl_union_map_read_from_str(ctx, str);
1741         sv = isl_union_map_is_single_valued(umap);
1742         isl_union_map_free(umap);
1743         if (sv < 0)
1744                 return -1;
1745         if (!sv)
1746                 isl_die(ctx, isl_error_internal,
1747                         "map not detected as single valued", return -1);
1748
1749         str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1750         umap = isl_union_map_read_from_str(ctx, str);
1751         sv = isl_union_map_is_single_valued(umap);
1752         isl_union_map_free(umap);
1753         if (sv < 0)
1754                 return -1;
1755         if (sv)
1756                 isl_die(ctx, isl_error_internal,
1757                         "map detected as single valued", return -1);
1758
1759         return 0;
1760 }
1761
1762 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1763 {
1764         isl_map *map;
1765
1766         map = isl_map_read_from_str(ctx, str);
1767         if (bijective)
1768                 assert(isl_map_is_bijective(map));
1769         else
1770                 assert(!isl_map_is_bijective(map));
1771         isl_map_free(map);
1772 }
1773
1774 void test_bijective(struct isl_ctx *ctx)
1775 {
1776         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1777         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1778         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1779         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1780         test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1781         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1782         test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1783         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1784         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1785         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1786         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1787         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1788         test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1789 }
1790
1791 void test_pwqp(struct isl_ctx *ctx)
1792 {
1793         const char *str;
1794         isl_set *set;
1795         isl_pw_qpolynomial *pwqp1, *pwqp2;
1796
1797         str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1798         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1799
1800         pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1801                                                 isl_dim_in, 1, 1);
1802
1803         str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
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 }";
1813         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1814         str = "{ [k] : exists a : k = 2a }";
1815         set = isl_set_read_from_str(ctx, str);
1816         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1817         str = "{ [i] -> i }";
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 + [ (i + [i/3])/2 ] }";
1827         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1828         str = "{ [10] }";
1829         set = isl_set_read_from_str(ctx, str);
1830         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1831         str = "{ [i] -> 16 }";
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]) }";
1841         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1842         str = "{ [k] : exists a : k = 2a+1 }";
1843         set = isl_set_read_from_str(ctx, str);
1844         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1845         str = "{ [i] -> -1/2 + 1/2 * i }";
1846         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1847
1848         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1849
1850         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1851
1852         isl_pw_qpolynomial_free(pwqp1);
1853
1854         str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1855         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1856         str = "{ [i] -> ([(2 * [i/2])/5]) }";
1857         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1858
1859         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1860
1861         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1862
1863         isl_pw_qpolynomial_free(pwqp1);
1864
1865         str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1866         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1867         str = "{ [x] -> x }";
1868         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1869
1870         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1871
1872         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1873
1874         isl_pw_qpolynomial_free(pwqp1);
1875
1876         str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1877         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1878         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1879         pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1880         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1881         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1882         isl_pw_qpolynomial_free(pwqp1);
1883 }
1884
1885 void test_split_periods(isl_ctx *ctx)
1886 {
1887         const char *str;
1888         isl_pw_qpolynomial *pwqp;
1889
1890         str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1891                 "U + 2V + 3 >= 0 and - U -2V  >= 0 and - U + 10 >= 0 and "
1892                 "U  >= 0; [U,V] -> U^2 : U >= 100 }";
1893         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1894
1895         pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1896         assert(pwqp);
1897
1898         isl_pw_qpolynomial_free(pwqp);
1899 }
1900
1901 void test_union(isl_ctx *ctx)
1902 {
1903         const char *str;
1904         isl_union_set *uset1, *uset2;
1905         isl_union_map *umap1, *umap2;
1906
1907         str = "{ [i] : 0 <= i <= 1 }";
1908         uset1 = isl_union_set_read_from_str(ctx, str);
1909         str = "{ [1] -> [0] }";
1910         umap1 = isl_union_map_read_from_str(ctx, str);
1911
1912         umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1913         assert(isl_union_map_is_equal(umap1, umap2));
1914
1915         isl_union_map_free(umap1);
1916         isl_union_map_free(umap2);
1917
1918         str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1919         umap1 = isl_union_map_read_from_str(ctx, str);
1920         str = "{ A[i]; B[i] }";
1921         uset1 = isl_union_set_read_from_str(ctx, str);
1922
1923         uset2 = isl_union_map_domain(umap1);
1924
1925         assert(isl_union_set_is_equal(uset1, uset2));
1926
1927         isl_union_set_free(uset1);
1928         isl_union_set_free(uset2);
1929 }
1930
1931 void test_bound(isl_ctx *ctx)
1932 {
1933         const char *str;
1934         isl_pw_qpolynomial *pwqp;
1935         isl_pw_qpolynomial_fold *pwf;
1936
1937         str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1938         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1939         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1940         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1941         isl_pw_qpolynomial_fold_free(pwf);
1942
1943         str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1944         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1945         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1946         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1947         isl_pw_qpolynomial_fold_free(pwf);
1948 }
1949
1950 void test_lift(isl_ctx *ctx)
1951 {
1952         const char *str;
1953         isl_basic_map *bmap;
1954         isl_basic_set *bset;
1955
1956         str = "{ [i0] : exists e0 : i0 = 4e0 }";
1957         bset = isl_basic_set_read_from_str(ctx, str);
1958         bset = isl_basic_set_lift(bset);
1959         bmap = isl_basic_map_from_range(bset);
1960         bset = isl_basic_map_domain(bmap);
1961         isl_basic_set_free(bset);
1962 }
1963
1964 struct {
1965         const char *set1;
1966         const char *set2;
1967         int subset;
1968 } subset_tests[] = {
1969         { "{ [112, 0] }",
1970           "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1971                 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1972                 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
1973         { "{ [65] }",
1974           "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
1975                 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
1976                 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
1977                     "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
1978                     "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
1979                     "256e0 <= 255i and 256e0 >= -255 + 255i and "
1980                     "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
1981                     "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
1982                     "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
1983         { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
1984         { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
1985         { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
1986         { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
1987 };
1988
1989 static int test_subset(isl_ctx *ctx)
1990 {
1991         int i;
1992         isl_set *set1, *set2;
1993         int subset;
1994
1995         for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
1996                 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
1997                 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
1998                 subset = isl_set_is_subset(set1, set2);
1999                 isl_set_free(set1);
2000                 isl_set_free(set2);
2001                 if (subset < 0)
2002                         return -1;
2003                 if (subset != subset_tests[i].subset)
2004                         isl_die(ctx, isl_error_unknown,
2005                                 "incorrect subset result", return -1);
2006         }
2007
2008         return 0;
2009 }
2010
2011 struct {
2012         const char *minuend;
2013         const char *subtrahend;
2014         const char *difference;
2015 } subtract_domain_tests[] = {
2016         { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2017         { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2018         { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2019 };
2020
2021 static int test_subtract(isl_ctx *ctx)
2022 {
2023         int i;
2024         isl_union_map *umap1, *umap2;
2025         isl_union_set *uset;
2026         int equal;
2027
2028         for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2029                 umap1 = isl_union_map_read_from_str(ctx,
2030                                 subtract_domain_tests[i].minuend);
2031                 uset = isl_union_set_read_from_str(ctx,
2032                                 subtract_domain_tests[i].subtrahend);
2033                 umap2 = isl_union_map_read_from_str(ctx,
2034                                 subtract_domain_tests[i].difference);
2035                 umap1 = isl_union_map_subtract_domain(umap1, uset);
2036                 equal = isl_union_map_is_equal(umap1, umap2);
2037                 isl_union_map_free(umap1);
2038                 isl_union_map_free(umap2);
2039                 if (equal < 0)
2040                         return -1;
2041                 if (!equal)
2042                         isl_die(ctx, isl_error_unknown,
2043                                 "incorrect subtract domain result", return -1);
2044         }
2045
2046         return 0;
2047 }
2048
2049 int test_factorize(isl_ctx *ctx)
2050 {
2051         const char *str;
2052         isl_basic_set *bset;
2053         isl_factorizer *f;
2054
2055         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2056             "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2057             "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2058             "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2059             "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2060             "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2061             "3i5 >= -2i0 - i2 + 3i4 }";
2062         bset = isl_basic_set_read_from_str(ctx, str);
2063         f = isl_basic_set_factorizer(bset);
2064         isl_basic_set_free(bset);
2065         isl_factorizer_free(f);
2066         if (!f)
2067                 isl_die(ctx, isl_error_unknown,
2068                         "failed to construct factorizer", return -1);
2069
2070         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2071             "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2072             "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2073             "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2074             "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2075             "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2076             "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2077             "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2078             "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2079             "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2080         bset = isl_basic_set_read_from_str(ctx, str);
2081         f = isl_basic_set_factorizer(bset);
2082         isl_basic_set_free(bset);
2083         isl_factorizer_free(f);
2084         if (!f)
2085                 isl_die(ctx, isl_error_unknown,
2086                         "failed to construct factorizer", return -1);
2087
2088         return 0;
2089 }
2090
2091 static int check_injective(__isl_take isl_map *map, void *user)
2092 {
2093         int *injective = user;
2094
2095         *injective = isl_map_is_injective(map);
2096         isl_map_free(map);
2097
2098         if (*injective < 0 || !*injective)
2099                 return -1;
2100
2101         return 0;
2102 }
2103
2104 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2105         const char *r, const char *s, int tilable, int parallel)
2106 {
2107         int i;
2108         isl_union_set *D;
2109         isl_union_map *W, *R, *S;
2110         isl_union_map *empty;
2111         isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2112         isl_union_map *validity, *proximity;
2113         isl_union_map *schedule;
2114         isl_union_map *test;
2115         isl_union_set *delta;
2116         isl_union_set *domain;
2117         isl_set *delta_set;
2118         isl_set *slice;
2119         isl_set *origin;
2120         isl_schedule *sched;
2121         int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2122
2123         D = isl_union_set_read_from_str(ctx, d);
2124         W = isl_union_map_read_from_str(ctx, w);
2125         R = isl_union_map_read_from_str(ctx, r);
2126         S = isl_union_map_read_from_str(ctx, s);
2127
2128         W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2129         R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2130
2131         empty = isl_union_map_empty(isl_union_map_get_space(S));
2132         isl_union_map_compute_flow(isl_union_map_copy(R),
2133                                    isl_union_map_copy(W), empty,
2134                                    isl_union_map_copy(S),
2135                                    &dep_raw, NULL, NULL, NULL);
2136         isl_union_map_compute_flow(isl_union_map_copy(W),
2137                                    isl_union_map_copy(W),
2138                                    isl_union_map_copy(R),
2139                                    isl_union_map_copy(S),
2140                                    &dep_waw, &dep_war, NULL, NULL);
2141
2142         dep = isl_union_map_union(dep_waw, dep_war);
2143         dep = isl_union_map_union(dep, dep_raw);
2144         validity = isl_union_map_copy(dep);
2145         proximity = isl_union_map_copy(dep);
2146
2147         sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
2148                                                validity, proximity);
2149         schedule = isl_schedule_get_map(sched);
2150         isl_schedule_free(sched);
2151         isl_union_map_free(W);
2152         isl_union_map_free(R);
2153         isl_union_map_free(S);
2154
2155         is_injection = 1;
2156         isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2157
2158         domain = isl_union_map_domain(isl_union_map_copy(schedule));
2159         is_complete = isl_union_set_is_subset(D, domain);
2160         isl_union_set_free(D);
2161         isl_union_set_free(domain);
2162
2163         test = isl_union_map_reverse(isl_union_map_copy(schedule));
2164         test = isl_union_map_apply_range(test, dep);
2165         test = isl_union_map_apply_range(test, schedule);
2166
2167         delta = isl_union_map_deltas(test);
2168         if (isl_union_set_n_set(delta) == 0) {
2169                 is_tilable = 1;
2170                 is_parallel = 1;
2171                 is_nonneg = 1;
2172                 isl_union_set_free(delta);
2173         } else {
2174                 delta_set = isl_set_from_union_set(delta);
2175
2176                 slice = isl_set_universe(isl_set_get_space(delta_set));
2177                 for (i = 0; i < tilable; ++i)
2178                         slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2179                 is_tilable = isl_set_is_subset(delta_set, slice);
2180                 isl_set_free(slice);
2181
2182                 slice = isl_set_universe(isl_set_get_space(delta_set));
2183                 for (i = 0; i < parallel; ++i)
2184                         slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2185                 is_parallel = isl_set_is_subset(delta_set, slice);
2186                 isl_set_free(slice);
2187
2188                 origin = isl_set_universe(isl_set_get_space(delta_set));
2189                 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2190                         origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2191
2192                 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2193                 delta_set = isl_set_lexmin(delta_set);
2194
2195                 is_nonneg = isl_set_is_equal(delta_set, origin);
2196
2197                 isl_set_free(origin);
2198                 isl_set_free(delta_set);
2199         }
2200
2201         if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2202             is_injection < 0 || is_complete < 0)
2203                 return -1;
2204         if (!is_complete)
2205                 isl_die(ctx, isl_error_unknown,
2206                         "generated schedule incomplete", return -1);
2207         if (!is_injection)
2208                 isl_die(ctx, isl_error_unknown,
2209                         "generated schedule not injective on each statement",
2210                         return -1);
2211         if (!is_nonneg)
2212                 isl_die(ctx, isl_error_unknown,
2213                         "negative dependences in generated schedule",
2214                         return -1);
2215         if (!is_tilable)
2216                 isl_die(ctx, isl_error_unknown,
2217                         "generated schedule not as tilable as expected",
2218                         return -1);
2219         if (!is_parallel)
2220                 isl_die(ctx, isl_error_unknown,
2221                         "generated schedule not as parallel as expected",
2222                         return -1);
2223
2224         return 0;
2225 }
2226
2227 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2228         const char *domain, const char *validity, const char *proximity)
2229 {
2230         isl_union_set *dom;
2231         isl_union_map *dep;
2232         isl_union_map *prox;
2233         isl_schedule *schedule;
2234         isl_union_map *sched;
2235
2236         dom = isl_union_set_read_from_str(ctx, domain);
2237         dep = isl_union_map_read_from_str(ctx, validity);
2238         prox = isl_union_map_read_from_str(ctx, proximity);
2239         schedule = isl_union_set_compute_schedule(dom, dep, prox);
2240         sched = isl_schedule_get_map(schedule);
2241         isl_schedule_free(schedule);
2242
2243         return sched;
2244 }
2245
2246 /* Check that a schedule can be constructed on the given domain
2247  * with the given validity and proximity constraints.
2248  */
2249 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2250         const char *validity, const char *proximity)
2251 {
2252         isl_union_map *sched;
2253
2254         sched = compute_schedule(ctx, domain, validity, proximity);
2255         if (!sched)
2256                 return -1;
2257
2258         isl_union_map_free(sched);
2259         return 0;
2260 }
2261
2262 int test_special_schedule(isl_ctx *ctx, const char *domain,
2263         const char *validity, const char *proximity, const char *expected_sched)
2264 {
2265         isl_union_map *sched1, *sched2;
2266         int equal;
2267
2268         sched1 = compute_schedule(ctx, domain, validity, proximity);
2269         sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2270
2271         equal = isl_union_map_is_equal(sched1, sched2);
2272         isl_union_map_free(sched1);
2273         isl_union_map_free(sched2);
2274
2275         if (equal < 0)
2276                 return -1;
2277         if (!equal)
2278                 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2279                         return -1);
2280
2281         return 0;
2282 }
2283
2284 /* Check that the schedule map is properly padded, even after being
2285  * reconstructed from the band forest.
2286  */
2287 static int test_padded_schedule(isl_ctx *ctx)
2288 {
2289         const char *str;
2290         isl_union_set *D;
2291         isl_union_map *validity, *proximity;
2292         isl_schedule *sched;
2293         isl_union_map *map1, *map2;
2294         isl_band_list *list;
2295         int equal;
2296
2297         str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2298         D = isl_union_set_read_from_str(ctx, str);
2299         validity = isl_union_map_empty(isl_union_set_get_space(D));
2300         proximity = isl_union_map_copy(validity);
2301         sched = isl_union_set_compute_schedule(D, validity, proximity);
2302         map1 = isl_schedule_get_map(sched);
2303         list = isl_schedule_get_band_forest(sched);
2304         isl_band_list_free(list);
2305         map2 = isl_schedule_get_map(sched);
2306         isl_schedule_free(sched);
2307         equal = isl_union_map_is_equal(map1, map2);
2308         isl_union_map_free(map1);
2309         isl_union_map_free(map2);
2310
2311         if (equal < 0)
2312                 return -1;
2313         if (!equal)
2314                 isl_die(ctx, isl_error_unknown,
2315                         "reconstructed schedule map not the same as original",
2316                         return -1);
2317
2318         return 0;
2319 }
2320
2321 int test_schedule(isl_ctx *ctx)
2322 {
2323         const char *D, *W, *R, *V, *P, *S;
2324
2325         /* Handle resulting schedule with zero bands. */
2326         if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2327                 return -1;
2328
2329         /* Jacobi */
2330         D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2331         W = "{ S1[t,i] -> a[t,i] }";
2332         R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2333                 "S1[t,i] -> a[t-1,i+1] }";
2334         S = "{ S1[t,i] -> [t,i] }";
2335         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2336                 return -1;
2337
2338         /* Fig. 5 of CC2008 */
2339         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2340                                 "j <= -1 + N }";
2341         W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2342                                 "j >= 2 and j <= -1 + N }";
2343         R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2344                                 "j >= 2 and j <= -1 + N; "
2345                     "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2346                                 "j >= 2 and j <= -1 + N }";
2347         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2348         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2349                 return -1;
2350
2351         D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2352         W = "{ S1[i] -> a[i] }";
2353         R = "{ S2[i] -> a[i+1] }";
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] }";
2360         R = "{ S2[i] -> a[9-i] }";
2361         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2362         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2363                 return -1;
2364
2365         D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2366         W = "{ S1[i] -> a[i] }";
2367         R = "[N] -> { S2[i] -> a[N-1-i] }";
2368         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2369         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2370                 return -1;
2371         
2372         D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2373         W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2374         R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2375         S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2376         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2377                 return -1;
2378
2379         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2380         W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2381         R = "{ S2[i,j] -> a[i-1,j] }";
2382         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2383         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2384                 return -1;
2385
2386         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2387         W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2388         R = "{ S2[i,j] -> a[i,j-1] }";
2389         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2390         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2391                 return -1;
2392
2393         D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2394         W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2395                     "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2396         R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2397         S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2398                     "S_0[] -> [0, 0, 0] }";
2399         if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2400                 return -1;
2401         ctx->opt->schedule_parametric = 0;
2402         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2403                 return -1;
2404         ctx->opt->schedule_parametric = 1;
2405
2406         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2407                     "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2408         W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2409         R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2410                     "S4[i] -> a[i,N] }";
2411         S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2412                 "S4[i] -> [4,i,0] }";
2413         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2414                 return -1;
2415
2416         D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2417         W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2418                                         "j <= N }";
2419         R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2420                                         "j <= N; "
2421                     "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2422                                         "j <= N }";
2423         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2424         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2425                 return -1;
2426
2427         D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2428                     " S_2[t] : t >= 0 and t <= -1 + N; "
2429                     " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2430                                 "i <= -1 + N }";
2431         W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2432                     " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2433                     " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2434                                                 "i >= 0 and i <= -1 + N }";
2435         R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2436                                             "i >= 0 and i <= -1 + N; "
2437                     " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2438         S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2439                     " S_0[t] -> [0, t, 0] }";
2440
2441         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2442                 return -1;
2443         ctx->opt->schedule_parametric = 0;
2444         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2445                 return -1;
2446         ctx->opt->schedule_parametric = 1;
2447
2448         D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2449         S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2450         if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2451                 return -1;
2452
2453         D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2454             "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2455         W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2456                                             "j >= 0 and j <= -1 + N; "
2457                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2458         R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2459                                             "j >= 0 and j <= -1 + N; "
2460                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2461         S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2462         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2463                 return -1;
2464
2465         D = "{ S_0[i] : i >= 0 }";
2466         W = "{ S_0[i] -> a[i] : i >= 0 }";
2467         R = "{ S_0[i] -> a[0] : i >= 0 }";
2468         S = "{ S_0[i] -> [0, i, 0] }";
2469         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2470                 return -1;
2471
2472         D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2473         W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2474         R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2475         S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2476         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2477                 return -1;
2478
2479         D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2480                                 "k <= -1 + n and k >= 0 }";
2481         W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "                                                  "k <= -1 + n and k >= 0 }";
2482         R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2483                                         "k <= -1 + n and k >= 0; "
2484                     "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2485                                         "k <= -1 + n and k >= 0; "
2486                     "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2487                                         "k <= -1 + n and k >= 0 }";
2488         S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2489         ctx->opt->schedule_outer_zero_distance = 1;
2490         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2491                 return -1;
2492         ctx->opt->schedule_outer_zero_distance = 0;
2493
2494         D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2495                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2496                 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2497              "Stmt_for_body24[i0, i1, 1, 0]:"
2498                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2499              "Stmt_for_body7[i0, i1, i2]:"
2500                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2501                 "i2 <= 7 }";
2502
2503         V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2504                 "Stmt_for_body24[1, i1, i2, i3]:"
2505                 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2506                 "i2 >= 1;"
2507              "Stmt_for_body24[0, i1, i2, i3] -> "
2508                 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2509                 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2510                 "i3 >= 0;"
2511               "Stmt_for_body24[0, i1, i2, i3] ->"
2512                 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2513                 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2514               "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2515                 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2516                 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2517                 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2518               "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2519                 "i1 <= 6 and i1 >= 0;"
2520               "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2521               "Stmt_for_body7[i0, i1, i2] -> "
2522                 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2523                 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2524                 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2525               "Stmt_for_body7[i0, i1, i2] -> "
2526                 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2527                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2528                 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2529         P = V;
2530         S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2531                 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2532             "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2533
2534         if (test_special_schedule(ctx, D, V, P, S) < 0)
2535                 return -1;
2536
2537         D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2538         V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2539                                            "j >= 1 and j <= 7;"
2540                 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2541                                              "j >= 1 and j <= 8 }";
2542         P = "{ }";
2543         S = "{ S_0[i, j] -> [i + j, 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         /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2550         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2551                                  "j >= 0 and j <= -1 + i }";
2552         V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2553                                         "i <= -1 + N and j >= 0;"
2554                      "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2555                                         "i <= -2 + N }";
2556         P = "{ }";
2557         S = "{ S_0[i, j] -> [i, j] }";
2558         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2559         if (test_special_schedule(ctx, D, V, P, S) < 0)
2560                 return -1;
2561         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2562
2563         /* Test both algorithms on a case with only proximity dependences. */
2564         D = "{ S[i,j] : 0 <= i <= 10 }";
2565         V = "{ }";
2566         P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2567         S = "{ S[i, j] -> [j, i] }";
2568         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2569         if (test_special_schedule(ctx, D, V, P, S) < 0)
2570                 return -1;
2571         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2572         if (test_special_schedule(ctx, D, V, P, S) < 0)
2573                 return -1;
2574         
2575         D = "{ A[a]; B[] }";
2576         V = "{}";
2577         P = "{ A[a] -> B[] }";
2578         if (test_has_schedule(ctx, D, V, P) < 0)
2579                 return -1;
2580
2581         if (test_padded_schedule(ctx) < 0)
2582                 return -1;
2583
2584         return 0;
2585 }
2586
2587 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2588 {
2589         isl_union_map *umap;
2590         int test;
2591
2592         umap = isl_union_map_read_from_str(ctx, str);
2593         test = isl_union_map_plain_is_injective(umap);
2594         isl_union_map_free(umap);
2595         if (test < 0)
2596                 return -1;
2597         if (test == injective)
2598                 return 0;
2599         if (injective)
2600                 isl_die(ctx, isl_error_unknown,
2601                         "map not detected as injective", return -1);
2602         else
2603                 isl_die(ctx, isl_error_unknown,
2604                         "map detected as injective", return -1);
2605 }
2606
2607 int test_injective(isl_ctx *ctx)
2608 {
2609         const char *str;
2610
2611         if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2612                 return -1;
2613         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2614                 return -1;
2615         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2616                 return -1;
2617         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2618                 return -1;
2619         if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2620                 return -1;
2621         if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2622                 return -1;
2623         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2624                 return -1;
2625         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2626                 return -1;
2627
2628         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2629         if (test_plain_injective(ctx, str, 1))
2630                 return -1;
2631         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2632         if (test_plain_injective(ctx, str, 0))
2633                 return -1;
2634
2635         return 0;
2636 }
2637
2638 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
2639 {
2640         isl_aff *aff2;
2641         int equal;
2642
2643         if (!aff)
2644                 return -1;
2645
2646         aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
2647         equal = isl_aff_plain_is_equal(aff, aff2);
2648         isl_aff_free(aff2);
2649
2650         return equal;
2651 }
2652
2653 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
2654 {
2655         int equal;
2656
2657         equal = aff_plain_is_equal(aff, str);
2658         if (equal < 0)
2659                 return -1;
2660         if (!equal)
2661                 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
2662                         "result not as expected", return -1);
2663         return 0;
2664 }
2665
2666 int test_aff(isl_ctx *ctx)
2667 {
2668         const char *str;
2669         isl_set *set;
2670         isl_space *space;
2671         isl_local_space *ls;
2672         isl_aff *aff;
2673         int zero, equal;
2674
2675         space = isl_space_set_alloc(ctx, 0, 1);
2676         ls = isl_local_space_from_space(space);
2677         aff = isl_aff_zero_on_domain(ls);
2678
2679         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2680         aff = isl_aff_scale_down_ui(aff, 3);
2681         aff = isl_aff_floor(aff);
2682         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2683         aff = isl_aff_scale_down_ui(aff, 2);
2684         aff = isl_aff_floor(aff);
2685         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2686
2687         str = "{ [10] }";
2688         set = isl_set_read_from_str(ctx, str);
2689         aff = isl_aff_gist(aff, set);
2690
2691         aff = isl_aff_add_constant_si(aff, -16);
2692         zero = isl_aff_plain_is_zero(aff);
2693         isl_aff_free(aff);
2694
2695         if (zero < 0)
2696                 return -1;
2697         if (!zero)
2698                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2699
2700         aff = isl_aff_read_from_str(ctx, "{ [-1] }");
2701         aff = isl_aff_scale_down_ui(aff, 64);
2702         aff = isl_aff_floor(aff);
2703         equal = aff_check_plain_equal(aff, "{ [-1] }");
2704         isl_aff_free(aff);
2705         if (equal < 0)
2706                 return -1;
2707
2708         return 0;
2709 }
2710
2711 int test_dim_max(isl_ctx *ctx)
2712 {
2713         int equal;
2714         const char *str;
2715         isl_set *set1, *set2;
2716         isl_set *set;
2717         isl_map *map;
2718         isl_pw_aff *pwaff;
2719
2720         str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
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] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
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 <= max(2N,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] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2749         set = isl_set_read_from_str(ctx, str);
2750         pwaff = isl_set_dim_max(set, 0);
2751         set1 = isl_set_from_pw_aff(pwaff);
2752         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2753         set2 = isl_set_read_from_str(ctx, str);
2754         equal = isl_set_is_equal(set1, set2);
2755         isl_set_free(set1);
2756         isl_set_free(set2);
2757         if (equal < 0)
2758                 return -1;
2759         if (!equal)
2760                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2761
2762         str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2763                         "0 <= i < N and 0 <= j < M }";
2764         map = isl_map_read_from_str(ctx, str);
2765         set = isl_map_range(map);
2766
2767         pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2768         set1 = isl_set_from_pw_aff(pwaff);
2769         str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2770         set2 = isl_set_read_from_str(ctx, str);
2771         equal = isl_set_is_equal(set1, set2);
2772         isl_set_free(set1);
2773         isl_set_free(set2);
2774
2775         pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2776         set1 = isl_set_from_pw_aff(pwaff);
2777         str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2778         set2 = isl_set_read_from_str(ctx, str);
2779         if (equal >= 0 && equal)
2780                 equal = isl_set_is_equal(set1, set2);
2781         isl_set_free(set1);
2782         isl_set_free(set2);
2783
2784         isl_set_free(set);
2785
2786         if (equal < 0)
2787                 return -1;
2788         if (!equal)
2789                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2790
2791         /* Check that solutions are properly merged. */
2792         str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
2793                                 "c <= -1 + n - 4a - 2b and c >= -2b and "
2794                                 "4a >= -4 + n and c >= 0 }";
2795         set = isl_set_read_from_str(ctx, str);
2796         pwaff = isl_set_dim_min(set, 2);
2797         set1 = isl_set_from_pw_aff(pwaff);
2798         str = "[n] -> { [(0)] : n >= 1 }";
2799         set2 = isl_set_read_from_str(ctx, str);
2800         equal = isl_set_is_equal(set1, set2);
2801         isl_set_free(set1);
2802         isl_set_free(set2);
2803
2804         if (equal < 0)
2805                 return -1;
2806         if (!equal)
2807                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2808
2809         return 0;
2810 }
2811
2812 int test_product(isl_ctx *ctx)
2813 {
2814         const char *str;
2815         isl_set *set;
2816         isl_union_set *uset1, *uset2;
2817         int ok;
2818
2819         str = "{ A[i] }";
2820         set = isl_set_read_from_str(ctx, str);
2821         set = isl_set_product(set, isl_set_copy(set));
2822         ok = isl_set_is_wrapping(set);
2823         isl_set_free(set);
2824         if (ok < 0)
2825                 return -1;
2826         if (!ok)
2827                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2828
2829         str = "{ [] }";
2830         uset1 = isl_union_set_read_from_str(ctx, str);
2831         uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
2832         str = "{ [[] -> []] }";
2833         uset2 = isl_union_set_read_from_str(ctx, str);
2834         ok = isl_union_set_is_equal(uset1, uset2);
2835         isl_union_set_free(uset1);
2836         isl_union_set_free(uset2);
2837         if (ok < 0)
2838                 return -1;
2839         if (!ok)
2840                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2841
2842         return 0;
2843 }
2844
2845 int test_equal(isl_ctx *ctx)
2846 {
2847         const char *str;
2848         isl_set *set, *set2;
2849         int equal;
2850
2851         str = "{ S_6[i] }";
2852         set = isl_set_read_from_str(ctx, str);
2853         str = "{ S_7[i] }";
2854         set2 = isl_set_read_from_str(ctx, str);
2855         equal = isl_set_is_equal(set, set2);
2856         isl_set_free(set);
2857         isl_set_free(set2);
2858         if (equal < 0)
2859                 return -1;
2860         if (equal)
2861                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2862
2863         return 0;
2864 }
2865
2866 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2867         enum isl_dim_type type, unsigned pos, int fixed)
2868 {
2869         int test;
2870
2871         test = isl_map_plain_is_fixed(map, type, pos, NULL);
2872         isl_map_free(map);
2873         if (test < 0)
2874                 return -1;
2875         if (test == fixed)
2876                 return 0;
2877         if (fixed)
2878                 isl_die(ctx, isl_error_unknown,
2879                         "map not detected as fixed", return -1);
2880         else
2881                 isl_die(ctx, isl_error_unknown,
2882                         "map detected as fixed", return -1);
2883 }
2884
2885 int test_fixed(isl_ctx *ctx)
2886 {
2887         const char *str;
2888         isl_map *map;
2889
2890         str = "{ [i] -> [i] }";
2891         map = isl_map_read_from_str(ctx, str);
2892         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2893                 return -1;
2894         str = "{ [i] -> [1] }";
2895         map = isl_map_read_from_str(ctx, str);
2896         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2897                 return -1;
2898         str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2899         map = isl_map_read_from_str(ctx, str);
2900         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2901                 return -1;
2902         map = isl_map_read_from_str(ctx, str);
2903         map = isl_map_neg(map);
2904         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2905                 return -1;
2906
2907         return 0;
2908 }
2909
2910 int test_vertices(isl_ctx *ctx)
2911 {
2912         const char *str;
2913         isl_basic_set *bset;
2914         isl_vertices *vertices;
2915
2916         str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
2917         bset = isl_basic_set_read_from_str(ctx, str);
2918         vertices = isl_basic_set_compute_vertices(bset);
2919         isl_basic_set_free(bset);
2920         isl_vertices_free(vertices);
2921         if (!vertices)
2922                 return -1;
2923
2924         str = "{ A[t, i] : t = 14 and i = 1 }";
2925         bset = isl_basic_set_read_from_str(ctx, str);
2926         vertices = isl_basic_set_compute_vertices(bset);
2927         isl_basic_set_free(bset);
2928         isl_vertices_free(vertices);
2929         if (!vertices)
2930                 return -1;
2931
2932         return 0;
2933 }
2934
2935 int test_union_pw(isl_ctx *ctx)
2936 {
2937         int equal;
2938         const char *str;
2939         isl_union_set *uset;
2940         isl_union_pw_qpolynomial *upwqp1, *upwqp2;
2941
2942         str = "{ [x] -> x^2 }";
2943         upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
2944         upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
2945         uset = isl_union_pw_qpolynomial_domain(upwqp1);
2946         upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
2947         upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
2948         equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
2949         isl_union_pw_qpolynomial_free(upwqp1);
2950         isl_union_pw_qpolynomial_free(upwqp2);
2951         if (equal < 0)
2952                 return -1;
2953         if (!equal)
2954                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2955
2956         return 0;
2957 }
2958
2959 int test_output(isl_ctx *ctx)
2960 {
2961         char *s;
2962         const char *str;
2963         isl_pw_aff *pa;
2964         isl_printer *p;
2965         int equal;
2966
2967         str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
2968         pa = isl_pw_aff_read_from_str(ctx, str);
2969
2970         p = isl_printer_to_str(ctx);
2971         p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2972         p = isl_printer_print_pw_aff(p, pa);
2973         s = isl_printer_get_str(p);
2974         isl_printer_free(p);
2975         isl_pw_aff_free(pa);
2976         if (!s)
2977                 equal = -1;
2978         else
2979                 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
2980         free(s);
2981         if (equal < 0)
2982                 return -1;
2983         if (!equal)
2984                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2985
2986         return 0;
2987 }
2988
2989 int test_sample(isl_ctx *ctx)
2990 {
2991         const char *str;
2992         isl_basic_set *bset1, *bset2;
2993         int empty, subset;
2994
2995         str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
2996             "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
2997             "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
2998             "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
2999             "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3000             "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3001             "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3002             "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3003             "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3004             "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3005             "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3006             "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3007                 "d - 1073741821e and "
3008             "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3009             "3j >= 1 - a + b + 2e and "
3010             "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3011             "3i <= 4 - a + 4b - e and "
3012             "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3013             "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3014             "c <= -1 + a and 3i >= -2 - a + 3e and "
3015             "1073741822e <= 1073741823 - a + 1073741822b + c and "
3016             "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3017             "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3018             "1073741823e >= 1 + 1073741823b - d and "
3019             "3i >= 1073741823b + c - 1073741823e - f and "
3020             "3i >= 1 + 2b + e + 3g }";
3021         bset1 = isl_basic_set_read_from_str(ctx, str);
3022         bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3023         empty = isl_basic_set_is_empty(bset2);
3024         subset = isl_basic_set_is_subset(bset2, bset1);
3025         isl_basic_set_free(bset1);
3026         isl_basic_set_free(bset2);
3027         if (empty < 0 || subset < 0)
3028                 return -1;
3029         if (empty)
3030                 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3031         if (!subset)
3032                 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3033
3034         return 0;
3035 }
3036
3037 int test_fixed_power(isl_ctx *ctx)
3038 {
3039         const char *str;
3040         isl_map *map;
3041         isl_int exp;
3042         int equal;
3043
3044         isl_int_init(exp);
3045         str = "{ [i] -> [i + 1] }";
3046         map = isl_map_read_from_str(ctx, str);
3047         isl_int_set_si(exp, 23);
3048         map = isl_map_fixed_power(map, exp);
3049         equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3050         isl_int_clear(exp);
3051         isl_map_free(map);
3052         if (equal < 0)
3053                 return -1;
3054
3055         return 0;
3056 }
3057
3058 int test_slice(isl_ctx *ctx)
3059 {
3060         const char *str;
3061         isl_map *map;
3062         int equal;
3063
3064         str = "{ [i] -> [j] }";
3065         map = isl_map_read_from_str(ctx, str);
3066         map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3067         equal = map_check_equal(map, "{ [i] -> [i] }");
3068         isl_map_free(map);
3069         if (equal < 0)
3070                 return -1;
3071
3072         str = "{ [i] -> [j] }";
3073         map = isl_map_read_from_str(ctx, str);
3074         map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3075         equal = map_check_equal(map, "{ [i] -> [j] }");
3076         isl_map_free(map);
3077         if (equal < 0)
3078                 return -1;
3079
3080         str = "{ [i] -> [j] }";
3081         map = isl_map_read_from_str(ctx, str);
3082         map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3083         equal = map_check_equal(map, "{ [i] -> [-i] }");
3084         isl_map_free(map);
3085         if (equal < 0)
3086                 return -1;
3087
3088         str = "{ [i] -> [j] }";
3089         map = isl_map_read_from_str(ctx, str);
3090         map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3091         equal = map_check_equal(map, "{ [0] -> [j] }");
3092         isl_map_free(map);
3093         if (equal < 0)
3094                 return -1;
3095
3096         str = "{ [i] -> [j] }";
3097         map = isl_map_read_from_str(ctx, str);
3098         map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3099         equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3100         isl_map_free(map);
3101         if (equal < 0)
3102                 return -1;
3103
3104         str = "{ [i] -> [j] }";
3105         map = isl_map_read_from_str(ctx, str);
3106         map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3107         equal = map_check_equal(map, "{ [i] -> [j] : false }");
3108         isl_map_free(map);
3109         if (equal < 0)
3110                 return -1;
3111
3112         return 0;
3113 }
3114
3115 int test_eliminate(isl_ctx *ctx)
3116 {
3117         const char *str;
3118         isl_map *map;
3119         int equal;
3120
3121         str = "{ [i] -> [j] : i = 2j }";
3122         map = isl_map_read_from_str(ctx, str);
3123         map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3124         equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3125         isl_map_free(map);
3126         if (equal < 0)
3127                 return -1;
3128
3129         return 0;
3130 }
3131
3132 /* Check that isl_set_dim_residue_class detects that the values of j
3133  * in the set below are all odd and that it does not detect any spurious
3134  * strides.
3135  */
3136 static int test_residue_class(isl_ctx *ctx)
3137 {
3138         const char *str;
3139         isl_set *set;
3140         isl_int m, r;
3141         int res;
3142
3143         str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3144                 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3145         set = isl_set_read_from_str(ctx, str);
3146         isl_int_init(m);
3147         isl_int_init(r);
3148         res = isl_set_dim_residue_class(set, 1, &m, &r);
3149         if (res >= 0 &&
3150             (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3151                 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3152                         res = -1);
3153         isl_int_clear(r);
3154         isl_int_clear(m);
3155         isl_set_free(set);
3156
3157         return res;
3158 }
3159
3160 int test_align_parameters(isl_ctx *ctx)
3161 {
3162         const char *str;
3163         isl_space *space;
3164         isl_multi_aff *ma1, *ma2;
3165         int equal;
3166
3167         str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3168         ma1 = isl_multi_aff_read_from_str(ctx, str);
3169
3170         space = isl_space_params_alloc(ctx, 1);
3171         space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3172         ma1 = isl_multi_aff_align_params(ma1, space);
3173
3174         str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3175         ma2 = isl_multi_aff_read_from_str(ctx, str);
3176
3177         equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3178
3179         isl_multi_aff_free(ma1);
3180         isl_multi_aff_free(ma2);
3181
3182         if (equal < 0)
3183                 return -1;
3184         if (!equal)
3185                 isl_die(ctx, isl_error_unknown,
3186                         "result not as expected", return -1);
3187
3188         return 0;
3189 }
3190
3191 static int test_list(isl_ctx *ctx)
3192 {
3193         isl_id *a, *b, *c, *d, *id;
3194         isl_id_list *list;
3195         int ok;
3196
3197         a = isl_id_alloc(ctx, "a", NULL);
3198         b = isl_id_alloc(ctx, "b", NULL);
3199         c = isl_id_alloc(ctx, "c", NULL);
3200         d = isl_id_alloc(ctx, "d", NULL);
3201
3202         list = isl_id_list_alloc(ctx, 4);
3203         list = isl_id_list_add(list, a);
3204         list = isl_id_list_add(list, b);
3205         list = isl_id_list_add(list, c);
3206         list = isl_id_list_add(list, d);
3207         list = isl_id_list_drop(list, 1, 1);
3208
3209         if (isl_id_list_n_id(list) != 3) {
3210                 isl_id_list_free(list);
3211                 isl_die(ctx, isl_error_unknown,
3212                         "unexpected number of elements in list", return -1);
3213         }
3214
3215         id = isl_id_list_get_id(list, 0);
3216         ok = id == a;
3217         isl_id_free(id);
3218         id = isl_id_list_get_id(list, 1);
3219         ok = ok && id == c;
3220         isl_id_free(id);
3221         id = isl_id_list_get_id(list, 2);
3222         ok = ok && id == d;
3223         isl_id_free(id);
3224
3225         isl_id_list_free(list);
3226
3227         if (!ok)
3228                 isl_die(ctx, isl_error_unknown,
3229                         "unexpected elements in list", return -1);
3230
3231         return 0;
3232 }
3233
3234 const char *set_conversion_tests[] = {
3235         "[N] -> { [i] : N - 1 <= 2 i <= N }",
3236         "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3237         "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3238         "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3239 };
3240
3241 /* Check that converting from isl_set to isl_pw_multi_aff and back
3242  * to isl_set produces the original isl_set.
3243  */
3244 static int test_set_conversion(isl_ctx *ctx)
3245 {
3246         int i;
3247         const char *str;
3248         isl_set *set1, *set2;
3249         isl_pw_multi_aff *pma;
3250         int equal;
3251
3252         for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3253                 str = set_conversion_tests[i];
3254                 set1 = isl_set_read_from_str(ctx, str);
3255                 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3256                 set2 = isl_set_from_pw_multi_aff(pma);
3257                 equal = isl_set_is_equal(set1, set2);
3258                 isl_set_free(set1);
3259                 isl_set_free(set2);
3260
3261                 if (equal < 0)
3262                         return -1;
3263                 if (!equal)
3264                         isl_die(ctx, isl_error_unknown, "bad conversion",
3265                                 return -1);
3266         }
3267
3268         return 0;
3269 }
3270
3271 /* Check that converting from isl_map to isl_pw_multi_aff and back
3272  * to isl_map produces the original isl_map.
3273  */
3274 static int test_map_conversion(isl_ctx *ctx)
3275 {
3276         const char *str;
3277         isl_map *map1, *map2;
3278         isl_pw_multi_aff *pma;
3279         int equal;
3280
3281         str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3282                 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3283                 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3284                 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3285                 "9e <= -2 - 2a) }";
3286         map1 = isl_map_read_from_str(ctx, str);
3287         pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3288         map2 = isl_map_from_pw_multi_aff(pma);
3289         equal = isl_map_is_equal(map1, map2);
3290         isl_map_free(map1);
3291         isl_map_free(map2);
3292
3293         if (equal < 0)
3294                 return -1;
3295         if (!equal)
3296                 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3297
3298         return 0;
3299 }
3300
3301 static int test_conversion(isl_ctx *ctx)
3302 {
3303         if (test_set_conversion(ctx) < 0)
3304                 return -1;
3305         if (test_map_conversion(ctx) < 0)
3306                 return -1;
3307         return 0;
3308 }
3309
3310 /* Check that isl_basic_map_curry does not modify input.
3311  */
3312 static int test_curry(isl_ctx *ctx)
3313 {
3314         const char *str;
3315         isl_basic_map *bmap1, *bmap2;
3316         int equal;
3317
3318         str = "{ [A[] -> B[]] -> C[] }";
3319         bmap1 = isl_basic_map_read_from_str(ctx, str);
3320         bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3321         equal = isl_basic_map_is_equal(bmap1, bmap2);
3322         isl_basic_map_free(bmap1);
3323         isl_basic_map_free(bmap2);
3324
3325         if (equal < 0)
3326                 return -1;
3327         if (equal)
3328                 isl_die(ctx, isl_error_unknown,
3329                         "curried map should not be equal to original",
3330                         return -1);
3331
3332         return 0;
3333 }
3334
3335 struct {
3336         const char *set;
3337         const char *ma;
3338         const char *res;
3339 } preimage_tests[] = {
3340         { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3341           "{ A[j,i] -> B[i,j] }",
3342           "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3343         { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3344           "{ A[a,b] -> B[a/2,b/6] }",
3345           "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3346         { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3347           "{ A[a,b] -> B[a/2,b/6] }",
3348           "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3349                     "exists i,j : a = 2 i and b = 6 j }" },
3350         { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3351           "[n] -> { : 0 <= n <= 100 }" },
3352         { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3353           "{ A[a] -> B[2a] }",
3354           "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3355         { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3356           "{ A[a] -> B[([a/2])] }",
3357           "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3358         { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3359           "{ A[a] -> B[a,a,a/3] }",
3360           "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3361         { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3362           "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3363 };
3364
3365 static int test_preimage_basic_set(isl_ctx *ctx)
3366 {
3367         int i;
3368         isl_basic_set *bset1, *bset2;
3369         isl_multi_aff *ma;
3370         int equal;
3371
3372         for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3373                 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3374                 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3375                 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3376                 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3377                 equal = isl_basic_set_is_equal(bset1, bset2);
3378                 isl_basic_set_free(bset1);
3379                 isl_basic_set_free(bset2);
3380                 if (equal < 0)
3381                         return -1;
3382                 if (!equal)
3383                         isl_die(ctx, isl_error_unknown, "bad preimage",
3384                                 return -1);
3385         }
3386
3387         return 0;
3388 }
3389
3390 struct {
3391         const char *map;
3392         const char *ma;
3393         const char *res;
3394 } preimage_domain_tests[] = {
3395         { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
3396           "{ A[j,i] -> B[i,j] }",
3397           "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
3398         { "{ B[i] -> C[i]; D[i] -> E[i] }",
3399           "{ A[i] -> B[i + 1] }",
3400           "{ A[i] -> C[i + 1] }" },
3401         { "{ B[i] -> C[i]; B[i] -> E[i] }",
3402           "{ A[i] -> B[i + 1] }",
3403           "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
3404         { "{ B[i] -> C[([i/2])] }",
3405           "{ A[i] -> B[2i] }",
3406           "{ A[i] -> C[i] }" },
3407         { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
3408           "{ A[i] -> B[([i/5]), ([i/7])] }",
3409           "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
3410         { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
3411           "[N] -> { A[] -> B[([N/5])] }",
3412           "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
3413         { "{ B[i] -> C[i] : exists a : i = 5 a }",
3414           "{ A[i] -> B[2i] }",
3415           "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
3416         { "{ B[i] -> C[i] : exists a : i = 2 a; "
3417             "B[i] -> D[i] : exists a : i = 2 a + 1 }",
3418           "{ A[i] -> B[2i] }",
3419           "{ A[i] -> C[2i] }" },
3420 };
3421
3422 static int test_preimage_union_map(isl_ctx *ctx)
3423 {
3424         int i;
3425         isl_union_map *umap1, *umap2;
3426         isl_multi_aff *ma;
3427         int equal;
3428
3429         for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
3430                 umap1 = isl_union_map_read_from_str(ctx,
3431                                                 preimage_domain_tests[i].map);
3432                 ma = isl_multi_aff_read_from_str(ctx,
3433                                                 preimage_domain_tests[i].ma);
3434                 umap2 = isl_union_map_read_from_str(ctx,
3435                                                 preimage_domain_tests[i].res);
3436                 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
3437                 equal = isl_union_map_is_equal(umap1, umap2);
3438                 isl_union_map_free(umap1);
3439                 isl_union_map_free(umap2);
3440                 if (equal < 0)
3441                         return -1;
3442                 if (!equal)
3443                         isl_die(ctx, isl_error_unknown, "bad preimage",
3444                                 return -1);
3445         }
3446
3447         return 0;
3448 }
3449
3450 static int test_preimage(isl_ctx *ctx)
3451 {
3452         if (test_preimage_basic_set(ctx) < 0)
3453                 return -1;
3454         if (test_preimage_union_map(ctx) < 0)
3455                 return -1;
3456
3457         return 0;
3458 }
3459
3460 struct {
3461         const char *ma1;
3462         const char *ma;
3463         const char *res;
3464 } pullback_tests[] = {
3465         { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
3466           "{ A[a,b] -> C[b + 2a] }" },
3467         { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
3468         { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
3469         { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
3470           "{ A[a] -> C[(a)/6] }" },
3471         { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
3472         { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
3473           "{ A[a] -> C[(2a)/3] }" },
3474         { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
3475         { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
3476           "{ A[i,j] -> C[i + j, i + j] }"},
3477         { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
3478         { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
3479           "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
3480 };
3481
3482 static int test_pullback(isl_ctx *ctx)
3483 {
3484         int i;
3485         isl_multi_aff *ma1, *ma2;
3486         isl_multi_aff *ma;
3487         int equal;
3488
3489         for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
3490                 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
3491                 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
3492                 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
3493                 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
3494                 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3495                 isl_multi_aff_free(ma1);
3496                 isl_multi_aff_free(ma2);
3497                 if (equal < 0)
3498                         return -1;
3499                 if (!equal)
3500                         isl_die(ctx, isl_error_unknown, "bad pullback",
3501                                 return -1);
3502         }
3503
3504         return 0;
3505 }
3506
3507 /* Check that negation is printed correctly.
3508  */
3509 static int test_ast(isl_ctx *ctx)
3510 {
3511         isl_ast_expr *expr, *expr1, *expr2, *expr3;
3512         char *str;
3513         int ok;
3514
3515         expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3516         expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3517         expr = isl_ast_expr_add(expr1, expr2);
3518         expr = isl_ast_expr_neg(expr);
3519         str = isl_ast_expr_to_str(expr);
3520         ok = str ? !strcmp(str, "-(A + B)") : -1;
3521         free(str);
3522         isl_ast_expr_free(expr);
3523
3524         if (ok < 0)
3525                 return -1;
3526         if (!ok)
3527                 isl_die(ctx, isl_error_unknown,
3528                         "isl_ast_expr printed incorrectly", return -1);
3529
3530         expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3531         expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3532         expr = isl_ast_expr_add(expr1, expr2);
3533         expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
3534         expr = isl_ast_expr_sub(expr3, expr);
3535         str = isl_ast_expr_to_str(expr);
3536         ok = str ? !strcmp(str, "C - (A + B)") : -1;
3537         free(str);
3538         isl_ast_expr_free(expr);
3539
3540         if (ok < 0)
3541                 return -1;
3542         if (!ok)
3543                 isl_die(ctx, isl_error_unknown,
3544                         "isl_ast_expr printed incorrectly", return -1);
3545
3546         return 0;
3547 }
3548
3549 /* Internal data structure for before_for and after_for callbacks.
3550  *
3551  * depth is the current depth
3552  * before is the number of times before_for has been called
3553  * after is the number of times after_for has been called
3554  */
3555 struct isl_test_codegen_data {
3556         int depth;
3557         int before;
3558         int after;
3559 };
3560
3561 /* This function is called before each for loop in the AST generated
3562  * from test_ast_gen1.
3563  *
3564  * Increment the number of calls and the depth.
3565  * Check that the space returned by isl_ast_build_get_schedule_space
3566  * matches the target space of the schedule returned by
3567  * isl_ast_build_get_schedule.
3568  * Return an isl_id that is checked by the corresponding call
3569  * to after_for.
3570  */
3571 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
3572         void *user)
3573 {
3574         struct isl_test_codegen_data *data = user;
3575         isl_ctx *ctx;
3576         isl_space *space;
3577         isl_union_map *schedule;
3578         isl_union_set *uset;
3579         isl_set *set;
3580         int empty;
3581         char name[] = "d0";
3582
3583         ctx = isl_ast_build_get_ctx(build);
3584
3585         if (data->before >= 3)
3586                 isl_die(ctx, isl_error_unknown,
3587                         "unexpected number of for nodes", return NULL);
3588         if (data->depth >= 2)
3589                 isl_die(ctx, isl_error_unknown,
3590                         "unexpected depth", return NULL);
3591
3592         snprintf(name, sizeof(name), "d%d", data->depth);
3593         data->before++;
3594         data->depth++;
3595
3596         schedule = isl_ast_build_get_schedule(build);
3597         uset = isl_union_map_range(schedule);
3598         if (!uset)
3599                 return NULL;
3600         if (isl_union_set_n_set(uset) != 1) {
3601                 isl_union_set_free(uset);
3602                 isl_die(ctx, isl_error_unknown,
3603                         "expecting single range space", return NULL);
3604         }
3605
3606         space = isl_ast_build_get_schedule_space(build);
3607         set = isl_union_set_extract_set(uset, space);
3608         isl_union_set_free(uset);
3609         empty = isl_set_is_empty(set);
3610         isl_set_free(set);
3611
3612         if (empty < 0)
3613                 return NULL;
3614         if (empty)
3615                 isl_die(ctx, isl_error_unknown,
3616                         "spaces don't match", return NULL);
3617
3618         return isl_id_alloc(ctx, name, NULL);
3619 }
3620
3621 /* This function is called after each for loop in the AST generated
3622  * from test_ast_gen1.
3623  *
3624  * Increment the number of calls and decrement the depth.
3625  * Check that the annotation attached to the node matches
3626  * the isl_id returned by the corresponding call to before_for.
3627  */
3628 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
3629         __isl_keep isl_ast_build *build, void *user)
3630 {
3631         struct isl_test_codegen_data *data = user;
3632         isl_id *id;
3633         const char *name;
3634         int valid;
3635
3636         data->after++;
3637         data->depth--;
3638
3639         if (data->after > data->before)
3640                 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3641                         "mismatch in number of for nodes",
3642                         return isl_ast_node_free(node));
3643
3644         id = isl_ast_node_get_annotation(node);
3645         if (!id)
3646                 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3647                         "missing annotation", return isl_ast_node_free(node));
3648
3649         name = isl_id_get_name(id);
3650         valid = name && atoi(name + 1) == data->depth;
3651         isl_id_free(id);
3652
3653         if (!valid)
3654                 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3655                         "wrong annotation", return isl_ast_node_free(node));
3656
3657         return node;
3658 }
3659
3660 /* Check that the before_each_for and after_each_for callbacks
3661  * are called for each for loop in the generated code,
3662  * that they are called in the right order and that the isl_id
3663  * returned from the before_each_for callback is attached to
3664  * the isl_ast_node passed to the corresponding after_each_for call.
3665  */
3666 static int test_ast_gen1(isl_ctx *ctx)
3667 {
3668         const char *str;
3669         isl_set *set;
3670         isl_union_map *schedule;
3671         isl_ast_build *build;
3672         isl_ast_node *tree;
3673         struct isl_test_codegen_data data;
3674
3675         str = "[N] -> { : N >= 10 }";
3676         set = isl_set_read_from_str(ctx, str);
3677         str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
3678                     "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
3679         schedule = isl_union_map_read_from_str(ctx, str);
3680
3681         data.before = 0;
3682         data.after = 0;
3683         data.depth = 0;
3684         build = isl_ast_build_from_context(set);
3685         build = isl_ast_build_set_before_each_for(build,
3686                         &before_for, &data);
3687         build = isl_ast_build_set_after_each_for(build,
3688                         &after_for, &data);
3689         tree = isl_ast_build_ast_from_schedule(build, schedule);
3690         isl_ast_build_free(build);
3691         if (!tree)
3692                 return -1;
3693
3694         isl_ast_node_free(tree);
3695
3696         if (data.before != 3 || data.after != 3)
3697                 isl_die(ctx, isl_error_unknown,
3698                         "unexpected number of for nodes", return -1);
3699
3700         return 0;
3701 }
3702
3703 /* Check that the AST generator handles domains that are integrally disjoint
3704  * but not ratinoally disjoint.
3705  */
3706 static int test_ast_gen2(isl_ctx *ctx)
3707 {
3708         const char *str;
3709         isl_set *set;
3710         isl_union_map *schedule;
3711         isl_union_map *options;
3712         isl_ast_build *build;
3713         isl_ast_node *tree;
3714
3715         str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
3716         schedule = isl_union_map_read_from_str(ctx, str);
3717         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3718         build = isl_ast_build_from_context(set);
3719
3720         str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
3721         options = isl_union_map_read_from_str(ctx, str);
3722         build = isl_ast_build_set_options(build, options);
3723         tree = isl_ast_build_ast_from_schedule(build, schedule);
3724         isl_ast_build_free(build);
3725         if (!tree)
3726                 return -1;
3727         isl_ast_node_free(tree);
3728
3729         return 0;
3730 }
3731
3732 /* Increment *user on each call.
3733  */
3734 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
3735         __isl_keep isl_ast_build *build, void *user)
3736 {
3737         int *n = user;
3738
3739         (*n)++;
3740
3741         return node;
3742 }
3743
3744 /* Test that unrolling tries to minimize the number of instances.
3745  * In particular, for the schedule given below, make sure it generates
3746  * 3 nodes (rather than 101).
3747  */
3748 static int test_ast_gen3(isl_ctx *ctx)
3749 {
3750         const char *str;
3751         isl_set *set;
3752         isl_union_map *schedule;
3753         isl_union_map *options;
3754         isl_ast_build *build;
3755         isl_ast_node *tree;
3756         int n_domain = 0;
3757
3758         str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
3759         schedule = isl_union_map_read_from_str(ctx, str);
3760         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3761
3762         str = "{ [i] -> unroll[0] }";
3763         options = isl_union_map_read_from_str(ctx, str);
3764
3765         build = isl_ast_build_from_context(set);
3766         build = isl_ast_build_set_options(build, options);
3767         build = isl_ast_build_set_at_each_domain(build,
3768                         &count_domains, &n_domain);
3769         tree = isl_ast_build_ast_from_schedule(build, schedule);
3770         isl_ast_build_free(build);
3771         if (!tree)
3772                 return -1;
3773
3774         isl_ast_node_free(tree);
3775
3776         if (n_domain != 3)
3777                 isl_die(ctx, isl_error_unknown,
3778                         "unexpected number of for nodes", return -1);
3779
3780         return 0;
3781 }
3782
3783 /* Check that if the ast_build_exploit_nested_bounds options is set,
3784  * we do not get an outer if node in the generated AST,
3785  * while we do get such an outer if node if the options is not set.
3786  */
3787 static int test_ast_gen4(isl_ctx *ctx)
3788 {
3789         const char *str;
3790         isl_set *set;
3791         isl_union_map *schedule;
3792         isl_ast_build *build;
3793         isl_ast_node *tree;
3794         enum isl_ast_node_type type;
3795         int enb;
3796
3797         enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
3798         str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
3799
3800         isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
3801
3802         schedule = isl_union_map_read_from_str(ctx, str);
3803         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3804         build = isl_ast_build_from_context(set);
3805         tree = isl_ast_build_ast_from_schedule(build, schedule);
3806         isl_ast_build_free(build);
3807         if (!tree)
3808                 return -1;
3809
3810         type = isl_ast_node_get_type(tree);
3811         isl_ast_node_free(tree);
3812
3813         if (type == isl_ast_node_if)
3814                 isl_die(ctx, isl_error_unknown,
3815                         "not expecting if node", return -1);
3816
3817         isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
3818
3819         schedule = isl_union_map_read_from_str(ctx, str);
3820         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3821         build = isl_ast_build_from_context(set);
3822         tree = isl_ast_build_ast_from_schedule(build, schedule);
3823         isl_ast_build_free(build);
3824         if (!tree)
3825                 return -1;
3826
3827         type = isl_ast_node_get_type(tree);
3828         isl_ast_node_free(tree);
3829
3830         if (type != isl_ast_node_if)
3831                 isl_die(ctx, isl_error_unknown,
3832                         "expecting if node", return -1);
3833
3834         isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
3835
3836         return 0;
3837 }
3838
3839 /* This function is called for each leaf in the AST generated
3840  * from test_ast_gen5.
3841  *
3842  * We finalize the AST generation by extending the outer schedule
3843  * with a zero-dimensional schedule.  If this results in any for loops,
3844  * then this means that we did not pass along enough information
3845  * about the outer schedule to the inner AST generation.
3846  */
3847 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
3848         void *user)
3849 {
3850         isl_union_map *schedule, *extra;
3851         isl_ast_node *tree;
3852
3853         schedule = isl_ast_build_get_schedule(build);
3854         extra = isl_union_map_copy(schedule);
3855         extra = isl_union_map_from_domain(isl_union_map_domain(extra));
3856         schedule = isl_union_map_range_product(schedule, extra);
3857         tree = isl_ast_build_ast_from_schedule(build, schedule);
3858         isl_ast_build_free(build);
3859
3860         if (!tree)
3861                 return NULL;
3862
3863         if (isl_ast_node_get_type(tree) == isl_ast_node_for)
3864                 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
3865                         "code should not contain any for loop",
3866                         return isl_ast_node_free(tree));
3867
3868         return tree;
3869 }
3870
3871 /* Check that we do not lose any information when going back and
3872  * forth between internal and external schedule.
3873  *
3874  * In particular, we create an AST where we unroll the only
3875  * non-constant dimension in the schedule.  We therefore do
3876  * not expect any for loops in the AST.  However, older versions
3877  * of isl would not pass along enough information about the outer
3878  * schedule when performing an inner code generation from a create_leaf
3879  * callback, resulting in the inner code generation producing a for loop.
3880  */
3881 static int test_ast_gen5(isl_ctx *ctx)
3882 {
3883         const char *str;
3884         isl_set *set;
3885         isl_union_map *schedule, *options;
3886         isl_ast_build *build;
3887         isl_ast_node *tree;
3888
3889         str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
3890         schedule = isl_union_map_read_from_str(ctx, str);
3891
3892         str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
3893                                 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
3894         options = isl_union_map_read_from_str(ctx, str);
3895
3896         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3897         build = isl_ast_build_from_context(set);
3898         build = isl_ast_build_set_options(build, options);
3899         build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
3900         tree = isl_ast_build_ast_from_schedule(build, schedule);
3901         isl_ast_build_free(build);
3902         isl_ast_node_free(tree);
3903         if (!tree)
3904                 return -1;
3905
3906         return 0;
3907 }
3908
3909 static int test_ast_gen(isl_ctx *ctx)
3910 {
3911         if (test_ast_gen1(ctx) < 0)
3912                 return -1;
3913         if (test_ast_gen2(ctx) < 0)
3914                 return -1;
3915         if (test_ast_gen3(ctx) < 0)
3916                 return -1;
3917         if (test_ast_gen4(ctx) < 0)
3918                 return -1;
3919         if (test_ast_gen5(ctx) < 0)
3920                 return -1;
3921         return 0;
3922 }
3923
3924 /* Check if dropping output dimensions from an isl_pw_multi_aff
3925  * works properly.
3926  */
3927 static int test_pw_multi_aff(isl_ctx *ctx)
3928 {
3929         const char *str;
3930         isl_pw_multi_aff *pma1, *pma2;
3931         int equal;
3932
3933         str = "{ [i,j] -> [i+j, 4i-j] }";
3934         pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3935         str = "{ [i,j] -> [4i-j] }";
3936         pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3937
3938         pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
3939
3940         equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
3941
3942         isl_pw_multi_aff_free(pma1);
3943         isl_pw_multi_aff_free(pma2);
3944         if (equal < 0)
3945                 return -1;
3946         if (!equal)
3947                 isl_die(ctx, isl_error_unknown,
3948                         "expressions not equal", return -1);
3949
3950         return 0;
3951 }
3952
3953 /* This is a regression test for a bug where isl_basic_map_simplify
3954  * would end up in an infinite loop.  In particular, we construct
3955  * an empty basic set that is not obviously empty.
3956  * isl_basic_set_is_empty marks the basic set as empty.
3957  * After projecting out i3, the variable can be dropped completely,
3958  * but isl_basic_map_simplify refrains from doing so if the basic set
3959  * is empty and would end up in an infinite loop if it didn't test
3960  * explicitly for empty basic maps in the outer loop.
3961  */
3962 static int test_simplify(isl_ctx *ctx)
3963 {
3964         const char *str;
3965         isl_basic_set *bset;
3966         int empty;
3967
3968         str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
3969                 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
3970                 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
3971                 "i3 >= i2 }";
3972         bset = isl_basic_set_read_from_str(ctx, str);
3973         empty = isl_basic_set_is_empty(bset);
3974         bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
3975         isl_basic_set_free(bset);
3976         if (!bset)
3977                 return -1;
3978         if (!empty)
3979                 isl_die(ctx, isl_error_unknown,
3980                         "basic set should be empty", return -1);
3981
3982         return 0;
3983 }
3984
3985 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
3986  * with gbr context would fail to disable the use of the shifted tableau
3987  * when transferring equalities for the input to the context, resulting
3988  * in invalid sample values.
3989  */
3990 static int test_partial_lexmin(isl_ctx *ctx)
3991 {
3992         const char *str;
3993         isl_basic_set *bset;
3994         isl_basic_map *bmap;
3995         isl_map *map;
3996
3997         str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
3998         bmap = isl_basic_map_read_from_str(ctx, str);
3999         str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4000         bset = isl_basic_set_read_from_str(ctx, str);
4001         map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4002         isl_map_free(map);
4003
4004         if (!map)
4005                 return -1;
4006
4007         return 0;
4008 }
4009
4010 /* Check that the variable compression performed on the existentially
4011  * quantified variables inside isl_basic_set_compute_divs is not confused
4012  * by the implicit equalities among the parameters.
4013  */
4014 static int test_compute_divs(isl_ctx *ctx)
4015 {
4016         const char *str;
4017         isl_basic_set *bset;
4018         isl_set *set;
4019
4020         str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4021                 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4022                 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4023                 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4024         bset = isl_basic_set_read_from_str(ctx, str);
4025         set = isl_basic_set_compute_divs(bset);
4026         isl_set_free(set);
4027         if (!set)
4028                 return -1;
4029
4030         return 0;
4031 }
4032
4033 struct {
4034         const char *name;
4035         int (*fn)(isl_ctx *ctx);
4036 } tests [] = {
4037         { "compute divs", &test_compute_divs },
4038         { "partial lexmin", &test_partial_lexmin },
4039         { "simplify", &test_simplify },
4040         { "curry", &test_curry },
4041         { "piecewise multi affine expressions", &test_pw_multi_aff },
4042         { "conversion", &test_conversion },
4043         { "list", &test_list },
4044         { "align parameters", &test_align_parameters },
4045         { "preimage", &test_preimage },
4046         { "pullback", &test_pullback },
4047         { "AST", &test_ast },
4048         { "AST generation", &test_ast_gen },
4049         { "eliminate", &test_eliminate },
4050         { "residue class", &test_residue_class },
4051         { "div", &test_div },
4052         { "slice", &test_slice },
4053         { "fixed power", &test_fixed_power },
4054         { "sample", &test_sample },
4055         { "output", &test_output },
4056         { "vertices", &test_vertices },
4057         { "fixed", &test_fixed },
4058         { "equal", &test_equal },
4059         { "product", &test_product },
4060         { "dim_max", &test_dim_max },
4061         { "affine", &test_aff },
4062         { "injective", &test_injective },
4063         { "schedule", &test_schedule },
4064         { "union_pw", &test_union_pw },
4065         { "parse", &test_parse },
4066         { "single-valued", &test_sv },
4067         { "affine hull", &test_affine_hull },
4068         { "coalesce", &test_coalesce },
4069         { "factorize", &test_factorize },
4070         { "subset", &test_subset },
4071         { "subtract", &test_subtract },
4072         { "lexmin", &test_lexmin },
4073 };
4074
4075 int main()
4076 {
4077         int i;
4078         struct isl_ctx *ctx;
4079
4080         srcdir = getenv("srcdir");
4081         assert(srcdir);
4082
4083         ctx = isl_ctx_alloc();
4084         for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4085                 printf("%s\n", tests[i].name);
4086                 if (tests[i].fn(ctx) < 0)
4087                         goto error;
4088         }
4089         test_lift(ctx);
4090         test_bound(ctx);
4091         test_union(ctx);
4092         test_split_periods(ctx);
4093         test_pwqp(ctx);
4094         test_lex(ctx);
4095         test_bijective(ctx);
4096         test_dep(ctx);
4097         test_read(ctx);
4098         test_bounded(ctx);
4099         test_construction(ctx);
4100         test_dim(ctx);
4101         test_application(ctx);
4102         test_convex_hull(ctx);
4103         test_gist(ctx);
4104         test_closure(ctx);
4105         isl_ctx_free(ctx);
4106         return 0;
4107 error:
4108         isl_ctx_free(ctx);
4109         return -1;
4110 }