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