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