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