bff9ea924578316df0e7dff28d65cad1f37065f2
[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 GNU LGPLv2.1 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.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
24 static char *srcdir;
25
26 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
27         char *filename;
28         int length;
29         char *pattern = "%s/test_inputs/%s.%s";
30
31         length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
32                 + strlen(suffix) + 1;
33         filename = isl_alloc_array(ctx, char, length);
34
35         if (!filename)
36                 return NULL;
37
38         sprintf(filename, pattern, srcdir, name, suffix);
39
40         return filename;
41 }
42
43 void test_parse_map(isl_ctx *ctx, const char *str)
44 {
45         isl_map *map;
46
47         map = isl_map_read_from_str(ctx, str);
48         assert(map);
49         isl_map_free(map);
50 }
51
52 void test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
53 {
54         isl_map *map, *map2;
55
56         map = isl_map_read_from_str(ctx, str);
57         map2 = isl_map_read_from_str(ctx, str2);
58         assert(map && map2 && isl_map_is_equal(map, map2));
59         isl_map_free(map);
60         isl_map_free(map2);
61 }
62
63 void test_parse_pwqp(isl_ctx *ctx, const char *str)
64 {
65         isl_pw_qpolynomial *pwqp;
66
67         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
68         assert(pwqp);
69         isl_pw_qpolynomial_free(pwqp);
70 }
71
72 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
73 {
74         isl_pw_aff *pwaff;
75
76         pwaff = isl_pw_aff_read_from_str(ctx, str);
77         assert(pwaff);
78         isl_pw_aff_free(pwaff);
79 }
80
81 void test_parse(struct isl_ctx *ctx)
82 {
83         isl_map *map, *map2;
84         const char *str, *str2;
85
86         str = "{ [i] -> [-i] }";
87         map = isl_map_read_from_str(ctx, str);
88         assert(map);
89         isl_map_free(map);
90
91         str = "{ A[i] -> L[([i/3])] }";
92         map = isl_map_read_from_str(ctx, str);
93         assert(map);
94         isl_map_free(map);
95
96         test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
97         test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
98                                 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
99
100         str = "{ [x,y]  : [([x/2]+y)/3] >= 1 }";
101         str2 = "{ [x, y] : 2y >= 6 - x }";
102         test_parse_map_equal(ctx, str, str2);
103
104         test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
105                                   "{ [x,y] : x <= y, 2*y + 3 }");
106         str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
107         test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }", str);
108
109         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
110         map = isl_map_read_from_str(ctx, str);
111         str = "{ [new, old] -> [o0, o1] : "
112                "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
113                "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
114                "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
115         map2 = isl_map_read_from_str(ctx, str);
116         assert(isl_map_is_equal(map, map2));
117         isl_map_free(map);
118         isl_map_free(map2);
119
120         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
121         map = isl_map_read_from_str(ctx, str);
122         str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
123         map2 = isl_map_read_from_str(ctx, str);
124         assert(isl_map_is_equal(map, map2));
125         isl_map_free(map);
126         isl_map_free(map2);
127
128         str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
129         str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
130         test_parse_map_equal(ctx, str, str2);
131
132         str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
133         str2 = "{ [i,j] -> [min(i,j)] }";
134         test_parse_map_equal(ctx, str, str2);
135
136         str = "{ [i,j] : i != j }";
137         str2 = "{ [i,j] : i < j or i > j }";
138         test_parse_map_equal(ctx, str, str2);
139
140         str = "{ [i,j] : (i+1)*2 >= j }";
141         str2 = "{ [i, j] : j <= 2 + 2i }";
142         test_parse_map_equal(ctx, str, str2);
143
144         str = "{ [i] -> [i > 0 ? 4 : 5] }";
145         str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
146         test_parse_map_equal(ctx, str, str2);
147
148         str = "[N=2,M] -> { [i=[(M+N)/4]] }";
149         str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
150         test_parse_map_equal(ctx, str, str2);
151
152         str = "{ [x] : x >= 0 }";
153         str2 = "{ [x] : x-0 >= 0 }";
154         test_parse_map_equal(ctx, str, str2);
155
156         str = "{ [i] : ((i > 10)) }";
157         str2 = "{ [i] : i >= 11 }";
158         test_parse_map_equal(ctx, str, str2);
159
160         str = "{ [i] -> [0] }";
161         str2 = "{ [i] -> [0 * i] }";
162         test_parse_map_equal(ctx, str, str2);
163
164         test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
165         test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
166         test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
167 }
168
169 void test_read(struct isl_ctx *ctx)
170 {
171         char *filename;
172         FILE *input;
173         struct isl_basic_set *bset1, *bset2;
174         const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
175
176         filename = get_filename(ctx, "set", "omega");
177         assert(filename);
178         input = fopen(filename, "r");
179         assert(input);
180
181         bset1 = isl_basic_set_read_from_file(ctx, input);
182         bset2 = isl_basic_set_read_from_str(ctx, str);
183
184         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
185
186         isl_basic_set_free(bset1);
187         isl_basic_set_free(bset2);
188         free(filename);
189
190         fclose(input);
191 }
192
193 void test_bounded(struct isl_ctx *ctx)
194 {
195         isl_set *set;
196         int bounded;
197
198         set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
199         bounded = isl_set_is_bounded(set);
200         assert(bounded);
201         isl_set_free(set);
202
203         set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
204         bounded = isl_set_is_bounded(set);
205         assert(!bounded);
206         isl_set_free(set);
207
208         set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
209         bounded = isl_set_is_bounded(set);
210         assert(!bounded);
211         isl_set_free(set);
212 }
213
214 /* Construct the basic set { [i] : 5 <= i <= N } */
215 void test_construction(struct isl_ctx *ctx)
216 {
217         isl_int v;
218         isl_space *dim;
219         isl_local_space *ls;
220         struct isl_basic_set *bset;
221         struct isl_constraint *c;
222
223         isl_int_init(v);
224
225         dim = isl_space_set_alloc(ctx, 1, 1);
226         bset = isl_basic_set_universe(isl_space_copy(dim));
227         ls = isl_local_space_from_space(dim);
228
229         c = isl_inequality_alloc(isl_local_space_copy(ls));
230         isl_int_set_si(v, -1);
231         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
232         isl_int_set_si(v, 1);
233         isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
234         bset = isl_basic_set_add_constraint(bset, c);
235
236         c = isl_inequality_alloc(isl_local_space_copy(ls));
237         isl_int_set_si(v, 1);
238         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
239         isl_int_set_si(v, -5);
240         isl_constraint_set_constant(c, v);
241         bset = isl_basic_set_add_constraint(bset, c);
242
243         isl_local_space_free(ls);
244         isl_basic_set_free(bset);
245
246         isl_int_clear(v);
247 }
248
249 void test_dim(struct isl_ctx *ctx)
250 {
251         const char *str;
252         isl_map *map1, *map2;
253
254         map1 = isl_map_read_from_str(ctx,
255             "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
256         map1 = isl_map_add_dims(map1, isl_dim_in, 1);
257         map2 = isl_map_read_from_str(ctx,
258             "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
259         assert(isl_map_is_equal(map1, map2));
260         isl_map_free(map2);
261
262         map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
263         map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
264         assert(isl_map_is_equal(map1, map2));
265
266         isl_map_free(map1);
267         isl_map_free(map2);
268
269         str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
270         map1 = isl_map_read_from_str(ctx, str);
271         str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
272         map2 = isl_map_read_from_str(ctx, str);
273         map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
274         assert(isl_map_is_equal(map1, map2));
275
276         isl_map_free(map1);
277         isl_map_free(map2);
278 }
279
280 void test_div(struct isl_ctx *ctx)
281 {
282         isl_int v;
283         isl_space *dim;
284         isl_local_space *ls;
285         struct isl_basic_set *bset;
286         struct isl_constraint *c;
287
288         isl_int_init(v);
289
290         /* test 1 */
291         dim = isl_space_set_alloc(ctx, 0, 3);
292         bset = isl_basic_set_universe(isl_space_copy(dim));
293         ls = isl_local_space_from_space(dim);
294
295         c = isl_equality_alloc(isl_local_space_copy(ls));
296         isl_int_set_si(v, -1);
297         isl_constraint_set_constant(c, v);
298         isl_int_set_si(v, 1);
299         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
300         isl_int_set_si(v, 3);
301         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
302         bset = isl_basic_set_add_constraint(bset, c);
303
304         c = isl_equality_alloc(isl_local_space_copy(ls));
305         isl_int_set_si(v, 1);
306         isl_constraint_set_constant(c, v);
307         isl_int_set_si(v, -1);
308         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
309         isl_int_set_si(v, 3);
310         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
311         bset = isl_basic_set_add_constraint(bset, c);
312
313         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
314
315         assert(bset && bset->n_div == 1);
316         isl_local_space_free(ls);
317         isl_basic_set_free(bset);
318
319         /* test 2 */
320         dim = isl_space_set_alloc(ctx, 0, 3);
321         bset = isl_basic_set_universe(isl_space_copy(dim));
322         ls = isl_local_space_from_space(dim);
323
324         c = isl_equality_alloc(isl_local_space_copy(ls));
325         isl_int_set_si(v, 1);
326         isl_constraint_set_constant(c, v);
327         isl_int_set_si(v, -1);
328         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
329         isl_int_set_si(v, 3);
330         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
331         bset = isl_basic_set_add_constraint(bset, c);
332
333         c = isl_equality_alloc(isl_local_space_copy(ls));
334         isl_int_set_si(v, -1);
335         isl_constraint_set_constant(c, v);
336         isl_int_set_si(v, 1);
337         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
338         isl_int_set_si(v, 3);
339         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
340         bset = isl_basic_set_add_constraint(bset, c);
341
342         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
343
344         assert(bset && bset->n_div == 1);
345         isl_local_space_free(ls);
346         isl_basic_set_free(bset);
347
348         /* test 3 */
349         dim = isl_space_set_alloc(ctx, 0, 3);
350         bset = isl_basic_set_universe(isl_space_copy(dim));
351         ls = isl_local_space_from_space(dim);
352
353         c = isl_equality_alloc(isl_local_space_copy(ls));
354         isl_int_set_si(v, 1);
355         isl_constraint_set_constant(c, v);
356         isl_int_set_si(v, -1);
357         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
358         isl_int_set_si(v, 3);
359         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
360         bset = isl_basic_set_add_constraint(bset, c);
361
362         c = isl_equality_alloc(isl_local_space_copy(ls));
363         isl_int_set_si(v, -3);
364         isl_constraint_set_constant(c, v);
365         isl_int_set_si(v, 1);
366         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
367         isl_int_set_si(v, 4);
368         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
369         bset = isl_basic_set_add_constraint(bset, c);
370
371         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
372
373         assert(bset && bset->n_div == 1);
374         isl_local_space_free(ls);
375         isl_basic_set_free(bset);
376
377         /* test 4 */
378         dim = isl_space_set_alloc(ctx, 0, 3);
379         bset = isl_basic_set_universe(isl_space_copy(dim));
380         ls = isl_local_space_from_space(dim);
381
382         c = isl_equality_alloc(isl_local_space_copy(ls));
383         isl_int_set_si(v, 2);
384         isl_constraint_set_constant(c, v);
385         isl_int_set_si(v, -1);
386         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
387         isl_int_set_si(v, 3);
388         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
389         bset = isl_basic_set_add_constraint(bset, c);
390
391         c = isl_equality_alloc(isl_local_space_copy(ls));
392         isl_int_set_si(v, -1);
393         isl_constraint_set_constant(c, v);
394         isl_int_set_si(v, 1);
395         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
396         isl_int_set_si(v, 6);
397         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
398         bset = isl_basic_set_add_constraint(bset, c);
399
400         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
401
402         assert(isl_basic_set_is_empty(bset));
403         isl_local_space_free(ls);
404         isl_basic_set_free(bset);
405
406         /* test 5 */
407         dim = isl_space_set_alloc(ctx, 0, 3);
408         bset = isl_basic_set_universe(isl_space_copy(dim));
409         ls = isl_local_space_from_space(dim);
410
411         c = isl_equality_alloc(isl_local_space_copy(ls));
412         isl_int_set_si(v, -1);
413         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
414         isl_int_set_si(v, 3);
415         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
416         bset = isl_basic_set_add_constraint(bset, c);
417
418         c = isl_equality_alloc(isl_local_space_copy(ls));
419         isl_int_set_si(v, 1);
420         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
421         isl_int_set_si(v, -3);
422         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
423         bset = isl_basic_set_add_constraint(bset, c);
424
425         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
426
427         assert(bset && bset->n_div == 0);
428         isl_basic_set_free(bset);
429         isl_local_space_free(ls);
430
431         /* test 6 */
432         dim = isl_space_set_alloc(ctx, 0, 3);
433         bset = isl_basic_set_universe(isl_space_copy(dim));
434         ls = isl_local_space_from_space(dim);
435
436         c = isl_equality_alloc(isl_local_space_copy(ls));
437         isl_int_set_si(v, -1);
438         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
439         isl_int_set_si(v, 6);
440         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
441         bset = isl_basic_set_add_constraint(bset, c);
442
443         c = isl_equality_alloc(isl_local_space_copy(ls));
444         isl_int_set_si(v, 1);
445         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
446         isl_int_set_si(v, -3);
447         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
448         bset = isl_basic_set_add_constraint(bset, c);
449
450         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
451
452         assert(bset && bset->n_div == 1);
453         isl_basic_set_free(bset);
454         isl_local_space_free(ls);
455
456         /* test 7 */
457         /* This test is a bit tricky.  We set up an equality
458          *              a + 3b + 3c = 6 e0
459          * Normalization of divs creates _two_ divs
460          *              a = 3 e0
461          *              c - b - e0 = 2 e1
462          * Afterwards e0 is removed again because it has coefficient -1
463          * and we end up with the original equality and div again.
464          * Perhaps we can avoid the introduction of this temporary div.
465          */
466         dim = isl_space_set_alloc(ctx, 0, 4);
467         bset = isl_basic_set_universe(isl_space_copy(dim));
468         ls = isl_local_space_from_space(dim);
469
470         c = isl_equality_alloc(isl_local_space_copy(ls));
471         isl_int_set_si(v, -1);
472         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
473         isl_int_set_si(v, -3);
474         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
475         isl_int_set_si(v, -3);
476         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
477         isl_int_set_si(v, 6);
478         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
479         bset = isl_basic_set_add_constraint(bset, c);
480
481         bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
482
483         /* Test disabled for now */
484         /*
485         assert(bset && bset->n_div == 1);
486         */
487         isl_local_space_free(ls);
488         isl_basic_set_free(bset);
489
490         /* test 8 */
491         dim = isl_space_set_alloc(ctx, 0, 5);
492         bset = isl_basic_set_universe(isl_space_copy(dim));
493         ls = isl_local_space_from_space(dim);
494
495         c = isl_equality_alloc(isl_local_space_copy(ls));
496         isl_int_set_si(v, -1);
497         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
498         isl_int_set_si(v, -3);
499         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
500         isl_int_set_si(v, -3);
501         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
502         isl_int_set_si(v, 6);
503         isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
504         bset = isl_basic_set_add_constraint(bset, c);
505
506         c = isl_equality_alloc(isl_local_space_copy(ls));
507         isl_int_set_si(v, -1);
508         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
509         isl_int_set_si(v, 1);
510         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
511         isl_int_set_si(v, 1);
512         isl_constraint_set_constant(c, v);
513         bset = isl_basic_set_add_constraint(bset, c);
514
515         bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
516
517         /* Test disabled for now */
518         /*
519         assert(bset && bset->n_div == 1);
520         */
521         isl_local_space_free(ls);
522         isl_basic_set_free(bset);
523
524         /* test 9 */
525         dim = isl_space_set_alloc(ctx, 0, 4);
526         bset = isl_basic_set_universe(isl_space_copy(dim));
527         ls = isl_local_space_from_space(dim);
528
529         c = isl_equality_alloc(isl_local_space_copy(ls));
530         isl_int_set_si(v, 1);
531         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
532         isl_int_set_si(v, -1);
533         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
534         isl_int_set_si(v, -2);
535         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
536         bset = isl_basic_set_add_constraint(bset, c);
537
538         c = isl_equality_alloc(isl_local_space_copy(ls));
539         isl_int_set_si(v, -1);
540         isl_constraint_set_coefficient(c, isl_dim_set, 0, 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, 2);
544         isl_constraint_set_constant(c, v);
545         bset = isl_basic_set_add_constraint(bset, c);
546
547         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
548
549         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
550
551         assert(!isl_basic_set_is_empty(bset));
552
553         isl_local_space_free(ls);
554         isl_basic_set_free(bset);
555
556         /* test 10 */
557         dim = isl_space_set_alloc(ctx, 0, 3);
558         bset = isl_basic_set_universe(isl_space_copy(dim));
559         ls = isl_local_space_from_space(dim);
560
561         c = isl_equality_alloc(isl_local_space_copy(ls));
562         isl_int_set_si(v, 1);
563         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
564         isl_int_set_si(v, -2);
565         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
566         bset = isl_basic_set_add_constraint(bset, c);
567
568         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
569
570         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
571
572         isl_local_space_free(ls);
573         isl_basic_set_free(bset);
574
575         isl_int_clear(v);
576 }
577
578 void test_application_case(struct isl_ctx *ctx, const char *name)
579 {
580         char *filename;
581         FILE *input;
582         struct isl_basic_set *bset1, *bset2;
583         struct isl_basic_map *bmap;
584
585         filename = get_filename(ctx, name, "omega");
586         assert(filename);
587         input = fopen(filename, "r");
588         assert(input);
589
590         bset1 = isl_basic_set_read_from_file(ctx, input);
591         bmap = isl_basic_map_read_from_file(ctx, input);
592
593         bset1 = isl_basic_set_apply(bset1, bmap);
594
595         bset2 = isl_basic_set_read_from_file(ctx, input);
596
597         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
598
599         isl_basic_set_free(bset1);
600         isl_basic_set_free(bset2);
601         free(filename);
602
603         fclose(input);
604 }
605
606 void test_application(struct isl_ctx *ctx)
607 {
608         test_application_case(ctx, "application");
609         test_application_case(ctx, "application2");
610 }
611
612 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
613 {
614         char *filename;
615         FILE *input;
616         struct isl_basic_set *bset1, *bset2;
617
618         filename = get_filename(ctx, name, "polylib");
619         assert(filename);
620         input = fopen(filename, "r");
621         assert(input);
622
623         bset1 = isl_basic_set_read_from_file(ctx, input);
624         bset2 = isl_basic_set_read_from_file(ctx, input);
625
626         bset1 = isl_basic_set_affine_hull(bset1);
627
628         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
629
630         isl_basic_set_free(bset1);
631         isl_basic_set_free(bset2);
632         free(filename);
633
634         fclose(input);
635 }
636
637 void test_affine_hull(struct isl_ctx *ctx)
638 {
639         test_affine_hull_case(ctx, "affine2");
640         test_affine_hull_case(ctx, "affine");
641         test_affine_hull_case(ctx, "affine3");
642 }
643
644 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
645 {
646         char *filename;
647         FILE *input;
648         struct isl_basic_set *bset1, *bset2;
649         struct isl_set *set;
650
651         filename = get_filename(ctx, name, "polylib");
652         assert(filename);
653         input = fopen(filename, "r");
654         assert(input);
655
656         bset1 = isl_basic_set_read_from_file(ctx, input);
657         bset2 = isl_basic_set_read_from_file(ctx, input);
658
659         set = isl_basic_set_union(bset1, bset2);
660         bset1 = isl_set_convex_hull(set);
661
662         bset2 = isl_basic_set_read_from_file(ctx, input);
663
664         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
665
666         isl_basic_set_free(bset1);
667         isl_basic_set_free(bset2);
668         free(filename);
669
670         fclose(input);
671 }
672
673 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
674 {
675         const char *str1, *str2;
676         isl_set *set1, *set2;
677         int orig_convex = ctx->opt->convex;
678         ctx->opt->convex = convex;
679
680         test_convex_hull_case(ctx, "convex0");
681         test_convex_hull_case(ctx, "convex1");
682         test_convex_hull_case(ctx, "convex2");
683         test_convex_hull_case(ctx, "convex3");
684         test_convex_hull_case(ctx, "convex4");
685         test_convex_hull_case(ctx, "convex5");
686         test_convex_hull_case(ctx, "convex6");
687         test_convex_hull_case(ctx, "convex7");
688         test_convex_hull_case(ctx, "convex8");
689         test_convex_hull_case(ctx, "convex9");
690         test_convex_hull_case(ctx, "convex10");
691         test_convex_hull_case(ctx, "convex11");
692         test_convex_hull_case(ctx, "convex12");
693         test_convex_hull_case(ctx, "convex13");
694         test_convex_hull_case(ctx, "convex14");
695         test_convex_hull_case(ctx, "convex15");
696
697         str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
698                "(i0 = 1 and i1 = 0 and i2 = 1) or "
699                "(i0 = 0 and i1 = 0 and i2 = 0) }";
700         str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
701         set1 = isl_set_read_from_str(ctx, str1);
702         set2 = isl_set_read_from_str(ctx, str2);
703         set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
704         assert(isl_set_is_equal(set1, set2));
705         isl_set_free(set1);
706         isl_set_free(set2);
707
708         ctx->opt->convex = orig_convex;
709 }
710
711 void test_convex_hull(struct isl_ctx *ctx)
712 {
713         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
714         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
715 }
716
717 void test_gist_case(struct isl_ctx *ctx, const char *name)
718 {
719         char *filename;
720         FILE *input;
721         struct isl_basic_set *bset1, *bset2;
722
723         filename = get_filename(ctx, name, "polylib");
724         assert(filename);
725         input = fopen(filename, "r");
726         assert(input);
727
728         bset1 = isl_basic_set_read_from_file(ctx, input);
729         bset2 = isl_basic_set_read_from_file(ctx, input);
730
731         bset1 = isl_basic_set_gist(bset1, bset2);
732
733         bset2 = isl_basic_set_read_from_file(ctx, input);
734
735         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
736
737         isl_basic_set_free(bset1);
738         isl_basic_set_free(bset2);
739         free(filename);
740
741         fclose(input);
742 }
743
744 void test_gist(struct isl_ctx *ctx)
745 {
746         const char *str;
747         isl_basic_set *bset1, *bset2;
748
749         test_gist_case(ctx, "gist1");
750
751         str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
752             "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
753             "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
754             "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
755             "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
756             "16e0 >= 16 + 16p6 + 15p10 and  p2 <= 15 and p3 >= 0 and "
757             "p3 <= 31 and  p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
758             "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
759             "p10 <= 15 and p10 <= -1 + p0 - p6) }";
760         bset1 = isl_basic_set_read_from_str(ctx, str);
761         str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
762             "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
763             "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
764             "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
765             "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
766             "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
767             "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
768             "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
769         bset2 = isl_basic_set_read_from_str(ctx, str);
770         bset1 = isl_basic_set_gist(bset1, bset2);
771         assert(bset1 && bset1->n_div == 0);
772         isl_basic_set_free(bset1);
773 }
774
775 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
776 {
777         isl_set *set, *set2;
778
779         set = isl_set_read_from_str(ctx, str);
780         set = isl_set_coalesce(set);
781         set2 = isl_set_read_from_str(ctx, str);
782         assert(isl_set_is_equal(set, set2));
783         if (check_one)
784                 assert(set && set->n == 1);
785         isl_set_free(set);
786         isl_set_free(set2);
787 }
788
789 void test_coalesce(struct isl_ctx *ctx)
790 {
791         const char *str;
792         struct isl_set *set, *set2;
793         struct isl_map *map, *map2;
794
795         set = isl_set_read_from_str(ctx,
796                 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
797                        "y >= x & x >= 2 & 5 >= y }");
798         set = isl_set_coalesce(set);
799         assert(set && set->n == 1);
800         isl_set_free(set);
801
802         set = isl_set_read_from_str(ctx,
803                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
804                        "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}");
805         set = isl_set_coalesce(set);
806         assert(set && set->n == 1);
807         isl_set_free(set);
808
809         set = isl_set_read_from_str(ctx,
810                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
811                        "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}");
812         set = isl_set_coalesce(set);
813         assert(set && set->n == 2);
814         isl_set_free(set);
815
816         set = isl_set_read_from_str(ctx,
817                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
818                        "y >= 0 & x >= 6 & x <= 10 & y <= x}");
819         set = isl_set_coalesce(set);
820         assert(set && set->n == 1);
821         isl_set_free(set);
822
823         set = isl_set_read_from_str(ctx,
824                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
825                        "y >= 0 & x >= 7 & x <= 10 & y <= x}");
826         set = isl_set_coalesce(set);
827         assert(set && set->n == 2);
828         isl_set_free(set);
829
830         set = isl_set_read_from_str(ctx,
831                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
832                        "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}");
833         set = isl_set_coalesce(set);
834         assert(set && set->n == 2);
835         isl_set_free(set);
836
837         set = isl_set_read_from_str(ctx,
838                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
839                        "y >= 0 & x = 6 & y <= 6}");
840         set = isl_set_coalesce(set);
841         assert(set && set->n == 1);
842         isl_set_free(set);
843
844         set = isl_set_read_from_str(ctx,
845                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
846                        "y >= 0 & x = 7 & y <= 6}");
847         set = isl_set_coalesce(set);
848         assert(set && set->n == 2);
849         isl_set_free(set);
850
851         set = isl_set_read_from_str(ctx,
852                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
853                        "y >= 0 & x = 6 & y <= 5}");
854         set = isl_set_coalesce(set);
855         assert(set && set->n == 1);
856         set2 = isl_set_read_from_str(ctx,
857                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
858                        "y >= 0 & x = 6 & y <= 5}");
859         assert(isl_set_is_equal(set, set2));
860         isl_set_free(set);
861         isl_set_free(set2);
862
863         set = isl_set_read_from_str(ctx,
864                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
865                        "y >= 0 & x = 6 & y <= 7}");
866         set = isl_set_coalesce(set);
867         assert(set && set->n == 2);
868         isl_set_free(set);
869
870         set = isl_set_read_from_str(ctx,
871                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
872         set = isl_set_coalesce(set);
873         assert(set && set->n == 1);
874         set2 = isl_set_read_from_str(ctx,
875                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
876         assert(isl_set_is_equal(set, set2));
877         isl_set_free(set);
878         isl_set_free(set2);
879
880         set = isl_set_read_from_str(ctx,
881                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
882         set = isl_set_coalesce(set);
883         set2 = isl_set_read_from_str(ctx,
884                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
885         assert(isl_set_is_equal(set, set2));
886         isl_set_free(set);
887         isl_set_free(set2);
888
889         set = isl_set_read_from_str(ctx,
890                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
891                                 "2 <= i and i <= n }");
892         set = isl_set_coalesce(set);
893         assert(set && set->n == 1);
894         set2 = isl_set_read_from_str(ctx,
895                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
896                                 "2 <= i and i <= n }");
897         assert(isl_set_is_equal(set, set2));
898         isl_set_free(set);
899         isl_set_free(set2);
900
901         map = isl_map_read_from_str(ctx,
902                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
903                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
904                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
905                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
906                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
907                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
908                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
909                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
910                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
911                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
912                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
913                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
914                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
915                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
916                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
917                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
918                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
919                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
920         map = isl_map_coalesce(map);
921         map2 = isl_map_read_from_str(ctx,
922                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
923                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
924                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
925                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
926                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
927                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
928                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
929                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
930                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
931                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
932                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
933                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
934                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
935                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
936                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
937                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
938                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
939                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
940         assert(isl_map_is_equal(map, map2));
941         isl_map_free(map);
942         isl_map_free(map2);
943
944         str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
945               "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
946               "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
947               "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
948         map = isl_map_read_from_str(ctx, str);
949         map = isl_map_coalesce(map);
950         map2 = isl_map_read_from_str(ctx, str);
951         assert(isl_map_is_equal(map, map2));
952         isl_map_free(map);
953         isl_map_free(map2);
954
955         str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
956           "[o0, o1, o2, o3, o4, o5, o6] : "
957           "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
958           "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
959           "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
960           "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
961           "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
962           "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
963           "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
964           "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
965           "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
966           "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
967           "o6 >= i3 + i6 - o3 and M >= 0 and "
968           "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
969           "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
970         map = isl_map_read_from_str(ctx, str);
971         map = isl_map_coalesce(map);
972         map2 = isl_map_read_from_str(ctx, str);
973         assert(isl_map_is_equal(map, map2));
974         isl_map_free(map);
975         isl_map_free(map2);
976
977         str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
978                 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
979                 "(o0 = 0 and M >= 2 and N >= 3) or "
980                 "(M = 0 and o0 = 0 and N >= 3) }";
981         map = isl_map_read_from_str(ctx, str);
982         map = isl_map_coalesce(map);
983         map2 = isl_map_read_from_str(ctx, str);
984         assert(isl_map_is_equal(map, map2));
985         isl_map_free(map);
986         isl_map_free(map2);
987
988         str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
989                 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
990                 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
991                 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
992         map = isl_map_read_from_str(ctx, str);
993         map = isl_map_coalesce(map);
994         map2 = isl_map_read_from_str(ctx, str);
995         assert(isl_map_is_equal(map, map2));
996         isl_map_free(map);
997         isl_map_free(map2);
998
999         test_coalesce_set(ctx,
1000                 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
1001                                 "(i1 = M and M >= 1) }", 0);
1002         test_coalesce_set(ctx,
1003                 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
1004         test_coalesce_set(ctx,
1005                 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1006                 "(y = 3 and x = 1) }", 1);
1007         test_coalesce_set(ctx,
1008                 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1009                 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1010                 "i1 <= M and i3 <= M and i4 <= M) or "
1011                 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1012                 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1013                 "i4 <= -1 + M) }", 1);
1014         test_coalesce_set(ctx,
1015                 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1016                 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
1017         test_coalesce_set(ctx,
1018                 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1019                         "-x - y + 1 >= 0 and -3 <= z <= 3;"
1020                 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1021                         "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
1022         test_coalesce_set(ctx,
1023                 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
1024         test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
1025         test_coalesce_set(ctx,
1026                 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
1027         test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
1028         test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1);
1029         test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0);
1030         test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1);
1031         test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0);
1032         test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0);
1033         test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0);
1034 }
1035
1036 void test_closure(struct isl_ctx *ctx)
1037 {
1038         const char *str;
1039         isl_set *dom;
1040         isl_map *up, *right;
1041         isl_map *map, *map2;
1042         int exact;
1043
1044         /* COCOA example 1 */
1045         map = isl_map_read_from_str(ctx,
1046                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1047                         "1 <= i and i < n and 1 <= j and j < n or "
1048                         "i2 = i + 1 and j2 = j - 1 and "
1049                         "1 <= i and i < n and 2 <= j and j <= n }");
1050         map = isl_map_power(map, &exact);
1051         assert(exact);
1052         isl_map_free(map);
1053
1054         /* COCOA example 1 */
1055         map = isl_map_read_from_str(ctx,
1056                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1057                         "1 <= i and i < n and 1 <= j and j < n or "
1058                         "i2 = i + 1 and j2 = j - 1 and "
1059                         "1 <= i and i < n and 2 <= j and j <= n }");
1060         map = isl_map_transitive_closure(map, &exact);
1061         assert(exact);
1062         map2 = isl_map_read_from_str(ctx,
1063                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1064                         "1 <= i and i < n and 1 <= j and j <= n and "
1065                         "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1066                         "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1067                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1068         assert(isl_map_is_equal(map, map2));
1069         isl_map_free(map2);
1070         isl_map_free(map);
1071
1072         map = isl_map_read_from_str(ctx,
1073                 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1074                                      " 0 <= y and y <= n }");
1075         map = isl_map_transitive_closure(map, &exact);
1076         map2 = isl_map_read_from_str(ctx,
1077                 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1078                                      " 0 <= y and y <= n }");
1079         assert(isl_map_is_equal(map, map2));
1080         isl_map_free(map2);
1081         isl_map_free(map);
1082
1083         /* COCOA example 2 */
1084         map = isl_map_read_from_str(ctx,
1085                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1086                         "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1087                         "i2 = i + 2 and j2 = j - 2 and "
1088                         "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1089         map = isl_map_transitive_closure(map, &exact);
1090         assert(exact);
1091         map2 = isl_map_read_from_str(ctx,
1092                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1093                         "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1094                         "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1095                         "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1096                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1097         assert(isl_map_is_equal(map, map2));
1098         isl_map_free(map);
1099         isl_map_free(map2);
1100
1101         /* COCOA Fig.2 left */
1102         map = isl_map_read_from_str(ctx,
1103                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1104                         "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1105                         "j <= n or "
1106                         "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1107                         "j <= 2 i - 3 and j <= n - 2 or "
1108                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1109                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1110         map = isl_map_transitive_closure(map, &exact);
1111         assert(exact);
1112         isl_map_free(map);
1113
1114         /* COCOA Fig.2 right */
1115         map = isl_map_read_from_str(ctx,
1116                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1117                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1118                         "j <= n or "
1119                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1120                         "j <= 2 i - 4 and j <= n - 3 or "
1121                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1122                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1123         map = isl_map_power(map, &exact);
1124         assert(exact);
1125         isl_map_free(map);
1126
1127         /* COCOA Fig.2 right */
1128         map = isl_map_read_from_str(ctx,
1129                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1130                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1131                         "j <= n or "
1132                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1133                         "j <= 2 i - 4 and j <= n - 3 or "
1134                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1135                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1136         map = isl_map_transitive_closure(map, &exact);
1137         assert(exact);
1138         map2 = isl_map_read_from_str(ctx,
1139                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1140                         "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1141                         "j <= n and 3 + i + 2 j <= 3 n and "
1142                         "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1143                         "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1144                         "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1145                         "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1146                         "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1147         assert(isl_map_is_equal(map, map2));
1148         isl_map_free(map2);
1149         isl_map_free(map);
1150
1151         /* COCOA Fig.1 right */
1152         dom = isl_set_read_from_str(ctx,
1153                 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1154                         "2 x - 3 y + 3 >= 0 }");
1155         right = isl_map_read_from_str(ctx,
1156                 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1157         up = isl_map_read_from_str(ctx,
1158                 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1159         right = isl_map_intersect_domain(right, isl_set_copy(dom));
1160         right = isl_map_intersect_range(right, isl_set_copy(dom));
1161         up = isl_map_intersect_domain(up, isl_set_copy(dom));
1162         up = isl_map_intersect_range(up, dom);
1163         map = isl_map_union(up, right);
1164         map = isl_map_transitive_closure(map, &exact);
1165         assert(exact);
1166         map2 = isl_map_read_from_str(ctx,
1167                 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1168                 "  [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1169         assert(isl_map_is_equal(map, map2));
1170         isl_map_free(map2);
1171         isl_map_free(map);
1172
1173         /* COCOA Theorem 1 counter example */
1174         map = isl_map_read_from_str(ctx,
1175                 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1176                         "i2 = 1 and j2 = j or "
1177                         "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1178         map = isl_map_transitive_closure(map, &exact);
1179         assert(exact);
1180         isl_map_free(map);
1181
1182         map = isl_map_read_from_str(ctx,
1183                 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1184                         "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1185                         "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1186                         "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1187         map = isl_map_transitive_closure(map, &exact);
1188         assert(exact);
1189         isl_map_free(map);
1190
1191         /* Kelly et al 1996, fig 12 */
1192         map = isl_map_read_from_str(ctx,
1193                 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1194                         "1 <= i,j,j+1 <= n or "
1195                         "j = n and j2 = 1 and i2 = i + 1 and "
1196                         "1 <= i,i+1 <= n }");
1197         map = isl_map_transitive_closure(map, &exact);
1198         assert(exact);
1199         map2 = isl_map_read_from_str(ctx,
1200                 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1201                         "1 <= i <= n and i = i2 or "
1202                         "1 <= i < i2 <= n and 1 <= j <= n and "
1203                         "1 <= j2 <= n }");
1204         assert(isl_map_is_equal(map, map2));
1205         isl_map_free(map2);
1206         isl_map_free(map);
1207
1208         /* Omega's closure4 */
1209         map = isl_map_read_from_str(ctx,
1210                 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1211                         "1 <= x,y <= 10 or "
1212                         "x2 = x + 1 and y2 = y and "
1213                         "1 <= x <= 20 && 5 <= y <= 15 }");
1214         map = isl_map_transitive_closure(map, &exact);
1215         assert(exact);
1216         isl_map_free(map);
1217
1218         map = isl_map_read_from_str(ctx,
1219                 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1220         map = isl_map_transitive_closure(map, &exact);
1221         assert(!exact);
1222         map2 = isl_map_read_from_str(ctx,
1223                 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1224         assert(isl_map_is_equal(map, map2));
1225         isl_map_free(map);
1226         isl_map_free(map2);
1227
1228         str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1229             "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1230             "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1231             "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1232         map = isl_map_read_from_str(ctx, str);
1233         map = isl_map_transitive_closure(map, &exact);
1234         assert(exact);
1235         map2 = isl_map_read_from_str(ctx, str);
1236         assert(isl_map_is_equal(map, map2));
1237         isl_map_free(map);
1238         isl_map_free(map2);
1239
1240         str = "{[0] -> [1]; [2] -> [3]}";
1241         map = isl_map_read_from_str(ctx, str);
1242         map = isl_map_transitive_closure(map, &exact);
1243         assert(exact);
1244         map2 = isl_map_read_from_str(ctx, str);
1245         assert(isl_map_is_equal(map, map2));
1246         isl_map_free(map);
1247         isl_map_free(map2);
1248
1249         str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1250             "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1251             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1252             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1253             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1254             "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1255             "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1256             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1257             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1258             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1259             "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1260             "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1261             "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1262             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1263             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1264             "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1265             "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1266             "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1267             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1268             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1269         map = isl_map_read_from_str(ctx, str);
1270         map = isl_map_transitive_closure(map, NULL);
1271         assert(map);
1272         isl_map_free(map);
1273 }
1274
1275 void test_lex(struct isl_ctx *ctx)
1276 {
1277         isl_space *dim;
1278         isl_map *map;
1279
1280         dim = isl_space_set_alloc(ctx, 0, 0);
1281         map = isl_map_lex_le(dim);
1282         assert(!isl_map_is_empty(map));
1283         isl_map_free(map);
1284 }
1285
1286 void test_lexmin(struct isl_ctx *ctx)
1287 {
1288         const char *str;
1289         isl_basic_map *bmap;
1290         isl_map *map, *map2;
1291         isl_set *set;
1292         isl_set *set2;
1293         isl_pw_multi_aff *pma;
1294
1295         str = "[p0, p1] -> { [] -> [] : "
1296             "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1297             "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1298             "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1299             "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1300             "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1301             "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1302             "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1303             "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1304             "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1305             "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1306             "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1307         map = isl_map_read_from_str(ctx, str);
1308         map = isl_map_lexmin(map);
1309         isl_map_free(map);
1310
1311         str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1312             "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1313         set = isl_set_read_from_str(ctx, str);
1314         set = isl_set_lexmax(set);
1315         str = "[C] -> { [obj,a,b,c] : C = 8 }";
1316         set2 = isl_set_read_from_str(ctx, str);
1317         set = isl_set_intersect(set, set2);
1318         assert(!isl_set_is_empty(set));
1319         isl_set_free(set);
1320
1321         str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1322         map = isl_map_read_from_str(ctx, str);
1323         map = isl_map_lexmin(map);
1324         str = "{ [x] -> [5] : 6 <= x <= 8; "
1325                 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1326         map2 = isl_map_read_from_str(ctx, str);
1327         assert(isl_map_is_equal(map, map2));
1328         isl_map_free(map);
1329         isl_map_free(map2);
1330
1331         str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1332         map = isl_map_read_from_str(ctx, str);
1333         map2 = isl_map_copy(map);
1334         map = isl_map_lexmin(map);
1335         assert(isl_map_is_equal(map, map2));
1336         isl_map_free(map);
1337         isl_map_free(map2);
1338
1339         str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1340         map = isl_map_read_from_str(ctx, str);
1341         map = isl_map_lexmin(map);
1342         str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1343                 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1344                 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1345                 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1346         map2 = isl_map_read_from_str(ctx, str);
1347         assert(isl_map_is_equal(map, map2));
1348         isl_map_free(map);
1349         isl_map_free(map2);
1350
1351         str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1352                                 " 8i' <= i and 8i' >= -7 + i }";
1353         bmap = isl_basic_map_read_from_str(ctx, str);
1354         pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1355         map2 = isl_map_from_pw_multi_aff(pma);
1356         map = isl_map_from_basic_map(bmap);
1357         assert(isl_map_is_equal(map, map2));
1358         isl_map_free(map);
1359         isl_map_free(map2);
1360
1361         str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1362         map = isl_map_read_from_str(ctx, str);
1363         map = isl_map_lexmin(map);
1364         str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1365         map2 = isl_map_read_from_str(ctx, str);
1366         assert(isl_map_is_equal(map, map2));
1367         isl_map_free(map);
1368         isl_map_free(map2);
1369 }
1370
1371 struct must_may {
1372         isl_map *must;
1373         isl_map *may;
1374 };
1375
1376 static int collect_must_may(__isl_take isl_map *dep, int must,
1377         void *dep_user, void *user)
1378 {
1379         struct must_may *mm = (struct must_may *)user;
1380
1381         if (must)
1382                 mm->must = isl_map_union(mm->must, dep);
1383         else
1384                 mm->may = isl_map_union(mm->may, dep);
1385
1386         return 0;
1387 }
1388
1389 static int common_space(void *first, void *second)
1390 {
1391         int depth = *(int *)first;
1392         return 2 * depth;
1393 }
1394
1395 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1396 {
1397         isl_map *map2;
1398         int equal;
1399
1400         if (!map)
1401                 return -1;
1402
1403         map2 = isl_map_read_from_str(map->ctx, str);
1404         equal = isl_map_is_equal(map, map2);
1405         isl_map_free(map2);
1406
1407         return equal;
1408 }
1409
1410 void test_dep(struct isl_ctx *ctx)
1411 {
1412         const char *str;
1413         isl_space *dim;
1414         isl_map *map;
1415         isl_access_info *ai;
1416         isl_flow *flow;
1417         int depth;
1418         struct must_may mm;
1419
1420         depth = 3;
1421
1422         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1423         map = isl_map_read_from_str(ctx, str);
1424         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1425
1426         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1427         map = isl_map_read_from_str(ctx, str);
1428         ai = isl_access_info_add_source(ai, map, 1, &depth);
1429
1430         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1431         map = isl_map_read_from_str(ctx, str);
1432         ai = isl_access_info_add_source(ai, map, 1, &depth);
1433
1434         flow = isl_access_info_compute_flow(ai);
1435         dim = isl_space_alloc(ctx, 0, 3, 3);
1436         mm.must = isl_map_empty(isl_space_copy(dim));
1437         mm.may = isl_map_empty(dim);
1438
1439         isl_flow_foreach(flow, collect_must_may, &mm);
1440
1441         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1442               "  [1,10,0] -> [2,5,0] }";
1443         assert(map_is_equal(mm.must, str));
1444         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1445         assert(map_is_equal(mm.may, str));
1446
1447         isl_map_free(mm.must);
1448         isl_map_free(mm.may);
1449         isl_flow_free(flow);
1450
1451
1452         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1453         map = isl_map_read_from_str(ctx, str);
1454         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1455
1456         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1457         map = isl_map_read_from_str(ctx, str);
1458         ai = isl_access_info_add_source(ai, map, 1, &depth);
1459
1460         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1461         map = isl_map_read_from_str(ctx, str);
1462         ai = isl_access_info_add_source(ai, map, 0, &depth);
1463
1464         flow = isl_access_info_compute_flow(ai);
1465         dim = isl_space_alloc(ctx, 0, 3, 3);
1466         mm.must = isl_map_empty(isl_space_copy(dim));
1467         mm.may = isl_map_empty(dim);
1468
1469         isl_flow_foreach(flow, collect_must_may, &mm);
1470
1471         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1472         assert(map_is_equal(mm.must, str));
1473         str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1474         assert(map_is_equal(mm.may, str));
1475
1476         isl_map_free(mm.must);
1477         isl_map_free(mm.may);
1478         isl_flow_free(flow);
1479
1480
1481         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1482         map = isl_map_read_from_str(ctx, str);
1483         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1484
1485         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1486         map = isl_map_read_from_str(ctx, str);
1487         ai = isl_access_info_add_source(ai, map, 0, &depth);
1488
1489         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1490         map = isl_map_read_from_str(ctx, str);
1491         ai = isl_access_info_add_source(ai, map, 0, &depth);
1492
1493         flow = isl_access_info_compute_flow(ai);
1494         dim = isl_space_alloc(ctx, 0, 3, 3);
1495         mm.must = isl_map_empty(isl_space_copy(dim));
1496         mm.may = isl_map_empty(dim);
1497
1498         isl_flow_foreach(flow, collect_must_may, &mm);
1499
1500         str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1501               "  [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1502         assert(map_is_equal(mm.may, str));
1503         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1504         assert(map_is_equal(mm.must, str));
1505
1506         isl_map_free(mm.must);
1507         isl_map_free(mm.may);
1508         isl_flow_free(flow);
1509
1510
1511         str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1512         map = isl_map_read_from_str(ctx, str);
1513         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1514
1515         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1516         map = isl_map_read_from_str(ctx, str);
1517         ai = isl_access_info_add_source(ai, map, 0, &depth);
1518
1519         str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1520         map = isl_map_read_from_str(ctx, str);
1521         ai = isl_access_info_add_source(ai, map, 0, &depth);
1522
1523         flow = isl_access_info_compute_flow(ai);
1524         dim = isl_space_alloc(ctx, 0, 3, 3);
1525         mm.must = isl_map_empty(isl_space_copy(dim));
1526         mm.may = isl_map_empty(dim);
1527
1528         isl_flow_foreach(flow, collect_must_may, &mm);
1529
1530         str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1531               "  [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1532         assert(map_is_equal(mm.may, str));
1533         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1534         assert(map_is_equal(mm.must, str));
1535
1536         isl_map_free(mm.must);
1537         isl_map_free(mm.may);
1538         isl_flow_free(flow);
1539
1540
1541         str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1542         map = isl_map_read_from_str(ctx, str);
1543         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1544
1545         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1546         map = isl_map_read_from_str(ctx, str);
1547         ai = isl_access_info_add_source(ai, map, 0, &depth);
1548
1549         str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1550         map = isl_map_read_from_str(ctx, str);
1551         ai = isl_access_info_add_source(ai, map, 0, &depth);
1552
1553         flow = isl_access_info_compute_flow(ai);
1554         dim = isl_space_alloc(ctx, 0, 3, 3);
1555         mm.must = isl_map_empty(isl_space_copy(dim));
1556         mm.may = isl_map_empty(dim);
1557
1558         isl_flow_foreach(flow, collect_must_may, &mm);
1559
1560         str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1561               "  [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1562         assert(map_is_equal(mm.may, str));
1563         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1564         assert(map_is_equal(mm.must, str));
1565
1566         isl_map_free(mm.must);
1567         isl_map_free(mm.may);
1568         isl_flow_free(flow);
1569
1570
1571         depth = 5;
1572
1573         str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1574         map = isl_map_read_from_str(ctx, str);
1575         ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1576
1577         str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1578         map = isl_map_read_from_str(ctx, str);
1579         ai = isl_access_info_add_source(ai, map, 1, &depth);
1580
1581         flow = isl_access_info_compute_flow(ai);
1582         dim = isl_space_alloc(ctx, 0, 5, 5);
1583         mm.must = isl_map_empty(isl_space_copy(dim));
1584         mm.may = isl_map_empty(dim);
1585
1586         isl_flow_foreach(flow, collect_must_may, &mm);
1587
1588         str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1589         assert(map_is_equal(mm.must, str));
1590         str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1591         assert(map_is_equal(mm.may, str));
1592
1593         isl_map_free(mm.must);
1594         isl_map_free(mm.may);
1595         isl_flow_free(flow);
1596 }
1597
1598 int test_sv(isl_ctx *ctx)
1599 {
1600         const char *str;
1601         isl_map *map;
1602         isl_union_map *umap;
1603         int sv;
1604
1605         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1606         map = isl_map_read_from_str(ctx, str);
1607         sv = isl_map_is_single_valued(map);
1608         isl_map_free(map);
1609         if (sv < 0)
1610                 return -1;
1611         if (!sv)
1612                 isl_die(ctx, isl_error_internal,
1613                         "map not detected as single valued", return -1);
1614
1615         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1616         map = isl_map_read_from_str(ctx, str);
1617         sv = isl_map_is_single_valued(map);
1618         isl_map_free(map);
1619         if (sv < 0)
1620                 return -1;
1621         if (sv)
1622                 isl_die(ctx, isl_error_internal,
1623                         "map detected as single valued", return -1);
1624
1625         str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1626         umap = isl_union_map_read_from_str(ctx, str);
1627         sv = isl_union_map_is_single_valued(umap);
1628         isl_union_map_free(umap);
1629         if (sv < 0)
1630                 return -1;
1631         if (!sv)
1632                 isl_die(ctx, isl_error_internal,
1633                         "map not detected as single valued", return -1);
1634
1635         str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1636         umap = isl_union_map_read_from_str(ctx, str);
1637         sv = isl_union_map_is_single_valued(umap);
1638         isl_union_map_free(umap);
1639         if (sv < 0)
1640                 return -1;
1641         if (sv)
1642                 isl_die(ctx, isl_error_internal,
1643                         "map detected as single valued", return -1);
1644
1645         return 0;
1646 }
1647
1648 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1649 {
1650         isl_map *map;
1651
1652         map = isl_map_read_from_str(ctx, str);
1653         if (bijective)
1654                 assert(isl_map_is_bijective(map));
1655         else
1656                 assert(!isl_map_is_bijective(map));
1657         isl_map_free(map);
1658 }
1659
1660 void test_bijective(struct isl_ctx *ctx)
1661 {
1662         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1663         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1664         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1665         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1666         test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1667         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1668         test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1669         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1670         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1671         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1672         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1673         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1674         test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1675 }
1676
1677 void test_pwqp(struct isl_ctx *ctx)
1678 {
1679         const char *str;
1680         isl_set *set;
1681         isl_pw_qpolynomial *pwqp1, *pwqp2;
1682
1683         str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1684         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1685
1686         pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1687                                                 isl_dim_in, 1, 1);
1688
1689         str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1690         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1691
1692         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1693
1694         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1695
1696         isl_pw_qpolynomial_free(pwqp1);
1697
1698         str = "{ [i] -> i }";
1699         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1700         str = "{ [k] : exists a : k = 2a }";
1701         set = isl_set_read_from_str(ctx, str);
1702         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1703         str = "{ [i] -> i }";
1704         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1705
1706         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1707
1708         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1709
1710         isl_pw_qpolynomial_free(pwqp1);
1711
1712         str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1713         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1714         str = "{ [10] }";
1715         set = isl_set_read_from_str(ctx, str);
1716         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1717         str = "{ [i] -> 16 }";
1718         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1719
1720         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1721
1722         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1723
1724         isl_pw_qpolynomial_free(pwqp1);
1725
1726         str = "{ [i] -> ([(i)/2]) }";
1727         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1728         str = "{ [k] : exists a : k = 2a+1 }";
1729         set = isl_set_read_from_str(ctx, str);
1730         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1731         str = "{ [i] -> -1/2 + 1/2 * i }";
1732         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1733
1734         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1735
1736         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1737
1738         isl_pw_qpolynomial_free(pwqp1);
1739
1740         str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1741         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1742         str = "{ [i] -> ([(2 * [i/2])/5]) }";
1743         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1744
1745         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1746
1747         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1748
1749         isl_pw_qpolynomial_free(pwqp1);
1750
1751         str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1752         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1753         str = "{ [x] -> x }";
1754         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1755
1756         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1757
1758         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1759
1760         isl_pw_qpolynomial_free(pwqp1);
1761
1762         str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1763         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1764         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1765         pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1766         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1767         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1768         isl_pw_qpolynomial_free(pwqp1);
1769 }
1770
1771 void test_split_periods(isl_ctx *ctx)
1772 {
1773         const char *str;
1774         isl_pw_qpolynomial *pwqp;
1775
1776         str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1777                 "U + 2V + 3 >= 0 and - U -2V  >= 0 and - U + 10 >= 0 and "
1778                 "U  >= 0; [U,V] -> U^2 : U >= 100 }";
1779         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1780
1781         pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1782         assert(pwqp);
1783
1784         isl_pw_qpolynomial_free(pwqp);
1785 }
1786
1787 void test_union(isl_ctx *ctx)
1788 {
1789         const char *str;
1790         isl_union_set *uset1, *uset2;
1791         isl_union_map *umap1, *umap2;
1792
1793         str = "{ [i] : 0 <= i <= 1 }";
1794         uset1 = isl_union_set_read_from_str(ctx, str);
1795         str = "{ [1] -> [0] }";
1796         umap1 = isl_union_map_read_from_str(ctx, str);
1797
1798         umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1799         assert(isl_union_map_is_equal(umap1, umap2));
1800
1801         isl_union_map_free(umap1);
1802         isl_union_map_free(umap2);
1803
1804         str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1805         umap1 = isl_union_map_read_from_str(ctx, str);
1806         str = "{ A[i]; B[i] }";
1807         uset1 = isl_union_set_read_from_str(ctx, str);
1808
1809         uset2 = isl_union_map_domain(umap1);
1810
1811         assert(isl_union_set_is_equal(uset1, uset2));
1812
1813         isl_union_set_free(uset1);
1814         isl_union_set_free(uset2);
1815 }
1816
1817 void test_bound(isl_ctx *ctx)
1818 {
1819         const char *str;
1820         isl_pw_qpolynomial *pwqp;
1821         isl_pw_qpolynomial_fold *pwf;
1822
1823         str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1824         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1825         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1826         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1827         isl_pw_qpolynomial_fold_free(pwf);
1828
1829         str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1830         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1831         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1832         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1833         isl_pw_qpolynomial_fold_free(pwf);
1834 }
1835
1836 void test_lift(isl_ctx *ctx)
1837 {
1838         const char *str;
1839         isl_basic_map *bmap;
1840         isl_basic_set *bset;
1841
1842         str = "{ [i0] : exists e0 : i0 = 4e0 }";
1843         bset = isl_basic_set_read_from_str(ctx, str);
1844         bset = isl_basic_set_lift(bset);
1845         bmap = isl_basic_map_from_range(bset);
1846         bset = isl_basic_map_domain(bmap);
1847         isl_basic_set_free(bset);
1848 }
1849
1850 void test_subset(isl_ctx *ctx)
1851 {
1852         const char *str;
1853         isl_set *set1, *set2;
1854
1855         str = "{ [112, 0] }";
1856         set1 = isl_set_read_from_str(ctx, str);
1857         str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1858                 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1859                 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1860         set2 = isl_set_read_from_str(ctx, str);
1861         assert(isl_set_is_subset(set1, set2));
1862         isl_set_free(set1);
1863         isl_set_free(set2);
1864 }
1865
1866 void test_factorize(isl_ctx *ctx)
1867 {
1868         const char *str;
1869         isl_basic_set *bset;
1870         isl_factorizer *f;
1871
1872         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1873             "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1874             "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1875             "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1876             "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1877             "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1878             "3i5 >= -2i0 - i2 + 3i4 }";
1879         bset = isl_basic_set_read_from_str(ctx, str);
1880         f = isl_basic_set_factorizer(bset);
1881         assert(f);
1882         isl_basic_set_free(bset);
1883         isl_factorizer_free(f);
1884
1885         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1886             "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
1887             "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
1888             "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
1889             "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
1890             "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
1891             "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
1892             "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
1893             "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
1894             "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
1895         bset = isl_basic_set_read_from_str(ctx, str);
1896         f = isl_basic_set_factorizer(bset);
1897         assert(f);
1898         isl_basic_set_free(bset);
1899         isl_factorizer_free(f);
1900 }
1901
1902 static int check_injective(__isl_take isl_map *map, void *user)
1903 {
1904         int *injective = user;
1905
1906         *injective = isl_map_is_injective(map);
1907         isl_map_free(map);
1908
1909         if (*injective < 0 || !*injective)
1910                 return -1;
1911
1912         return 0;
1913 }
1914
1915 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
1916         const char *r, const char *s, int tilable, int parallel)
1917 {
1918         int i;
1919         isl_union_set *D;
1920         isl_union_map *W, *R, *S;
1921         isl_union_map *empty;
1922         isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
1923         isl_union_map *validity, *proximity;
1924         isl_union_map *schedule;
1925         isl_union_map *test;
1926         isl_union_set *delta;
1927         isl_union_set *domain;
1928         isl_set *delta_set;
1929         isl_set *slice;
1930         isl_set *origin;
1931         isl_schedule *sched;
1932         int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
1933
1934         D = isl_union_set_read_from_str(ctx, d);
1935         W = isl_union_map_read_from_str(ctx, w);
1936         R = isl_union_map_read_from_str(ctx, r);
1937         S = isl_union_map_read_from_str(ctx, s);
1938
1939         W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
1940         R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
1941
1942         empty = isl_union_map_empty(isl_union_map_get_space(S));
1943         isl_union_map_compute_flow(isl_union_map_copy(R),
1944                                    isl_union_map_copy(W), empty,
1945                                    isl_union_map_copy(S),
1946                                    &dep_raw, NULL, NULL, NULL);
1947         isl_union_map_compute_flow(isl_union_map_copy(W),
1948                                    isl_union_map_copy(W),
1949                                    isl_union_map_copy(R),
1950                                    isl_union_map_copy(S),
1951                                    &dep_waw, &dep_war, NULL, NULL);
1952
1953         dep = isl_union_map_union(dep_waw, dep_war);
1954         dep = isl_union_map_union(dep, dep_raw);
1955         validity = isl_union_map_copy(dep);
1956         proximity = isl_union_map_copy(dep);
1957
1958         sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
1959                                                validity, proximity);
1960         schedule = isl_schedule_get_map(sched);
1961         isl_schedule_free(sched);
1962         isl_union_map_free(W);
1963         isl_union_map_free(R);
1964         isl_union_map_free(S);
1965
1966         is_injection = 1;
1967         isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
1968
1969         domain = isl_union_map_domain(isl_union_map_copy(schedule));
1970         is_complete = isl_union_set_is_subset(D, domain);
1971         isl_union_set_free(D);
1972         isl_union_set_free(domain);
1973
1974         test = isl_union_map_reverse(isl_union_map_copy(schedule));
1975         test = isl_union_map_apply_range(test, dep);
1976         test = isl_union_map_apply_range(test, schedule);
1977
1978         delta = isl_union_map_deltas(test);
1979         if (isl_union_set_n_set(delta) == 0) {
1980                 is_tilable = 1;
1981                 is_parallel = 1;
1982                 is_nonneg = 1;
1983                 isl_union_set_free(delta);
1984         } else {
1985                 delta_set = isl_set_from_union_set(delta);
1986
1987                 slice = isl_set_universe(isl_set_get_space(delta_set));
1988                 for (i = 0; i < tilable; ++i)
1989                         slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
1990                 is_tilable = isl_set_is_subset(delta_set, slice);
1991                 isl_set_free(slice);
1992
1993                 slice = isl_set_universe(isl_set_get_space(delta_set));
1994                 for (i = 0; i < parallel; ++i)
1995                         slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
1996                 is_parallel = isl_set_is_subset(delta_set, slice);
1997                 isl_set_free(slice);
1998
1999                 origin = isl_set_universe(isl_set_get_space(delta_set));
2000                 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2001                         origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2002
2003                 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2004                 delta_set = isl_set_lexmin(delta_set);
2005
2006                 is_nonneg = isl_set_is_equal(delta_set, origin);
2007
2008                 isl_set_free(origin);
2009                 isl_set_free(delta_set);
2010         }
2011
2012         if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2013             is_injection < 0 || is_complete < 0)
2014                 return -1;
2015         if (!is_complete)
2016                 isl_die(ctx, isl_error_unknown,
2017                         "generated schedule incomplete", return -1);
2018         if (!is_injection)
2019                 isl_die(ctx, isl_error_unknown,
2020                         "generated schedule not injective on each statement",
2021                         return -1);
2022         if (!is_nonneg)
2023                 isl_die(ctx, isl_error_unknown,
2024                         "negative dependences in generated schedule",
2025                         return -1);
2026         if (!is_tilable)
2027                 isl_die(ctx, isl_error_unknown,
2028                         "generated schedule not as tilable as expected",
2029                         return -1);
2030         if (!is_parallel)
2031                 isl_die(ctx, isl_error_unknown,
2032                         "generated schedule not as parallel as expected",
2033                         return -1);
2034
2035         return 0;
2036 }
2037
2038 int test_special_schedule(isl_ctx *ctx)
2039 {
2040         const char *str;
2041         isl_union_set *dom;
2042         isl_union_map *empty;
2043         isl_union_map *dep;
2044         isl_union_map *sched1, *sched2;
2045         isl_schedule *schedule;
2046         int equal;
2047
2048         str = "{ S[i,j] : 0 <= i <= 10 }";
2049         dom = isl_union_set_read_from_str(ctx, str);
2050         str = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2051         dep = isl_union_map_read_from_str(ctx, str);
2052         empty = isl_union_map_read_from_str(ctx, "{}");
2053         schedule = isl_union_set_compute_schedule(dom, empty, dep);
2054         sched1 = isl_schedule_get_map(schedule);
2055         isl_schedule_free(schedule);
2056
2057         str = "{ S[i, j] -> [j, i] }";
2058         sched2 = isl_union_map_read_from_str(ctx, str);
2059
2060         equal = isl_union_map_is_equal(sched1, sched2);
2061         isl_union_map_free(sched1);
2062         isl_union_map_free(sched2);
2063
2064         if (equal < 0)
2065                 return -1;
2066         if (!equal)
2067                 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2068                         return -1);
2069
2070         return 0;
2071 }
2072
2073 int test_schedule(isl_ctx *ctx)
2074 {
2075         const char *D, *W, *R, *S;
2076
2077         /* Jacobi */
2078         D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2079         W = "{ S1[t,i] -> a[t,i] }";
2080         R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2081                 "S1[t,i] -> a[t-1,i+1] }";
2082         S = "{ S1[t,i] -> [t,i] }";
2083         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2084                 return -1;
2085
2086         /* Fig. 5 of CC2008 */
2087         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2088                                 "j <= -1 + N }";
2089         W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2090                                 "j >= 2 and j <= -1 + N }";
2091         R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2092                                 "j >= 2 and j <= -1 + N; "
2093                     "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2094                                 "j >= 2 and j <= -1 + N }";
2095         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2096         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2097                 return -1;
2098
2099         D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2100         W = "{ S1[i] -> a[i] }";
2101         R = "{ S2[i] -> a[i+1] }";
2102         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2103         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2104                 return -1;
2105
2106         D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2107         W = "{ S1[i] -> a[i] }";
2108         R = "{ S2[i] -> a[9-i] }";
2109         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2110         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2111                 return -1;
2112
2113         D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2114         W = "{ S1[i] -> a[i] }";
2115         R = "[N] -> { S2[i] -> a[N-1-i] }";
2116         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2117         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2118                 return -1;
2119         
2120         D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2121         W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2122         R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2123         S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2124         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2125                 return -1;
2126
2127         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2128         W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2129         R = "{ S2[i,j] -> a[i-1,j] }";
2130         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2131         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2132                 return -1;
2133
2134         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2135         W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2136         R = "{ S2[i,j] -> a[i,j-1] }";
2137         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2138         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2139                 return -1;
2140
2141         D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2142         W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2143                     "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2144         R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2145         S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2146                     "S_0[] -> [0, 0, 0] }";
2147         if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2148                 return -1;
2149         ctx->opt->schedule_parametric = 0;
2150         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2151                 return -1;
2152         ctx->opt->schedule_parametric = 1;
2153
2154         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2155                     "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2156         W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2157         R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2158                     "S4[i] -> a[i,N] }";
2159         S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2160                 "S4[i] -> [4,i,0] }";
2161         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2162                 return -1;
2163
2164         D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2165         W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2166                                         "j <= N }";
2167         R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2168                                         "j <= N; "
2169                     "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2170                                         "j <= N }";
2171         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2172         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2173                 return -1;
2174
2175         D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2176                     " S_2[t] : t >= 0 and t <= -1 + N; "
2177                     " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2178                                 "i <= -1 + N }";
2179         W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2180                     " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2181                     " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2182                                                 "i >= 0 and i <= -1 + N }";
2183         R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2184                                             "i >= 0 and i <= -1 + N; "
2185                     " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2186         S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2187                     " S_0[t] -> [0, t, 0] }";
2188
2189         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2190                 return -1;
2191         ctx->opt->schedule_parametric = 0;
2192         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2193                 return -1;
2194         ctx->opt->schedule_parametric = 1;
2195
2196         D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2197         S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2198         if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2199                 return -1;
2200
2201         D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2202             "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2203         W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2204                                             "j >= 0 and j <= -1 + N; "
2205                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2206         R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2207                                             "j >= 0 and j <= -1 + N; "
2208                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2209         S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2210         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2211                 return -1;
2212
2213         D = "{ S_0[i] : i >= 0 }";
2214         W = "{ S_0[i] -> a[i] : i >= 0 }";
2215         R = "{ S_0[i] -> a[0] : i >= 0 }";
2216         S = "{ S_0[i] -> [0, i, 0] }";
2217         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2218                 return -1;
2219
2220         D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2221         W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2222         R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2223         S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2224         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2225                 return -1;
2226
2227         D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2228                                 "k <= -1 + n and k >= 0 }";
2229         W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "                                                  "k <= -1 + n and k >= 0 }";
2230         R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2231                                         "k <= -1 + n and k >= 0; "
2232                     "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2233                                         "k <= -1 + n and k >= 0; "
2234                     "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2235                                         "k <= -1 + n and k >= 0 }";
2236         S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2237         ctx->opt->schedule_outer_zero_distance = 1;
2238         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2239                 return -1;
2240         ctx->opt->schedule_outer_zero_distance = 0;
2241
2242         return test_special_schedule(ctx);
2243 }
2244
2245 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2246 {
2247         isl_union_map *umap;
2248         int test;
2249
2250         umap = isl_union_map_read_from_str(ctx, str);
2251         test = isl_union_map_plain_is_injective(umap);
2252         isl_union_map_free(umap);
2253         if (test < 0)
2254                 return -1;
2255         if (test == injective)
2256                 return 0;
2257         if (injective)
2258                 isl_die(ctx, isl_error_unknown,
2259                         "map not detected as injective", return -1);
2260         else
2261                 isl_die(ctx, isl_error_unknown,
2262                         "map detected as injective", return -1);
2263 }
2264
2265 int test_injective(isl_ctx *ctx)
2266 {
2267         const char *str;
2268
2269         if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2270                 return -1;
2271         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2272                 return -1;
2273         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2274                 return -1;
2275         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2276                 return -1;
2277         if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2278                 return -1;
2279         if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2280                 return -1;
2281         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2282                 return -1;
2283         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2284                 return -1;
2285
2286         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2287         if (test_plain_injective(ctx, str, 1))
2288                 return -1;
2289         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2290         if (test_plain_injective(ctx, str, 0))
2291                 return -1;
2292
2293         return 0;
2294 }
2295
2296 int test_aff(isl_ctx *ctx)
2297 {
2298         const char *str;
2299         isl_set *set;
2300         isl_space *dim;
2301         isl_local_space *ls;
2302         isl_aff *aff;
2303         int zero;
2304
2305         dim = isl_space_set_alloc(ctx, 0, 1);
2306         ls = isl_local_space_from_space(dim);
2307         aff = isl_aff_zero_on_domain(ls);
2308
2309         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2310         aff = isl_aff_scale_down_ui(aff, 3);
2311         aff = isl_aff_floor(aff);
2312         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2313         aff = isl_aff_scale_down_ui(aff, 2);
2314         aff = isl_aff_floor(aff);
2315         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2316
2317         str = "{ [10] }";
2318         set = isl_set_read_from_str(ctx, str);
2319         aff = isl_aff_gist(aff, set);
2320
2321         aff = isl_aff_add_constant_si(aff, -16);
2322         zero = isl_aff_plain_is_zero(aff);
2323         isl_aff_free(aff);
2324
2325         if (zero < 0)
2326                 return -1;
2327         if (!zero)
2328                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2329
2330         return 0;
2331 }
2332
2333 int test_dim_max(isl_ctx *ctx)
2334 {
2335         int equal;
2336         const char *str;
2337         isl_set *set1, *set2;
2338         isl_set *set;
2339         isl_map *map;
2340         isl_pw_aff *pwaff;
2341
2342         str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2343         set = isl_set_read_from_str(ctx, str);
2344         pwaff = isl_set_dim_max(set, 0);
2345         set1 = isl_set_from_pw_aff(pwaff);
2346         str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2347         set2 = isl_set_read_from_str(ctx, str);
2348         equal = isl_set_is_equal(set1, set2);
2349         isl_set_free(set1);
2350         isl_set_free(set2);
2351         if (equal < 0)
2352                 return -1;
2353         if (!equal)
2354                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2355
2356         str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2357         set = isl_set_read_from_str(ctx, str);
2358         pwaff = isl_set_dim_max(set, 0);
2359         set1 = isl_set_from_pw_aff(pwaff);
2360         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2361         set2 = isl_set_read_from_str(ctx, str);
2362         equal = isl_set_is_equal(set1, set2);
2363         isl_set_free(set1);
2364         isl_set_free(set2);
2365         if (equal < 0)
2366                 return -1;
2367         if (!equal)
2368                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2369
2370         str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2371         set = isl_set_read_from_str(ctx, str);
2372         pwaff = isl_set_dim_max(set, 0);
2373         set1 = isl_set_from_pw_aff(pwaff);
2374         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2375         set2 = isl_set_read_from_str(ctx, str);
2376         equal = isl_set_is_equal(set1, set2);
2377         isl_set_free(set1);
2378         isl_set_free(set2);
2379         if (equal < 0)
2380                 return -1;
2381         if (!equal)
2382                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2383
2384         str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2385                         "0 <= i < N and 0 <= j < M }";
2386         map = isl_map_read_from_str(ctx, str);
2387         set = isl_map_range(map);
2388
2389         pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2390         set1 = isl_set_from_pw_aff(pwaff);
2391         str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2392         set2 = isl_set_read_from_str(ctx, str);
2393         equal = isl_set_is_equal(set1, set2);
2394         isl_set_free(set1);
2395         isl_set_free(set2);
2396
2397         pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2398         set1 = isl_set_from_pw_aff(pwaff);
2399         str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2400         set2 = isl_set_read_from_str(ctx, str);
2401         if (equal >= 0 && equal)
2402                 equal = isl_set_is_equal(set1, set2);
2403         isl_set_free(set1);
2404         isl_set_free(set2);
2405
2406         isl_set_free(set);
2407
2408         if (equal < 0)
2409                 return -1;
2410         if (!equal)
2411                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2412
2413         return 0;
2414 }
2415
2416 int test_product(isl_ctx *ctx)
2417 {
2418         const char *str;
2419         isl_set *set;
2420         int ok;
2421
2422         str = "{ A[i] }";
2423         set = isl_set_read_from_str(ctx, str);
2424         set = isl_set_product(set, isl_set_copy(set));
2425         ok = isl_set_is_wrapping(set);
2426         isl_set_free(set);
2427         if (ok < 0)
2428                 return -1;
2429         if (!ok)
2430                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2431
2432         return 0;
2433 }
2434
2435 int test_equal(isl_ctx *ctx)
2436 {
2437         const char *str;
2438         isl_set *set, *set2;
2439         int equal;
2440
2441         str = "{ S_6[i] }";
2442         set = isl_set_read_from_str(ctx, str);
2443         str = "{ S_7[i] }";
2444         set2 = isl_set_read_from_str(ctx, str);
2445         equal = isl_set_is_equal(set, set2);
2446         isl_set_free(set);
2447         isl_set_free(set2);
2448         if (equal < 0)
2449                 return -1;
2450         if (equal)
2451                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2452
2453         return 0;
2454 }
2455
2456 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2457         enum isl_dim_type type, unsigned pos, int fixed)
2458 {
2459         int test;
2460
2461         test = isl_map_plain_is_fixed(map, type, pos, NULL);
2462         isl_map_free(map);
2463         if (test < 0)
2464                 return -1;
2465         if (test == fixed)
2466                 return 0;
2467         if (fixed)
2468                 isl_die(ctx, isl_error_unknown,
2469                         "map not detected as fixed", return -1);
2470         else
2471                 isl_die(ctx, isl_error_unknown,
2472                         "map detected as fixed", return -1);
2473 }
2474
2475 int test_fixed(isl_ctx *ctx)
2476 {
2477         const char *str;
2478         isl_map *map;
2479
2480         str = "{ [i] -> [i] }";
2481         map = isl_map_read_from_str(ctx, str);
2482         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2483                 return -1;
2484         str = "{ [i] -> [1] }";
2485         map = isl_map_read_from_str(ctx, str);
2486         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2487                 return -1;
2488         str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2489         map = isl_map_read_from_str(ctx, str);
2490         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2491                 return -1;
2492         map = isl_map_read_from_str(ctx, str);
2493         map = isl_map_neg(map);
2494         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2495                 return -1;
2496
2497         return 0;
2498 }
2499
2500 int test_union_pw(isl_ctx *ctx)
2501 {
2502         int equal;
2503         const char *str;
2504         isl_union_set *uset;
2505         isl_union_pw_qpolynomial *upwqp1, *upwqp2;
2506
2507         str = "{ [x] -> x^2 }";
2508         upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
2509         upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
2510         uset = isl_union_pw_qpolynomial_domain(upwqp1);
2511         upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
2512         upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
2513         equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
2514         isl_union_pw_qpolynomial_free(upwqp1);
2515         isl_union_pw_qpolynomial_free(upwqp2);
2516         if (equal < 0)
2517                 return -1;
2518         if (!equal)
2519                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2520
2521         return 0;
2522 }
2523
2524 int main()
2525 {
2526         struct isl_ctx *ctx;
2527
2528         srcdir = getenv("srcdir");
2529         assert(srcdir);
2530
2531         ctx = isl_ctx_alloc();
2532         if (test_fixed(ctx) < 0)
2533                 goto error;
2534         if (test_equal(ctx) < 0)
2535                 goto error;
2536         if (test_product(ctx) < 0)
2537                 goto error;
2538         if (test_dim_max(ctx) < 0)
2539                 goto error;
2540         if (test_aff(ctx) < 0)
2541                 goto error;
2542         if (test_injective(ctx) < 0)
2543                 goto error;
2544         if (test_schedule(ctx) < 0)
2545                 goto error;
2546         if (test_union_pw(ctx) < 0)
2547                 goto error;
2548         test_factorize(ctx);
2549         test_subset(ctx);
2550         test_lift(ctx);
2551         test_bound(ctx);
2552         test_union(ctx);
2553         test_split_periods(ctx);
2554         test_parse(ctx);
2555         test_pwqp(ctx);
2556         test_lex(ctx);
2557         if (test_sv(ctx) < 0)
2558                 goto error;
2559         test_bijective(ctx);
2560         test_dep(ctx);
2561         test_read(ctx);
2562         test_bounded(ctx);
2563         test_construction(ctx);
2564         test_dim(ctx);
2565         test_div(ctx);
2566         test_application(ctx);
2567         test_affine_hull(ctx);
2568         test_convex_hull(ctx);
2569         test_gist(ctx);
2570         test_coalesce(ctx);
2571         test_closure(ctx);
2572         test_lexmin(ctx);
2573         isl_ctx_free(ctx);
2574         return 0;
2575 error:
2576         isl_ctx_free(ctx);
2577         return -1;
2578 }