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