add isl_set_has_rational
[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
26 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
27
28 static char *srcdir;
29
30 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
31         char *filename;
32         int length;
33         char *pattern = "%s/test_inputs/%s.%s";
34
35         length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
36                 + strlen(suffix) + 1;
37         filename = isl_alloc_array(ctx, char, length);
38
39         if (!filename)
40                 return NULL;
41
42         sprintf(filename, pattern, srcdir, name, suffix);
43
44         return filename;
45 }
46
47 void test_parse_map(isl_ctx *ctx, const char *str)
48 {
49         isl_map *map;
50
51         map = isl_map_read_from_str(ctx, str);
52         assert(map);
53         isl_map_free(map);
54 }
55
56 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
57 {
58         isl_map *map, *map2;
59         int equal;
60
61         map = isl_map_read_from_str(ctx, str);
62         map2 = isl_map_read_from_str(ctx, str2);
63         equal = isl_map_is_equal(map, map2);
64         isl_map_free(map);
65         isl_map_free(map2);
66
67         if (equal < 0)
68                 return -1;
69         if (!equal)
70                 isl_die(ctx, isl_error_unknown, "maps not equal",
71                         return -1);
72
73         return 0;
74 }
75
76 void test_parse_pwqp(isl_ctx *ctx, const char *str)
77 {
78         isl_pw_qpolynomial *pwqp;
79
80         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
81         assert(pwqp);
82         isl_pw_qpolynomial_free(pwqp);
83 }
84
85 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
86 {
87         isl_pw_aff *pwaff;
88
89         pwaff = isl_pw_aff_read_from_str(ctx, str);
90         assert(pwaff);
91         isl_pw_aff_free(pwaff);
92 }
93
94 int test_parse(struct isl_ctx *ctx)
95 {
96         isl_map *map, *map2;
97         const char *str, *str2;
98
99         str = "{ [i] -> [-i] }";
100         map = isl_map_read_from_str(ctx, str);
101         assert(map);
102         isl_map_free(map);
103
104         str = "{ A[i] -> L[([i/3])] }";
105         map = isl_map_read_from_str(ctx, str);
106         assert(map);
107         isl_map_free(map);
108
109         test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
110         test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
111                                 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
112
113         str = "{ [x,y]  : [([x/2]+y)/3] >= 1 }";
114         str2 = "{ [x, y] : 2y >= 6 - x }";
115         if (test_parse_map_equal(ctx, str, str2) < 0)
116                 return -1;
117
118         if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
119                                       "{ [x,y] : x <= y, 2*y + 3 }") < 0)
120                 return -1;
121         str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
122         if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
123                                         str) < 0)
124                 return -1;
125
126         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
127         map = isl_map_read_from_str(ctx, str);
128         str = "{ [new, old] -> [o0, o1] : "
129                "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
130                "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
131                "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
132         map2 = isl_map_read_from_str(ctx, str);
133         assert(isl_map_is_equal(map, map2));
134         isl_map_free(map);
135         isl_map_free(map2);
136
137         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
138         map = isl_map_read_from_str(ctx, str);
139         str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
140         map2 = isl_map_read_from_str(ctx, str);
141         assert(isl_map_is_equal(map, map2));
142         isl_map_free(map);
143         isl_map_free(map2);
144
145         str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
146         str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
147         if (test_parse_map_equal(ctx, str, str2) < 0)
148                 return -1;
149
150         str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
151         str2 = "{ [i,j] -> [min(i,j)] }";
152         if (test_parse_map_equal(ctx, str, str2) < 0)
153                 return -1;
154
155         str = "{ [i,j] : i != j }";
156         str2 = "{ [i,j] : i < j or i > j }";
157         if (test_parse_map_equal(ctx, str, str2) < 0)
158                 return -1;
159
160         str = "{ [i,j] : (i+1)*2 >= j }";
161         str2 = "{ [i, j] : j <= 2 + 2i }";
162         if (test_parse_map_equal(ctx, str, str2) < 0)
163                 return -1;
164
165         str = "{ [i] -> [i > 0 ? 4 : 5] }";
166         str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
167         if (test_parse_map_equal(ctx, str, str2) < 0)
168                 return -1;
169
170         str = "[N=2,M] -> { [i=[(M+N)/4]] }";
171         str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
172         if (test_parse_map_equal(ctx, str, str2) < 0)
173                 return -1;
174
175         str = "{ [x] : x >= 0 }";
176         str2 = "{ [x] : x-0 >= 0 }";
177         if (test_parse_map_equal(ctx, str, str2) < 0)
178                 return -1;
179
180         str = "{ [i] : ((i > 10)) }";
181         str2 = "{ [i] : i >= 11 }";
182         if (test_parse_map_equal(ctx, str, str2) < 0)
183                 return -1;
184
185         str = "{ [i] -> [0] }";
186         str2 = "{ [i] -> [0 * i] }";
187         if (test_parse_map_equal(ctx, str, str2) < 0)
188                 return -1;
189
190         test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
191         test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
192         test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
193         test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
194
195         if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
196                                       "{ [a] -> [b] : true }") < 0)
197                 return -1;
198
199         if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
200                                       "{ [i] : i <= 10 }") < 0)
201                 return -1;
202
203         return 0;
204 }
205
206 void test_read(struct isl_ctx *ctx)
207 {
208         char *filename;
209         FILE *input;
210         struct isl_basic_set *bset1, *bset2;
211         const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
212
213         filename = get_filename(ctx, "set", "omega");
214         assert(filename);
215         input = fopen(filename, "r");
216         assert(input);
217
218         bset1 = isl_basic_set_read_from_file(ctx, input);
219         bset2 = isl_basic_set_read_from_str(ctx, str);
220
221         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
222
223         isl_basic_set_free(bset1);
224         isl_basic_set_free(bset2);
225         free(filename);
226
227         fclose(input);
228 }
229
230 void test_bounded(struct isl_ctx *ctx)
231 {
232         isl_set *set;
233         int bounded;
234
235         set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
236         bounded = isl_set_is_bounded(set);
237         assert(bounded);
238         isl_set_free(set);
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] : i <= n }");
246         bounded = isl_set_is_bounded(set);
247         assert(!bounded);
248         isl_set_free(set);
249 }
250
251 /* Construct the basic set { [i] : 5 <= i <= N } */
252 void test_construction(struct isl_ctx *ctx)
253 {
254         isl_int v;
255         isl_space *dim;
256         isl_local_space *ls;
257         struct isl_basic_set *bset;
258         struct isl_constraint *c;
259
260         isl_int_init(v);
261
262         dim = isl_space_set_alloc(ctx, 1, 1);
263         bset = isl_basic_set_universe(isl_space_copy(dim));
264         ls = isl_local_space_from_space(dim);
265
266         c = isl_inequality_alloc(isl_local_space_copy(ls));
267         isl_int_set_si(v, -1);
268         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
269         isl_int_set_si(v, 1);
270         isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
271         bset = isl_basic_set_add_constraint(bset, c);
272
273         c = isl_inequality_alloc(isl_local_space_copy(ls));
274         isl_int_set_si(v, 1);
275         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
276         isl_int_set_si(v, -5);
277         isl_constraint_set_constant(c, v);
278         bset = isl_basic_set_add_constraint(bset, c);
279
280         isl_local_space_free(ls);
281         isl_basic_set_free(bset);
282
283         isl_int_clear(v);
284 }
285
286 void test_dim(struct isl_ctx *ctx)
287 {
288         const char *str;
289         isl_map *map1, *map2;
290
291         map1 = isl_map_read_from_str(ctx,
292             "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
293         map1 = isl_map_add_dims(map1, isl_dim_in, 1);
294         map2 = isl_map_read_from_str(ctx,
295             "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
296         assert(isl_map_is_equal(map1, map2));
297         isl_map_free(map2);
298
299         map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
300         map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
301         assert(isl_map_is_equal(map1, map2));
302
303         isl_map_free(map1);
304         isl_map_free(map2);
305
306         str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
307         map1 = isl_map_read_from_str(ctx, str);
308         str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
309         map2 = isl_map_read_from_str(ctx, str);
310         map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
311         assert(isl_map_is_equal(map1, map2));
312
313         isl_map_free(map1);
314         isl_map_free(map2);
315 }
316
317 static int test_div(isl_ctx *ctx)
318 {
319         unsigned n;
320         const char *str;
321         int empty;
322         isl_int v;
323         isl_space *dim;
324         isl_set *set;
325         isl_local_space *ls;
326         struct isl_basic_set *bset;
327         struct isl_constraint *c;
328
329         isl_int_init(v);
330
331         /* test 1 */
332         dim = isl_space_set_alloc(ctx, 0, 3);
333         bset = isl_basic_set_universe(isl_space_copy(dim));
334         ls = isl_local_space_from_space(dim);
335
336         c = isl_equality_alloc(isl_local_space_copy(ls));
337         isl_int_set_si(v, -1);
338         isl_constraint_set_constant(c, v);
339         isl_int_set_si(v, 1);
340         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
341         isl_int_set_si(v, 3);
342         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
343         bset = isl_basic_set_add_constraint(bset, c);
344
345         c = isl_equality_alloc(isl_local_space_copy(ls));
346         isl_int_set_si(v, 1);
347         isl_constraint_set_constant(c, v);
348         isl_int_set_si(v, -1);
349         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
350         isl_int_set_si(v, 3);
351         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
352         bset = isl_basic_set_add_constraint(bset, c);
353
354         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
355
356         assert(bset && bset->n_div == 1);
357         isl_local_space_free(ls);
358         isl_basic_set_free(bset);
359
360         /* test 2 */
361         dim = isl_space_set_alloc(ctx, 0, 3);
362         bset = isl_basic_set_universe(isl_space_copy(dim));
363         ls = isl_local_space_from_space(dim);
364
365         c = isl_equality_alloc(isl_local_space_copy(ls));
366         isl_int_set_si(v, 1);
367         isl_constraint_set_constant(c, v);
368         isl_int_set_si(v, -1);
369         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
370         isl_int_set_si(v, 3);
371         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
372         bset = isl_basic_set_add_constraint(bset, c);
373
374         c = isl_equality_alloc(isl_local_space_copy(ls));
375         isl_int_set_si(v, -1);
376         isl_constraint_set_constant(c, v);
377         isl_int_set_si(v, 1);
378         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
379         isl_int_set_si(v, 3);
380         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
381         bset = isl_basic_set_add_constraint(bset, c);
382
383         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
384
385         assert(bset && bset->n_div == 1);
386         isl_local_space_free(ls);
387         isl_basic_set_free(bset);
388
389         /* test 3 */
390         dim = isl_space_set_alloc(ctx, 0, 3);
391         bset = isl_basic_set_universe(isl_space_copy(dim));
392         ls = isl_local_space_from_space(dim);
393
394         c = isl_equality_alloc(isl_local_space_copy(ls));
395         isl_int_set_si(v, 1);
396         isl_constraint_set_constant(c, v);
397         isl_int_set_si(v, -1);
398         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
399         isl_int_set_si(v, 3);
400         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
401         bset = isl_basic_set_add_constraint(bset, c);
402
403         c = isl_equality_alloc(isl_local_space_copy(ls));
404         isl_int_set_si(v, -3);
405         isl_constraint_set_constant(c, v);
406         isl_int_set_si(v, 1);
407         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
408         isl_int_set_si(v, 4);
409         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
410         bset = isl_basic_set_add_constraint(bset, c);
411
412         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
413
414         assert(bset && bset->n_div == 1);
415         isl_local_space_free(ls);
416         isl_basic_set_free(bset);
417
418         /* test 4 */
419         dim = isl_space_set_alloc(ctx, 0, 3);
420         bset = isl_basic_set_universe(isl_space_copy(dim));
421         ls = isl_local_space_from_space(dim);
422
423         c = isl_equality_alloc(isl_local_space_copy(ls));
424         isl_int_set_si(v, 2);
425         isl_constraint_set_constant(c, v);
426         isl_int_set_si(v, -1);
427         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
428         isl_int_set_si(v, 3);
429         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
430         bset = isl_basic_set_add_constraint(bset, c);
431
432         c = isl_equality_alloc(isl_local_space_copy(ls));
433         isl_int_set_si(v, -1);
434         isl_constraint_set_constant(c, v);
435         isl_int_set_si(v, 1);
436         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
437         isl_int_set_si(v, 6);
438         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
439         bset = isl_basic_set_add_constraint(bset, c);
440
441         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
442
443         assert(isl_basic_set_is_empty(bset));
444         isl_local_space_free(ls);
445         isl_basic_set_free(bset);
446
447         /* test 5 */
448         dim = isl_space_set_alloc(ctx, 0, 3);
449         bset = isl_basic_set_universe(isl_space_copy(dim));
450         ls = isl_local_space_from_space(dim);
451
452         c = isl_equality_alloc(isl_local_space_copy(ls));
453         isl_int_set_si(v, -1);
454         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
455         isl_int_set_si(v, 3);
456         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
457         bset = isl_basic_set_add_constraint(bset, c);
458
459         c = isl_equality_alloc(isl_local_space_copy(ls));
460         isl_int_set_si(v, 1);
461         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
462         isl_int_set_si(v, -3);
463         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
464         bset = isl_basic_set_add_constraint(bset, c);
465
466         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
467
468         assert(bset && bset->n_div == 0);
469         isl_basic_set_free(bset);
470         isl_local_space_free(ls);
471
472         /* test 6 */
473         dim = isl_space_set_alloc(ctx, 0, 3);
474         bset = isl_basic_set_universe(isl_space_copy(dim));
475         ls = isl_local_space_from_space(dim);
476
477         c = isl_equality_alloc(isl_local_space_copy(ls));
478         isl_int_set_si(v, -1);
479         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
480         isl_int_set_si(v, 6);
481         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
482         bset = isl_basic_set_add_constraint(bset, c);
483
484         c = isl_equality_alloc(isl_local_space_copy(ls));
485         isl_int_set_si(v, 1);
486         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
487         isl_int_set_si(v, -3);
488         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
489         bset = isl_basic_set_add_constraint(bset, c);
490
491         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
492
493         assert(bset && bset->n_div == 1);
494         isl_basic_set_free(bset);
495         isl_local_space_free(ls);
496
497         /* test 7 */
498         /* This test is a bit tricky.  We set up an equality
499          *              a + 3b + 3c = 6 e0
500          * Normalization of divs creates _two_ divs
501          *              a = 3 e0
502          *              c - b - e0 = 2 e1
503          * Afterwards e0 is removed again because it has coefficient -1
504          * and we end up with the original equality and div again.
505          * Perhaps we can avoid the introduction of this temporary div.
506          */
507         dim = isl_space_set_alloc(ctx, 0, 4);
508         bset = isl_basic_set_universe(isl_space_copy(dim));
509         ls = isl_local_space_from_space(dim);
510
511         c = isl_equality_alloc(isl_local_space_copy(ls));
512         isl_int_set_si(v, -1);
513         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
514         isl_int_set_si(v, -3);
515         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
516         isl_int_set_si(v, -3);
517         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
518         isl_int_set_si(v, 6);
519         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
520         bset = isl_basic_set_add_constraint(bset, c);
521
522         bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
523
524         /* Test disabled for now */
525         /*
526         assert(bset && bset->n_div == 1);
527         */
528         isl_local_space_free(ls);
529         isl_basic_set_free(bset);
530
531         /* test 8 */
532         dim = isl_space_set_alloc(ctx, 0, 5);
533         bset = isl_basic_set_universe(isl_space_copy(dim));
534         ls = isl_local_space_from_space(dim);
535
536         c = isl_equality_alloc(isl_local_space_copy(ls));
537         isl_int_set_si(v, -1);
538         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
539         isl_int_set_si(v, -3);
540         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
541         isl_int_set_si(v, -3);
542         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
543         isl_int_set_si(v, 6);
544         isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
545         bset = isl_basic_set_add_constraint(bset, c);
546
547         c = isl_equality_alloc(isl_local_space_copy(ls));
548         isl_int_set_si(v, -1);
549         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
550         isl_int_set_si(v, 1);
551         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
552         isl_int_set_si(v, 1);
553         isl_constraint_set_constant(c, v);
554         bset = isl_basic_set_add_constraint(bset, c);
555
556         bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
557
558         /* Test disabled for now */
559         /*
560         assert(bset && bset->n_div == 1);
561         */
562         isl_local_space_free(ls);
563         isl_basic_set_free(bset);
564
565         /* test 9 */
566         dim = isl_space_set_alloc(ctx, 0, 4);
567         bset = isl_basic_set_universe(isl_space_copy(dim));
568         ls = isl_local_space_from_space(dim);
569
570         c = isl_equality_alloc(isl_local_space_copy(ls));
571         isl_int_set_si(v, 1);
572         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
573         isl_int_set_si(v, -1);
574         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
575         isl_int_set_si(v, -2);
576         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
577         bset = isl_basic_set_add_constraint(bset, c);
578
579         c = isl_equality_alloc(isl_local_space_copy(ls));
580         isl_int_set_si(v, -1);
581         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
582         isl_int_set_si(v, 3);
583         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
584         isl_int_set_si(v, 2);
585         isl_constraint_set_constant(c, v);
586         bset = isl_basic_set_add_constraint(bset, c);
587
588         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
589
590         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
591
592         assert(!isl_basic_set_is_empty(bset));
593
594         isl_local_space_free(ls);
595         isl_basic_set_free(bset);
596
597         /* test 10 */
598         dim = isl_space_set_alloc(ctx, 0, 3);
599         bset = isl_basic_set_universe(isl_space_copy(dim));
600         ls = isl_local_space_from_space(dim);
601
602         c = isl_equality_alloc(isl_local_space_copy(ls));
603         isl_int_set_si(v, 1);
604         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
605         isl_int_set_si(v, -2);
606         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
607         bset = isl_basic_set_add_constraint(bset, c);
608
609         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
610
611         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
612
613         isl_local_space_free(ls);
614         isl_basic_set_free(bset);
615
616         isl_int_clear(v);
617
618         str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
619             "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
620         set = isl_set_read_from_str(ctx, str);
621         set = isl_set_compute_divs(set);
622         isl_set_free(set);
623         if (!set)
624                 return -1;
625
626         str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
627         bset = isl_basic_set_read_from_str(ctx, str);
628         n = isl_basic_set_dim(bset, isl_dim_div);
629         isl_basic_set_free(bset);
630         if (!bset)
631                 return -1;
632         if (n != 0)
633                 isl_die(ctx, isl_error_unknown,
634                         "expecting no existentials", return -1);
635
636         str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
637         set = isl_set_read_from_str(ctx, str);
638         set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
639         set = isl_set_fix_si(set, isl_dim_set, 2, -3);
640         empty = isl_set_is_empty(set);
641         isl_set_free(set);
642         if (empty < 0)
643                 return -1;
644         if (!empty)
645                 isl_die(ctx, isl_error_unknown,
646                         "result not as accurate as expected", return -1);
647
648         return 0;
649 }
650
651 void test_application_case(struct isl_ctx *ctx, const char *name)
652 {
653         char *filename;
654         FILE *input;
655         struct isl_basic_set *bset1, *bset2;
656         struct isl_basic_map *bmap;
657
658         filename = get_filename(ctx, name, "omega");
659         assert(filename);
660         input = fopen(filename, "r");
661         assert(input);
662
663         bset1 = isl_basic_set_read_from_file(ctx, input);
664         bmap = isl_basic_map_read_from_file(ctx, input);
665
666         bset1 = isl_basic_set_apply(bset1, bmap);
667
668         bset2 = isl_basic_set_read_from_file(ctx, input);
669
670         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
671
672         isl_basic_set_free(bset1);
673         isl_basic_set_free(bset2);
674         free(filename);
675
676         fclose(input);
677 }
678
679 void test_application(struct isl_ctx *ctx)
680 {
681         test_application_case(ctx, "application");
682         test_application_case(ctx, "application2");
683 }
684
685 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
686 {
687         char *filename;
688         FILE *input;
689         struct isl_basic_set *bset1, *bset2;
690
691         filename = get_filename(ctx, name, "polylib");
692         assert(filename);
693         input = fopen(filename, "r");
694         assert(input);
695
696         bset1 = isl_basic_set_read_from_file(ctx, input);
697         bset2 = isl_basic_set_read_from_file(ctx, input);
698
699         bset1 = isl_basic_set_affine_hull(bset1);
700
701         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
702
703         isl_basic_set_free(bset1);
704         isl_basic_set_free(bset2);
705         free(filename);
706
707         fclose(input);
708 }
709
710 int test_affine_hull(struct isl_ctx *ctx)
711 {
712         const char *str;
713         isl_set *set;
714         isl_basic_set *bset;
715         int n;
716
717         test_affine_hull_case(ctx, "affine2");
718         test_affine_hull_case(ctx, "affine");
719         test_affine_hull_case(ctx, "affine3");
720
721         str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
722                         "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
723                         "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
724                         "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
725         set = isl_set_read_from_str(ctx, str);
726         bset = isl_set_affine_hull(set);
727         n = isl_basic_set_dim(bset, isl_dim_div);
728         isl_basic_set_free(bset);
729         if (n != 0)
730                 isl_die(ctx, isl_error_unknown, "not expecting any divs",
731                         return -1);
732
733         return 0;
734 }
735
736 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
737 {
738         char *filename;
739         FILE *input;
740         struct isl_basic_set *bset1, *bset2;
741         struct isl_set *set;
742
743         filename = get_filename(ctx, name, "polylib");
744         assert(filename);
745         input = fopen(filename, "r");
746         assert(input);
747
748         bset1 = isl_basic_set_read_from_file(ctx, input);
749         bset2 = isl_basic_set_read_from_file(ctx, input);
750
751         set = isl_basic_set_union(bset1, bset2);
752         bset1 = isl_set_convex_hull(set);
753
754         bset2 = isl_basic_set_read_from_file(ctx, input);
755
756         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
757
758         isl_basic_set_free(bset1);
759         isl_basic_set_free(bset2);
760         free(filename);
761
762         fclose(input);
763 }
764
765 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
766 {
767         const char *str1, *str2;
768         isl_set *set1, *set2;
769         int orig_convex = ctx->opt->convex;
770         ctx->opt->convex = convex;
771
772         test_convex_hull_case(ctx, "convex0");
773         test_convex_hull_case(ctx, "convex1");
774         test_convex_hull_case(ctx, "convex2");
775         test_convex_hull_case(ctx, "convex3");
776         test_convex_hull_case(ctx, "convex4");
777         test_convex_hull_case(ctx, "convex5");
778         test_convex_hull_case(ctx, "convex6");
779         test_convex_hull_case(ctx, "convex7");
780         test_convex_hull_case(ctx, "convex8");
781         test_convex_hull_case(ctx, "convex9");
782         test_convex_hull_case(ctx, "convex10");
783         test_convex_hull_case(ctx, "convex11");
784         test_convex_hull_case(ctx, "convex12");
785         test_convex_hull_case(ctx, "convex13");
786         test_convex_hull_case(ctx, "convex14");
787         test_convex_hull_case(ctx, "convex15");
788
789         str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
790                "(i0 = 1 and i1 = 0 and i2 = 1) or "
791                "(i0 = 0 and i1 = 0 and i2 = 0) }";
792         str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
793         set1 = isl_set_read_from_str(ctx, str1);
794         set2 = isl_set_read_from_str(ctx, str2);
795         set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
796         assert(isl_set_is_equal(set1, set2));
797         isl_set_free(set1);
798         isl_set_free(set2);
799
800         ctx->opt->convex = orig_convex;
801 }
802
803 void test_convex_hull(struct isl_ctx *ctx)
804 {
805         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
806         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
807 }
808
809 void test_gist_case(struct isl_ctx *ctx, const char *name)
810 {
811         char *filename;
812         FILE *input;
813         struct isl_basic_set *bset1, *bset2;
814
815         filename = get_filename(ctx, name, "polylib");
816         assert(filename);
817         input = fopen(filename, "r");
818         assert(input);
819
820         bset1 = isl_basic_set_read_from_file(ctx, input);
821         bset2 = isl_basic_set_read_from_file(ctx, input);
822
823         bset1 = isl_basic_set_gist(bset1, bset2);
824
825         bset2 = isl_basic_set_read_from_file(ctx, input);
826
827         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
828
829         isl_basic_set_free(bset1);
830         isl_basic_set_free(bset2);
831         free(filename);
832
833         fclose(input);
834 }
835
836 void test_gist(struct isl_ctx *ctx)
837 {
838         const char *str;
839         isl_basic_set *bset1, *bset2;
840
841         test_gist_case(ctx, "gist1");
842
843         str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
844             "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
845             "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
846             "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
847             "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
848             "16e0 >= 16 + 16p6 + 15p10 and  p2 <= 15 and p3 >= 0 and "
849             "p3 <= 31 and  p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
850             "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
851             "p10 <= 15 and p10 <= -1 + p0 - p6) }";
852         bset1 = isl_basic_set_read_from_str(ctx, str);
853         str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
854             "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
855             "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
856             "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
857             "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
858             "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
859             "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
860             "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
861         bset2 = isl_basic_set_read_from_str(ctx, str);
862         bset1 = isl_basic_set_gist(bset1, bset2);
863         assert(bset1 && bset1->n_div == 0);
864         isl_basic_set_free(bset1);
865 }
866
867 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
868 {
869         isl_set *set, *set2;
870         int equal;
871         int one;
872
873         set = isl_set_read_from_str(ctx, str);
874         set = isl_set_coalesce(set);
875         set2 = isl_set_read_from_str(ctx, str);
876         equal = isl_set_is_equal(set, set2);
877         one = set && set->n == 1;
878         isl_set_free(set);
879         isl_set_free(set2);
880
881         if (equal < 0)
882                 return -1;
883         if (!equal)
884                 isl_die(ctx, isl_error_unknown,
885                         "coalesced set not equal to input", return -1);
886         if (check_one && !one)
887                 isl_die(ctx, isl_error_unknown,
888                         "coalesced set should not be a union", return -1);
889
890         return 0;
891 }
892
893 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
894 {
895         int r = 0;
896         int bounded;
897
898         bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
899         isl_options_set_coalesce_bounded_wrapping(ctx, 0);
900
901         if (test_coalesce_set(ctx,
902                 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
903                         "-x - y + 1 >= 0 and -3 <= z <= 3;"
904                 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
905                         "x-z + 20 >= 0 and x+z + 20 >= 0 and "
906                         "-10 <= y <= 0}", 1) < 0)
907                 goto error;
908         if (test_coalesce_set(ctx,
909                 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
910                 goto error;
911         if (test_coalesce_set(ctx,
912                 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
913                 goto error;
914
915         if (0) {
916 error:
917                 r = -1;
918         }
919
920         isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
921
922         return r;
923 }
924
925 int test_coalesce(struct isl_ctx *ctx)
926 {
927         const char *str;
928         struct isl_set *set, *set2;
929         struct isl_map *map, *map2;
930
931         set = isl_set_read_from_str(ctx,
932                 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
933                        "y >= x & x >= 2 & 5 >= y }");
934         set = isl_set_coalesce(set);
935         assert(set && set->n == 1);
936         isl_set_free(set);
937
938         set = isl_set_read_from_str(ctx,
939                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
940                        "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}");
941         set = isl_set_coalesce(set);
942         assert(set && set->n == 1);
943         isl_set_free(set);
944
945         set = isl_set_read_from_str(ctx,
946                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
947                        "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}");
948         set = isl_set_coalesce(set);
949         assert(set && set->n == 2);
950         isl_set_free(set);
951
952         set = isl_set_read_from_str(ctx,
953                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
954                        "y >= 0 & x >= 6 & x <= 10 & y <= x}");
955         set = isl_set_coalesce(set);
956         assert(set && set->n == 1);
957         isl_set_free(set);
958
959         set = isl_set_read_from_str(ctx,
960                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
961                        "y >= 0 & x >= 7 & x <= 10 & y <= x}");
962         set = isl_set_coalesce(set);
963         assert(set && set->n == 2);
964         isl_set_free(set);
965
966         set = isl_set_read_from_str(ctx,
967                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
968                        "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}");
969         set = isl_set_coalesce(set);
970         assert(set && set->n == 2);
971         isl_set_free(set);
972
973         set = isl_set_read_from_str(ctx,
974                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
975                        "y >= 0 & x = 6 & y <= 6}");
976         set = isl_set_coalesce(set);
977         assert(set && set->n == 1);
978         isl_set_free(set);
979
980         set = isl_set_read_from_str(ctx,
981                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
982                        "y >= 0 & x = 7 & y <= 6}");
983         set = isl_set_coalesce(set);
984         assert(set && set->n == 2);
985         isl_set_free(set);
986
987         set = isl_set_read_from_str(ctx,
988                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
989                        "y >= 0 & x = 6 & y <= 5}");
990         set = isl_set_coalesce(set);
991         assert(set && set->n == 1);
992         set2 = isl_set_read_from_str(ctx,
993                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
994                        "y >= 0 & x = 6 & y <= 5}");
995         assert(isl_set_is_equal(set, set2));
996         isl_set_free(set);
997         isl_set_free(set2);
998
999         set = isl_set_read_from_str(ctx,
1000                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1001                        "y >= 0 & x = 6 & y <= 7}");
1002         set = isl_set_coalesce(set);
1003         assert(set && set->n == 2);
1004         isl_set_free(set);
1005
1006         set = isl_set_read_from_str(ctx,
1007                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
1008         set = isl_set_coalesce(set);
1009         assert(set && set->n == 1);
1010         set2 = isl_set_read_from_str(ctx,
1011                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
1012         assert(isl_set_is_equal(set, set2));
1013         isl_set_free(set);
1014         isl_set_free(set2);
1015
1016         set = isl_set_read_from_str(ctx,
1017                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
1018         set = isl_set_coalesce(set);
1019         set2 = isl_set_read_from_str(ctx,
1020                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
1021         assert(isl_set_is_equal(set, set2));
1022         isl_set_free(set);
1023         isl_set_free(set2);
1024
1025         set = isl_set_read_from_str(ctx,
1026                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
1027                                 "2 <= i and i <= n }");
1028         set = isl_set_coalesce(set);
1029         assert(set && set->n == 1);
1030         set2 = isl_set_read_from_str(ctx,
1031                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
1032                                 "2 <= i and i <= n }");
1033         assert(isl_set_is_equal(set, set2));
1034         isl_set_free(set);
1035         isl_set_free(set2);
1036
1037         map = isl_map_read_from_str(ctx,
1038                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1039                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1040                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1041                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1042                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1043                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1044                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1045                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1046                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1047                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1048                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1049                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1050                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1051                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1052                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1053                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1054                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1055                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
1056         map = isl_map_coalesce(map);
1057         map2 = isl_map_read_from_str(ctx,
1058                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1059                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1060                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1061                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1062                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1063                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1064                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1065                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1066                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1067                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1068                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1069                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1070                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1071                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1072                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1073                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1074                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1075                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
1076         assert(isl_map_is_equal(map, map2));
1077         isl_map_free(map);
1078         isl_map_free(map2);
1079
1080         str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1081               "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1082               "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1083               "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
1084         map = isl_map_read_from_str(ctx, str);
1085         map = isl_map_coalesce(map);
1086         map2 = isl_map_read_from_str(ctx, str);
1087         assert(isl_map_is_equal(map, map2));
1088         isl_map_free(map);
1089         isl_map_free(map2);
1090
1091         str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
1092           "[o0, o1, o2, o3, o4, o5, o6] : "
1093           "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1094           "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1095           "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1096           "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1097           "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1098           "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1099           "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1100           "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1101           "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1102           "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1103           "o6 >= i3 + i6 - o3 and M >= 0 and "
1104           "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1105           "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
1106         map = isl_map_read_from_str(ctx, str);
1107         map = isl_map_coalesce(map);
1108         map2 = isl_map_read_from_str(ctx, str);
1109         assert(isl_map_is_equal(map, map2));
1110         isl_map_free(map);
1111         isl_map_free(map2);
1112
1113         str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1114                 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1115                 "(o0 = 0 and M >= 2 and N >= 3) or "
1116                 "(M = 0 and o0 = 0 and N >= 3) }";
1117         map = isl_map_read_from_str(ctx, str);
1118         map = isl_map_coalesce(map);
1119         map2 = isl_map_read_from_str(ctx, str);
1120         assert(isl_map_is_equal(map, map2));
1121         isl_map_free(map);
1122         isl_map_free(map2);
1123
1124         str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1125                 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1126                 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1127                 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
1128         map = isl_map_read_from_str(ctx, str);
1129         map = isl_map_coalesce(map);
1130         map2 = isl_map_read_from_str(ctx, str);
1131         assert(isl_map_is_equal(map, map2));
1132         isl_map_free(map);
1133         isl_map_free(map2);
1134
1135         test_coalesce_set(ctx,
1136                 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
1137                                 "(i1 = M and M >= 1) }", 0);
1138         test_coalesce_set(ctx,
1139                 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
1140         test_coalesce_set(ctx,
1141                 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1142                 "(y = 3 and x = 1) }", 1);
1143         test_coalesce_set(ctx,
1144                 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1145                 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1146                 "i1 <= M and i3 <= M and i4 <= M) or "
1147                 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1148                 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1149                 "i4 <= -1 + M) }", 1);
1150         test_coalesce_set(ctx,
1151                 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1152                 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
1153         if (test_coalesce_unbounded_wrapping(ctx) < 0)
1154                 return -1;
1155         if (test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0) < 0)
1156                 return -1;
1157         if (test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1) < 0)
1158                 return -1;
1159         if (test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1) < 0)
1160                 return -1;
1161         if (test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0) < 0)
1162                 return -1;
1163         if (test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1) < 0)
1164                 return -1;
1165         if (test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0) < 0)
1166                 return -1;
1167         if (test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1168                 return -1;
1169         if (test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1170                 return -1;
1171         if (test_coalesce_set(ctx, "{ [a, b] : exists e : 2e = a and "
1172                     "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }", 0) < 0)
1173                 return -1;
1174         if (test_coalesce_set(ctx,
1175                 "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1176                         "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1177                         "j >= 1 and j' <= i + j - i' and i >= 1; "
1178                 "[1, 1, 1, 1] }", 0) < 0)
1179                 return -1;
1180         if (test_coalesce_set(ctx, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1181                                      "[i,j] : exists a : j = 3a }", 1) < 0)
1182                 return -1;
1183         return 0;
1184 }
1185
1186 void test_closure(struct isl_ctx *ctx)
1187 {
1188         const char *str;
1189         isl_set *dom;
1190         isl_map *up, *right;
1191         isl_map *map, *map2;
1192         int exact;
1193
1194         /* COCOA example 1 */
1195         map = isl_map_read_from_str(ctx,
1196                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1197                         "1 <= i and i < n and 1 <= j and j < n or "
1198                         "i2 = i + 1 and j2 = j - 1 and "
1199                         "1 <= i and i < n and 2 <= j and j <= n }");
1200         map = isl_map_power(map, &exact);
1201         assert(exact);
1202         isl_map_free(map);
1203
1204         /* COCOA example 1 */
1205         map = isl_map_read_from_str(ctx,
1206                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1207                         "1 <= i and i < n and 1 <= j and j < n or "
1208                         "i2 = i + 1 and j2 = j - 1 and "
1209                         "1 <= i and i < n and 2 <= j and j <= n }");
1210         map = isl_map_transitive_closure(map, &exact);
1211         assert(exact);
1212         map2 = isl_map_read_from_str(ctx,
1213                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1214                         "1 <= i and i < n and 1 <= j and j <= n and "
1215                         "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1216                         "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1217                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1218         assert(isl_map_is_equal(map, map2));
1219         isl_map_free(map2);
1220         isl_map_free(map);
1221
1222         map = isl_map_read_from_str(ctx,
1223                 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1224                                      " 0 <= y and y <= n }");
1225         map = isl_map_transitive_closure(map, &exact);
1226         map2 = isl_map_read_from_str(ctx,
1227                 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1228                                      " 0 <= y and y <= n }");
1229         assert(isl_map_is_equal(map, map2));
1230         isl_map_free(map2);
1231         isl_map_free(map);
1232
1233         /* COCOA example 2 */
1234         map = isl_map_read_from_str(ctx,
1235                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1236                         "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1237                         "i2 = i + 2 and j2 = j - 2 and "
1238                         "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1239         map = isl_map_transitive_closure(map, &exact);
1240         assert(exact);
1241         map2 = isl_map_read_from_str(ctx,
1242                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1243                         "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1244                         "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1245                         "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1246                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1247         assert(isl_map_is_equal(map, map2));
1248         isl_map_free(map);
1249         isl_map_free(map2);
1250
1251         /* COCOA Fig.2 left */
1252         map = isl_map_read_from_str(ctx,
1253                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1254                         "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1255                         "j <= n or "
1256                         "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1257                         "j <= 2 i - 3 and j <= n - 2 or "
1258                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1259                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1260         map = isl_map_transitive_closure(map, &exact);
1261         assert(exact);
1262         isl_map_free(map);
1263
1264         /* COCOA Fig.2 right */
1265         map = isl_map_read_from_str(ctx,
1266                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1267                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1268                         "j <= n or "
1269                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1270                         "j <= 2 i - 4 and j <= n - 3 or "
1271                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1272                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1273         map = isl_map_power(map, &exact);
1274         assert(exact);
1275         isl_map_free(map);
1276
1277         /* COCOA Fig.2 right */
1278         map = isl_map_read_from_str(ctx,
1279                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1280                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1281                         "j <= n or "
1282                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1283                         "j <= 2 i - 4 and j <= n - 3 or "
1284                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1285                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1286         map = isl_map_transitive_closure(map, &exact);
1287         assert(exact);
1288         map2 = isl_map_read_from_str(ctx,
1289                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1290                         "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1291                         "j <= n and 3 + i + 2 j <= 3 n and "
1292                         "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1293                         "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1294                         "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1295                         "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1296                         "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1297         assert(isl_map_is_equal(map, map2));
1298         isl_map_free(map2);
1299         isl_map_free(map);
1300
1301         /* COCOA Fig.1 right */
1302         dom = isl_set_read_from_str(ctx,
1303                 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1304                         "2 x - 3 y + 3 >= 0 }");
1305         right = isl_map_read_from_str(ctx,
1306                 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1307         up = isl_map_read_from_str(ctx,
1308                 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1309         right = isl_map_intersect_domain(right, isl_set_copy(dom));
1310         right = isl_map_intersect_range(right, isl_set_copy(dom));
1311         up = isl_map_intersect_domain(up, isl_set_copy(dom));
1312         up = isl_map_intersect_range(up, dom);
1313         map = isl_map_union(up, right);
1314         map = isl_map_transitive_closure(map, &exact);
1315         assert(exact);
1316         map2 = isl_map_read_from_str(ctx,
1317                 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1318                 "  [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1319         assert(isl_map_is_equal(map, map2));
1320         isl_map_free(map2);
1321         isl_map_free(map);
1322
1323         /* COCOA Theorem 1 counter example */
1324         map = isl_map_read_from_str(ctx,
1325                 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1326                         "i2 = 1 and j2 = j or "
1327                         "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1328         map = isl_map_transitive_closure(map, &exact);
1329         assert(exact);
1330         isl_map_free(map);
1331
1332         map = isl_map_read_from_str(ctx,
1333                 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1334                         "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1335                         "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1336                         "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1337         map = isl_map_transitive_closure(map, &exact);
1338         assert(exact);
1339         isl_map_free(map);
1340
1341         /* Kelly et al 1996, fig 12 */
1342         map = isl_map_read_from_str(ctx,
1343                 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1344                         "1 <= i,j,j+1 <= n or "
1345                         "j = n and j2 = 1 and i2 = i + 1 and "
1346                         "1 <= i,i+1 <= n }");
1347         map = isl_map_transitive_closure(map, &exact);
1348         assert(exact);
1349         map2 = isl_map_read_from_str(ctx,
1350                 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1351                         "1 <= i <= n and i = i2 or "
1352                         "1 <= i < i2 <= n and 1 <= j <= n and "
1353                         "1 <= j2 <= n }");
1354         assert(isl_map_is_equal(map, map2));
1355         isl_map_free(map2);
1356         isl_map_free(map);
1357
1358         /* Omega's closure4 */
1359         map = isl_map_read_from_str(ctx,
1360                 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1361                         "1 <= x,y <= 10 or "
1362                         "x2 = x + 1 and y2 = y and "
1363                         "1 <= x <= 20 && 5 <= y <= 15 }");
1364         map = isl_map_transitive_closure(map, &exact);
1365         assert(exact);
1366         isl_map_free(map);
1367
1368         map = isl_map_read_from_str(ctx,
1369                 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1370         map = isl_map_transitive_closure(map, &exact);
1371         assert(!exact);
1372         map2 = isl_map_read_from_str(ctx,
1373                 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1374         assert(isl_map_is_equal(map, map2));
1375         isl_map_free(map);
1376         isl_map_free(map2);
1377
1378         str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1379             "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1380             "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1381             "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1382         map = isl_map_read_from_str(ctx, str);
1383         map = isl_map_transitive_closure(map, &exact);
1384         assert(exact);
1385         map2 = isl_map_read_from_str(ctx, str);
1386         assert(isl_map_is_equal(map, map2));
1387         isl_map_free(map);
1388         isl_map_free(map2);
1389
1390         str = "{[0] -> [1]; [2] -> [3]}";
1391         map = isl_map_read_from_str(ctx, str);
1392         map = isl_map_transitive_closure(map, &exact);
1393         assert(exact);
1394         map2 = isl_map_read_from_str(ctx, str);
1395         assert(isl_map_is_equal(map, map2));
1396         isl_map_free(map);
1397         isl_map_free(map2);
1398
1399         str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1400             "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1401             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1402             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1403             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1404             "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1405             "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1406             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 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, 1, 0, i0] -> [i5, 2]] -> "
1410             "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1411             "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 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, 2, 0, i0] -> [i5, 2]] -> "
1415             "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1416             "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 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         map = isl_map_read_from_str(ctx, str);
1420         map = isl_map_transitive_closure(map, NULL);
1421         assert(map);
1422         isl_map_free(map);
1423 }
1424
1425 void test_lex(struct isl_ctx *ctx)
1426 {
1427         isl_space *dim;
1428         isl_map *map;
1429
1430         dim = isl_space_set_alloc(ctx, 0, 0);
1431         map = isl_map_lex_le(dim);
1432         assert(!isl_map_is_empty(map));
1433         isl_map_free(map);
1434 }
1435
1436 void test_lexmin(struct isl_ctx *ctx)
1437 {
1438         const char *str;
1439         isl_basic_map *bmap;
1440         isl_map *map, *map2;
1441         isl_set *set;
1442         isl_set *set2;
1443         isl_pw_multi_aff *pma;
1444
1445         str = "[p0, p1] -> { [] -> [] : "
1446             "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1447             "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1448             "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1449             "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1450             "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1451             "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1452             "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1453             "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1454             "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1455             "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1456             "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1457         map = isl_map_read_from_str(ctx, str);
1458         map = isl_map_lexmin(map);
1459         isl_map_free(map);
1460
1461         str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1462             "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1463         set = isl_set_read_from_str(ctx, str);
1464         set = isl_set_lexmax(set);
1465         str = "[C] -> { [obj,a,b,c] : C = 8 }";
1466         set2 = isl_set_read_from_str(ctx, str);
1467         set = isl_set_intersect(set, set2);
1468         assert(!isl_set_is_empty(set));
1469         isl_set_free(set);
1470
1471         str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1472         map = isl_map_read_from_str(ctx, str);
1473         map = isl_map_lexmin(map);
1474         str = "{ [x] -> [5] : 6 <= x <= 8; "
1475                 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1476         map2 = isl_map_read_from_str(ctx, str);
1477         assert(isl_map_is_equal(map, map2));
1478         isl_map_free(map);
1479         isl_map_free(map2);
1480
1481         str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1482         map = isl_map_read_from_str(ctx, str);
1483         map2 = isl_map_copy(map);
1484         map = isl_map_lexmin(map);
1485         assert(isl_map_is_equal(map, map2));
1486         isl_map_free(map);
1487         isl_map_free(map2);
1488
1489         str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1490         map = isl_map_read_from_str(ctx, str);
1491         map = isl_map_lexmin(map);
1492         str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1493                 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1494                 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1495                 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1496         map2 = isl_map_read_from_str(ctx, str);
1497         assert(isl_map_is_equal(map, map2));
1498         isl_map_free(map);
1499         isl_map_free(map2);
1500
1501         str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1502                                 " 8i' <= i and 8i' >= -7 + i }";
1503         bmap = isl_basic_map_read_from_str(ctx, str);
1504         pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1505         map2 = isl_map_from_pw_multi_aff(pma);
1506         map = isl_map_from_basic_map(bmap);
1507         assert(isl_map_is_equal(map, map2));
1508         isl_map_free(map);
1509         isl_map_free(map2);
1510
1511         str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1512         map = isl_map_read_from_str(ctx, str);
1513         map = isl_map_lexmin(map);
1514         str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1515         map2 = isl_map_read_from_str(ctx, str);
1516         assert(isl_map_is_equal(map, map2));
1517         isl_map_free(map);
1518         isl_map_free(map2);
1519 }
1520
1521 struct must_may {
1522         isl_map *must;
1523         isl_map *may;
1524 };
1525
1526 static int collect_must_may(__isl_take isl_map *dep, int must,
1527         void *dep_user, void *user)
1528 {
1529         struct must_may *mm = (struct must_may *)user;
1530
1531         if (must)
1532                 mm->must = isl_map_union(mm->must, dep);
1533         else
1534                 mm->may = isl_map_union(mm->may, dep);
1535
1536         return 0;
1537 }
1538
1539 static int common_space(void *first, void *second)
1540 {
1541         int depth = *(int *)first;
1542         return 2 * depth;
1543 }
1544
1545 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1546 {
1547         isl_map *map2;
1548         int equal;
1549
1550         if (!map)
1551                 return -1;
1552
1553         map2 = isl_map_read_from_str(map->ctx, str);
1554         equal = isl_map_is_equal(map, map2);
1555         isl_map_free(map2);
1556
1557         return equal;
1558 }
1559
1560 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1561 {
1562         int equal;
1563
1564         equal = map_is_equal(map, str);
1565         if (equal < 0)
1566                 return -1;
1567         if (!equal)
1568                 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1569                         "result not as expected", return -1);
1570         return 0;
1571 }
1572
1573 void test_dep(struct isl_ctx *ctx)
1574 {
1575         const char *str;
1576         isl_space *dim;
1577         isl_map *map;
1578         isl_access_info *ai;
1579         isl_flow *flow;
1580         int depth;
1581         struct must_may mm;
1582
1583         depth = 3;
1584
1585         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1586         map = isl_map_read_from_str(ctx, str);
1587         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1588
1589         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1590         map = isl_map_read_from_str(ctx, str);
1591         ai = isl_access_info_add_source(ai, map, 1, &depth);
1592
1593         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1594         map = isl_map_read_from_str(ctx, str);
1595         ai = isl_access_info_add_source(ai, map, 1, &depth);
1596
1597         flow = isl_access_info_compute_flow(ai);
1598         dim = isl_space_alloc(ctx, 0, 3, 3);
1599         mm.must = isl_map_empty(isl_space_copy(dim));
1600         mm.may = isl_map_empty(dim);
1601
1602         isl_flow_foreach(flow, collect_must_may, &mm);
1603
1604         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1605               "  [1,10,0] -> [2,5,0] }";
1606         assert(map_is_equal(mm.must, str));
1607         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1608         assert(map_is_equal(mm.may, str));
1609
1610         isl_map_free(mm.must);
1611         isl_map_free(mm.may);
1612         isl_flow_free(flow);
1613
1614
1615         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1616         map = isl_map_read_from_str(ctx, str);
1617         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1618
1619         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1620         map = isl_map_read_from_str(ctx, str);
1621         ai = isl_access_info_add_source(ai, map, 1, &depth);
1622
1623         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1624         map = isl_map_read_from_str(ctx, str);
1625         ai = isl_access_info_add_source(ai, map, 0, &depth);
1626
1627         flow = isl_access_info_compute_flow(ai);
1628         dim = isl_space_alloc(ctx, 0, 3, 3);
1629         mm.must = isl_map_empty(isl_space_copy(dim));
1630         mm.may = isl_map_empty(dim);
1631
1632         isl_flow_foreach(flow, collect_must_may, &mm);
1633
1634         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1635         assert(map_is_equal(mm.must, str));
1636         str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1637         assert(map_is_equal(mm.may, str));
1638
1639         isl_map_free(mm.must);
1640         isl_map_free(mm.may);
1641         isl_flow_free(flow);
1642
1643
1644         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1645         map = isl_map_read_from_str(ctx, str);
1646         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1647
1648         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1649         map = isl_map_read_from_str(ctx, str);
1650         ai = isl_access_info_add_source(ai, map, 0, &depth);
1651
1652         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1653         map = isl_map_read_from_str(ctx, str);
1654         ai = isl_access_info_add_source(ai, map, 0, &depth);
1655
1656         flow = isl_access_info_compute_flow(ai);
1657         dim = isl_space_alloc(ctx, 0, 3, 3);
1658         mm.must = isl_map_empty(isl_space_copy(dim));
1659         mm.may = isl_map_empty(dim);
1660
1661         isl_flow_foreach(flow, collect_must_may, &mm);
1662
1663         str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1664               "  [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1665         assert(map_is_equal(mm.may, str));
1666         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1667         assert(map_is_equal(mm.must, str));
1668
1669         isl_map_free(mm.must);
1670         isl_map_free(mm.may);
1671         isl_flow_free(flow);
1672
1673
1674         str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1675         map = isl_map_read_from_str(ctx, str);
1676         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1677
1678         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1679         map = isl_map_read_from_str(ctx, str);
1680         ai = isl_access_info_add_source(ai, map, 0, &depth);
1681
1682         str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1683         map = isl_map_read_from_str(ctx, str);
1684         ai = isl_access_info_add_source(ai, map, 0, &depth);
1685
1686         flow = isl_access_info_compute_flow(ai);
1687         dim = isl_space_alloc(ctx, 0, 3, 3);
1688         mm.must = isl_map_empty(isl_space_copy(dim));
1689         mm.may = isl_map_empty(dim);
1690
1691         isl_flow_foreach(flow, collect_must_may, &mm);
1692
1693         str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1694               "  [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1695         assert(map_is_equal(mm.may, str));
1696         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1697         assert(map_is_equal(mm.must, str));
1698
1699         isl_map_free(mm.must);
1700         isl_map_free(mm.may);
1701         isl_flow_free(flow);
1702
1703
1704         str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1705         map = isl_map_read_from_str(ctx, str);
1706         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1707
1708         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1709         map = isl_map_read_from_str(ctx, str);
1710         ai = isl_access_info_add_source(ai, map, 0, &depth);
1711
1712         str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1713         map = isl_map_read_from_str(ctx, str);
1714         ai = isl_access_info_add_source(ai, map, 0, &depth);
1715
1716         flow = isl_access_info_compute_flow(ai);
1717         dim = isl_space_alloc(ctx, 0, 3, 3);
1718         mm.must = isl_map_empty(isl_space_copy(dim));
1719         mm.may = isl_map_empty(dim);
1720
1721         isl_flow_foreach(flow, collect_must_may, &mm);
1722
1723         str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1724               "  [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1725         assert(map_is_equal(mm.may, str));
1726         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1727         assert(map_is_equal(mm.must, str));
1728
1729         isl_map_free(mm.must);
1730         isl_map_free(mm.may);
1731         isl_flow_free(flow);
1732
1733
1734         depth = 5;
1735
1736         str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1737         map = isl_map_read_from_str(ctx, str);
1738         ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1739
1740         str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1741         map = isl_map_read_from_str(ctx, str);
1742         ai = isl_access_info_add_source(ai, map, 1, &depth);
1743
1744         flow = isl_access_info_compute_flow(ai);
1745         dim = isl_space_alloc(ctx, 0, 5, 5);
1746         mm.must = isl_map_empty(isl_space_copy(dim));
1747         mm.may = isl_map_empty(dim);
1748
1749         isl_flow_foreach(flow, collect_must_may, &mm);
1750
1751         str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1752         assert(map_is_equal(mm.must, str));
1753         str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1754         assert(map_is_equal(mm.may, str));
1755
1756         isl_map_free(mm.must);
1757         isl_map_free(mm.may);
1758         isl_flow_free(flow);
1759 }
1760
1761 int test_sv(isl_ctx *ctx)
1762 {
1763         const char *str;
1764         isl_map *map;
1765         isl_union_map *umap;
1766         int sv;
1767
1768         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1769         map = isl_map_read_from_str(ctx, str);
1770         sv = isl_map_is_single_valued(map);
1771         isl_map_free(map);
1772         if (sv < 0)
1773                 return -1;
1774         if (!sv)
1775                 isl_die(ctx, isl_error_internal,
1776                         "map not detected as single valued", return -1);
1777
1778         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1779         map = isl_map_read_from_str(ctx, str);
1780         sv = isl_map_is_single_valued(map);
1781         isl_map_free(map);
1782         if (sv < 0)
1783                 return -1;
1784         if (sv)
1785                 isl_die(ctx, isl_error_internal,
1786                         "map detected as single valued", return -1);
1787
1788         str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1789         umap = isl_union_map_read_from_str(ctx, str);
1790         sv = isl_union_map_is_single_valued(umap);
1791         isl_union_map_free(umap);
1792         if (sv < 0)
1793                 return -1;
1794         if (!sv)
1795                 isl_die(ctx, isl_error_internal,
1796                         "map not detected as single valued", return -1);
1797
1798         str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1799         umap = isl_union_map_read_from_str(ctx, str);
1800         sv = isl_union_map_is_single_valued(umap);
1801         isl_union_map_free(umap);
1802         if (sv < 0)
1803                 return -1;
1804         if (sv)
1805                 isl_die(ctx, isl_error_internal,
1806                         "map detected as single valued", return -1);
1807
1808         return 0;
1809 }
1810
1811 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1812 {
1813         isl_map *map;
1814
1815         map = isl_map_read_from_str(ctx, str);
1816         if (bijective)
1817                 assert(isl_map_is_bijective(map));
1818         else
1819                 assert(!isl_map_is_bijective(map));
1820         isl_map_free(map);
1821 }
1822
1823 void test_bijective(struct isl_ctx *ctx)
1824 {
1825         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1826         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1827         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1828         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1829         test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1830         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1831         test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1832         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1833         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1834         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1835         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1836         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1837         test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1838 }
1839
1840 void test_pwqp(struct isl_ctx *ctx)
1841 {
1842         const char *str;
1843         isl_set *set;
1844         isl_pw_qpolynomial *pwqp1, *pwqp2;
1845
1846         str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1847         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1848
1849         pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1850                                                 isl_dim_in, 1, 1);
1851
1852         str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1853         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1854
1855         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1856
1857         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1858
1859         isl_pw_qpolynomial_free(pwqp1);
1860
1861         str = "{ [i] -> i }";
1862         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1863         str = "{ [k] : exists a : k = 2a }";
1864         set = isl_set_read_from_str(ctx, str);
1865         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1866         str = "{ [i] -> i }";
1867         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1868
1869         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1870
1871         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1872
1873         isl_pw_qpolynomial_free(pwqp1);
1874
1875         str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1876         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1877         str = "{ [10] }";
1878         set = isl_set_read_from_str(ctx, str);
1879         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1880         str = "{ [i] -> 16 }";
1881         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1882
1883         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1884
1885         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1886
1887         isl_pw_qpolynomial_free(pwqp1);
1888
1889         str = "{ [i] -> ([(i)/2]) }";
1890         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1891         str = "{ [k] : exists a : k = 2a+1 }";
1892         set = isl_set_read_from_str(ctx, str);
1893         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1894         str = "{ [i] -> -1/2 + 1/2 * i }";
1895         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1896
1897         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1898
1899         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1900
1901         isl_pw_qpolynomial_free(pwqp1);
1902
1903         str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1904         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1905         str = "{ [i] -> ([(2 * [i/2])/5]) }";
1906         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1907
1908         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1909
1910         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1911
1912         isl_pw_qpolynomial_free(pwqp1);
1913
1914         str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1915         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1916         str = "{ [x] -> x }";
1917         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1918
1919         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1920
1921         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1922
1923         isl_pw_qpolynomial_free(pwqp1);
1924
1925         str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1926         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1927         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1928         pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1929         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1930         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1931         isl_pw_qpolynomial_free(pwqp1);
1932 }
1933
1934 void test_split_periods(isl_ctx *ctx)
1935 {
1936         const char *str;
1937         isl_pw_qpolynomial *pwqp;
1938
1939         str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1940                 "U + 2V + 3 >= 0 and - U -2V  >= 0 and - U + 10 >= 0 and "
1941                 "U  >= 0; [U,V] -> U^2 : U >= 100 }";
1942         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1943
1944         pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1945         assert(pwqp);
1946
1947         isl_pw_qpolynomial_free(pwqp);
1948 }
1949
1950 void test_union(isl_ctx *ctx)
1951 {
1952         const char *str;
1953         isl_union_set *uset1, *uset2;
1954         isl_union_map *umap1, *umap2;
1955
1956         str = "{ [i] : 0 <= i <= 1 }";
1957         uset1 = isl_union_set_read_from_str(ctx, str);
1958         str = "{ [1] -> [0] }";
1959         umap1 = isl_union_map_read_from_str(ctx, str);
1960
1961         umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1962         assert(isl_union_map_is_equal(umap1, umap2));
1963
1964         isl_union_map_free(umap1);
1965         isl_union_map_free(umap2);
1966
1967         str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1968         umap1 = isl_union_map_read_from_str(ctx, str);
1969         str = "{ A[i]; B[i] }";
1970         uset1 = isl_union_set_read_from_str(ctx, str);
1971
1972         uset2 = isl_union_map_domain(umap1);
1973
1974         assert(isl_union_set_is_equal(uset1, uset2));
1975
1976         isl_union_set_free(uset1);
1977         isl_union_set_free(uset2);
1978 }
1979
1980 void test_bound(isl_ctx *ctx)
1981 {
1982         const char *str;
1983         isl_pw_qpolynomial *pwqp;
1984         isl_pw_qpolynomial_fold *pwf;
1985
1986         str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1987         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1988         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1989         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1990         isl_pw_qpolynomial_fold_free(pwf);
1991
1992         str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1993         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1994         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1995         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1996         isl_pw_qpolynomial_fold_free(pwf);
1997 }
1998
1999 void test_lift(isl_ctx *ctx)
2000 {
2001         const char *str;
2002         isl_basic_map *bmap;
2003         isl_basic_set *bset;
2004
2005         str = "{ [i0] : exists e0 : i0 = 4e0 }";
2006         bset = isl_basic_set_read_from_str(ctx, str);
2007         bset = isl_basic_set_lift(bset);
2008         bmap = isl_basic_map_from_range(bset);
2009         bset = isl_basic_map_domain(bmap);
2010         isl_basic_set_free(bset);
2011 }
2012
2013 struct {
2014         const char *set1;
2015         const char *set2;
2016         int subset;
2017 } subset_tests[] = {
2018         { "{ [112, 0] }",
2019           "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2020                 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2021                 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2022         { "{ [65] }",
2023           "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2024                 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2025                 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2026                     "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2027                     "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2028                     "256e0 <= 255i and 256e0 >= -255 + 255i and "
2029                     "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2030                     "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2031                     "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2032 };
2033
2034 static int test_subset(isl_ctx *ctx)
2035 {
2036         int i;
2037         isl_set *set1, *set2;
2038         int subset;
2039
2040         for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2041                 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2042                 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2043                 subset = isl_set_is_subset(set1, set2);
2044                 isl_set_free(set1);
2045                 isl_set_free(set2);
2046                 if (subset < 0)
2047                         return -1;
2048                 if (subset != subset_tests[i].subset)
2049                         isl_die(ctx, isl_error_unknown,
2050                                 "incorrect subset result", return -1);
2051         }
2052
2053         return 0;
2054 }
2055
2056 struct {
2057         const char *minuend;
2058         const char *subtrahend;
2059         const char *difference;
2060 } subtract_domain_tests[] = {
2061         { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2062         { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2063         { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2064 };
2065
2066 static int test_subtract(isl_ctx *ctx)
2067 {
2068         int i;
2069         isl_union_map *umap1, *umap2;
2070         isl_union_set *uset;
2071         int equal;
2072
2073         for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2074                 umap1 = isl_union_map_read_from_str(ctx,
2075                                 subtract_domain_tests[i].minuend);
2076                 uset = isl_union_set_read_from_str(ctx,
2077                                 subtract_domain_tests[i].subtrahend);
2078                 umap2 = isl_union_map_read_from_str(ctx,
2079                                 subtract_domain_tests[i].difference);
2080                 umap1 = isl_union_map_subtract_domain(umap1, uset);
2081                 equal = isl_union_map_is_equal(umap1, umap2);
2082                 isl_union_map_free(umap1);
2083                 isl_union_map_free(umap2);
2084                 if (equal < 0)
2085                         return -1;
2086                 if (!equal)
2087                         isl_die(ctx, isl_error_unknown,
2088                                 "incorrect subtract domain result", return -1);
2089         }
2090
2091         return 0;
2092 }
2093
2094 int test_factorize(isl_ctx *ctx)
2095 {
2096         const char *str;
2097         isl_basic_set *bset;
2098         isl_factorizer *f;
2099
2100         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2101             "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2102             "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2103             "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2104             "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2105             "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2106             "3i5 >= -2i0 - i2 + 3i4 }";
2107         bset = isl_basic_set_read_from_str(ctx, str);
2108         f = isl_basic_set_factorizer(bset);
2109         isl_basic_set_free(bset);
2110         isl_factorizer_free(f);
2111         if (!f)
2112                 isl_die(ctx, isl_error_unknown,
2113                         "failed to construct factorizer", return -1);
2114
2115         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2116             "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2117             "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2118             "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2119             "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2120             "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2121             "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2122             "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2123             "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2124             "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2125         bset = isl_basic_set_read_from_str(ctx, str);
2126         f = isl_basic_set_factorizer(bset);
2127         isl_basic_set_free(bset);
2128         isl_factorizer_free(f);
2129         if (!f)
2130                 isl_die(ctx, isl_error_unknown,
2131                         "failed to construct factorizer", return -1);
2132
2133         return 0;
2134 }
2135
2136 static int check_injective(__isl_take isl_map *map, void *user)
2137 {
2138         int *injective = user;
2139
2140         *injective = isl_map_is_injective(map);
2141         isl_map_free(map);
2142
2143         if (*injective < 0 || !*injective)
2144                 return -1;
2145
2146         return 0;
2147 }
2148
2149 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2150         const char *r, const char *s, int tilable, int parallel)
2151 {
2152         int i;
2153         isl_union_set *D;
2154         isl_union_map *W, *R, *S;
2155         isl_union_map *empty;
2156         isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2157         isl_union_map *validity, *proximity;
2158         isl_union_map *schedule;
2159         isl_union_map *test;
2160         isl_union_set *delta;
2161         isl_union_set *domain;
2162         isl_set *delta_set;
2163         isl_set *slice;
2164         isl_set *origin;
2165         isl_schedule *sched;
2166         int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2167
2168         D = isl_union_set_read_from_str(ctx, d);
2169         W = isl_union_map_read_from_str(ctx, w);
2170         R = isl_union_map_read_from_str(ctx, r);
2171         S = isl_union_map_read_from_str(ctx, s);
2172
2173         W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2174         R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2175
2176         empty = isl_union_map_empty(isl_union_map_get_space(S));
2177         isl_union_map_compute_flow(isl_union_map_copy(R),
2178                                    isl_union_map_copy(W), empty,
2179                                    isl_union_map_copy(S),
2180                                    &dep_raw, NULL, NULL, NULL);
2181         isl_union_map_compute_flow(isl_union_map_copy(W),
2182                                    isl_union_map_copy(W),
2183                                    isl_union_map_copy(R),
2184                                    isl_union_map_copy(S),
2185                                    &dep_waw, &dep_war, NULL, NULL);
2186
2187         dep = isl_union_map_union(dep_waw, dep_war);
2188         dep = isl_union_map_union(dep, dep_raw);
2189         validity = isl_union_map_copy(dep);
2190         proximity = isl_union_map_copy(dep);
2191
2192         sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
2193                                                validity, proximity);
2194         schedule = isl_schedule_get_map(sched);
2195         isl_schedule_free(sched);
2196         isl_union_map_free(W);
2197         isl_union_map_free(R);
2198         isl_union_map_free(S);
2199
2200         is_injection = 1;
2201         isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2202
2203         domain = isl_union_map_domain(isl_union_map_copy(schedule));
2204         is_complete = isl_union_set_is_subset(D, domain);
2205         isl_union_set_free(D);
2206         isl_union_set_free(domain);
2207
2208         test = isl_union_map_reverse(isl_union_map_copy(schedule));
2209         test = isl_union_map_apply_range(test, dep);
2210         test = isl_union_map_apply_range(test, schedule);
2211
2212         delta = isl_union_map_deltas(test);
2213         if (isl_union_set_n_set(delta) == 0) {
2214                 is_tilable = 1;
2215                 is_parallel = 1;
2216                 is_nonneg = 1;
2217                 isl_union_set_free(delta);
2218         } else {
2219                 delta_set = isl_set_from_union_set(delta);
2220
2221                 slice = isl_set_universe(isl_set_get_space(delta_set));
2222                 for (i = 0; i < tilable; ++i)
2223                         slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2224                 is_tilable = isl_set_is_subset(delta_set, slice);
2225                 isl_set_free(slice);
2226
2227                 slice = isl_set_universe(isl_set_get_space(delta_set));
2228                 for (i = 0; i < parallel; ++i)
2229                         slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2230                 is_parallel = isl_set_is_subset(delta_set, slice);
2231                 isl_set_free(slice);
2232
2233                 origin = isl_set_universe(isl_set_get_space(delta_set));
2234                 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2235                         origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2236
2237                 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2238                 delta_set = isl_set_lexmin(delta_set);
2239
2240                 is_nonneg = isl_set_is_equal(delta_set, origin);
2241
2242                 isl_set_free(origin);
2243                 isl_set_free(delta_set);
2244         }
2245
2246         if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2247             is_injection < 0 || is_complete < 0)
2248                 return -1;
2249         if (!is_complete)
2250                 isl_die(ctx, isl_error_unknown,
2251                         "generated schedule incomplete", return -1);
2252         if (!is_injection)
2253                 isl_die(ctx, isl_error_unknown,
2254                         "generated schedule not injective on each statement",
2255                         return -1);
2256         if (!is_nonneg)
2257                 isl_die(ctx, isl_error_unknown,
2258                         "negative dependences in generated schedule",
2259                         return -1);
2260         if (!is_tilable)
2261                 isl_die(ctx, isl_error_unknown,
2262                         "generated schedule not as tilable as expected",
2263                         return -1);
2264         if (!is_parallel)
2265                 isl_die(ctx, isl_error_unknown,
2266                         "generated schedule not as parallel as expected",
2267                         return -1);
2268
2269         return 0;
2270 }
2271
2272 int test_special_schedule(isl_ctx *ctx, const char *domain,
2273         const char *validity, const char *proximity, const char *expected_sched)
2274 {
2275         isl_union_set *dom;
2276         isl_union_map *dep;
2277         isl_union_map *prox;
2278         isl_union_map *sched1, *sched2;
2279         isl_schedule *schedule;
2280         int equal;
2281
2282         dom = isl_union_set_read_from_str(ctx, domain);
2283         dep = isl_union_map_read_from_str(ctx, validity);
2284         prox = isl_union_map_read_from_str(ctx, proximity);
2285         schedule = isl_union_set_compute_schedule(dom, dep, prox);
2286         sched1 = isl_schedule_get_map(schedule);
2287         isl_schedule_free(schedule);
2288
2289         sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2290
2291         equal = isl_union_map_is_equal(sched1, sched2);
2292         isl_union_map_free(sched1);
2293         isl_union_map_free(sched2);
2294
2295         if (equal < 0)
2296                 return -1;
2297         if (!equal)
2298                 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2299                         return -1);
2300
2301         return 0;
2302 }
2303
2304 int test_schedule(isl_ctx *ctx)
2305 {
2306         const char *D, *W, *R, *V, *P, *S;
2307
2308         /* Jacobi */
2309         D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2310         W = "{ S1[t,i] -> a[t,i] }";
2311         R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2312                 "S1[t,i] -> a[t-1,i+1] }";
2313         S = "{ S1[t,i] -> [t,i] }";
2314         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2315                 return -1;
2316
2317         /* Fig. 5 of CC2008 */
2318         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2319                                 "j <= -1 + N }";
2320         W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2321                                 "j >= 2 and j <= -1 + N }";
2322         R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2323                                 "j >= 2 and j <= -1 + N; "
2324                     "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2325                                 "j >= 2 and j <= -1 + N }";
2326         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2327         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2328                 return -1;
2329
2330         D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2331         W = "{ S1[i] -> a[i] }";
2332         R = "{ S2[i] -> a[i+1] }";
2333         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2334         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2335                 return -1;
2336
2337         D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2338         W = "{ S1[i] -> a[i] }";
2339         R = "{ S2[i] -> a[9-i] }";
2340         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2341         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2342                 return -1;
2343
2344         D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2345         W = "{ S1[i] -> a[i] }";
2346         R = "[N] -> { S2[i] -> a[N-1-i] }";
2347         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2348         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2349                 return -1;
2350         
2351         D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2352         W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2353         R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2354         S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2355         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2356                 return -1;
2357
2358         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2359         W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2360         R = "{ S2[i,j] -> a[i-1,j] }";
2361         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2362         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2363                 return -1;
2364
2365         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2366         W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2367         R = "{ S2[i,j] -> a[i,j-1] }";
2368         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2369         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2370                 return -1;
2371
2372         D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2373         W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2374                     "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2375         R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2376         S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2377                     "S_0[] -> [0, 0, 0] }";
2378         if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2379                 return -1;
2380         ctx->opt->schedule_parametric = 0;
2381         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2382                 return -1;
2383         ctx->opt->schedule_parametric = 1;
2384
2385         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2386                     "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2387         W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2388         R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2389                     "S4[i] -> a[i,N] }";
2390         S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2391                 "S4[i] -> [4,i,0] }";
2392         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2393                 return -1;
2394
2395         D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2396         W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2397                                         "j <= N }";
2398         R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2399                                         "j <= N; "
2400                     "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2401                                         "j <= N }";
2402         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2403         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2404                 return -1;
2405
2406         D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2407                     " S_2[t] : t >= 0 and t <= -1 + N; "
2408                     " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2409                                 "i <= -1 + N }";
2410         W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2411                     " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2412                     " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2413                                                 "i >= 0 and i <= -1 + N }";
2414         R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2415                                             "i >= 0 and i <= -1 + N; "
2416                     " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2417         S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2418                     " S_0[t] -> [0, t, 0] }";
2419
2420         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2421                 return -1;
2422         ctx->opt->schedule_parametric = 0;
2423         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2424                 return -1;
2425         ctx->opt->schedule_parametric = 1;
2426
2427         D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2428         S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2429         if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2430                 return -1;
2431
2432         D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2433             "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2434         W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2435                                             "j >= 0 and j <= -1 + N; "
2436                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2437         R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2438                                             "j >= 0 and j <= -1 + N; "
2439                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2440         S = "[M, N] -> { S_1[i] -> [1, i, 0]; 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 = "{ S_0[i] : i >= 0 }";
2445         W = "{ S_0[i] -> a[i] : i >= 0 }";
2446         R = "{ S_0[i] -> a[0] : i >= 0 }";
2447         S = "{ S_0[i] -> [0, i, 0] }";
2448         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2449                 return -1;
2450
2451         D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2452         W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2453         R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2454         S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2455         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2456                 return -1;
2457
2458         D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2459                                 "k <= -1 + n and k >= 0 }";
2460         W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "                                                  "k <= -1 + n and k >= 0 }";
2461         R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2462                                         "k <= -1 + n and k >= 0; "
2463                     "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2464                                         "k <= -1 + n and k >= 0; "
2465                     "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2466                                         "k <= -1 + n and k >= 0 }";
2467         S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2468         ctx->opt->schedule_outer_zero_distance = 1;
2469         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2470                 return -1;
2471         ctx->opt->schedule_outer_zero_distance = 0;
2472
2473         D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2474                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2475                 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2476              "Stmt_for_body24[i0, i1, 1, 0]:"
2477                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2478              "Stmt_for_body7[i0, i1, i2]:"
2479                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2480                 "i2 <= 7 }";
2481
2482         V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2483                 "Stmt_for_body24[1, i1, i2, i3]:"
2484                 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2485                 "i2 >= 1;"
2486              "Stmt_for_body24[0, i1, i2, i3] -> "
2487                 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2488                 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2489                 "i3 >= 0;"
2490               "Stmt_for_body24[0, i1, i2, i3] ->"
2491                 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2492                 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2493               "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2494                 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2495                 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2496                 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2497               "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2498                 "i1 <= 6 and i1 >= 0;"
2499               "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2500               "Stmt_for_body7[i0, i1, i2] -> "
2501                 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2502                 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2503                 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2504               "Stmt_for_body7[i0, i1, i2] -> "
2505                 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2506                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2507                 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2508         P = V;
2509         S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2510                 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2511             "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2512
2513         if (test_special_schedule(ctx, D, V, P, S) < 0)
2514                 return -1;
2515
2516         D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2517         V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2518                                            "j >= 1 and j <= 7;"
2519                 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2520                                              "j >= 1 and j <= 8 }";
2521         P = "{ }";
2522         S = "{ S_0[i, j] -> [i + j, j] }";
2523         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2524         if (test_special_schedule(ctx, D, V, P, S) < 0)
2525                 return -1;
2526         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2527
2528         /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2529         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2530                                  "j >= 0 and j <= -1 + i }";
2531         V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2532                                         "i <= -1 + N and j >= 0;"
2533                      "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2534                                         "i <= -2 + N }";
2535         P = "{ }";
2536         S = "{ S_0[i, j] -> [i, j] }";
2537         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2538         if (test_special_schedule(ctx, D, V, P, S) < 0)
2539                 return -1;
2540         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2541
2542         /* Test both algorithms on a case with only proximity dependences. */
2543         D = "{ S[i,j] : 0 <= i <= 10 }";
2544         V = "{ }";
2545         P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2546         S = "{ S[i, j] -> [j, i] }";
2547         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2548         if (test_special_schedule(ctx, D, V, P, S) < 0)
2549                 return -1;
2550         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2551         return test_special_schedule(ctx, D, V, P, S);
2552 }
2553
2554 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2555 {
2556         isl_union_map *umap;
2557         int test;
2558
2559         umap = isl_union_map_read_from_str(ctx, str);
2560         test = isl_union_map_plain_is_injective(umap);
2561         isl_union_map_free(umap);
2562         if (test < 0)
2563                 return -1;
2564         if (test == injective)
2565                 return 0;
2566         if (injective)
2567                 isl_die(ctx, isl_error_unknown,
2568                         "map not detected as injective", return -1);
2569         else
2570                 isl_die(ctx, isl_error_unknown,
2571                         "map detected as injective", return -1);
2572 }
2573
2574 int test_injective(isl_ctx *ctx)
2575 {
2576         const char *str;
2577
2578         if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2579                 return -1;
2580         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2581                 return -1;
2582         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2583                 return -1;
2584         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2585                 return -1;
2586         if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2587                 return -1;
2588         if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2589                 return -1;
2590         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2591                 return -1;
2592         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2593                 return -1;
2594
2595         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2596         if (test_plain_injective(ctx, str, 1))
2597                 return -1;
2598         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2599         if (test_plain_injective(ctx, str, 0))
2600                 return -1;
2601
2602         return 0;
2603 }
2604
2605 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
2606 {
2607         isl_aff *aff2;
2608         int equal;
2609
2610         if (!aff)
2611                 return -1;
2612
2613         aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
2614         equal = isl_aff_plain_is_equal(aff, aff2);
2615         isl_aff_free(aff2);
2616
2617         return equal;
2618 }
2619
2620 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
2621 {
2622         int equal;
2623
2624         equal = aff_plain_is_equal(aff, str);
2625         if (equal < 0)
2626                 return -1;
2627         if (!equal)
2628                 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
2629                         "result not as expected", return -1);
2630         return 0;
2631 }
2632
2633 int test_aff(isl_ctx *ctx)
2634 {
2635         const char *str;
2636         isl_set *set;
2637         isl_space *space;
2638         isl_local_space *ls;
2639         isl_aff *aff;
2640         int zero, equal;
2641
2642         space = isl_space_set_alloc(ctx, 0, 1);
2643         ls = isl_local_space_from_space(space);
2644         aff = isl_aff_zero_on_domain(ls);
2645
2646         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2647         aff = isl_aff_scale_down_ui(aff, 3);
2648         aff = isl_aff_floor(aff);
2649         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2650         aff = isl_aff_scale_down_ui(aff, 2);
2651         aff = isl_aff_floor(aff);
2652         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2653
2654         str = "{ [10] }";
2655         set = isl_set_read_from_str(ctx, str);
2656         aff = isl_aff_gist(aff, set);
2657
2658         aff = isl_aff_add_constant_si(aff, -16);
2659         zero = isl_aff_plain_is_zero(aff);
2660         isl_aff_free(aff);
2661
2662         if (zero < 0)
2663                 return -1;
2664         if (!zero)
2665                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2666
2667         aff = isl_aff_read_from_str(ctx, "{ [-1] }");
2668         aff = isl_aff_scale_down_ui(aff, 64);
2669         aff = isl_aff_floor(aff);
2670         equal = aff_check_plain_equal(aff, "{ [-1] }");
2671         isl_aff_free(aff);
2672         if (equal < 0)
2673                 return -1;
2674
2675         return 0;
2676 }
2677
2678 int test_dim_max(isl_ctx *ctx)
2679 {
2680         int equal;
2681         const char *str;
2682         isl_set *set1, *set2;
2683         isl_set *set;
2684         isl_map *map;
2685         isl_pw_aff *pwaff;
2686
2687         str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2688         set = isl_set_read_from_str(ctx, str);
2689         pwaff = isl_set_dim_max(set, 0);
2690         set1 = isl_set_from_pw_aff(pwaff);
2691         str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2692         set2 = isl_set_read_from_str(ctx, str);
2693         equal = isl_set_is_equal(set1, set2);
2694         isl_set_free(set1);
2695         isl_set_free(set2);
2696         if (equal < 0)
2697                 return -1;
2698         if (!equal)
2699                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2700
2701         str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2702         set = isl_set_read_from_str(ctx, str);
2703         pwaff = isl_set_dim_max(set, 0);
2704         set1 = isl_set_from_pw_aff(pwaff);
2705         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2706         set2 = isl_set_read_from_str(ctx, str);
2707         equal = isl_set_is_equal(set1, set2);
2708         isl_set_free(set1);
2709         isl_set_free(set2);
2710         if (equal < 0)
2711                 return -1;
2712         if (!equal)
2713                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2714
2715         str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2716         set = isl_set_read_from_str(ctx, str);
2717         pwaff = isl_set_dim_max(set, 0);
2718         set1 = isl_set_from_pw_aff(pwaff);
2719         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2720         set2 = isl_set_read_from_str(ctx, str);
2721         equal = isl_set_is_equal(set1, set2);
2722         isl_set_free(set1);
2723         isl_set_free(set2);
2724         if (equal < 0)
2725                 return -1;
2726         if (!equal)
2727                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2728
2729         str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2730                         "0 <= i < N and 0 <= j < M }";
2731         map = isl_map_read_from_str(ctx, str);
2732         set = isl_map_range(map);
2733
2734         pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2735         set1 = isl_set_from_pw_aff(pwaff);
2736         str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2737         set2 = isl_set_read_from_str(ctx, str);
2738         equal = isl_set_is_equal(set1, set2);
2739         isl_set_free(set1);
2740         isl_set_free(set2);
2741
2742         pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2743         set1 = isl_set_from_pw_aff(pwaff);
2744         str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2745         set2 = isl_set_read_from_str(ctx, str);
2746         if (equal >= 0 && equal)
2747                 equal = isl_set_is_equal(set1, set2);
2748         isl_set_free(set1);
2749         isl_set_free(set2);
2750
2751         isl_set_free(set);
2752
2753         if (equal < 0)
2754                 return -1;
2755         if (!equal)
2756                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2757
2758         return 0;
2759 }
2760
2761 int test_product(isl_ctx *ctx)
2762 {
2763         const char *str;
2764         isl_set *set;
2765         isl_union_set *uset1, *uset2;
2766         int ok;
2767
2768         str = "{ A[i] }";
2769         set = isl_set_read_from_str(ctx, str);
2770         set = isl_set_product(set, isl_set_copy(set));
2771         ok = isl_set_is_wrapping(set);
2772         isl_set_free(set);
2773         if (ok < 0)
2774                 return -1;
2775         if (!ok)
2776                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2777
2778         str = "{ [] }";
2779         uset1 = isl_union_set_read_from_str(ctx, str);
2780         uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
2781         str = "{ [[] -> []] }";
2782         uset2 = isl_union_set_read_from_str(ctx, str);
2783         ok = isl_union_set_is_equal(uset1, uset2);
2784         isl_union_set_free(uset1);
2785         isl_union_set_free(uset2);
2786         if (ok < 0)
2787                 return -1;
2788         if (!ok)
2789                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2790
2791         return 0;
2792 }
2793
2794 int test_equal(isl_ctx *ctx)
2795 {
2796         const char *str;
2797         isl_set *set, *set2;
2798         int equal;
2799
2800         str = "{ S_6[i] }";
2801         set = isl_set_read_from_str(ctx, str);
2802         str = "{ S_7[i] }";
2803         set2 = isl_set_read_from_str(ctx, str);
2804         equal = isl_set_is_equal(set, set2);
2805         isl_set_free(set);
2806         isl_set_free(set2);
2807         if (equal < 0)
2808                 return -1;
2809         if (equal)
2810                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2811
2812         return 0;
2813 }
2814
2815 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2816         enum isl_dim_type type, unsigned pos, int fixed)
2817 {
2818         int test;
2819
2820         test = isl_map_plain_is_fixed(map, type, pos, NULL);
2821         isl_map_free(map);
2822         if (test < 0)
2823                 return -1;
2824         if (test == fixed)
2825                 return 0;
2826         if (fixed)
2827                 isl_die(ctx, isl_error_unknown,
2828                         "map not detected as fixed", return -1);
2829         else
2830                 isl_die(ctx, isl_error_unknown,
2831                         "map detected as fixed", return -1);
2832 }
2833
2834 int test_fixed(isl_ctx *ctx)
2835 {
2836         const char *str;
2837         isl_map *map;
2838
2839         str = "{ [i] -> [i] }";
2840         map = isl_map_read_from_str(ctx, str);
2841         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2842                 return -1;
2843         str = "{ [i] -> [1] }";
2844         map = isl_map_read_from_str(ctx, str);
2845         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2846                 return -1;
2847         str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2848         map = isl_map_read_from_str(ctx, str);
2849         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2850                 return -1;
2851         map = isl_map_read_from_str(ctx, str);
2852         map = isl_map_neg(map);
2853         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2854                 return -1;
2855
2856         return 0;
2857 }
2858
2859 int test_vertices(isl_ctx *ctx)
2860 {
2861         const char *str;
2862         isl_basic_set *bset;
2863         isl_vertices *vertices;
2864
2865         str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
2866         bset = isl_basic_set_read_from_str(ctx, str);
2867         vertices = isl_basic_set_compute_vertices(bset);
2868         isl_basic_set_free(bset);
2869         isl_vertices_free(vertices);
2870         if (!vertices)
2871                 return -1;
2872
2873         str = "{ A[t, i] : t = 14 and i = 1 }";
2874         bset = isl_basic_set_read_from_str(ctx, str);
2875         vertices = isl_basic_set_compute_vertices(bset);
2876         isl_basic_set_free(bset);
2877         isl_vertices_free(vertices);
2878         if (!vertices)
2879                 return -1;
2880
2881         return 0;
2882 }
2883
2884 int test_union_pw(isl_ctx *ctx)
2885 {
2886         int equal;
2887         const char *str;
2888         isl_union_set *uset;
2889         isl_union_pw_qpolynomial *upwqp1, *upwqp2;
2890
2891         str = "{ [x] -> x^2 }";
2892         upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
2893         upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
2894         uset = isl_union_pw_qpolynomial_domain(upwqp1);
2895         upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
2896         upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
2897         equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
2898         isl_union_pw_qpolynomial_free(upwqp1);
2899         isl_union_pw_qpolynomial_free(upwqp2);
2900         if (equal < 0)
2901                 return -1;
2902         if (!equal)
2903                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2904
2905         return 0;
2906 }
2907
2908 int test_output(isl_ctx *ctx)
2909 {
2910         char *s;
2911         const char *str;
2912         isl_pw_aff *pa;
2913         isl_printer *p;
2914         int equal;
2915
2916         str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
2917         pa = isl_pw_aff_read_from_str(ctx, str);
2918
2919         p = isl_printer_to_str(ctx);
2920         p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2921         p = isl_printer_print_pw_aff(p, pa);
2922         s = isl_printer_get_str(p);
2923         isl_printer_free(p);
2924         isl_pw_aff_free(pa);
2925         equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
2926         free(s);
2927         if (equal < 0)
2928                 return -1;
2929         if (!equal)
2930                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2931
2932         return 0;
2933 }
2934
2935 int test_sample(isl_ctx *ctx)
2936 {
2937         const char *str;
2938         isl_basic_set *bset1, *bset2;
2939         int empty, subset;
2940
2941         str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
2942             "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
2943             "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
2944             "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
2945             "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
2946             "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
2947             "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
2948             "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
2949             "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
2950             "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
2951             "3i >= 1 + 2b - 2c + e + 2f + 3g and "
2952             "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
2953                 "d - 1073741821e and "
2954             "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
2955             "3j >= 1 - a + b + 2e and "
2956             "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
2957             "3i <= 4 - a + 4b - e and "
2958             "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
2959             "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
2960             "c <= -1 + a and 3i >= -2 - a + 3e and "
2961             "1073741822e <= 1073741823 - a + 1073741822b + c and "
2962             "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
2963             "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
2964             "1073741823e >= 1 + 1073741823b - d and "
2965             "3i >= 1073741823b + c - 1073741823e - f and "
2966             "3i >= 1 + 2b + e + 3g }";
2967         bset1 = isl_basic_set_read_from_str(ctx, str);
2968         bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
2969         empty = isl_basic_set_is_empty(bset2);
2970         subset = isl_basic_set_is_subset(bset2, bset1);
2971         isl_basic_set_free(bset1);
2972         isl_basic_set_free(bset2);
2973         if (empty)
2974                 isl_die(ctx, isl_error_unknown, "point not found", return -1);
2975         if (!subset)
2976                 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
2977
2978         return 0;
2979 }
2980
2981 int test_fixed_power(isl_ctx *ctx)
2982 {
2983         const char *str;
2984         isl_map *map;
2985         isl_int exp;
2986         int equal;
2987
2988         isl_int_init(exp);
2989         str = "{ [i] -> [i + 1] }";
2990         map = isl_map_read_from_str(ctx, str);
2991         isl_int_set_si(exp, 23);
2992         map = isl_map_fixed_power(map, exp);
2993         equal = map_check_equal(map, "{ [i] -> [i + 23] }");
2994         isl_int_clear(exp);
2995         isl_map_free(map);
2996         if (equal < 0)
2997                 return -1;
2998
2999         return 0;
3000 }
3001
3002 int test_slice(isl_ctx *ctx)
3003 {
3004         const char *str;
3005         isl_map *map;
3006         int equal;
3007
3008         str = "{ [i] -> [j] }";
3009         map = isl_map_read_from_str(ctx, str);
3010         map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3011         equal = map_check_equal(map, "{ [i] -> [i] }");
3012         isl_map_free(map);
3013         if (equal < 0)
3014                 return -1;
3015
3016         str = "{ [i] -> [j] }";
3017         map = isl_map_read_from_str(ctx, str);
3018         map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3019         equal = map_check_equal(map, "{ [i] -> [j] }");
3020         isl_map_free(map);
3021         if (equal < 0)
3022                 return -1;
3023
3024         str = "{ [i] -> [j] }";
3025         map = isl_map_read_from_str(ctx, str);
3026         map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3027         equal = map_check_equal(map, "{ [i] -> [-i] }");
3028         isl_map_free(map);
3029         if (equal < 0)
3030                 return -1;
3031
3032         str = "{ [i] -> [j] }";
3033         map = isl_map_read_from_str(ctx, str);
3034         map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3035         equal = map_check_equal(map, "{ [0] -> [j] }");
3036         isl_map_free(map);
3037         if (equal < 0)
3038                 return -1;
3039
3040         str = "{ [i] -> [j] }";
3041         map = isl_map_read_from_str(ctx, str);
3042         map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3043         equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3044         isl_map_free(map);
3045         if (equal < 0)
3046                 return -1;
3047
3048         str = "{ [i] -> [j] }";
3049         map = isl_map_read_from_str(ctx, str);
3050         map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3051         equal = map_check_equal(map, "{ [i] -> [j] : false }");
3052         isl_map_free(map);
3053         if (equal < 0)
3054                 return -1;
3055
3056         return 0;
3057 }
3058
3059 int test_eliminate(isl_ctx *ctx)
3060 {
3061         const char *str;
3062         isl_map *map;
3063         int equal;
3064
3065         str = "{ [i] -> [j] : i = 2j }";
3066         map = isl_map_read_from_str(ctx, str);
3067         map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3068         equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3069         isl_map_free(map);
3070         if (equal < 0)
3071                 return -1;
3072
3073         return 0;
3074 }
3075
3076 /* Check that isl_set_dim_residue_class detects that the values of j
3077  * in the set below are all odd and that it does not detect any spurious
3078  * strides.
3079  */
3080 static int test_residue_class(isl_ctx *ctx)
3081 {
3082         const char *str;
3083         isl_set *set;
3084         isl_int m, r;
3085         int res;
3086
3087         str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3088                 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3089         set = isl_set_read_from_str(ctx, str);
3090         isl_int_init(m);
3091         isl_int_init(r);
3092         res = isl_set_dim_residue_class(set, 1, &m, &r);
3093         if (res >= 0 &&
3094             (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3095                 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3096                         res = -1);
3097         isl_int_clear(r);
3098         isl_int_clear(m);
3099         isl_set_free(set);
3100
3101         return res;
3102 }
3103
3104 int test_align_parameters(isl_ctx *ctx)
3105 {
3106         const char *str;
3107         isl_space *space;
3108         isl_multi_aff *ma1, *ma2;
3109         int equal;
3110
3111         str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3112         ma1 = isl_multi_aff_read_from_str(ctx, str);
3113
3114         space = isl_space_params_alloc(ctx, 1);
3115         space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3116         ma1 = isl_multi_aff_align_params(ma1, space);
3117
3118         str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3119         ma2 = isl_multi_aff_read_from_str(ctx, str);
3120
3121         equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3122
3123         isl_multi_aff_free(ma1);
3124         isl_multi_aff_free(ma2);
3125
3126         if (equal < 0)
3127                 return -1;
3128         if (!equal)
3129                 isl_die(ctx, isl_error_unknown,
3130                         "result not as expected", return -1);
3131
3132         return 0;
3133 }
3134
3135 static int test_list(isl_ctx *ctx)
3136 {
3137         isl_id *a, *b, *c, *d, *id;
3138         isl_id_list *list;
3139         int ok;
3140
3141         a = isl_id_alloc(ctx, "a", NULL);
3142         b = isl_id_alloc(ctx, "b", NULL);
3143         c = isl_id_alloc(ctx, "c", NULL);
3144         d = isl_id_alloc(ctx, "d", NULL);
3145
3146         list = isl_id_list_alloc(ctx, 4);
3147         list = isl_id_list_add(list, a);
3148         list = isl_id_list_add(list, b);
3149         list = isl_id_list_add(list, c);
3150         list = isl_id_list_add(list, d);
3151         list = isl_id_list_drop(list, 1, 1);
3152
3153         if (isl_id_list_n_id(list) != 3) {
3154                 isl_id_list_free(list);
3155                 isl_die(ctx, isl_error_unknown,
3156                         "unexpected number of elements in list", return -1);
3157         }
3158
3159         id = isl_id_list_get_id(list, 0);
3160         ok = id == a;
3161         isl_id_free(id);
3162         id = isl_id_list_get_id(list, 1);
3163         ok = ok && id == c;
3164         isl_id_free(id);
3165         id = isl_id_list_get_id(list, 2);
3166         ok = ok && id == d;
3167         isl_id_free(id);
3168
3169         isl_id_list_free(list);
3170
3171         if (!ok)
3172                 isl_die(ctx, isl_error_unknown,
3173                         "unexpected elements in list", return -1);
3174
3175         return 0;
3176 }
3177
3178 struct {
3179         const char *name;
3180         int (*fn)(isl_ctx *ctx);
3181 } tests [] = {
3182         { "list", &test_list },
3183         { "align parameters", &test_align_parameters },
3184         { "eliminate", &test_eliminate },
3185         { "reisdue class", &test_residue_class },
3186         { "div", &test_div },
3187         { "slice", &test_slice },
3188         { "fixed power", &test_fixed_power },
3189         { "sample", &test_sample },
3190         { "output", &test_output },
3191         { "vertices", &test_vertices },
3192         { "fixed", &test_fixed },
3193         { "equal", &test_equal },
3194         { "product", &test_product },
3195         { "dim_max", &test_dim_max },
3196         { "affine", &test_aff },
3197         { "injective", &test_injective },
3198         { "schedule", &test_schedule },
3199         { "union_pw", &test_union_pw },
3200         { "parse", &test_parse },
3201         { "single-valued", &test_sv },
3202         { "affine hull", &test_affine_hull },
3203         { "coalesce", &test_coalesce },
3204         { "factorize", &test_factorize },
3205         { "subset", &test_subset },
3206         { "subtract", &test_subtract },
3207 };
3208
3209 int main()
3210 {
3211         int i;
3212         struct isl_ctx *ctx;
3213
3214         srcdir = getenv("srcdir");
3215         assert(srcdir);
3216
3217         ctx = isl_ctx_alloc();
3218         for (i = 0; i < ARRAY_SIZE(tests); ++i) {
3219                 printf("%s\n", tests[i].name);
3220                 if (tests[i].fn(ctx) < 0)
3221                         goto error;
3222         }
3223         test_lift(ctx);
3224         test_bound(ctx);
3225         test_union(ctx);
3226         test_split_periods(ctx);
3227         test_pwqp(ctx);
3228         test_lex(ctx);
3229         test_bijective(ctx);
3230         test_dep(ctx);
3231         test_read(ctx);
3232         test_bounded(ctx);
3233         test_construction(ctx);
3234         test_dim(ctx);
3235         test_application(ctx);
3236         test_convex_hull(ctx);
3237         test_gist(ctx);
3238         test_closure(ctx);
3239         test_lexmin(ctx);
3240         isl_ctx_free(ctx);
3241         return 0;
3242 error:
3243         isl_ctx_free(ctx);
3244         return -1;
3245 }