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