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