isl_basic_set_factorizer: make sure group information gets updated
[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
1105 void test_lex(struct isl_ctx *ctx)
1106 {
1107         isl_dim *dim;
1108         isl_map *map;
1109
1110         dim = isl_dim_alloc(ctx, 0, 0, 0);
1111         map = isl_map_lex_le(dim);
1112         assert(!isl_map_is_empty(map));
1113         isl_map_free(map);
1114 }
1115
1116 void test_lexmin(struct isl_ctx *ctx)
1117 {
1118         const char *str;
1119         isl_map *map;
1120         isl_set *set;
1121         isl_set *set2;
1122
1123         str = "[p0, p1] -> { [] -> [] : "
1124             "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1125             "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1126             "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1127             "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1128             "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1129             "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1130             "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1131             "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1132             "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1133             "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1134             "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1135         map = isl_map_read_from_str(ctx, str, -1);
1136         map = isl_map_lexmin(map);
1137         isl_map_free(map);
1138
1139         str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1140             "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1141         set = isl_set_read_from_str(ctx, str, -1);
1142         set = isl_set_lexmax(set);
1143         str = "[C] -> { [obj,a,b,c] : C = 8 }";
1144         set2 = isl_set_read_from_str(ctx, str, -1);
1145         set = isl_set_intersect(set, set2);
1146         assert(!isl_set_is_empty(set));
1147         isl_set_free(set);
1148 }
1149
1150 struct must_may {
1151         isl_map *must;
1152         isl_map *may;
1153 };
1154
1155 static int collect_must_may(__isl_take isl_map *dep, int must,
1156         void *dep_user, void *user)
1157 {
1158         struct must_may *mm = (struct must_may *)user;
1159
1160         if (must)
1161                 mm->must = isl_map_union(mm->must, dep);
1162         else
1163                 mm->may = isl_map_union(mm->may, dep);
1164
1165         return 0;
1166 }
1167
1168 static int common_space(void *first, void *second)
1169 {
1170         int depth = *(int *)first;
1171         return 2 * depth;
1172 }
1173
1174 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1175 {
1176         isl_map *map2;
1177         int equal;
1178
1179         if (!map)
1180                 return -1;
1181
1182         map2 = isl_map_read_from_str(map->ctx, str, -1);
1183         equal = isl_map_is_equal(map, map2);
1184         isl_map_free(map2);
1185
1186         return equal;
1187 }
1188
1189 void test_dep(struct isl_ctx *ctx)
1190 {
1191         const char *str;
1192         isl_dim *dim;
1193         isl_map *map;
1194         isl_access_info *ai;
1195         isl_flow *flow;
1196         int depth;
1197         struct must_may mm;
1198
1199         depth = 3;
1200
1201         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1202         map = isl_map_read_from_str(ctx, str, -1);
1203         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1204
1205         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1206         map = isl_map_read_from_str(ctx, str, -1);
1207         ai = isl_access_info_add_source(ai, map, 1, &depth);
1208
1209         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1210         map = isl_map_read_from_str(ctx, str, -1);
1211         ai = isl_access_info_add_source(ai, map, 1, &depth);
1212
1213         flow = isl_access_info_compute_flow(ai);
1214         dim = isl_dim_alloc(ctx, 0, 3, 3);
1215         mm.must = isl_map_empty(isl_dim_copy(dim));
1216         mm.may = isl_map_empty(dim);
1217
1218         isl_flow_foreach(flow, collect_must_may, &mm);
1219
1220         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1221               "  [1,10,0] -> [2,5,0] }";
1222         assert(map_is_equal(mm.must, str));
1223         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1224         assert(map_is_equal(mm.may, str));
1225
1226         isl_map_free(mm.must);
1227         isl_map_free(mm.may);
1228         isl_flow_free(flow);
1229
1230
1231         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1232         map = isl_map_read_from_str(ctx, str, -1);
1233         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1234
1235         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1236         map = isl_map_read_from_str(ctx, str, -1);
1237         ai = isl_access_info_add_source(ai, map, 1, &depth);
1238
1239         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1240         map = isl_map_read_from_str(ctx, str, -1);
1241         ai = isl_access_info_add_source(ai, map, 0, &depth);
1242
1243         flow = isl_access_info_compute_flow(ai);
1244         dim = isl_dim_alloc(ctx, 0, 3, 3);
1245         mm.must = isl_map_empty(isl_dim_copy(dim));
1246         mm.may = isl_map_empty(dim);
1247
1248         isl_flow_foreach(flow, collect_must_may, &mm);
1249
1250         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1251         assert(map_is_equal(mm.must, str));
1252         str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1253         assert(map_is_equal(mm.may, str));
1254
1255         isl_map_free(mm.must);
1256         isl_map_free(mm.may);
1257         isl_flow_free(flow);
1258
1259
1260         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1261         map = isl_map_read_from_str(ctx, str, -1);
1262         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1263
1264         str = "{ [0,i,0] -> [i] : 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         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1269         map = isl_map_read_from_str(ctx, str, -1);
1270         ai = isl_access_info_add_source(ai, map, 0, &depth);
1271
1272         flow = isl_access_info_compute_flow(ai);
1273         dim = isl_dim_alloc(ctx, 0, 3, 3);
1274         mm.must = isl_map_empty(isl_dim_copy(dim));
1275         mm.may = isl_map_empty(dim);
1276
1277         isl_flow_foreach(flow, collect_must_may, &mm);
1278
1279         str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1280               "  [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1281         assert(map_is_equal(mm.may, str));
1282         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1283         assert(map_is_equal(mm.must, str));
1284
1285         isl_map_free(mm.must);
1286         isl_map_free(mm.may);
1287         isl_flow_free(flow);
1288
1289
1290         str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1291         map = isl_map_read_from_str(ctx, str, -1);
1292         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1293
1294         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1295         map = isl_map_read_from_str(ctx, str, -1);
1296         ai = isl_access_info_add_source(ai, map, 0, &depth);
1297
1298         str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1299         map = isl_map_read_from_str(ctx, str, -1);
1300         ai = isl_access_info_add_source(ai, map, 0, &depth);
1301
1302         flow = isl_access_info_compute_flow(ai);
1303         dim = isl_dim_alloc(ctx, 0, 3, 3);
1304         mm.must = isl_map_empty(isl_dim_copy(dim));
1305         mm.may = isl_map_empty(dim);
1306
1307         isl_flow_foreach(flow, collect_must_may, &mm);
1308
1309         str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1310               "  [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1311         assert(map_is_equal(mm.may, str));
1312         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1313         assert(map_is_equal(mm.must, str));
1314
1315         isl_map_free(mm.must);
1316         isl_map_free(mm.may);
1317         isl_flow_free(flow);
1318
1319
1320         str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1321         map = isl_map_read_from_str(ctx, str, -1);
1322         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1323
1324         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1325         map = isl_map_read_from_str(ctx, str, -1);
1326         ai = isl_access_info_add_source(ai, map, 0, &depth);
1327
1328         str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1329         map = isl_map_read_from_str(ctx, str, -1);
1330         ai = isl_access_info_add_source(ai, map, 0, &depth);
1331
1332         flow = isl_access_info_compute_flow(ai);
1333         dim = isl_dim_alloc(ctx, 0, 3, 3);
1334         mm.must = isl_map_empty(isl_dim_copy(dim));
1335         mm.may = isl_map_empty(dim);
1336
1337         isl_flow_foreach(flow, collect_must_may, &mm);
1338
1339         str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1340               "  [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1341         assert(map_is_equal(mm.may, str));
1342         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1343         assert(map_is_equal(mm.must, str));
1344
1345         isl_map_free(mm.must);
1346         isl_map_free(mm.may);
1347         isl_flow_free(flow);
1348
1349
1350         depth = 5;
1351
1352         str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1353         map = isl_map_read_from_str(ctx, str, -1);
1354         ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1355
1356         str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1357         map = isl_map_read_from_str(ctx, str, -1);
1358         ai = isl_access_info_add_source(ai, map, 1, &depth);
1359
1360         flow = isl_access_info_compute_flow(ai);
1361         dim = isl_dim_alloc(ctx, 0, 5, 5);
1362         mm.must = isl_map_empty(isl_dim_copy(dim));
1363         mm.may = isl_map_empty(dim);
1364
1365         isl_flow_foreach(flow, collect_must_may, &mm);
1366
1367         str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1368         assert(map_is_equal(mm.must, str));
1369         str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1370         assert(map_is_equal(mm.may, str));
1371
1372         isl_map_free(mm.must);
1373         isl_map_free(mm.may);
1374         isl_flow_free(flow);
1375 }
1376
1377 void test_sv(struct isl_ctx *ctx)
1378 {
1379         const char *str;
1380         isl_map *map;
1381
1382         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1383         map = isl_map_read_from_str(ctx, str, -1);
1384         assert(isl_map_is_single_valued(map));
1385         isl_map_free(map);
1386
1387         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1388         map = isl_map_read_from_str(ctx, str, -1);
1389         assert(!isl_map_is_single_valued(map));
1390         isl_map_free(map);
1391 }
1392
1393 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1394 {
1395         isl_map *map;
1396
1397         map = isl_map_read_from_str(ctx, str, -1);
1398         if (bijective)
1399                 assert(isl_map_is_bijective(map));
1400         else
1401                 assert(!isl_map_is_bijective(map));
1402         isl_map_free(map);
1403 }
1404
1405 void test_bijective(struct isl_ctx *ctx)
1406 {
1407         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1408         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1409         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1410         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1411         test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1412         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1413         test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1414         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1415         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1416         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1417         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1418         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1419         test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1420 }
1421
1422 void test_pwqp(struct isl_ctx *ctx)
1423 {
1424         const char *str;
1425         isl_set *set;
1426         isl_pw_qpolynomial *pwqp1, *pwqp2;
1427
1428         str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1429         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1430
1431         pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1432                                                 isl_dim_set, 1, 1);
1433
1434         str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1435         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1436
1437         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1438
1439         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1440
1441         isl_pw_qpolynomial_free(pwqp1);
1442
1443         str = "{ [i] -> i }";
1444         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1445         str = "{ [k] : exists a : k = 2a }";
1446         set = isl_set_read_from_str(ctx, str, 0);
1447         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1448         str = "{ [i] -> i }";
1449         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1450
1451         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1452
1453         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1454
1455         isl_pw_qpolynomial_free(pwqp1);
1456
1457         str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1458         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1459         str = "{ [10] }";
1460         set = isl_set_read_from_str(ctx, str, 0);
1461         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1462         str = "{ [i] -> 16 }";
1463         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1464
1465         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1466
1467         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1468
1469         isl_pw_qpolynomial_free(pwqp1);
1470
1471         str = "{ [i] -> ([(i)/2]) }";
1472         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1473         str = "{ [k] : exists a : k = 2a+1 }";
1474         set = isl_set_read_from_str(ctx, str, 0);
1475         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1476         str = "{ [i] -> -1/2 + 1/2 * i }";
1477         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1478
1479         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1480
1481         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1482
1483         isl_pw_qpolynomial_free(pwqp1);
1484
1485         str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1486         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1487         str = "{ [i] -> ([(2 * [i/2])/5]) }";
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 = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1497         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1498         str = "{ [x] -> x }";
1499         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1500
1501         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1502
1503         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1504
1505         isl_pw_qpolynomial_free(pwqp1);
1506 }
1507
1508 void test_split_periods(isl_ctx *ctx)
1509 {
1510         const char *str;
1511         isl_pw_qpolynomial *pwqp;
1512
1513         str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1514                 "U + 2V + 3 >= 0 and - U -2V  >= 0 and - U + 10 >= 0 and "
1515                 "U  >= 0; [U,V] -> U^2 : U >= 100 }";
1516         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1517
1518         pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1519         assert(pwqp);
1520
1521         isl_pw_qpolynomial_free(pwqp);
1522 }
1523
1524 void test_union(isl_ctx *ctx)
1525 {
1526         const char *str;
1527         isl_union_set *uset1, *uset2;
1528         isl_union_map *umap1, *umap2;
1529
1530         str = "{ [i] : 0 <= i <= 1 }";
1531         uset1 = isl_union_set_read_from_str(ctx, str);
1532         str = "{ [1] -> [0] }";
1533         umap1 = isl_union_map_read_from_str(ctx, str);
1534
1535         umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1536         assert(isl_union_map_is_equal(umap1, umap2));
1537
1538         isl_union_map_free(umap1);
1539         isl_union_map_free(umap2);
1540
1541         str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1542         umap1 = isl_union_map_read_from_str(ctx, str);
1543         str = "{ A[i]; B[i] }";
1544         uset1 = isl_union_set_read_from_str(ctx, str);
1545
1546         uset2 = isl_union_map_domain(umap1);
1547
1548         assert(isl_union_set_is_equal(uset1, uset2));
1549
1550         isl_union_set_free(uset1);
1551         isl_union_set_free(uset2);
1552 }
1553
1554 void test_bound(isl_ctx *ctx)
1555 {
1556         const char *str;
1557         isl_pw_qpolynomial *pwqp;
1558         isl_pw_qpolynomial_fold *pwf;
1559
1560         str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1561         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1562         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1563         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 4);
1564         isl_pw_qpolynomial_fold_free(pwf);
1565 }
1566
1567 void test_lift(isl_ctx *ctx)
1568 {
1569         const char *str;
1570         isl_basic_map *bmap;
1571         isl_basic_set *bset;
1572
1573         str = "{ [i0] : exists e0 : i0 = 4e0 }";
1574         bset = isl_basic_set_read_from_str(ctx, str, 0);
1575         bset = isl_basic_set_lift(bset);
1576         bmap = isl_basic_map_from_range(bset);
1577         bset = isl_basic_map_domain(bmap);
1578         isl_basic_set_free(bset);
1579 }
1580
1581 void test_subset(isl_ctx *ctx)
1582 {
1583         const char *str;
1584         isl_set *set1, *set2;
1585
1586         str = "{ [112, 0] }";
1587         set1 = isl_set_read_from_str(ctx, str, 0);
1588         str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1589                 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1590                 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1591         set2 = isl_set_read_from_str(ctx, str, 0);
1592         assert(isl_set_is_subset(set1, set2));
1593         isl_set_free(set1);
1594         isl_set_free(set2);
1595 }
1596
1597 void test_factorize(isl_ctx *ctx)
1598 {
1599         const char *str;
1600         isl_basic_set *bset;
1601         isl_factorizer *f;
1602
1603         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1604             "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1605             "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1606             "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1607             "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1608             "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1609             "3i5 >= -2i0 - i2 + 3i4 }";
1610         bset = isl_basic_set_read_from_str(ctx, str, 0);
1611         f = isl_basic_set_factorizer(bset);
1612         assert(f);
1613         isl_basic_set_free(bset);
1614         isl_factorizer_free(f);
1615
1616         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1617             "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
1618             "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
1619             "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
1620             "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
1621             "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
1622             "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
1623             "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
1624             "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
1625             "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
1626         bset = isl_basic_set_read_from_str(ctx, str, 0);
1627         f = isl_basic_set_factorizer(bset);
1628         assert(f);
1629         isl_basic_set_free(bset);
1630         isl_factorizer_free(f);
1631 }
1632
1633 int main()
1634 {
1635         struct isl_ctx *ctx;
1636
1637         srcdir = getenv("srcdir");
1638         assert(srcdir);
1639
1640         ctx = isl_ctx_alloc();
1641         test_factorize(ctx);
1642         test_subset(ctx);
1643         test_lift(ctx);
1644         test_bound(ctx);
1645         test_union(ctx);
1646         test_split_periods(ctx);
1647         test_parse(ctx);
1648         test_pwqp(ctx);
1649         test_lex(ctx);
1650         test_sv(ctx);
1651         test_bijective(ctx);
1652         test_dep(ctx);
1653         test_read(ctx);
1654         test_bounded(ctx);
1655         test_construction(ctx);
1656         test_dim(ctx);
1657         test_div(ctx);
1658         test_application(ctx);
1659         test_affine_hull(ctx);
1660         test_convex_hull(ctx);
1661         test_gist(ctx);
1662         test_coalesce(ctx);
1663         test_closure(ctx);
1664         test_lexmin(ctx);
1665         isl_ctx_free(ctx);
1666         return 0;
1667 }