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