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