isl_stream_read_map: accept min and max expressions in constraints
[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
22 static char *srcdir;
23
24 void test_parse_map(isl_ctx *ctx, const char *str)
25 {
26         isl_map *map;
27
28         map = isl_map_read_from_str(ctx, str, -1);
29         assert(map);
30         isl_map_free(map);
31 }
32
33 void test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
34 {
35         isl_map *map, *map2;
36
37         map = isl_map_read_from_str(ctx, str, -1);
38         map2 = isl_map_read_from_str(ctx, str2, -1);
39         assert(map && map2 && isl_map_is_equal(map, map2));
40         isl_map_free(map);
41         isl_map_free(map2);
42 }
43
44 void test_parse_pwqp(isl_ctx *ctx, const char *str)
45 {
46         isl_pw_qpolynomial *pwqp;
47
48         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
49         assert(pwqp);
50         isl_pw_qpolynomial_free(pwqp);
51 }
52
53 void test_parse(struct isl_ctx *ctx)
54 {
55         isl_map *map, *map2;
56         const char *str, *str2;
57
58         str = "{ [i] -> [-i] }";
59         map = isl_map_read_from_str(ctx, str, -1);
60         assert(map);
61         isl_map_free(map);
62
63         str = "{ A[i] -> L[([i/3])] }";
64         map = isl_map_read_from_str(ctx, str, -1);
65         assert(map);
66         isl_map_free(map);
67
68         test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
69         test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
70                                 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
71
72         str = "{ [x,y]  : [([x/2]+y)/3] >= 1 }";
73         str2 = "{ [x, y] : 2y >= 6 - x }";
74         test_parse_map_equal(ctx, str, str2);
75
76         test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
77                                   "{ [x,y] : x <= y, 2*y + 3 }");
78         str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
79         test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }", str);
80
81         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
82         map = isl_map_read_from_str(ctx, str, -1);
83         str = "{ [new, old] -> [o0, o1] : "
84                "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
85                "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
86                "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
87         map2 = isl_map_read_from_str(ctx, str, -1);
88         assert(isl_map_is_equal(map, map2));
89         isl_map_free(map);
90         isl_map_free(map2);
91
92         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
93         map = isl_map_read_from_str(ctx, str, -1);
94         str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
95         map2 = isl_map_read_from_str(ctx, str, -1);
96         assert(isl_map_is_equal(map, map2));
97         isl_map_free(map);
98         isl_map_free(map2);
99
100         test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
101 }
102
103 void test_read(struct isl_ctx *ctx)
104 {
105         char filename[PATH_MAX];
106         FILE *input;
107         int n;
108         struct isl_basic_set *bset1, *bset2;
109         const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
110
111         n = snprintf(filename, sizeof(filename),
112                         "%s/test_inputs/set.omega", srcdir);
113         assert(n < sizeof(filename));
114         input = fopen(filename, "r");
115         assert(input);
116
117         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
118         bset2 = isl_basic_set_read_from_str(ctx, str, 0);
119
120         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
121
122         isl_basic_set_free(bset1);
123         isl_basic_set_free(bset2);
124
125         fclose(input);
126 }
127
128 void test_bounded(struct isl_ctx *ctx)
129 {
130         isl_set *set;
131         int bounded;
132
133         set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }", -1);
134         bounded = isl_set_is_bounded(set);
135         assert(bounded);
136         isl_set_free(set);
137
138         set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }", -1);
139         bounded = isl_set_is_bounded(set);
140         assert(!bounded);
141         isl_set_free(set);
142
143         set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }", -1);
144         bounded = isl_set_is_bounded(set);
145         assert(!bounded);
146         isl_set_free(set);
147 }
148
149 /* Construct the basic set { [i] : 5 <= i <= N } */
150 void test_construction(struct isl_ctx *ctx)
151 {
152         isl_int v;
153         struct isl_dim *dim;
154         struct isl_basic_set *bset;
155         struct isl_constraint *c;
156
157         isl_int_init(v);
158
159         dim = isl_dim_set_alloc(ctx, 1, 1);
160         bset = isl_basic_set_universe(dim);
161
162         c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
163         isl_int_set_si(v, -1);
164         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
165         isl_int_set_si(v, 1);
166         isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
167         bset = isl_basic_set_add_constraint(bset, c);
168
169         c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
170         isl_int_set_si(v, 1);
171         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
172         isl_int_set_si(v, -5);
173         isl_constraint_set_constant(c, v);
174         bset = isl_basic_set_add_constraint(bset, c);
175
176         isl_basic_set_free(bset);
177
178         isl_int_clear(v);
179 }
180
181 void test_dim(struct isl_ctx *ctx)
182 {
183         const char *str;
184         isl_map *map1, *map2;
185
186         map1 = isl_map_read_from_str(ctx,
187             "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
188         map1 = isl_map_add_dims(map1, isl_dim_in, 1);
189         map2 = isl_map_read_from_str(ctx,
190             "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
191         assert(isl_map_is_equal(map1, map2));
192         isl_map_free(map2);
193
194         map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
195         map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }", -1);
196         assert(isl_map_is_equal(map1, map2));
197
198         isl_map_free(map1);
199         isl_map_free(map2);
200
201         str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
202         map1 = isl_map_read_from_str(ctx, str, -1);
203         str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
204         map2 = isl_map_read_from_str(ctx, str, -1);
205         map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
206         assert(isl_map_is_equal(map1, map2));
207
208         isl_map_free(map1);
209         isl_map_free(map2);
210 }
211
212 void test_div(struct isl_ctx *ctx)
213 {
214         isl_int v;
215         int pos;
216         struct isl_dim *dim;
217         struct isl_div *div;
218         struct isl_basic_set *bset;
219         struct isl_constraint *c;
220
221         isl_int_init(v);
222
223         /* test 1 */
224         dim = isl_dim_set_alloc(ctx, 0, 1);
225         bset = isl_basic_set_universe(dim);
226
227         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
228         isl_int_set_si(v, -1);
229         isl_constraint_set_constant(c, v);
230         isl_int_set_si(v, 1);
231         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
232         div = isl_div_alloc(isl_basic_set_get_dim(bset));
233         c = isl_constraint_add_div(c, div, &pos);
234         isl_int_set_si(v, 3);
235         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
236         bset = isl_basic_set_add_constraint(bset, c);
237
238         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
239         isl_int_set_si(v, 1);
240         isl_constraint_set_constant(c, v);
241         isl_int_set_si(v, -1);
242         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
243         div = isl_div_alloc(isl_basic_set_get_dim(bset));
244         c = isl_constraint_add_div(c, div, &pos);
245         isl_int_set_si(v, 3);
246         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
247         bset = isl_basic_set_add_constraint(bset, c);
248
249         assert(bset && bset->n_div == 1);
250         isl_basic_set_free(bset);
251
252         /* test 2 */
253         dim = isl_dim_set_alloc(ctx, 0, 1);
254         bset = isl_basic_set_universe(dim);
255
256         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
257         isl_int_set_si(v, 1);
258         isl_constraint_set_constant(c, v);
259         isl_int_set_si(v, -1);
260         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
261         div = isl_div_alloc(isl_basic_set_get_dim(bset));
262         c = isl_constraint_add_div(c, div, &pos);
263         isl_int_set_si(v, 3);
264         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
265         bset = isl_basic_set_add_constraint(bset, c);
266
267         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
268         isl_int_set_si(v, -1);
269         isl_constraint_set_constant(c, v);
270         isl_int_set_si(v, 1);
271         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
272         div = isl_div_alloc(isl_basic_set_get_dim(bset));
273         c = isl_constraint_add_div(c, div, &pos);
274         isl_int_set_si(v, 3);
275         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
276         bset = isl_basic_set_add_constraint(bset, c);
277
278         assert(bset && bset->n_div == 1);
279         isl_basic_set_free(bset);
280
281         /* test 3 */
282         dim = isl_dim_set_alloc(ctx, 0, 1);
283         bset = isl_basic_set_universe(dim);
284
285         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
286         isl_int_set_si(v, 1);
287         isl_constraint_set_constant(c, v);
288         isl_int_set_si(v, -1);
289         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
290         div = isl_div_alloc(isl_basic_set_get_dim(bset));
291         c = isl_constraint_add_div(c, div, &pos);
292         isl_int_set_si(v, 3);
293         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
294         bset = isl_basic_set_add_constraint(bset, c);
295
296         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
297         isl_int_set_si(v, -3);
298         isl_constraint_set_constant(c, v);
299         isl_int_set_si(v, 1);
300         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
301         div = isl_div_alloc(isl_basic_set_get_dim(bset));
302         c = isl_constraint_add_div(c, div, &pos);
303         isl_int_set_si(v, 4);
304         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
305         bset = isl_basic_set_add_constraint(bset, c);
306
307         assert(bset && bset->n_div == 1);
308         isl_basic_set_free(bset);
309
310         /* test 4 */
311         dim = isl_dim_set_alloc(ctx, 0, 1);
312         bset = isl_basic_set_universe(dim);
313
314         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
315         isl_int_set_si(v, 2);
316         isl_constraint_set_constant(c, v);
317         isl_int_set_si(v, -1);
318         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
319         div = isl_div_alloc(isl_basic_set_get_dim(bset));
320         c = isl_constraint_add_div(c, div, &pos);
321         isl_int_set_si(v, 3);
322         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
323         bset = isl_basic_set_add_constraint(bset, c);
324
325         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
326         isl_int_set_si(v, -1);
327         isl_constraint_set_constant(c, v);
328         isl_int_set_si(v, 1);
329         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
330         div = isl_div_alloc(isl_basic_set_get_dim(bset));
331         c = isl_constraint_add_div(c, div, &pos);
332         isl_int_set_si(v, 6);
333         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
334         bset = isl_basic_set_add_constraint(bset, c);
335
336         assert(isl_basic_set_is_empty(bset));
337         isl_basic_set_free(bset);
338
339         /* test 5 */
340         dim = isl_dim_set_alloc(ctx, 0, 2);
341         bset = isl_basic_set_universe(dim);
342
343         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
344         isl_int_set_si(v, -1);
345         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
346         div = isl_div_alloc(isl_basic_set_get_dim(bset));
347         c = isl_constraint_add_div(c, div, &pos);
348         isl_int_set_si(v, 3);
349         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
350         bset = isl_basic_set_add_constraint(bset, c);
351
352         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
353         isl_int_set_si(v, 1);
354         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
355         isl_int_set_si(v, -3);
356         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
357         bset = isl_basic_set_add_constraint(bset, c);
358
359         assert(bset && bset->n_div == 0);
360         isl_basic_set_free(bset);
361
362         /* test 6 */
363         dim = isl_dim_set_alloc(ctx, 0, 2);
364         bset = isl_basic_set_universe(dim);
365
366         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
367         isl_int_set_si(v, -1);
368         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
369         div = isl_div_alloc(isl_basic_set_get_dim(bset));
370         c = isl_constraint_add_div(c, div, &pos);
371         isl_int_set_si(v, 6);
372         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
373         bset = isl_basic_set_add_constraint(bset, c);
374
375         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
376         isl_int_set_si(v, 1);
377         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
378         isl_int_set_si(v, -3);
379         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
380         bset = isl_basic_set_add_constraint(bset, c);
381
382         assert(bset && bset->n_div == 1);
383         isl_basic_set_free(bset);
384
385         /* test 7 */
386         /* This test is a bit tricky.  We set up an equality
387          *              a + 3b + 3c = 6 e0
388          * Normalization of divs creates _two_ divs
389          *              a = 3 e0
390          *              c - b - e0 = 2 e1
391          * Afterwards e0 is removed again because it has coefficient -1
392          * and we end up with the original equality and div again.
393          * Perhaps we can avoid the introduction of this temporary div.
394          */
395         dim = isl_dim_set_alloc(ctx, 0, 3);
396         bset = isl_basic_set_universe(dim);
397
398         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
399         isl_int_set_si(v, -1);
400         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
401         isl_int_set_si(v, -3);
402         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
403         isl_int_set_si(v, -3);
404         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
405         div = isl_div_alloc(isl_basic_set_get_dim(bset));
406         c = isl_constraint_add_div(c, div, &pos);
407         isl_int_set_si(v, 6);
408         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
409         bset = isl_basic_set_add_constraint(bset, c);
410
411         /* Test disabled for now */
412         /*
413         assert(bset && bset->n_div == 1);
414         */
415         isl_basic_set_free(bset);
416
417         /* test 8 */
418         dim = isl_dim_set_alloc(ctx, 0, 4);
419         bset = isl_basic_set_universe(dim);
420
421         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
422         isl_int_set_si(v, -1);
423         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
424         isl_int_set_si(v, -3);
425         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
426         isl_int_set_si(v, -3);
427         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
428         div = isl_div_alloc(isl_basic_set_get_dim(bset));
429         c = isl_constraint_add_div(c, div, &pos);
430         isl_int_set_si(v, 6);
431         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
432         bset = isl_basic_set_add_constraint(bset, c);
433
434         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
435         isl_int_set_si(v, -1);
436         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
437         isl_int_set_si(v, 1);
438         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
439         isl_int_set_si(v, 1);
440         isl_constraint_set_constant(c, v);
441         bset = isl_basic_set_add_constraint(bset, c);
442
443         /* Test disabled for now */
444         /*
445         assert(bset && bset->n_div == 1);
446         */
447         isl_basic_set_free(bset);
448
449         /* test 9 */
450         dim = isl_dim_set_alloc(ctx, 0, 2);
451         bset = isl_basic_set_universe(dim);
452
453         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
454         isl_int_set_si(v, 1);
455         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
456         isl_int_set_si(v, -1);
457         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
458         div = isl_div_alloc(isl_basic_set_get_dim(bset));
459         c = isl_constraint_add_div(c, div, &pos);
460         isl_int_set_si(v, -2);
461         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
462         bset = isl_basic_set_add_constraint(bset, c);
463
464         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
465         isl_int_set_si(v, -1);
466         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
467         div = isl_div_alloc(isl_basic_set_get_dim(bset));
468         c = isl_constraint_add_div(c, div, &pos);
469         isl_int_set_si(v, 3);
470         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
471         isl_int_set_si(v, 2);
472         isl_constraint_set_constant(c, v);
473         bset = isl_basic_set_add_constraint(bset, c);
474
475         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
476
477         assert(!isl_basic_set_is_empty(bset));
478
479         isl_basic_set_free(bset);
480
481         /* test 10 */
482         dim = isl_dim_set_alloc(ctx, 0, 2);
483         bset = isl_basic_set_universe(dim);
484
485         c = isl_equality_alloc(isl_basic_set_get_dim(bset));
486         isl_int_set_si(v, 1);
487         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
488         div = isl_div_alloc(isl_basic_set_get_dim(bset));
489         c = isl_constraint_add_div(c, div, &pos);
490         isl_int_set_si(v, -2);
491         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
492         bset = isl_basic_set_add_constraint(bset, c);
493
494         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
495
496         isl_basic_set_free(bset);
497
498         isl_int_clear(v);
499 }
500
501 void test_application_case(struct isl_ctx *ctx, const char *name)
502 {
503         char filename[PATH_MAX];
504         FILE *input;
505         int n;
506         struct isl_basic_set *bset1, *bset2;
507         struct isl_basic_map *bmap;
508
509         n = snprintf(filename, sizeof(filename),
510                         "%s/test_inputs/%s.omega", srcdir, name);
511         assert(n < sizeof(filename));
512         input = fopen(filename, "r");
513         assert(input);
514
515         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
516         bmap = isl_basic_map_read_from_file(ctx, input, 0);
517
518         bset1 = isl_basic_set_apply(bset1, bmap);
519
520         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
521
522         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
523
524         isl_basic_set_free(bset1);
525         isl_basic_set_free(bset2);
526
527         fclose(input);
528 }
529
530 void test_application(struct isl_ctx *ctx)
531 {
532         test_application_case(ctx, "application");
533         test_application_case(ctx, "application2");
534 }
535
536 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
537 {
538         char filename[PATH_MAX];
539         FILE *input;
540         int n;
541         struct isl_basic_set *bset1, *bset2;
542
543         n = snprintf(filename, sizeof(filename),
544                         "%s/test_inputs/%s.polylib", srcdir, name);
545         assert(n < sizeof(filename));
546         input = fopen(filename, "r");
547         assert(input);
548
549         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
550         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
551
552         bset1 = isl_basic_set_affine_hull(bset1);
553
554         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
555
556         isl_basic_set_free(bset1);
557         isl_basic_set_free(bset2);
558
559         fclose(input);
560 }
561
562 void test_affine_hull(struct isl_ctx *ctx)
563 {
564         test_affine_hull_case(ctx, "affine2");
565         test_affine_hull_case(ctx, "affine");
566         test_affine_hull_case(ctx, "affine3");
567 }
568
569 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
570 {
571         char filename[PATH_MAX];
572         FILE *input;
573         int n;
574         struct isl_basic_set *bset1, *bset2;
575         struct isl_set *set;
576
577         n = snprintf(filename, sizeof(filename),
578                         "%s/test_inputs/%s.polylib", srcdir, name);
579         assert(n < sizeof(filename));
580         input = fopen(filename, "r");
581         assert(input);
582
583         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
584         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
585
586         set = isl_basic_set_union(bset1, bset2);
587         bset1 = isl_set_convex_hull(set);
588
589         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
590
591         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
592
593         isl_basic_set_free(bset1);
594         isl_basic_set_free(bset2);
595
596         fclose(input);
597 }
598
599 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
600 {
601         const char *str1, *str2;
602         isl_set *set1, *set2;
603         int orig_convex = ctx->opt->convex;
604         ctx->opt->convex = convex;
605
606         test_convex_hull_case(ctx, "convex0");
607         test_convex_hull_case(ctx, "convex1");
608         test_convex_hull_case(ctx, "convex2");
609         test_convex_hull_case(ctx, "convex3");
610         test_convex_hull_case(ctx, "convex4");
611         test_convex_hull_case(ctx, "convex5");
612         test_convex_hull_case(ctx, "convex6");
613         test_convex_hull_case(ctx, "convex7");
614         test_convex_hull_case(ctx, "convex8");
615         test_convex_hull_case(ctx, "convex9");
616         test_convex_hull_case(ctx, "convex10");
617         test_convex_hull_case(ctx, "convex11");
618         test_convex_hull_case(ctx, "convex12");
619         test_convex_hull_case(ctx, "convex13");
620         test_convex_hull_case(ctx, "convex14");
621         test_convex_hull_case(ctx, "convex15");
622
623         str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
624                "(i0 = 1 and i1 = 0 and i2 = 1) or "
625                "(i0 = 0 and i1 = 0 and i2 = 0) }";
626         str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
627         set1 = isl_set_read_from_str(ctx, str1, -1);
628         set2 = isl_set_read_from_str(ctx, str2, -1);
629         set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
630         assert(isl_set_is_equal(set1, set2));
631         isl_set_free(set1);
632         isl_set_free(set2);
633
634         ctx->opt->convex = orig_convex;
635 }
636
637 void test_convex_hull(struct isl_ctx *ctx)
638 {
639         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
640         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
641 }
642
643 void test_gist_case(struct isl_ctx *ctx, const char *name)
644 {
645         char filename[PATH_MAX];
646         FILE *input;
647         int n;
648         struct isl_basic_set *bset1, *bset2;
649
650         n = snprintf(filename, sizeof(filename),
651                         "%s/test_inputs/%s.polylib", srcdir, name);
652         assert(n < sizeof(filename));
653         input = fopen(filename, "r");
654         assert(input);
655
656         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
657         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
658
659         bset1 = isl_basic_set_gist(bset1, bset2);
660
661         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
662
663         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
664
665         isl_basic_set_free(bset1);
666         isl_basic_set_free(bset2);
667
668         fclose(input);
669 }
670
671 void test_gist(struct isl_ctx *ctx)
672 {
673         const char *str;
674         isl_basic_set *bset1, *bset2;
675
676         test_gist_case(ctx, "gist1");
677
678         str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
679             "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
680             "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
681             "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
682             "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
683             "16e0 >= 16 + 16p6 + 15p10 and  p2 <= 15 and p3 >= 0 and "
684             "p3 <= 31 and  p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
685             "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
686             "p10 <= 15 and p10 <= -1 + p0 - p6) }";
687         bset1 = isl_basic_set_read_from_str(ctx, str, -1);
688         str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
689             "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
690             "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
691             "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
692             "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
693             "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
694             "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
695             "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
696         bset2 = isl_basic_set_read_from_str(ctx, str, -1);
697         bset1 = isl_basic_set_gist(bset1, bset2);
698         assert(bset1 && bset1->n_div == 0);
699         isl_basic_set_free(bset1);
700 }
701
702 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
703 {
704         isl_set *set, *set2;
705
706         set = isl_set_read_from_str(ctx, str, -1);
707         set = isl_set_coalesce(set);
708         set2 = isl_set_read_from_str(ctx, str, -1);
709         assert(isl_set_is_equal(set, set2));
710         if (check_one)
711                 assert(set && set->n == 1);
712         isl_set_free(set);
713         isl_set_free(set2);
714 }
715
716 void test_coalesce(struct isl_ctx *ctx)
717 {
718         const char *str;
719         struct isl_set *set, *set2;
720         struct isl_map *map, *map2;
721
722         set = isl_set_read_from_str(ctx,
723                 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
724                        "y >= x & x >= 2 & 5 >= y }", -1);
725         set = isl_set_coalesce(set);
726         assert(set && set->n == 1);
727         isl_set_free(set);
728
729         set = isl_set_read_from_str(ctx,
730                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
731                        "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
732         set = isl_set_coalesce(set);
733         assert(set && set->n == 1);
734         isl_set_free(set);
735
736         set = isl_set_read_from_str(ctx,
737                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
738                        "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
739         set = isl_set_coalesce(set);
740         assert(set && set->n == 2);
741         isl_set_free(set);
742
743         set = isl_set_read_from_str(ctx,
744                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
745                        "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
746         set = isl_set_coalesce(set);
747         assert(set && set->n == 1);
748         isl_set_free(set);
749
750         set = isl_set_read_from_str(ctx,
751                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
752                        "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
753         set = isl_set_coalesce(set);
754         assert(set && set->n == 2);
755         isl_set_free(set);
756
757         set = isl_set_read_from_str(ctx,
758                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
759                        "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
760         set = isl_set_coalesce(set);
761         assert(set && set->n == 2);
762         isl_set_free(set);
763
764         set = isl_set_read_from_str(ctx,
765                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
766                        "y >= 0 & x = 6 & y <= 6}", -1);
767         set = isl_set_coalesce(set);
768         assert(set && set->n == 1);
769         isl_set_free(set);
770
771         set = isl_set_read_from_str(ctx,
772                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
773                        "y >= 0 & x = 7 & y <= 6}", -1);
774         set = isl_set_coalesce(set);
775         assert(set && set->n == 2);
776         isl_set_free(set);
777
778         set = isl_set_read_from_str(ctx,
779                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
780                        "y >= 0 & x = 6 & y <= 5}", -1);
781         set = isl_set_coalesce(set);
782         assert(set && set->n == 1);
783         set2 = isl_set_read_from_str(ctx,
784                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
785                        "y >= 0 & x = 6 & y <= 5}", -1);
786         assert(isl_set_is_equal(set, set2));
787         isl_set_free(set);
788         isl_set_free(set2);
789
790         set = isl_set_read_from_str(ctx,
791                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
792                        "y >= 0 & x = 6 & y <= 7}", -1);
793         set = isl_set_coalesce(set);
794         assert(set && set->n == 2);
795         isl_set_free(set);
796
797         set = isl_set_read_from_str(ctx,
798                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
799         set = isl_set_coalesce(set);
800         assert(set && set->n == 1);
801         set2 = isl_set_read_from_str(ctx,
802                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
803         assert(isl_set_is_equal(set, set2));
804         isl_set_free(set);
805         isl_set_free(set2);
806
807         set = isl_set_read_from_str(ctx,
808                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
809         set = isl_set_coalesce(set);
810         set2 = isl_set_read_from_str(ctx,
811                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
812         assert(isl_set_is_equal(set, set2));
813         isl_set_free(set);
814         isl_set_free(set2);
815
816         set = isl_set_read_from_str(ctx,
817                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
818                                 "2 <= i and i <= n }", -1);
819         set = isl_set_coalesce(set);
820         assert(set && set->n == 1);
821         set2 = isl_set_read_from_str(ctx,
822                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
823                                 "2 <= i and i <= n }", -1);
824         assert(isl_set_is_equal(set, set2));
825         isl_set_free(set);
826         isl_set_free(set2);
827
828         map = isl_map_read_from_str(ctx,
829                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
830                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
831                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
832                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
833                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
834                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
835                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
836                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
837                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
838                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
839                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
840                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
841                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
842                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
843                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
844                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
845                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
846                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
847         map = isl_map_coalesce(map);
848         map2 = isl_map_read_from_str(ctx,
849                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
850                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
851                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
852                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
853                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
854                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
855                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
856                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
857                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
858                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
859                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
860                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
861                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
862                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
863                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
864                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
865                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
866                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
867         assert(isl_map_is_equal(map, map2));
868         isl_map_free(map);
869         isl_map_free(map2);
870
871         str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
872               "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
873               "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
874               "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
875         map = isl_map_read_from_str(ctx, str, -1);
876         map = isl_map_coalesce(map);
877         map2 = isl_map_read_from_str(ctx, str, -1);
878         assert(isl_map_is_equal(map, map2));
879         isl_map_free(map);
880         isl_map_free(map2);
881
882         str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
883           "[o0, o1, o2, o3, o4, o5, o6] : "
884           "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
885           "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
886           "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
887           "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
888           "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
889           "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
890           "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
891           "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
892           "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
893           "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
894           "o6 >= i3 + i6 - o3 and M >= 0 and "
895           "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
896           "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
897         map = isl_map_read_from_str(ctx, str, -1);
898         map = isl_map_coalesce(map);
899         map2 = isl_map_read_from_str(ctx, str, -1);
900         assert(isl_map_is_equal(map, map2));
901         isl_map_free(map);
902         isl_map_free(map2);
903
904         str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
905                 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
906                 "(o0 = 0 and M >= 2 and N >= 3) or "
907                 "(M = 0 and o0 = 0 and N >= 3) }";
908         map = isl_map_read_from_str(ctx, str, -1);
909         map = isl_map_coalesce(map);
910         map2 = isl_map_read_from_str(ctx, str, -1);
911         assert(isl_map_is_equal(map, map2));
912         isl_map_free(map);
913         isl_map_free(map2);
914
915         str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
916                 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
917                 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
918                 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
919         map = isl_map_read_from_str(ctx, str, -1);
920         map = isl_map_coalesce(map);
921         map2 = isl_map_read_from_str(ctx, str, -1);
922         assert(isl_map_is_equal(map, map2));
923         isl_map_free(map);
924         isl_map_free(map2);
925
926         test_coalesce_set(ctx,
927                 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
928                                 "(i1 = M and M >= 1) }", 0);
929         test_coalesce_set(ctx,
930                 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
931         test_coalesce_set(ctx,
932                 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
933                 "(y = 3 and x = 1) }", 1);
934         test_coalesce_set(ctx,
935                 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
936                 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
937                 "i1 <= M and i3 <= M and i4 <= M) or "
938                 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
939                 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
940                 "i4 <= -1 + M) }", 1);
941         test_coalesce_set(ctx,
942                 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
943                 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
944         test_coalesce_set(ctx,
945                 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
946                         "-x - y + 1 >= 0 and -3 <= z <= 3;"
947                 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
948                         "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
949         test_coalesce_set(ctx,
950                 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
951         test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
952         test_coalesce_set(ctx,
953                 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
954         test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
955         test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1);
956         test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0);
957         test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1);
958         test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0);
959         test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0);
960         test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0);
961 }
962
963 void test_closure(struct isl_ctx *ctx)
964 {
965         const char *str;
966         isl_set *dom;
967         isl_map *up, *right;
968         isl_map *map, *map2;
969         int exact;
970
971         /* COCOA example 1 */
972         map = isl_map_read_from_str(ctx,
973                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
974                         "1 <= i and i < n and 1 <= j and j < n or "
975                         "i2 = i + 1 and j2 = j - 1 and "
976                         "1 <= i and i < n and 2 <= j and j <= n }", -1);
977         map = isl_map_power(map, &exact);
978         assert(exact);
979         isl_map_free(map);
980
981         /* COCOA example 1 */
982         map = isl_map_read_from_str(ctx,
983                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
984                         "1 <= i and i < n and 1 <= j and j < n or "
985                         "i2 = i + 1 and j2 = j - 1 and "
986                         "1 <= i and i < n and 2 <= j and j <= n }", -1);
987         map = isl_map_transitive_closure(map, &exact);
988         assert(exact);
989         map2 = isl_map_read_from_str(ctx,
990                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
991                         "1 <= i and i < n and 1 <= j and j <= n and "
992                         "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
993                         "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
994                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
995         assert(isl_map_is_equal(map, map2));
996         isl_map_free(map2);
997         isl_map_free(map);
998
999         map = isl_map_read_from_str(ctx,
1000                 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1001                                      " 0 <= y and y <= n }", -1);
1002         map = isl_map_transitive_closure(map, &exact);
1003         map2 = isl_map_read_from_str(ctx,
1004                 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1005                                      " 0 <= y and y <= n }", -1);
1006         assert(isl_map_is_equal(map, map2));
1007         isl_map_free(map2);
1008         isl_map_free(map);
1009
1010         /* COCOA example 2 */
1011         map = isl_map_read_from_str(ctx,
1012                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1013                         "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1014                         "i2 = i + 2 and j2 = j - 2 and "
1015                         "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
1016         map = isl_map_transitive_closure(map, &exact);
1017         assert(exact);
1018         map2 = isl_map_read_from_str(ctx,
1019                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1020                         "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1021                         "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1022                         "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1023                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
1024         assert(isl_map_is_equal(map, map2));
1025         isl_map_free(map);
1026         isl_map_free(map2);
1027
1028         /* COCOA Fig.2 left */
1029         map = isl_map_read_from_str(ctx,
1030                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1031                         "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1032                         "j <= n or "
1033                         "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1034                         "j <= 2 i - 3 and j <= n - 2 or "
1035                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1036                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
1037         map = isl_map_transitive_closure(map, &exact);
1038         assert(exact);
1039         isl_map_free(map);
1040
1041         /* COCOA Fig.2 right */
1042         map = isl_map_read_from_str(ctx,
1043                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1044                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1045                         "j <= n or "
1046                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1047                         "j <= 2 i - 4 and j <= n - 3 or "
1048                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1049                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
1050         map = isl_map_power(map, &exact);
1051         assert(exact);
1052         isl_map_free(map);
1053
1054         /* COCOA Fig.2 right */
1055         map = isl_map_read_from_str(ctx,
1056                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1057                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1058                         "j <= n or "
1059                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1060                         "j <= 2 i - 4 and j <= n - 3 or "
1061                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1062                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
1063         map = isl_map_transitive_closure(map, &exact);
1064         assert(exact);
1065         map2 = isl_map_read_from_str(ctx,
1066                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1067                         "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1068                         "j <= n and 3 + i + 2 j <= 3 n and "
1069                         "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1070                         "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1071                         "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1072                         "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1073                         "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
1074         assert(isl_map_is_equal(map, map2));
1075         isl_map_free(map2);
1076         isl_map_free(map);
1077
1078         /* COCOA Fig.1 right */
1079         dom = isl_set_read_from_str(ctx,
1080                 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1081                         "2 x - 3 y + 3 >= 0 }", -1);
1082         right = isl_map_read_from_str(ctx,
1083                 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
1084         up = isl_map_read_from_str(ctx,
1085                 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
1086         right = isl_map_intersect_domain(right, isl_set_copy(dom));
1087         right = isl_map_intersect_range(right, isl_set_copy(dom));
1088         up = isl_map_intersect_domain(up, isl_set_copy(dom));
1089         up = isl_map_intersect_range(up, dom);
1090         map = isl_map_union(up, right);
1091         map = isl_map_transitive_closure(map, &exact);
1092         assert(exact);
1093         map2 = isl_map_read_from_str(ctx,
1094                 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1095                 "  [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }", -1);
1096         assert(isl_map_is_equal(map, map2));
1097         isl_map_free(map2);
1098         isl_map_free(map);
1099
1100         /* COCOA Theorem 1 counter example */
1101         map = isl_map_read_from_str(ctx,
1102                 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1103                         "i2 = 1 and j2 = j or "
1104                         "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
1105         map = isl_map_transitive_closure(map, &exact);
1106         assert(exact);
1107         isl_map_free(map);
1108
1109         map = isl_map_read_from_str(ctx,
1110                 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1111                         "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1112                         "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1113                         "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
1114         map = isl_map_transitive_closure(map, &exact);
1115         assert(exact);
1116         isl_map_free(map);
1117
1118         /* Kelly et al 1996, fig 12 */
1119         map = isl_map_read_from_str(ctx,
1120                 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1121                         "1 <= i,j,j+1 <= n or "
1122                         "j = n and j2 = 1 and i2 = i + 1 and "
1123                         "1 <= i,i+1 <= n }", -1);
1124         map = isl_map_transitive_closure(map, &exact);
1125         assert(exact);
1126         map2 = isl_map_read_from_str(ctx,
1127                 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1128                         "1 <= i <= n and i = i2 or "
1129                         "1 <= i < i2 <= n and 1 <= j <= n and "
1130                         "1 <= j2 <= n }", -1);
1131         assert(isl_map_is_equal(map, map2));
1132         isl_map_free(map2);
1133         isl_map_free(map);
1134
1135         /* Omega's closure4 */
1136         map = isl_map_read_from_str(ctx,
1137                 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1138                         "1 <= x,y <= 10 or "
1139                         "x2 = x + 1 and y2 = y and "
1140                         "1 <= x <= 20 && 5 <= y <= 15 }", -1);
1141         map = isl_map_transitive_closure(map, &exact);
1142         assert(exact);
1143         isl_map_free(map);
1144
1145         map = isl_map_read_from_str(ctx,
1146                 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
1147         map = isl_map_transitive_closure(map, &exact);
1148         assert(!exact);
1149         map2 = isl_map_read_from_str(ctx,
1150                 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
1151         assert(isl_map_is_equal(map, map2));
1152         isl_map_free(map);
1153         isl_map_free(map2);
1154
1155         str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1156             "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1157             "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1158             "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1159         map = isl_map_read_from_str(ctx, str, -1);
1160         map = isl_map_transitive_closure(map, &exact);
1161         assert(exact);
1162         map2 = isl_map_read_from_str(ctx, str, -1);
1163         assert(isl_map_is_equal(map, map2));
1164         isl_map_free(map);
1165         isl_map_free(map2);
1166
1167         str = "{[0] -> [1]; [2] -> [3]}";
1168         map = isl_map_read_from_str(ctx, str, -1);
1169         map = isl_map_transitive_closure(map, &exact);
1170         assert(exact);
1171         map2 = isl_map_read_from_str(ctx, str, -1);
1172         assert(isl_map_is_equal(map, map2));
1173         isl_map_free(map);
1174         isl_map_free(map2);
1175
1176         str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1177             "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1178             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1179             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1180             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1181             "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1182             "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1183             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1184             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1185             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1186             "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1187             "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1188             "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1189             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1190             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1191             "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1192             "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1193             "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1194             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1195             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1196         map = isl_map_read_from_str(ctx, str, -1);
1197         map = isl_map_transitive_closure(map, NULL);
1198         assert(map);
1199         isl_map_free(map);
1200 }
1201
1202 void test_lex(struct isl_ctx *ctx)
1203 {
1204         isl_dim *dim;
1205         isl_map *map;
1206
1207         dim = isl_dim_alloc(ctx, 0, 0, 0);
1208         map = isl_map_lex_le(dim);
1209         assert(!isl_map_is_empty(map));
1210         isl_map_free(map);
1211 }
1212
1213 void test_lexmin(struct isl_ctx *ctx)
1214 {
1215         const char *str;
1216         isl_map *map, *map2;
1217         isl_set *set;
1218         isl_set *set2;
1219
1220         str = "[p0, p1] -> { [] -> [] : "
1221             "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1222             "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1223             "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1224             "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1225             "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1226             "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1227             "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1228             "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1229             "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1230             "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1231             "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1232         map = isl_map_read_from_str(ctx, str, -1);
1233         map = isl_map_lexmin(map);
1234         isl_map_free(map);
1235
1236         str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1237             "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1238         set = isl_set_read_from_str(ctx, str, -1);
1239         set = isl_set_lexmax(set);
1240         str = "[C] -> { [obj,a,b,c] : C = 8 }";
1241         set2 = isl_set_read_from_str(ctx, str, -1);
1242         set = isl_set_intersect(set, set2);
1243         assert(!isl_set_is_empty(set));
1244         isl_set_free(set);
1245
1246         str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1247         map = isl_map_read_from_str(ctx, str, -1);
1248         map = isl_map_lexmin(map);
1249         str = "{ [x] -> [5] : 6 <= x <= 8; "
1250                 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1251         map2 = isl_map_read_from_str(ctx, str, -1);
1252         assert(isl_map_is_equal(map, map2));
1253         isl_map_free(map);
1254         isl_map_free(map2);
1255
1256         str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1257         map = isl_map_read_from_str(ctx, str, -1);
1258         map2 = isl_map_copy(map);
1259         map = isl_map_lexmin(map);
1260         assert(isl_map_is_equal(map, map2));
1261         isl_map_free(map);
1262         isl_map_free(map2);
1263
1264         str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1265         map = isl_map_read_from_str(ctx, str, -1);
1266         map = isl_map_lexmin(map);
1267         str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1268                 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1269                 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1270                 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1271         map2 = isl_map_read_from_str(ctx, str, -1);
1272         assert(isl_map_is_equal(map, map2));
1273         isl_map_free(map);
1274         isl_map_free(map2);
1275 }
1276
1277 struct must_may {
1278         isl_map *must;
1279         isl_map *may;
1280 };
1281
1282 static int collect_must_may(__isl_take isl_map *dep, int must,
1283         void *dep_user, void *user)
1284 {
1285         struct must_may *mm = (struct must_may *)user;
1286
1287         if (must)
1288                 mm->must = isl_map_union(mm->must, dep);
1289         else
1290                 mm->may = isl_map_union(mm->may, dep);
1291
1292         return 0;
1293 }
1294
1295 static int common_space(void *first, void *second)
1296 {
1297         int depth = *(int *)first;
1298         return 2 * depth;
1299 }
1300
1301 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1302 {
1303         isl_map *map2;
1304         int equal;
1305
1306         if (!map)
1307                 return -1;
1308
1309         map2 = isl_map_read_from_str(map->ctx, str, -1);
1310         equal = isl_map_is_equal(map, map2);
1311         isl_map_free(map2);
1312
1313         return equal;
1314 }
1315
1316 void test_dep(struct isl_ctx *ctx)
1317 {
1318         const char *str;
1319         isl_dim *dim;
1320         isl_map *map;
1321         isl_access_info *ai;
1322         isl_flow *flow;
1323         int depth;
1324         struct must_may mm;
1325
1326         depth = 3;
1327
1328         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1329         map = isl_map_read_from_str(ctx, str, -1);
1330         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1331
1332         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1333         map = isl_map_read_from_str(ctx, str, -1);
1334         ai = isl_access_info_add_source(ai, map, 1, &depth);
1335
1336         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1337         map = isl_map_read_from_str(ctx, str, -1);
1338         ai = isl_access_info_add_source(ai, map, 1, &depth);
1339
1340         flow = isl_access_info_compute_flow(ai);
1341         dim = isl_dim_alloc(ctx, 0, 3, 3);
1342         mm.must = isl_map_empty(isl_dim_copy(dim));
1343         mm.may = isl_map_empty(dim);
1344
1345         isl_flow_foreach(flow, collect_must_may, &mm);
1346
1347         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1348               "  [1,10,0] -> [2,5,0] }";
1349         assert(map_is_equal(mm.must, str));
1350         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1351         assert(map_is_equal(mm.may, str));
1352
1353         isl_map_free(mm.must);
1354         isl_map_free(mm.may);
1355         isl_flow_free(flow);
1356
1357
1358         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1359         map = isl_map_read_from_str(ctx, str, -1);
1360         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1361
1362         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1363         map = isl_map_read_from_str(ctx, str, -1);
1364         ai = isl_access_info_add_source(ai, map, 1, &depth);
1365
1366         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1367         map = isl_map_read_from_str(ctx, str, -1);
1368         ai = isl_access_info_add_source(ai, map, 0, &depth);
1369
1370         flow = isl_access_info_compute_flow(ai);
1371         dim = isl_dim_alloc(ctx, 0, 3, 3);
1372         mm.must = isl_map_empty(isl_dim_copy(dim));
1373         mm.may = isl_map_empty(dim);
1374
1375         isl_flow_foreach(flow, collect_must_may, &mm);
1376
1377         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1378         assert(map_is_equal(mm.must, str));
1379         str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1380         assert(map_is_equal(mm.may, str));
1381
1382         isl_map_free(mm.must);
1383         isl_map_free(mm.may);
1384         isl_flow_free(flow);
1385
1386
1387         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1388         map = isl_map_read_from_str(ctx, str, -1);
1389         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1390
1391         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1392         map = isl_map_read_from_str(ctx, str, -1);
1393         ai = isl_access_info_add_source(ai, map, 0, &depth);
1394
1395         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1396         map = isl_map_read_from_str(ctx, str, -1);
1397         ai = isl_access_info_add_source(ai, map, 0, &depth);
1398
1399         flow = isl_access_info_compute_flow(ai);
1400         dim = isl_dim_alloc(ctx, 0, 3, 3);
1401         mm.must = isl_map_empty(isl_dim_copy(dim));
1402         mm.may = isl_map_empty(dim);
1403
1404         isl_flow_foreach(flow, collect_must_may, &mm);
1405
1406         str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1407               "  [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1408         assert(map_is_equal(mm.may, str));
1409         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1410         assert(map_is_equal(mm.must, str));
1411
1412         isl_map_free(mm.must);
1413         isl_map_free(mm.may);
1414         isl_flow_free(flow);
1415
1416
1417         str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1418         map = isl_map_read_from_str(ctx, str, -1);
1419         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1420
1421         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1422         map = isl_map_read_from_str(ctx, str, -1);
1423         ai = isl_access_info_add_source(ai, map, 0, &depth);
1424
1425         str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1426         map = isl_map_read_from_str(ctx, str, -1);
1427         ai = isl_access_info_add_source(ai, map, 0, &depth);
1428
1429         flow = isl_access_info_compute_flow(ai);
1430         dim = isl_dim_alloc(ctx, 0, 3, 3);
1431         mm.must = isl_map_empty(isl_dim_copy(dim));
1432         mm.may = isl_map_empty(dim);
1433
1434         isl_flow_foreach(flow, collect_must_may, &mm);
1435
1436         str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1437               "  [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1438         assert(map_is_equal(mm.may, str));
1439         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1440         assert(map_is_equal(mm.must, str));
1441
1442         isl_map_free(mm.must);
1443         isl_map_free(mm.may);
1444         isl_flow_free(flow);
1445
1446
1447         str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1448         map = isl_map_read_from_str(ctx, str, -1);
1449         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1450
1451         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1452         map = isl_map_read_from_str(ctx, str, -1);
1453         ai = isl_access_info_add_source(ai, map, 0, &depth);
1454
1455         str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1456         map = isl_map_read_from_str(ctx, str, -1);
1457         ai = isl_access_info_add_source(ai, map, 0, &depth);
1458
1459         flow = isl_access_info_compute_flow(ai);
1460         dim = isl_dim_alloc(ctx, 0, 3, 3);
1461         mm.must = isl_map_empty(isl_dim_copy(dim));
1462         mm.may = isl_map_empty(dim);
1463
1464         isl_flow_foreach(flow, collect_must_may, &mm);
1465
1466         str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1467               "  [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1468         assert(map_is_equal(mm.may, str));
1469         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1470         assert(map_is_equal(mm.must, str));
1471
1472         isl_map_free(mm.must);
1473         isl_map_free(mm.may);
1474         isl_flow_free(flow);
1475
1476
1477         depth = 5;
1478
1479         str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1480         map = isl_map_read_from_str(ctx, str, -1);
1481         ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1482
1483         str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1484         map = isl_map_read_from_str(ctx, str, -1);
1485         ai = isl_access_info_add_source(ai, map, 1, &depth);
1486
1487         flow = isl_access_info_compute_flow(ai);
1488         dim = isl_dim_alloc(ctx, 0, 5, 5);
1489         mm.must = isl_map_empty(isl_dim_copy(dim));
1490         mm.may = isl_map_empty(dim);
1491
1492         isl_flow_foreach(flow, collect_must_may, &mm);
1493
1494         str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1495         assert(map_is_equal(mm.must, str));
1496         str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1497         assert(map_is_equal(mm.may, str));
1498
1499         isl_map_free(mm.must);
1500         isl_map_free(mm.may);
1501         isl_flow_free(flow);
1502 }
1503
1504 void test_sv(struct isl_ctx *ctx)
1505 {
1506         const char *str;
1507         isl_map *map;
1508
1509         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1510         map = isl_map_read_from_str(ctx, str, -1);
1511         assert(isl_map_is_single_valued(map));
1512         isl_map_free(map);
1513
1514         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1515         map = isl_map_read_from_str(ctx, str, -1);
1516         assert(!isl_map_is_single_valued(map));
1517         isl_map_free(map);
1518 }
1519
1520 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1521 {
1522         isl_map *map;
1523
1524         map = isl_map_read_from_str(ctx, str, -1);
1525         if (bijective)
1526                 assert(isl_map_is_bijective(map));
1527         else
1528                 assert(!isl_map_is_bijective(map));
1529         isl_map_free(map);
1530 }
1531
1532 void test_bijective(struct isl_ctx *ctx)
1533 {
1534         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1535         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1536         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1537         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1538         test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1539         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1540         test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1541         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1542         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1543         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1544         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1545         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1546         test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1547 }
1548
1549 void test_pwqp(struct isl_ctx *ctx)
1550 {
1551         const char *str;
1552         isl_set *set;
1553         isl_pw_qpolynomial *pwqp1, *pwqp2;
1554
1555         str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1556         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1557
1558         pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1559                                                 isl_dim_set, 1, 1);
1560
1561         str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1562         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1563
1564         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1565
1566         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1567
1568         isl_pw_qpolynomial_free(pwqp1);
1569
1570         str = "{ [i] -> i }";
1571         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1572         str = "{ [k] : exists a : k = 2a }";
1573         set = isl_set_read_from_str(ctx, str, 0);
1574         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1575         str = "{ [i] -> i }";
1576         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1577
1578         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1579
1580         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1581
1582         isl_pw_qpolynomial_free(pwqp1);
1583
1584         str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1585         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1586         str = "{ [10] }";
1587         set = isl_set_read_from_str(ctx, str, 0);
1588         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1589         str = "{ [i] -> 16 }";
1590         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1591
1592         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1593
1594         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1595
1596         isl_pw_qpolynomial_free(pwqp1);
1597
1598         str = "{ [i] -> ([(i)/2]) }";
1599         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1600         str = "{ [k] : exists a : k = 2a+1 }";
1601         set = isl_set_read_from_str(ctx, str, 0);
1602         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1603         str = "{ [i] -> -1/2 + 1/2 * i }";
1604         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1605
1606         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1607
1608         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1609
1610         isl_pw_qpolynomial_free(pwqp1);
1611
1612         str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1613         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1614         str = "{ [i] -> ([(2 * [i/2])/5]) }";
1615         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1616
1617         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1618
1619         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1620
1621         isl_pw_qpolynomial_free(pwqp1);
1622
1623         str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1624         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1625         str = "{ [x] -> x }";
1626         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1627
1628         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1629
1630         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1631
1632         isl_pw_qpolynomial_free(pwqp1);
1633 }
1634
1635 void test_split_periods(isl_ctx *ctx)
1636 {
1637         const char *str;
1638         isl_pw_qpolynomial *pwqp;
1639
1640         str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1641                 "U + 2V + 3 >= 0 and - U -2V  >= 0 and - U + 10 >= 0 and "
1642                 "U  >= 0; [U,V] -> U^2 : U >= 100 }";
1643         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1644
1645         pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1646         assert(pwqp);
1647
1648         isl_pw_qpolynomial_free(pwqp);
1649 }
1650
1651 void test_union(isl_ctx *ctx)
1652 {
1653         const char *str;
1654         isl_union_set *uset1, *uset2;
1655         isl_union_map *umap1, *umap2;
1656
1657         str = "{ [i] : 0 <= i <= 1 }";
1658         uset1 = isl_union_set_read_from_str(ctx, str);
1659         str = "{ [1] -> [0] }";
1660         umap1 = isl_union_map_read_from_str(ctx, str);
1661
1662         umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1663         assert(isl_union_map_is_equal(umap1, umap2));
1664
1665         isl_union_map_free(umap1);
1666         isl_union_map_free(umap2);
1667
1668         str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1669         umap1 = isl_union_map_read_from_str(ctx, str);
1670         str = "{ A[i]; B[i] }";
1671         uset1 = isl_union_set_read_from_str(ctx, str);
1672
1673         uset2 = isl_union_map_domain(umap1);
1674
1675         assert(isl_union_set_is_equal(uset1, uset2));
1676
1677         isl_union_set_free(uset1);
1678         isl_union_set_free(uset2);
1679 }
1680
1681 void test_bound(isl_ctx *ctx)
1682 {
1683         const char *str;
1684         isl_pw_qpolynomial *pwqp;
1685         isl_pw_qpolynomial_fold *pwf;
1686
1687         str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1688         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1689         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1690         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 4);
1691         isl_pw_qpolynomial_fold_free(pwf);
1692
1693         str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1694         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1695         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1696         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 1);
1697         isl_pw_qpolynomial_fold_free(pwf);
1698 }
1699
1700 void test_lift(isl_ctx *ctx)
1701 {
1702         const char *str;
1703         isl_basic_map *bmap;
1704         isl_basic_set *bset;
1705
1706         str = "{ [i0] : exists e0 : i0 = 4e0 }";
1707         bset = isl_basic_set_read_from_str(ctx, str, 0);
1708         bset = isl_basic_set_lift(bset);
1709         bmap = isl_basic_map_from_range(bset);
1710         bset = isl_basic_map_domain(bmap);
1711         isl_basic_set_free(bset);
1712 }
1713
1714 void test_subset(isl_ctx *ctx)
1715 {
1716         const char *str;
1717         isl_set *set1, *set2;
1718
1719         str = "{ [112, 0] }";
1720         set1 = isl_set_read_from_str(ctx, str, 0);
1721         str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1722                 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1723                 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1724         set2 = isl_set_read_from_str(ctx, str, 0);
1725         assert(isl_set_is_subset(set1, set2));
1726         isl_set_free(set1);
1727         isl_set_free(set2);
1728 }
1729
1730 void test_factorize(isl_ctx *ctx)
1731 {
1732         const char *str;
1733         isl_basic_set *bset;
1734         isl_factorizer *f;
1735
1736         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1737             "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1738             "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1739             "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1740             "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1741             "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1742             "3i5 >= -2i0 - i2 + 3i4 }";
1743         bset = isl_basic_set_read_from_str(ctx, str, 0);
1744         f = isl_basic_set_factorizer(bset);
1745         assert(f);
1746         isl_basic_set_free(bset);
1747         isl_factorizer_free(f);
1748
1749         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1750             "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
1751             "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
1752             "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
1753             "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
1754             "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
1755             "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
1756             "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
1757             "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
1758             "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
1759         bset = isl_basic_set_read_from_str(ctx, str, 0);
1760         f = isl_basic_set_factorizer(bset);
1761         assert(f);
1762         isl_basic_set_free(bset);
1763         isl_factorizer_free(f);
1764 }
1765
1766 int main()
1767 {
1768         struct isl_ctx *ctx;
1769
1770         srcdir = getenv("srcdir");
1771         assert(srcdir);
1772
1773         ctx = isl_ctx_alloc();
1774         test_factorize(ctx);
1775         test_subset(ctx);
1776         test_lift(ctx);
1777         test_bound(ctx);
1778         test_union(ctx);
1779         test_split_periods(ctx);
1780         test_parse(ctx);
1781         test_pwqp(ctx);
1782         test_lex(ctx);
1783         test_sv(ctx);
1784         test_bijective(ctx);
1785         test_dep(ctx);
1786         test_read(ctx);
1787         test_bounded(ctx);
1788         test_construction(ctx);
1789         test_dim(ctx);
1790         test_div(ctx);
1791         test_application(ctx);
1792         test_affine_hull(ctx);
1793         test_convex_hull(ctx);
1794         test_gist(ctx);
1795         test_coalesce(ctx);
1796         test_closure(ctx);
1797         test_lexmin(ctx);
1798         isl_ctx_free(ctx);
1799         return 0;
1800 }