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