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