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         /* Check that check for progress is not confused by rational
2585          * solution.
2586          */
2587         D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
2588         V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
2589                                                         "i0 <= -2 + N; "
2590                         "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
2591                                 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
2592         P = "{}";
2593         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2594         if (test_has_schedule(ctx, D, V, P) < 0)
2595                 return -1;
2596         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2597
2598         return 0;
2599 }
2600
2601 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2602 {
2603         isl_union_map *umap;
2604         int test;
2605
2606         umap = isl_union_map_read_from_str(ctx, str);
2607         test = isl_union_map_plain_is_injective(umap);
2608         isl_union_map_free(umap);
2609         if (test < 0)
2610                 return -1;
2611         if (test == injective)
2612                 return 0;
2613         if (injective)
2614                 isl_die(ctx, isl_error_unknown,
2615                         "map not detected as injective", return -1);
2616         else
2617                 isl_die(ctx, isl_error_unknown,
2618                         "map detected as injective", return -1);
2619 }
2620
2621 int test_injective(isl_ctx *ctx)
2622 {
2623         const char *str;
2624
2625         if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2626                 return -1;
2627         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2628                 return -1;
2629         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2630                 return -1;
2631         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2632                 return -1;
2633         if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2634                 return -1;
2635         if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2636                 return -1;
2637         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2638                 return -1;
2639         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2640                 return -1;
2641
2642         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2643         if (test_plain_injective(ctx, str, 1))
2644                 return -1;
2645         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2646         if (test_plain_injective(ctx, str, 0))
2647                 return -1;
2648
2649         return 0;
2650 }
2651
2652 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
2653 {
2654         isl_aff *aff2;
2655         int equal;
2656
2657         if (!aff)
2658                 return -1;
2659
2660         aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
2661         equal = isl_aff_plain_is_equal(aff, aff2);
2662         isl_aff_free(aff2);
2663
2664         return equal;
2665 }
2666
2667 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
2668 {
2669         int equal;
2670
2671         equal = aff_plain_is_equal(aff, str);
2672         if (equal < 0)
2673                 return -1;
2674         if (!equal)
2675                 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
2676                         "result not as expected", return -1);
2677         return 0;
2678 }
2679
2680 int test_aff(isl_ctx *ctx)
2681 {
2682         const char *str;
2683         isl_set *set;
2684         isl_space *space;
2685         isl_local_space *ls;
2686         isl_aff *aff;
2687         int zero, equal;
2688
2689         space = isl_space_set_alloc(ctx, 0, 1);
2690         ls = isl_local_space_from_space(space);
2691         aff = isl_aff_zero_on_domain(ls);
2692
2693         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2694         aff = isl_aff_scale_down_ui(aff, 3);
2695         aff = isl_aff_floor(aff);
2696         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2697         aff = isl_aff_scale_down_ui(aff, 2);
2698         aff = isl_aff_floor(aff);
2699         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2700
2701         str = "{ [10] }";
2702         set = isl_set_read_from_str(ctx, str);
2703         aff = isl_aff_gist(aff, set);
2704
2705         aff = isl_aff_add_constant_si(aff, -16);
2706         zero = isl_aff_plain_is_zero(aff);
2707         isl_aff_free(aff);
2708
2709         if (zero < 0)
2710                 return -1;
2711         if (!zero)
2712                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2713
2714         aff = isl_aff_read_from_str(ctx, "{ [-1] }");
2715         aff = isl_aff_scale_down_ui(aff, 64);
2716         aff = isl_aff_floor(aff);
2717         equal = aff_check_plain_equal(aff, "{ [-1] }");
2718         isl_aff_free(aff);
2719         if (equal < 0)
2720                 return -1;
2721
2722         return 0;
2723 }
2724
2725 int test_dim_max(isl_ctx *ctx)
2726 {
2727         int equal;
2728         const char *str;
2729         isl_set *set1, *set2;
2730         isl_set *set;
2731         isl_map *map;
2732         isl_pw_aff *pwaff;
2733
2734         str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
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] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
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 <= max(2N,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] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2763         set = isl_set_read_from_str(ctx, str);
2764         pwaff = isl_set_dim_max(set, 0);
2765         set1 = isl_set_from_pw_aff(pwaff);
2766         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2767         set2 = isl_set_read_from_str(ctx, str);
2768         equal = isl_set_is_equal(set1, set2);
2769         isl_set_free(set1);
2770         isl_set_free(set2);
2771         if (equal < 0)
2772                 return -1;
2773         if (!equal)
2774                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2775
2776         str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2777                         "0 <= i < N and 0 <= j < M }";
2778         map = isl_map_read_from_str(ctx, str);
2779         set = isl_map_range(map);
2780
2781         pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2782         set1 = isl_set_from_pw_aff(pwaff);
2783         str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2784         set2 = isl_set_read_from_str(ctx, str);
2785         equal = isl_set_is_equal(set1, set2);
2786         isl_set_free(set1);
2787         isl_set_free(set2);
2788
2789         pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2790         set1 = isl_set_from_pw_aff(pwaff);
2791         str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2792         set2 = isl_set_read_from_str(ctx, str);
2793         if (equal >= 0 && equal)
2794                 equal = isl_set_is_equal(set1, set2);
2795         isl_set_free(set1);
2796         isl_set_free(set2);
2797
2798         isl_set_free(set);
2799
2800         if (equal < 0)
2801                 return -1;
2802         if (!equal)
2803                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2804
2805         /* Check that solutions are properly merged. */
2806         str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
2807                                 "c <= -1 + n - 4a - 2b and c >= -2b and "
2808                                 "4a >= -4 + n and c >= 0 }";
2809         set = isl_set_read_from_str(ctx, str);
2810         pwaff = isl_set_dim_min(set, 2);
2811         set1 = isl_set_from_pw_aff(pwaff);
2812         str = "[n] -> { [(0)] : n >= 1 }";
2813         set2 = isl_set_read_from_str(ctx, str);
2814         equal = isl_set_is_equal(set1, set2);
2815         isl_set_free(set1);
2816         isl_set_free(set2);
2817
2818         if (equal < 0)
2819                 return -1;
2820         if (!equal)
2821                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2822
2823         return 0;
2824 }
2825
2826 int test_product(isl_ctx *ctx)
2827 {
2828         const char *str;
2829         isl_set *set;
2830         isl_union_set *uset1, *uset2;
2831         int ok;
2832
2833         str = "{ A[i] }";
2834         set = isl_set_read_from_str(ctx, str);
2835         set = isl_set_product(set, isl_set_copy(set));
2836         ok = isl_set_is_wrapping(set);
2837         isl_set_free(set);
2838         if (ok < 0)
2839                 return -1;
2840         if (!ok)
2841                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2842
2843         str = "{ [] }";
2844         uset1 = isl_union_set_read_from_str(ctx, str);
2845         uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
2846         str = "{ [[] -> []] }";
2847         uset2 = isl_union_set_read_from_str(ctx, str);
2848         ok = isl_union_set_is_equal(uset1, uset2);
2849         isl_union_set_free(uset1);
2850         isl_union_set_free(uset2);
2851         if (ok < 0)
2852                 return -1;
2853         if (!ok)
2854                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2855
2856         return 0;
2857 }
2858
2859 int test_equal(isl_ctx *ctx)
2860 {
2861         const char *str;
2862         isl_set *set, *set2;
2863         int equal;
2864
2865         str = "{ S_6[i] }";
2866         set = isl_set_read_from_str(ctx, str);
2867         str = "{ S_7[i] }";
2868         set2 = isl_set_read_from_str(ctx, str);
2869         equal = isl_set_is_equal(set, set2);
2870         isl_set_free(set);
2871         isl_set_free(set2);
2872         if (equal < 0)
2873                 return -1;
2874         if (equal)
2875                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2876
2877         return 0;
2878 }
2879
2880 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2881         enum isl_dim_type type, unsigned pos, int fixed)
2882 {
2883         int test;
2884
2885         test = isl_map_plain_is_fixed(map, type, pos, NULL);
2886         isl_map_free(map);
2887         if (test < 0)
2888                 return -1;
2889         if (test == fixed)
2890                 return 0;
2891         if (fixed)
2892                 isl_die(ctx, isl_error_unknown,
2893                         "map not detected as fixed", return -1);
2894         else
2895                 isl_die(ctx, isl_error_unknown,
2896                         "map detected as fixed", return -1);
2897 }
2898
2899 int test_fixed(isl_ctx *ctx)
2900 {
2901         const char *str;
2902         isl_map *map;
2903
2904         str = "{ [i] -> [i] }";
2905         map = isl_map_read_from_str(ctx, str);
2906         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2907                 return -1;
2908         str = "{ [i] -> [1] }";
2909         map = isl_map_read_from_str(ctx, str);
2910         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2911                 return -1;
2912         str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2913         map = isl_map_read_from_str(ctx, str);
2914         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2915                 return -1;
2916         map = isl_map_read_from_str(ctx, str);
2917         map = isl_map_neg(map);
2918         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2919                 return -1;
2920
2921         return 0;
2922 }
2923
2924 int test_vertices(isl_ctx *ctx)
2925 {
2926         const char *str;
2927         isl_basic_set *bset;
2928         isl_vertices *vertices;
2929
2930         str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
2931         bset = isl_basic_set_read_from_str(ctx, str);
2932         vertices = isl_basic_set_compute_vertices(bset);
2933         isl_basic_set_free(bset);
2934         isl_vertices_free(vertices);
2935         if (!vertices)
2936                 return -1;
2937
2938         str = "{ A[t, i] : t = 14 and i = 1 }";
2939         bset = isl_basic_set_read_from_str(ctx, str);
2940         vertices = isl_basic_set_compute_vertices(bset);
2941         isl_basic_set_free(bset);
2942         isl_vertices_free(vertices);
2943         if (!vertices)
2944                 return -1;
2945
2946         return 0;
2947 }
2948
2949 int test_union_pw(isl_ctx *ctx)
2950 {
2951         int equal;
2952         const char *str;
2953         isl_union_set *uset;
2954         isl_union_pw_qpolynomial *upwqp1, *upwqp2;
2955
2956         str = "{ [x] -> x^2 }";
2957         upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
2958         upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
2959         uset = isl_union_pw_qpolynomial_domain(upwqp1);
2960         upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
2961         upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
2962         equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
2963         isl_union_pw_qpolynomial_free(upwqp1);
2964         isl_union_pw_qpolynomial_free(upwqp2);
2965         if (equal < 0)
2966                 return -1;
2967         if (!equal)
2968                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2969
2970         return 0;
2971 }
2972
2973 int test_output(isl_ctx *ctx)
2974 {
2975         char *s;
2976         const char *str;
2977         isl_pw_aff *pa;
2978         isl_printer *p;
2979         int equal;
2980
2981         str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
2982         pa = isl_pw_aff_read_from_str(ctx, str);
2983
2984         p = isl_printer_to_str(ctx);
2985         p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2986         p = isl_printer_print_pw_aff(p, pa);
2987         s = isl_printer_get_str(p);
2988         isl_printer_free(p);
2989         isl_pw_aff_free(pa);
2990         if (!s)
2991                 equal = -1;
2992         else
2993                 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
2994         free(s);
2995         if (equal < 0)
2996                 return -1;
2997         if (!equal)
2998                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2999
3000         return 0;
3001 }
3002
3003 int test_sample(isl_ctx *ctx)
3004 {
3005         const char *str;
3006         isl_basic_set *bset1, *bset2;
3007         int empty, subset;
3008
3009         str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3010             "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3011             "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3012             "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3013             "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3014             "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3015             "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3016             "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3017             "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3018             "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3019             "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3020             "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3021                 "d - 1073741821e and "
3022             "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3023             "3j >= 1 - a + b + 2e and "
3024             "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3025             "3i <= 4 - a + 4b - e and "
3026             "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3027             "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3028             "c <= -1 + a and 3i >= -2 - a + 3e and "
3029             "1073741822e <= 1073741823 - a + 1073741822b + c and "
3030             "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3031             "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3032             "1073741823e >= 1 + 1073741823b - d and "
3033             "3i >= 1073741823b + c - 1073741823e - f and "
3034             "3i >= 1 + 2b + e + 3g }";
3035         bset1 = isl_basic_set_read_from_str(ctx, str);
3036         bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3037         empty = isl_basic_set_is_empty(bset2);
3038         subset = isl_basic_set_is_subset(bset2, bset1);
3039         isl_basic_set_free(bset1);
3040         isl_basic_set_free(bset2);
3041         if (empty < 0 || subset < 0)
3042                 return -1;
3043         if (empty)
3044                 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3045         if (!subset)
3046                 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3047
3048         return 0;
3049 }
3050
3051 int test_fixed_power(isl_ctx *ctx)
3052 {
3053         const char *str;
3054         isl_map *map;
3055         isl_int exp;
3056         int equal;
3057
3058         isl_int_init(exp);
3059         str = "{ [i] -> [i + 1] }";
3060         map = isl_map_read_from_str(ctx, str);
3061         isl_int_set_si(exp, 23);
3062         map = isl_map_fixed_power(map, exp);
3063         equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3064         isl_int_clear(exp);
3065         isl_map_free(map);
3066         if (equal < 0)
3067                 return -1;
3068
3069         return 0;
3070 }
3071
3072 int test_slice(isl_ctx *ctx)
3073 {
3074         const char *str;
3075         isl_map *map;
3076         int equal;
3077
3078         str = "{ [i] -> [j] }";
3079         map = isl_map_read_from_str(ctx, str);
3080         map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3081         equal = map_check_equal(map, "{ [i] -> [i] }");
3082         isl_map_free(map);
3083         if (equal < 0)
3084                 return -1;
3085
3086         str = "{ [i] -> [j] }";
3087         map = isl_map_read_from_str(ctx, str);
3088         map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3089         equal = map_check_equal(map, "{ [i] -> [j] }");
3090         isl_map_free(map);
3091         if (equal < 0)
3092                 return -1;
3093
3094         str = "{ [i] -> [j] }";
3095         map = isl_map_read_from_str(ctx, str);
3096         map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3097         equal = map_check_equal(map, "{ [i] -> [-i] }");
3098         isl_map_free(map);
3099         if (equal < 0)
3100                 return -1;
3101
3102         str = "{ [i] -> [j] }";
3103         map = isl_map_read_from_str(ctx, str);
3104         map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3105         equal = map_check_equal(map, "{ [0] -> [j] }");
3106         isl_map_free(map);
3107         if (equal < 0)
3108                 return -1;
3109
3110         str = "{ [i] -> [j] }";
3111         map = isl_map_read_from_str(ctx, str);
3112         map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3113         equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3114         isl_map_free(map);
3115         if (equal < 0)
3116                 return -1;
3117
3118         str = "{ [i] -> [j] }";
3119         map = isl_map_read_from_str(ctx, str);
3120         map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3121         equal = map_check_equal(map, "{ [i] -> [j] : false }");
3122         isl_map_free(map);
3123         if (equal < 0)
3124                 return -1;
3125
3126         return 0;
3127 }
3128
3129 int test_eliminate(isl_ctx *ctx)
3130 {
3131         const char *str;
3132         isl_map *map;
3133         int equal;
3134
3135         str = "{ [i] -> [j] : i = 2j }";
3136         map = isl_map_read_from_str(ctx, str);
3137         map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3138         equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3139         isl_map_free(map);
3140         if (equal < 0)
3141                 return -1;
3142
3143         return 0;
3144 }
3145
3146 /* Check that isl_set_dim_residue_class detects that the values of j
3147  * in the set below are all odd and that it does not detect any spurious
3148  * strides.
3149  */
3150 static int test_residue_class(isl_ctx *ctx)
3151 {
3152         const char *str;
3153         isl_set *set;
3154         isl_int m, r;
3155         int res;
3156
3157         str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3158                 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3159         set = isl_set_read_from_str(ctx, str);
3160         isl_int_init(m);
3161         isl_int_init(r);
3162         res = isl_set_dim_residue_class(set, 1, &m, &r);
3163         if (res >= 0 &&
3164             (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3165                 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3166                         res = -1);
3167         isl_int_clear(r);
3168         isl_int_clear(m);
3169         isl_set_free(set);
3170
3171         return res;
3172 }
3173
3174 int test_align_parameters(isl_ctx *ctx)
3175 {
3176         const char *str;
3177         isl_space *space;
3178         isl_multi_aff *ma1, *ma2;
3179         int equal;
3180
3181         str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3182         ma1 = isl_multi_aff_read_from_str(ctx, str);
3183
3184         space = isl_space_params_alloc(ctx, 1);
3185         space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3186         ma1 = isl_multi_aff_align_params(ma1, space);
3187
3188         str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3189         ma2 = isl_multi_aff_read_from_str(ctx, str);
3190
3191         equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3192
3193         isl_multi_aff_free(ma1);
3194         isl_multi_aff_free(ma2);
3195
3196         if (equal < 0)
3197                 return -1;
3198         if (!equal)
3199                 isl_die(ctx, isl_error_unknown,
3200                         "result not as expected", return -1);
3201
3202         return 0;
3203 }
3204
3205 static int test_list(isl_ctx *ctx)
3206 {
3207         isl_id *a, *b, *c, *d, *id;
3208         isl_id_list *list;
3209         int ok;
3210
3211         a = isl_id_alloc(ctx, "a", NULL);
3212         b = isl_id_alloc(ctx, "b", NULL);
3213         c = isl_id_alloc(ctx, "c", NULL);
3214         d = isl_id_alloc(ctx, "d", NULL);
3215
3216         list = isl_id_list_alloc(ctx, 4);
3217         list = isl_id_list_add(list, a);
3218         list = isl_id_list_add(list, b);
3219         list = isl_id_list_add(list, c);
3220         list = isl_id_list_add(list, d);
3221         list = isl_id_list_drop(list, 1, 1);
3222
3223         if (isl_id_list_n_id(list) != 3) {
3224                 isl_id_list_free(list);
3225                 isl_die(ctx, isl_error_unknown,
3226                         "unexpected number of elements in list", return -1);
3227         }
3228
3229         id = isl_id_list_get_id(list, 0);
3230         ok = id == a;
3231         isl_id_free(id);
3232         id = isl_id_list_get_id(list, 1);
3233         ok = ok && id == c;
3234         isl_id_free(id);
3235         id = isl_id_list_get_id(list, 2);
3236         ok = ok && id == d;
3237         isl_id_free(id);
3238
3239         isl_id_list_free(list);
3240
3241         if (!ok)
3242                 isl_die(ctx, isl_error_unknown,
3243                         "unexpected elements in list", return -1);
3244
3245         return 0;
3246 }
3247
3248 const char *set_conversion_tests[] = {
3249         "[N] -> { [i] : N - 1 <= 2 i <= N }",
3250         "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3251         "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3252         "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3253 };
3254
3255 /* Check that converting from isl_set to isl_pw_multi_aff and back
3256  * to isl_set produces the original isl_set.
3257  */
3258 static int test_set_conversion(isl_ctx *ctx)
3259 {
3260         int i;
3261         const char *str;
3262         isl_set *set1, *set2;
3263         isl_pw_multi_aff *pma;
3264         int equal;
3265
3266         for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3267                 str = set_conversion_tests[i];
3268                 set1 = isl_set_read_from_str(ctx, str);
3269                 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3270                 set2 = isl_set_from_pw_multi_aff(pma);
3271                 equal = isl_set_is_equal(set1, set2);
3272                 isl_set_free(set1);
3273                 isl_set_free(set2);
3274
3275                 if (equal < 0)
3276                         return -1;
3277                 if (!equal)
3278                         isl_die(ctx, isl_error_unknown, "bad conversion",
3279                                 return -1);
3280         }
3281
3282         return 0;
3283 }
3284
3285 /* Check that converting from isl_map to isl_pw_multi_aff and back
3286  * to isl_map produces the original isl_map.
3287  */
3288 static int test_map_conversion(isl_ctx *ctx)
3289 {
3290         const char *str;
3291         isl_map *map1, *map2;
3292         isl_pw_multi_aff *pma;
3293         int equal;
3294
3295         str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3296                 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3297                 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3298                 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3299                 "9e <= -2 - 2a) }";
3300         map1 = isl_map_read_from_str(ctx, str);
3301         pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3302         map2 = isl_map_from_pw_multi_aff(pma);
3303         equal = isl_map_is_equal(map1, map2);
3304         isl_map_free(map1);
3305         isl_map_free(map2);
3306
3307         if (equal < 0)
3308                 return -1;
3309         if (!equal)
3310                 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3311
3312         return 0;
3313 }
3314
3315 static int test_conversion(isl_ctx *ctx)
3316 {
3317         if (test_set_conversion(ctx) < 0)
3318                 return -1;
3319         if (test_map_conversion(ctx) < 0)
3320                 return -1;
3321         return 0;
3322 }
3323
3324 /* Check that isl_basic_map_curry does not modify input.
3325  */
3326 static int test_curry(isl_ctx *ctx)
3327 {
3328         const char *str;
3329         isl_basic_map *bmap1, *bmap2;
3330         int equal;
3331
3332         str = "{ [A[] -> B[]] -> C[] }";
3333         bmap1 = isl_basic_map_read_from_str(ctx, str);
3334         bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3335         equal = isl_basic_map_is_equal(bmap1, bmap2);
3336         isl_basic_map_free(bmap1);
3337         isl_basic_map_free(bmap2);
3338
3339         if (equal < 0)
3340                 return -1;
3341         if (equal)
3342                 isl_die(ctx, isl_error_unknown,
3343                         "curried map should not be equal to original",
3344                         return -1);
3345
3346         return 0;
3347 }
3348
3349 struct {
3350         const char *set;
3351         const char *ma;
3352         const char *res;
3353 } preimage_tests[] = {
3354         { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3355           "{ A[j,i] -> B[i,j] }",
3356           "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3357         { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3358           "{ A[a,b] -> B[a/2,b/6] }",
3359           "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3360         { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3361           "{ A[a,b] -> B[a/2,b/6] }",
3362           "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3363                     "exists i,j : a = 2 i and b = 6 j }" },
3364         { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3365           "[n] -> { : 0 <= n <= 100 }" },
3366         { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3367           "{ A[a] -> B[2a] }",
3368           "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3369         { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3370           "{ A[a] -> B[([a/2])] }",
3371           "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3372         { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3373           "{ A[a] -> B[a,a,a/3] }",
3374           "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3375         { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3376           "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3377 };
3378
3379 static int test_preimage_basic_set(isl_ctx *ctx)
3380 {
3381         int i;
3382         isl_basic_set *bset1, *bset2;
3383         isl_multi_aff *ma;
3384         int equal;
3385
3386         for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3387                 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3388                 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3389                 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3390                 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3391                 equal = isl_basic_set_is_equal(bset1, bset2);
3392                 isl_basic_set_free(bset1);
3393                 isl_basic_set_free(bset2);
3394                 if (equal < 0)
3395                         return -1;
3396                 if (!equal)
3397                         isl_die(ctx, isl_error_unknown, "bad preimage",
3398                                 return -1);
3399         }
3400
3401         return 0;
3402 }
3403
3404 struct {
3405         const char *map;
3406         const char *ma;
3407         const char *res;
3408 } preimage_domain_tests[] = {
3409         { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
3410           "{ A[j,i] -> B[i,j] }",
3411           "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
3412         { "{ B[i] -> C[i]; D[i] -> E[i] }",
3413           "{ A[i] -> B[i + 1] }",
3414           "{ A[i] -> C[i + 1] }" },
3415         { "{ B[i] -> C[i]; B[i] -> E[i] }",
3416           "{ A[i] -> B[i + 1] }",
3417           "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
3418         { "{ B[i] -> C[([i/2])] }",
3419           "{ A[i] -> B[2i] }",
3420           "{ A[i] -> C[i] }" },
3421         { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
3422           "{ A[i] -> B[([i/5]), ([i/7])] }",
3423           "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
3424         { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
3425           "[N] -> { A[] -> B[([N/5])] }",
3426           "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
3427         { "{ B[i] -> C[i] : exists a : i = 5 a }",
3428           "{ A[i] -> B[2i] }",
3429           "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
3430         { "{ B[i] -> C[i] : exists a : i = 2 a; "
3431             "B[i] -> D[i] : exists a : i = 2 a + 1 }",
3432           "{ A[i] -> B[2i] }",
3433           "{ A[i] -> C[2i] }" },
3434 };
3435
3436 static int test_preimage_union_map(isl_ctx *ctx)
3437 {
3438         int i;
3439         isl_union_map *umap1, *umap2;
3440         isl_multi_aff *ma;
3441         int equal;
3442
3443         for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
3444                 umap1 = isl_union_map_read_from_str(ctx,
3445                                                 preimage_domain_tests[i].map);
3446                 ma = isl_multi_aff_read_from_str(ctx,
3447                                                 preimage_domain_tests[i].ma);
3448                 umap2 = isl_union_map_read_from_str(ctx,
3449                                                 preimage_domain_tests[i].res);
3450                 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
3451                 equal = isl_union_map_is_equal(umap1, umap2);
3452                 isl_union_map_free(umap1);
3453                 isl_union_map_free(umap2);
3454                 if (equal < 0)
3455                         return -1;
3456                 if (!equal)
3457                         isl_die(ctx, isl_error_unknown, "bad preimage",
3458                                 return -1);
3459         }
3460
3461         return 0;
3462 }
3463
3464 static int test_preimage(isl_ctx *ctx)
3465 {
3466         if (test_preimage_basic_set(ctx) < 0)
3467                 return -1;
3468         if (test_preimage_union_map(ctx) < 0)
3469                 return -1;
3470
3471         return 0;
3472 }
3473
3474 struct {
3475         const char *ma1;
3476         const char *ma;
3477         const char *res;
3478 } pullback_tests[] = {
3479         { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
3480           "{ A[a,b] -> C[b + 2a] }" },
3481         { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
3482         { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
3483         { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
3484           "{ A[a] -> C[(a)/6] }" },
3485         { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
3486         { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
3487           "{ A[a] -> C[(2a)/3] }" },
3488         { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
3489         { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
3490           "{ A[i,j] -> C[i + j, i + j] }"},
3491         { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
3492         { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
3493           "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
3494 };
3495
3496 static int test_pullback(isl_ctx *ctx)
3497 {
3498         int i;
3499         isl_multi_aff *ma1, *ma2;
3500         isl_multi_aff *ma;
3501         int equal;
3502
3503         for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
3504                 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
3505                 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
3506                 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
3507                 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
3508                 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3509                 isl_multi_aff_free(ma1);
3510                 isl_multi_aff_free(ma2);
3511                 if (equal < 0)
3512                         return -1;
3513                 if (!equal)
3514                         isl_die(ctx, isl_error_unknown, "bad pullback",
3515                                 return -1);
3516         }
3517
3518         return 0;
3519 }
3520
3521 /* Check that negation is printed correctly.
3522  */
3523 static int test_ast(isl_ctx *ctx)
3524 {
3525         isl_ast_expr *expr, *expr1, *expr2, *expr3;
3526         char *str;
3527         int ok;
3528
3529         expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3530         expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3531         expr = isl_ast_expr_add(expr1, expr2);
3532         expr = isl_ast_expr_neg(expr);
3533         str = isl_ast_expr_to_str(expr);
3534         ok = str ? !strcmp(str, "-(A + B)") : -1;
3535         free(str);
3536         isl_ast_expr_free(expr);
3537
3538         if (ok < 0)
3539                 return -1;
3540         if (!ok)
3541                 isl_die(ctx, isl_error_unknown,
3542                         "isl_ast_expr printed incorrectly", return -1);
3543
3544         expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3545         expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3546         expr = isl_ast_expr_add(expr1, expr2);
3547         expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
3548         expr = isl_ast_expr_sub(expr3, expr);
3549         str = isl_ast_expr_to_str(expr);
3550         ok = str ? !strcmp(str, "C - (A + B)") : -1;
3551         free(str);
3552         isl_ast_expr_free(expr);
3553
3554         if (ok < 0)
3555                 return -1;
3556         if (!ok)
3557                 isl_die(ctx, isl_error_unknown,
3558                         "isl_ast_expr printed incorrectly", return -1);
3559
3560         return 0;
3561 }
3562
3563 /* Internal data structure for before_for and after_for callbacks.
3564  *
3565  * depth is the current depth
3566  * before is the number of times before_for has been called
3567  * after is the number of times after_for has been called
3568  */
3569 struct isl_test_codegen_data {
3570         int depth;
3571         int before;
3572         int after;
3573 };
3574
3575 /* This function is called before each for loop in the AST generated
3576  * from test_ast_gen1.
3577  *
3578  * Increment the number of calls and the depth.
3579  * Check that the space returned by isl_ast_build_get_schedule_space
3580  * matches the target space of the schedule returned by
3581  * isl_ast_build_get_schedule.
3582  * Return an isl_id that is checked by the corresponding call
3583  * to after_for.
3584  */
3585 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
3586         void *user)
3587 {
3588         struct isl_test_codegen_data *data = user;
3589         isl_ctx *ctx;
3590         isl_space *space;
3591         isl_union_map *schedule;
3592         isl_union_set *uset;
3593         isl_set *set;
3594         int empty;
3595         char name[] = "d0";
3596
3597         ctx = isl_ast_build_get_ctx(build);
3598
3599         if (data->before >= 3)
3600                 isl_die(ctx, isl_error_unknown,
3601                         "unexpected number of for nodes", return NULL);
3602         if (data->depth >= 2)
3603                 isl_die(ctx, isl_error_unknown,
3604                         "unexpected depth", return NULL);
3605
3606         snprintf(name, sizeof(name), "d%d", data->depth);
3607         data->before++;
3608         data->depth++;
3609
3610         schedule = isl_ast_build_get_schedule(build);
3611         uset = isl_union_map_range(schedule);
3612         if (!uset)
3613                 return NULL;
3614         if (isl_union_set_n_set(uset) != 1) {
3615                 isl_union_set_free(uset);
3616                 isl_die(ctx, isl_error_unknown,
3617                         "expecting single range space", return NULL);
3618         }
3619
3620         space = isl_ast_build_get_schedule_space(build);
3621         set = isl_union_set_extract_set(uset, space);
3622         isl_union_set_free(uset);
3623         empty = isl_set_is_empty(set);
3624         isl_set_free(set);
3625
3626         if (empty < 0)
3627                 return NULL;
3628         if (empty)
3629                 isl_die(ctx, isl_error_unknown,
3630                         "spaces don't match", return NULL);
3631
3632         return isl_id_alloc(ctx, name, NULL);
3633 }
3634
3635 /* This function is called after each for loop in the AST generated
3636  * from test_ast_gen1.
3637  *
3638  * Increment the number of calls and decrement the depth.
3639  * Check that the annotation attached to the node matches
3640  * the isl_id returned by the corresponding call to before_for.
3641  */
3642 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
3643         __isl_keep isl_ast_build *build, void *user)
3644 {
3645         struct isl_test_codegen_data *data = user;
3646         isl_id *id;
3647         const char *name;
3648         int valid;
3649
3650         data->after++;
3651         data->depth--;
3652
3653         if (data->after > data->before)
3654                 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3655                         "mismatch in number of for nodes",
3656                         return isl_ast_node_free(node));
3657
3658         id = isl_ast_node_get_annotation(node);
3659         if (!id)
3660                 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3661                         "missing annotation", return isl_ast_node_free(node));
3662
3663         name = isl_id_get_name(id);
3664         valid = name && atoi(name + 1) == data->depth;
3665         isl_id_free(id);
3666
3667         if (!valid)
3668                 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3669                         "wrong annotation", return isl_ast_node_free(node));
3670
3671         return node;
3672 }
3673
3674 /* Check that the before_each_for and after_each_for callbacks
3675  * are called for each for loop in the generated code,
3676  * that they are called in the right order and that the isl_id
3677  * returned from the before_each_for callback is attached to
3678  * the isl_ast_node passed to the corresponding after_each_for call.
3679  */
3680 static int test_ast_gen1(isl_ctx *ctx)
3681 {
3682         const char *str;
3683         isl_set *set;
3684         isl_union_map *schedule;
3685         isl_ast_build *build;
3686         isl_ast_node *tree;
3687         struct isl_test_codegen_data data;
3688
3689         str = "[N] -> { : N >= 10 }";
3690         set = isl_set_read_from_str(ctx, str);
3691         str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
3692                     "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
3693         schedule = isl_union_map_read_from_str(ctx, str);
3694
3695         data.before = 0;
3696         data.after = 0;
3697         data.depth = 0;
3698         build = isl_ast_build_from_context(set);
3699         build = isl_ast_build_set_before_each_for(build,
3700                         &before_for, &data);
3701         build = isl_ast_build_set_after_each_for(build,
3702                         &after_for, &data);
3703         tree = isl_ast_build_ast_from_schedule(build, schedule);
3704         isl_ast_build_free(build);
3705         if (!tree)
3706                 return -1;
3707
3708         isl_ast_node_free(tree);
3709
3710         if (data.before != 3 || data.after != 3)
3711                 isl_die(ctx, isl_error_unknown,
3712                         "unexpected number of for nodes", return -1);
3713
3714         return 0;
3715 }
3716
3717 /* Check that the AST generator handles domains that are integrally disjoint
3718  * but not ratinoally disjoint.
3719  */
3720 static int test_ast_gen2(isl_ctx *ctx)
3721 {
3722         const char *str;
3723         isl_set *set;
3724         isl_union_map *schedule;
3725         isl_union_map *options;
3726         isl_ast_build *build;
3727         isl_ast_node *tree;
3728
3729         str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
3730         schedule = isl_union_map_read_from_str(ctx, str);
3731         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3732         build = isl_ast_build_from_context(set);
3733
3734         str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
3735         options = isl_union_map_read_from_str(ctx, str);
3736         build = isl_ast_build_set_options(build, options);
3737         tree = isl_ast_build_ast_from_schedule(build, schedule);
3738         isl_ast_build_free(build);
3739         if (!tree)
3740                 return -1;
3741         isl_ast_node_free(tree);
3742
3743         return 0;
3744 }
3745
3746 /* Increment *user on each call.
3747  */
3748 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
3749         __isl_keep isl_ast_build *build, void *user)
3750 {
3751         int *n = user;
3752
3753         (*n)++;
3754
3755         return node;
3756 }
3757
3758 /* Test that unrolling tries to minimize the number of instances.
3759  * In particular, for the schedule given below, make sure it generates
3760  * 3 nodes (rather than 101).
3761  */
3762 static int test_ast_gen3(isl_ctx *ctx)
3763 {
3764         const char *str;
3765         isl_set *set;
3766         isl_union_map *schedule;
3767         isl_union_map *options;
3768         isl_ast_build *build;
3769         isl_ast_node *tree;
3770         int n_domain = 0;
3771
3772         str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
3773         schedule = isl_union_map_read_from_str(ctx, str);
3774         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3775
3776         str = "{ [i] -> unroll[0] }";
3777         options = isl_union_map_read_from_str(ctx, str);
3778
3779         build = isl_ast_build_from_context(set);
3780         build = isl_ast_build_set_options(build, options);
3781         build = isl_ast_build_set_at_each_domain(build,
3782                         &count_domains, &n_domain);
3783         tree = isl_ast_build_ast_from_schedule(build, schedule);
3784         isl_ast_build_free(build);
3785         if (!tree)
3786                 return -1;
3787
3788         isl_ast_node_free(tree);
3789
3790         if (n_domain != 3)
3791                 isl_die(ctx, isl_error_unknown,
3792                         "unexpected number of for nodes", return -1);
3793
3794         return 0;
3795 }
3796
3797 /* Check that if the ast_build_exploit_nested_bounds options is set,
3798  * we do not get an outer if node in the generated AST,
3799  * while we do get such an outer if node if the options is not set.
3800  */
3801 static int test_ast_gen4(isl_ctx *ctx)
3802 {
3803         const char *str;
3804         isl_set *set;
3805         isl_union_map *schedule;
3806         isl_ast_build *build;
3807         isl_ast_node *tree;
3808         enum isl_ast_node_type type;
3809         int enb;
3810
3811         enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
3812         str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
3813
3814         isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
3815
3816         schedule = isl_union_map_read_from_str(ctx, str);
3817         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3818         build = isl_ast_build_from_context(set);
3819         tree = isl_ast_build_ast_from_schedule(build, schedule);
3820         isl_ast_build_free(build);
3821         if (!tree)
3822                 return -1;
3823
3824         type = isl_ast_node_get_type(tree);
3825         isl_ast_node_free(tree);
3826
3827         if (type == isl_ast_node_if)
3828                 isl_die(ctx, isl_error_unknown,
3829                         "not expecting if node", return -1);
3830
3831         isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
3832
3833         schedule = isl_union_map_read_from_str(ctx, str);
3834         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3835         build = isl_ast_build_from_context(set);
3836         tree = isl_ast_build_ast_from_schedule(build, schedule);
3837         isl_ast_build_free(build);
3838         if (!tree)
3839                 return -1;
3840
3841         type = isl_ast_node_get_type(tree);
3842         isl_ast_node_free(tree);
3843
3844         if (type != isl_ast_node_if)
3845                 isl_die(ctx, isl_error_unknown,
3846                         "expecting if node", return -1);
3847
3848         isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
3849
3850         return 0;
3851 }
3852
3853 /* This function is called for each leaf in the AST generated
3854  * from test_ast_gen5.
3855  *
3856  * We finalize the AST generation by extending the outer schedule
3857  * with a zero-dimensional schedule.  If this results in any for loops,
3858  * then this means that we did not pass along enough information
3859  * about the outer schedule to the inner AST generation.
3860  */
3861 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
3862         void *user)
3863 {
3864         isl_union_map *schedule, *extra;
3865         isl_ast_node *tree;
3866
3867         schedule = isl_ast_build_get_schedule(build);
3868         extra = isl_union_map_copy(schedule);
3869         extra = isl_union_map_from_domain(isl_union_map_domain(extra));
3870         schedule = isl_union_map_range_product(schedule, extra);
3871         tree = isl_ast_build_ast_from_schedule(build, schedule);
3872         isl_ast_build_free(build);
3873
3874         if (!tree)
3875                 return NULL;
3876
3877         if (isl_ast_node_get_type(tree) == isl_ast_node_for)
3878                 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
3879                         "code should not contain any for loop",
3880                         return isl_ast_node_free(tree));
3881
3882         return tree;
3883 }
3884
3885 /* Check that we do not lose any information when going back and
3886  * forth between internal and external schedule.
3887  *
3888  * In particular, we create an AST where we unroll the only
3889  * non-constant dimension in the schedule.  We therefore do
3890  * not expect any for loops in the AST.  However, older versions
3891  * of isl would not pass along enough information about the outer
3892  * schedule when performing an inner code generation from a create_leaf
3893  * callback, resulting in the inner code generation producing a for loop.
3894  */
3895 static int test_ast_gen5(isl_ctx *ctx)
3896 {
3897         const char *str;
3898         isl_set *set;
3899         isl_union_map *schedule, *options;
3900         isl_ast_build *build;
3901         isl_ast_node *tree;
3902
3903         str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
3904         schedule = isl_union_map_read_from_str(ctx, str);
3905
3906         str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
3907                                 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
3908         options = isl_union_map_read_from_str(ctx, str);
3909
3910         set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3911         build = isl_ast_build_from_context(set);
3912         build = isl_ast_build_set_options(build, options);
3913         build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
3914         tree = isl_ast_build_ast_from_schedule(build, schedule);
3915         isl_ast_build_free(build);
3916         isl_ast_node_free(tree);
3917         if (!tree)
3918                 return -1;
3919
3920         return 0;
3921 }
3922
3923 static int test_ast_gen(isl_ctx *ctx)
3924 {
3925         if (test_ast_gen1(ctx) < 0)
3926                 return -1;
3927         if (test_ast_gen2(ctx) < 0)
3928                 return -1;
3929         if (test_ast_gen3(ctx) < 0)
3930                 return -1;
3931         if (test_ast_gen4(ctx) < 0)
3932                 return -1;
3933         if (test_ast_gen5(ctx) < 0)
3934                 return -1;
3935         return 0;
3936 }
3937
3938 /* Check if dropping output dimensions from an isl_pw_multi_aff
3939  * works properly.
3940  */
3941 static int test_pw_multi_aff(isl_ctx *ctx)
3942 {
3943         const char *str;
3944         isl_pw_multi_aff *pma1, *pma2;
3945         int equal;
3946
3947         str = "{ [i,j] -> [i+j, 4i-j] }";
3948         pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3949         str = "{ [i,j] -> [4i-j] }";
3950         pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3951
3952         pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
3953
3954         equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
3955
3956         isl_pw_multi_aff_free(pma1);
3957         isl_pw_multi_aff_free(pma2);
3958         if (equal < 0)
3959                 return -1;
3960         if (!equal)
3961                 isl_die(ctx, isl_error_unknown,
3962                         "expressions not equal", return -1);
3963
3964         return 0;
3965 }
3966
3967 /* This is a regression test for a bug where isl_basic_map_simplify
3968  * would end up in an infinite loop.  In particular, we construct
3969  * an empty basic set that is not obviously empty.
3970  * isl_basic_set_is_empty marks the basic set as empty.
3971  * After projecting out i3, the variable can be dropped completely,
3972  * but isl_basic_map_simplify refrains from doing so if the basic set
3973  * is empty and would end up in an infinite loop if it didn't test
3974  * explicitly for empty basic maps in the outer loop.
3975  */
3976 static int test_simplify(isl_ctx *ctx)
3977 {
3978         const char *str;
3979         isl_basic_set *bset;
3980         int empty;
3981
3982         str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
3983                 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
3984                 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
3985                 "i3 >= i2 }";
3986         bset = isl_basic_set_read_from_str(ctx, str);
3987         empty = isl_basic_set_is_empty(bset);
3988         bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
3989         isl_basic_set_free(bset);
3990         if (!bset)
3991                 return -1;
3992         if (!empty)
3993                 isl_die(ctx, isl_error_unknown,
3994                         "basic set should be empty", return -1);
3995
3996         return 0;
3997 }
3998
3999 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4000  * with gbr context would fail to disable the use of the shifted tableau
4001  * when transferring equalities for the input to the context, resulting
4002  * in invalid sample values.
4003  */
4004 static int test_partial_lexmin(isl_ctx *ctx)
4005 {
4006         const char *str;
4007         isl_basic_set *bset;
4008         isl_basic_map *bmap;
4009         isl_map *map;
4010
4011         str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4012         bmap = isl_basic_map_read_from_str(ctx, str);
4013         str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4014         bset = isl_basic_set_read_from_str(ctx, str);
4015         map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4016         isl_map_free(map);
4017
4018         if (!map)
4019                 return -1;
4020
4021         return 0;
4022 }
4023
4024 /* Check that the variable compression performed on the existentially
4025  * quantified variables inside isl_basic_set_compute_divs is not confused
4026  * by the implicit equalities among the parameters.
4027  */
4028 static int test_compute_divs(isl_ctx *ctx)
4029 {
4030         const char *str;
4031         isl_basic_set *bset;
4032         isl_set *set;
4033
4034         str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4035                 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4036                 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4037                 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4038         bset = isl_basic_set_read_from_str(ctx, str);
4039         set = isl_basic_set_compute_divs(bset);
4040         isl_set_free(set);
4041         if (!set)
4042                 return -1;
4043
4044         return 0;
4045 }
4046
4047 struct {
4048         const char *name;
4049         int (*fn)(isl_ctx *ctx);
4050 } tests [] = {
4051         { "compute divs", &test_compute_divs },
4052         { "partial lexmin", &test_partial_lexmin },
4053         { "simplify", &test_simplify },
4054         { "curry", &test_curry },
4055         { "piecewise multi affine expressions", &test_pw_multi_aff },
4056         { "conversion", &test_conversion },
4057         { "list", &test_list },
4058         { "align parameters", &test_align_parameters },
4059         { "preimage", &test_preimage },
4060         { "pullback", &test_pullback },
4061         { "AST", &test_ast },
4062         { "AST generation", &test_ast_gen },
4063         { "eliminate", &test_eliminate },
4064         { "residue class", &test_residue_class },
4065         { "div", &test_div },
4066         { "slice", &test_slice },
4067         { "fixed power", &test_fixed_power },
4068         { "sample", &test_sample },
4069         { "output", &test_output },
4070         { "vertices", &test_vertices },
4071         { "fixed", &test_fixed },
4072         { "equal", &test_equal },
4073         { "product", &test_product },
4074         { "dim_max", &test_dim_max },
4075         { "affine", &test_aff },
4076         { "injective", &test_injective },
4077         { "schedule", &test_schedule },
4078         { "union_pw", &test_union_pw },
4079         { "parse", &test_parse },
4080         { "single-valued", &test_sv },
4081         { "affine hull", &test_affine_hull },
4082         { "coalesce", &test_coalesce },
4083         { "factorize", &test_factorize },
4084         { "subset", &test_subset },
4085         { "subtract", &test_subtract },
4086         { "lexmin", &test_lexmin },
4087 };
4088
4089 int main()
4090 {
4091         int i;
4092         struct isl_ctx *ctx;
4093
4094         srcdir = getenv("srcdir");
4095         assert(srcdir);
4096
4097         ctx = isl_ctx_alloc();
4098         for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4099                 printf("%s\n", tests[i].name);
4100                 if (tests[i].fn(ctx) < 0)
4101                         goto error;
4102         }
4103         test_lift(ctx);
4104         test_bound(ctx);
4105         test_union(ctx);
4106         test_split_periods(ctx);
4107         test_pwqp(ctx);
4108         test_lex(ctx);
4109         test_bijective(ctx);
4110         test_dep(ctx);
4111         test_read(ctx);
4112         test_bounded(ctx);
4113         test_construction(ctx);
4114         test_dim(ctx);
4115         test_application(ctx);
4116         test_convex_hull(ctx);
4117         test_gist(ctx);
4118         test_closure(ctx);
4119         isl_ctx_free(ctx);
4120         return 0;
4121 error:
4122         isl_ctx_free(ctx);
4123         return -1;
4124 }