isl_set_wrap_facet: make sure set is marked rational
[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_constraint.h>
16
17 static char *srcdir;
18
19 void test_read(struct isl_ctx *ctx)
20 {
21         char filename[PATH_MAX];
22         FILE *input;
23         int n;
24         struct isl_basic_set *bset1, *bset2;
25         const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
26
27         n = snprintf(filename, sizeof(filename),
28                         "%s/test_inputs/set.omega", srcdir);
29         assert(n < sizeof(filename));
30         input = fopen(filename, "r");
31         assert(input);
32
33         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
34         bset2 = isl_basic_set_read_from_str(ctx, str, 0);
35
36         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
37
38         isl_basic_set_free(bset1);
39         isl_basic_set_free(bset2);
40
41         fclose(input);
42 }
43
44 /* Construct the basic set { [i] : 5 <= i <= N } */
45 void test_construction(struct isl_ctx *ctx)
46 {
47         isl_int v;
48         struct isl_dim *dim;
49         struct isl_basic_set *bset;
50         struct isl_constraint *c;
51
52         isl_int_init(v);
53
54         dim = isl_dim_set_alloc(ctx, 1, 1);
55         bset = isl_basic_set_universe(dim);
56
57         c = isl_inequality_alloc(isl_dim_copy(bset->dim));
58         isl_int_set_si(v, -1);
59         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
60         isl_int_set_si(v, 1);
61         isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
62         bset = isl_basic_set_add_constraint(bset, c);
63
64         c = isl_inequality_alloc(isl_dim_copy(bset->dim));
65         isl_int_set_si(v, 1);
66         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
67         isl_int_set_si(v, -5);
68         isl_constraint_set_constant(c, v);
69         bset = isl_basic_set_add_constraint(bset, c);
70
71         isl_basic_set_free(bset);
72
73         isl_int_clear(v);
74 }
75
76 void test_dim(struct isl_ctx *ctx)
77 {
78         isl_map *map1, *map2;
79
80         map1 = isl_map_read_from_str(ctx,
81             "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
82         map1 = isl_map_add(map1, isl_dim_in, 1);
83         map2 = isl_map_read_from_str(ctx,
84             "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
85         assert(isl_map_is_equal(map1, map2));
86         isl_map_free(map2);
87
88         map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
89         map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }", -1);
90         assert(isl_map_is_equal(map1, map2));
91
92         isl_map_free(map1);
93         isl_map_free(map2);
94 }
95
96 void test_div(struct isl_ctx *ctx)
97 {
98         isl_int v;
99         int pos;
100         struct isl_dim *dim;
101         struct isl_div *div;
102         struct isl_basic_set *bset;
103         struct isl_constraint *c;
104
105         isl_int_init(v);
106
107         /* test 1 */
108         dim = isl_dim_set_alloc(ctx, 0, 1);
109         bset = isl_basic_set_universe(dim);
110
111         c = isl_equality_alloc(isl_dim_copy(bset->dim));
112         isl_int_set_si(v, -1);
113         isl_constraint_set_constant(c, v);
114         isl_int_set_si(v, 1);
115         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
116         div = isl_div_alloc(isl_dim_copy(bset->dim));
117         c = isl_constraint_add_div(c, div, &pos);
118         isl_int_set_si(v, 3);
119         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
120         bset = isl_basic_set_add_constraint(bset, c);
121
122         c = isl_equality_alloc(isl_dim_copy(bset->dim));
123         isl_int_set_si(v, 1);
124         isl_constraint_set_constant(c, v);
125         isl_int_set_si(v, -1);
126         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
127         div = isl_div_alloc(isl_dim_copy(bset->dim));
128         c = isl_constraint_add_div(c, div, &pos);
129         isl_int_set_si(v, 3);
130         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
131         bset = isl_basic_set_add_constraint(bset, c);
132
133         assert(bset->n_div == 1);
134         isl_basic_set_free(bset);
135
136         /* test 2 */
137         dim = isl_dim_set_alloc(ctx, 0, 1);
138         bset = isl_basic_set_universe(dim);
139
140         c = isl_equality_alloc(isl_dim_copy(bset->dim));
141         isl_int_set_si(v, 1);
142         isl_constraint_set_constant(c, v);
143         isl_int_set_si(v, -1);
144         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
145         div = isl_div_alloc(isl_dim_copy(bset->dim));
146         c = isl_constraint_add_div(c, div, &pos);
147         isl_int_set_si(v, 3);
148         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
149         bset = isl_basic_set_add_constraint(bset, c);
150
151         c = isl_equality_alloc(isl_dim_copy(bset->dim));
152         isl_int_set_si(v, -1);
153         isl_constraint_set_constant(c, v);
154         isl_int_set_si(v, 1);
155         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
156         div = isl_div_alloc(isl_dim_copy(bset->dim));
157         c = isl_constraint_add_div(c, div, &pos);
158         isl_int_set_si(v, 3);
159         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
160         bset = isl_basic_set_add_constraint(bset, c);
161
162         assert(bset->n_div == 1);
163         isl_basic_set_free(bset);
164
165         /* test 3 */
166         dim = isl_dim_set_alloc(ctx, 0, 1);
167         bset = isl_basic_set_universe(dim);
168
169         c = isl_equality_alloc(isl_dim_copy(bset->dim));
170         isl_int_set_si(v, 1);
171         isl_constraint_set_constant(c, v);
172         isl_int_set_si(v, -1);
173         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
174         div = isl_div_alloc(isl_dim_copy(bset->dim));
175         c = isl_constraint_add_div(c, div, &pos);
176         isl_int_set_si(v, 3);
177         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
178         bset = isl_basic_set_add_constraint(bset, c);
179
180         c = isl_equality_alloc(isl_dim_copy(bset->dim));
181         isl_int_set_si(v, -3);
182         isl_constraint_set_constant(c, v);
183         isl_int_set_si(v, 1);
184         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
185         div = isl_div_alloc(isl_dim_copy(bset->dim));
186         c = isl_constraint_add_div(c, div, &pos);
187         isl_int_set_si(v, 4);
188         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
189         bset = isl_basic_set_add_constraint(bset, c);
190
191         assert(bset->n_div == 1);
192         isl_basic_set_free(bset);
193
194         /* test 4 */
195         dim = isl_dim_set_alloc(ctx, 0, 1);
196         bset = isl_basic_set_universe(dim);
197
198         c = isl_equality_alloc(isl_dim_copy(bset->dim));
199         isl_int_set_si(v, 2);
200         isl_constraint_set_constant(c, v);
201         isl_int_set_si(v, -1);
202         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
203         div = isl_div_alloc(isl_dim_copy(bset->dim));
204         c = isl_constraint_add_div(c, div, &pos);
205         isl_int_set_si(v, 3);
206         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
207         bset = isl_basic_set_add_constraint(bset, c);
208
209         c = isl_equality_alloc(isl_dim_copy(bset->dim));
210         isl_int_set_si(v, -1);
211         isl_constraint_set_constant(c, v);
212         isl_int_set_si(v, 1);
213         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
214         div = isl_div_alloc(isl_dim_copy(bset->dim));
215         c = isl_constraint_add_div(c, div, &pos);
216         isl_int_set_si(v, 6);
217         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
218         bset = isl_basic_set_add_constraint(bset, c);
219
220         assert(isl_basic_set_is_empty(bset));
221         isl_basic_set_free(bset);
222
223         /* test 5 */
224         dim = isl_dim_set_alloc(ctx, 0, 2);
225         bset = isl_basic_set_universe(dim);
226
227         c = isl_equality_alloc(isl_dim_copy(bset->dim));
228         isl_int_set_si(v, -1);
229         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
230         div = isl_div_alloc(isl_dim_copy(bset->dim));
231         c = isl_constraint_add_div(c, div, &pos);
232         isl_int_set_si(v, 3);
233         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
234         bset = isl_basic_set_add_constraint(bset, c);
235
236         c = isl_equality_alloc(isl_dim_copy(bset->dim));
237         isl_int_set_si(v, 1);
238         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
239         isl_int_set_si(v, -3);
240         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
241         bset = isl_basic_set_add_constraint(bset, c);
242
243         assert(bset->n_div == 0);
244         isl_basic_set_free(bset);
245
246         /* test 6 */
247         dim = isl_dim_set_alloc(ctx, 0, 2);
248         bset = isl_basic_set_universe(dim);
249
250         c = isl_equality_alloc(isl_dim_copy(bset->dim));
251         isl_int_set_si(v, -1);
252         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
253         div = isl_div_alloc(isl_dim_copy(bset->dim));
254         c = isl_constraint_add_div(c, div, &pos);
255         isl_int_set_si(v, 6);
256         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
257         bset = isl_basic_set_add_constraint(bset, c);
258
259         c = isl_equality_alloc(isl_dim_copy(bset->dim));
260         isl_int_set_si(v, 1);
261         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
262         isl_int_set_si(v, -3);
263         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
264         bset = isl_basic_set_add_constraint(bset, c);
265
266         assert(bset->n_div == 1);
267         isl_basic_set_free(bset);
268
269         /* test 7 */
270         /* This test is a bit tricky.  We set up an equality
271          *              a + 3b + 3c = 6 e0
272          * Normalization of divs creates _two_ divs
273          *              a = 3 e0
274          *              c - b - e0 = 2 e1
275          * Afterwards e0 is removed again because it has coefficient -1
276          * and we end up with the original equality and div again.
277          * Perhaps we can avoid the introduction of this temporary div.
278          */
279         dim = isl_dim_set_alloc(ctx, 0, 3);
280         bset = isl_basic_set_universe(dim);
281
282         c = isl_equality_alloc(isl_dim_copy(bset->dim));
283         isl_int_set_si(v, -1);
284         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
285         isl_int_set_si(v, -3);
286         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
287         isl_int_set_si(v, -3);
288         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
289         div = isl_div_alloc(isl_dim_copy(bset->dim));
290         c = isl_constraint_add_div(c, div, &pos);
291         isl_int_set_si(v, 6);
292         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
293         bset = isl_basic_set_add_constraint(bset, c);
294
295         assert(bset->n_div == 1);
296         isl_basic_set_free(bset);
297
298         /* test 8 */
299         dim = isl_dim_set_alloc(ctx, 0, 4);
300         bset = isl_basic_set_universe(dim);
301
302         c = isl_equality_alloc(isl_dim_copy(bset->dim));
303         isl_int_set_si(v, -1);
304         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
305         isl_int_set_si(v, -3);
306         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
307         isl_int_set_si(v, -3);
308         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
309         div = isl_div_alloc(isl_dim_copy(bset->dim));
310         c = isl_constraint_add_div(c, div, &pos);
311         isl_int_set_si(v, 6);
312         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
313         bset = isl_basic_set_add_constraint(bset, c);
314
315         c = isl_equality_alloc(isl_dim_copy(bset->dim));
316         isl_int_set_si(v, -1);
317         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
318         isl_int_set_si(v, 1);
319         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
320         isl_int_set_si(v, 1);
321         isl_constraint_set_constant(c, v);
322         bset = isl_basic_set_add_constraint(bset, c);
323
324         assert(bset->n_div == 1);
325         isl_basic_set_free(bset);
326
327         /* test 9 */
328         dim = isl_dim_set_alloc(ctx, 0, 2);
329         bset = isl_basic_set_universe(dim);
330
331         c = isl_equality_alloc(isl_dim_copy(bset->dim));
332         isl_int_set_si(v, 1);
333         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
334         isl_int_set_si(v, -1);
335         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
336         div = isl_div_alloc(isl_dim_copy(bset->dim));
337         c = isl_constraint_add_div(c, div, &pos);
338         isl_int_set_si(v, -2);
339         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
340         bset = isl_basic_set_add_constraint(bset, c);
341
342         c = isl_equality_alloc(isl_dim_copy(bset->dim));
343         isl_int_set_si(v, -1);
344         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
345         div = isl_div_alloc(isl_dim_copy(bset->dim));
346         c = isl_constraint_add_div(c, div, &pos);
347         isl_int_set_si(v, 3);
348         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
349         isl_int_set_si(v, 2);
350         isl_constraint_set_constant(c, v);
351         bset = isl_basic_set_add_constraint(bset, c);
352
353         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
354
355         assert(!isl_basic_set_is_empty(bset));
356
357         isl_basic_set_free(bset);
358
359         /* test 10 */
360         dim = isl_dim_set_alloc(ctx, 0, 2);
361         bset = isl_basic_set_universe(dim);
362
363         c = isl_equality_alloc(isl_dim_copy(bset->dim));
364         isl_int_set_si(v, 1);
365         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
366         div = isl_div_alloc(isl_dim_copy(bset->dim));
367         c = isl_constraint_add_div(c, div, &pos);
368         isl_int_set_si(v, -2);
369         isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
370         bset = isl_basic_set_add_constraint(bset, c);
371
372         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
373
374         isl_basic_set_free(bset);
375
376         isl_int_clear(v);
377 }
378
379 void test_application_case(struct isl_ctx *ctx, const char *name)
380 {
381         char filename[PATH_MAX];
382         FILE *input;
383         int n;
384         struct isl_basic_set *bset1, *bset2;
385         struct isl_basic_map *bmap;
386
387         n = snprintf(filename, sizeof(filename),
388                         "%s/test_inputs/%s.omega", srcdir, name);
389         assert(n < sizeof(filename));
390         input = fopen(filename, "r");
391         assert(input);
392
393         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
394         bmap = isl_basic_map_read_from_file(ctx, input, 0);
395
396         bset1 = isl_basic_set_apply(bset1, bmap);
397
398         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
399
400         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
401
402         isl_basic_set_free(bset1);
403         isl_basic_set_free(bset2);
404
405         fclose(input);
406 }
407
408 void test_application(struct isl_ctx *ctx)
409 {
410         test_application_case(ctx, "application");
411         test_application_case(ctx, "application2");
412 }
413
414 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
415 {
416         char filename[PATH_MAX];
417         FILE *input;
418         int n;
419         struct isl_basic_set *bset1, *bset2;
420
421         n = snprintf(filename, sizeof(filename),
422                         "%s/test_inputs/%s.polylib", srcdir, name);
423         assert(n < sizeof(filename));
424         input = fopen(filename, "r");
425         assert(input);
426
427         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
428         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
429
430         bset1 = isl_basic_set_affine_hull(bset1);
431
432         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
433
434         isl_basic_set_free(bset1);
435         isl_basic_set_free(bset2);
436
437         fclose(input);
438 }
439
440 void test_affine_hull(struct isl_ctx *ctx)
441 {
442         test_affine_hull_case(ctx, "affine2");
443         test_affine_hull_case(ctx, "affine");
444         test_affine_hull_case(ctx, "affine3");
445 }
446
447 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
448 {
449         char filename[PATH_MAX];
450         FILE *input;
451         int n;
452         struct isl_basic_set *bset1, *bset2;
453         struct isl_set *set;
454
455         n = snprintf(filename, sizeof(filename),
456                         "%s/test_inputs/%s.polylib", srcdir, name);
457         assert(n < sizeof(filename));
458         input = fopen(filename, "r");
459         assert(input);
460
461         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
462         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
463
464         set = isl_basic_set_union(bset1, bset2);
465         bset1 = isl_set_convex_hull(set);
466
467         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
468
469         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
470
471         isl_basic_set_free(bset1);
472         isl_basic_set_free(bset2);
473
474         fclose(input);
475 }
476
477 void test_convex_hull(struct isl_ctx *ctx)
478 {
479         test_convex_hull_case(ctx, "convex0");
480         test_convex_hull_case(ctx, "convex1");
481         test_convex_hull_case(ctx, "convex2");
482         test_convex_hull_case(ctx, "convex3");
483         test_convex_hull_case(ctx, "convex4");
484         test_convex_hull_case(ctx, "convex5");
485         test_convex_hull_case(ctx, "convex6");
486         test_convex_hull_case(ctx, "convex7");
487         test_convex_hull_case(ctx, "convex8");
488         test_convex_hull_case(ctx, "convex9");
489         test_convex_hull_case(ctx, "convex10");
490         test_convex_hull_case(ctx, "convex11");
491         test_convex_hull_case(ctx, "convex12");
492         test_convex_hull_case(ctx, "convex13");
493         test_convex_hull_case(ctx, "convex14");
494 }
495
496 void test_gist_case(struct isl_ctx *ctx, const char *name)
497 {
498         char filename[PATH_MAX];
499         FILE *input;
500         int n;
501         struct isl_basic_set *bset1, *bset2;
502
503         n = snprintf(filename, sizeof(filename),
504                         "%s/test_inputs/%s.polylib", srcdir, name);
505         assert(n < sizeof(filename));
506         input = fopen(filename, "r");
507         assert(input);
508
509         bset1 = isl_basic_set_read_from_file(ctx, input, 0);
510         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
511
512         bset1 = isl_basic_set_gist(bset1, bset2);
513
514         bset2 = isl_basic_set_read_from_file(ctx, input, 0);
515
516         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
517
518         isl_basic_set_free(bset1);
519         isl_basic_set_free(bset2);
520
521         fclose(input);
522 }
523
524 void test_gist(struct isl_ctx *ctx)
525 {
526         test_gist_case(ctx, "gist1");
527 }
528
529 void test_coalesce(struct isl_ctx *ctx)
530 {
531         struct isl_set *set, *set2;
532         struct isl_map *map, *map2;
533
534         set = isl_set_read_from_str(ctx,
535                 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
536                        "y >= x & x >= 2 & 5 >= y }", -1);
537         set = isl_set_coalesce(set);
538         assert(set && set->n == 1);
539         isl_set_free(set);
540
541         set = isl_set_read_from_str(ctx,
542                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
543                        "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
544         set = isl_set_coalesce(set);
545         assert(set && set->n == 1);
546         isl_set_free(set);
547
548         set = isl_set_read_from_str(ctx,
549                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
550                        "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
551         set = isl_set_coalesce(set);
552         assert(set && set->n == 2);
553         isl_set_free(set);
554
555         set = isl_set_read_from_str(ctx,
556                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
557                        "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
558         set = isl_set_coalesce(set);
559         assert(set && set->n == 1);
560         isl_set_free(set);
561
562         set = isl_set_read_from_str(ctx,
563                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
564                        "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
565         set = isl_set_coalesce(set);
566         assert(set && set->n == 2);
567         isl_set_free(set);
568
569         set = isl_set_read_from_str(ctx,
570                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
571                        "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
572         set = isl_set_coalesce(set);
573         assert(set && set->n == 2);
574         isl_set_free(set);
575
576         set = isl_set_read_from_str(ctx,
577                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
578                        "y >= 0 & x = 6 & y <= 6}", -1);
579         set = isl_set_coalesce(set);
580         assert(set && set->n == 1);
581         isl_set_free(set);
582
583         set = isl_set_read_from_str(ctx,
584                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
585                        "y >= 0 & x = 7 & y <= 6}", -1);
586         set = isl_set_coalesce(set);
587         assert(set && set->n == 2);
588         isl_set_free(set);
589
590         set = isl_set_read_from_str(ctx,
591                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
592                        "y >= 0 & x = 6 & y <= 5}", -1);
593         set = isl_set_coalesce(set);
594         assert(set && set->n == 1);
595         set2 = isl_set_read_from_str(ctx,
596                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
597                        "y >= 0 & x = 6 & y <= 5}", -1);
598         assert(isl_set_is_equal(set, set2));
599         isl_set_free(set);
600         isl_set_free(set2);
601
602         set = isl_set_read_from_str(ctx,
603                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
604                        "y >= 0 & x = 6 & y <= 7}", -1);
605         set = isl_set_coalesce(set);
606         assert(set && set->n == 2);
607         isl_set_free(set);
608
609         set = isl_set_read_from_str(ctx,
610                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
611         set = isl_set_coalesce(set);
612         assert(set && set->n == 1);
613         set2 = isl_set_read_from_str(ctx,
614                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
615         assert(isl_set_is_equal(set, set2));
616         isl_set_free(set);
617         isl_set_free(set2);
618
619         set = isl_set_read_from_str(ctx,
620                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
621         set = isl_set_coalesce(set);
622         set2 = isl_set_read_from_str(ctx,
623                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
624         assert(isl_set_is_equal(set, set2));
625         isl_set_free(set);
626         isl_set_free(set2);
627
628         set = isl_set_read_from_str(ctx,
629                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
630                                 "2 <= i and i <= n }", -1);
631         set = isl_set_coalesce(set);
632         assert(set && set->n == 1);
633         set2 = isl_set_read_from_str(ctx,
634                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
635                                 "2 <= i and i <= n }", -1);
636         assert(isl_set_is_equal(set, set2));
637         isl_set_free(set);
638         isl_set_free(set2);
639
640         map = isl_map_read_from_str(ctx,
641                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
642                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
643                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
644                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
645                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
646                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
647                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
648                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
649                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
650                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
651                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
652                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
653                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
654                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
655                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
656                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
657                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
658                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
659         map = isl_map_coalesce(map);
660         map2 = isl_map_read_from_str(ctx,
661                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
662                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
663                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
664                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
665                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
666                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
667                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
668                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
669                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
670                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
671                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
672                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
673                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
674                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
675                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
676                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
677                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
678                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
679         assert(isl_map_is_equal(map, map2));
680         isl_map_free(map);
681         isl_map_free(map2);
682 }
683
684 void test_closure(struct isl_ctx *ctx)
685 {
686         isl_set *dom;
687         isl_map *up, *right;
688         isl_map *map, *map2;
689         int exact;
690
691         /* COCOA example 1 */
692         map = isl_map_read_from_str(ctx,
693                 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
694                         "1 <= i and i < n and 1 <= j and j < n or "
695                         "i2 = i + 1 and j2 = j - 1 and "
696                         "1 <= i and i < n and 2 <= j and j <= n }", -1);
697         map = isl_map_power(map, 1, &exact);
698         assert(exact);
699         isl_map_free(map);
700
701         /* COCOA example 1 */
702         map = isl_map_read_from_str(ctx,
703                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
704                         "1 <= i and i < n and 1 <= j and j < n or "
705                         "i2 = i + 1 and j2 = j - 1 and "
706                         "1 <= i and i < n and 2 <= j and j <= n }", -1);
707         map = isl_map_transitive_closure(map, &exact);
708         assert(exact);
709         map2 = isl_map_read_from_str(ctx,
710                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
711                         "1 <= i and i < n and 1 <= j and j <= n and "
712                         "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
713                         "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
714                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
715         assert(isl_map_is_equal(map, map2));
716         isl_map_free(map2);
717         isl_map_free(map);
718
719         map = isl_map_read_from_str(ctx,
720                 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
721                                      " 0 <= y and y <= n }", -1);
722         map = isl_map_transitive_closure(map, &exact);
723         map2 = isl_map_read_from_str(ctx,
724                 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
725                                      " 0 <= y and y <= n }", -1);
726         assert(isl_map_is_equal(map, map2));
727         isl_map_free(map2);
728         isl_map_free(map);
729
730         /* COCOA example 2 */
731         map = isl_map_read_from_str(ctx,
732                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
733                         "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
734                         "i2 = i + 2 and j2 = j - 2 and "
735                         "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
736         map = isl_map_transitive_closure(map, &exact);
737         assert(exact);
738         map2 = isl_map_read_from_str(ctx,
739                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
740                         "1 <= i and i < n - 1 and 1 <= j and j <= n and "
741                         "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
742                         "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
743                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
744         assert(isl_map_is_equal(map, map2));
745         isl_map_free(map);
746         isl_map_free(map2);
747
748         /* COCOA Fig.2 left */
749         map = isl_map_read_from_str(ctx,
750                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
751                         "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
752                         "j <= n or "
753                         "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
754                         "j <= 2 i - 3 and j <= n - 2 or "
755                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
756                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
757         map = isl_map_transitive_closure(map, &exact);
758         assert(exact);
759         isl_map_free(map);
760
761         /* COCOA Fig.2 right */
762         map = isl_map_read_from_str(ctx,
763                 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
764                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
765                         "j <= n or "
766                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
767                         "j <= 2 i - 4 and j <= n - 3 or "
768                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
769                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
770         map = isl_map_power(map, 1, &exact);
771         assert(exact);
772         isl_map_free(map);
773
774         /* COCOA Fig.2 right */
775         map = isl_map_read_from_str(ctx,
776                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
777                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
778                         "j <= n or "
779                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
780                         "j <= 2 i - 4 and j <= n - 3 or "
781                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
782                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
783         map = isl_map_transitive_closure(map, &exact);
784         assert(exact);
785         map2 = isl_map_read_from_str(ctx,
786                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
787                         "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
788                         "j <= n and 3 + i + 2 j <= 3 n and "
789                         "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
790                         "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
791                         "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
792                         "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
793                         "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
794         assert(isl_map_is_equal(map, map2));
795         isl_map_free(map2);
796         isl_map_free(map);
797
798         /* COCOA Fig.1 right */
799         dom = isl_set_read_from_str(ctx,
800                 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
801                         "2 x - 3 y + 3 >= 0 }", -1);
802         right = isl_map_read_from_str(ctx,
803                 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
804         up = isl_map_read_from_str(ctx,
805                 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
806         right = isl_map_intersect_domain(right, isl_set_copy(dom));
807         right = isl_map_intersect_range(right, isl_set_copy(dom));
808         up = isl_map_intersect_domain(up, isl_set_copy(dom));
809         up = isl_map_intersect_range(up, dom);
810         map = isl_map_union(up, right);
811         map = isl_map_transitive_closure(map, &exact);
812         assert(!exact);
813         isl_map_free(map);
814
815         /* COCOA Theorem 1 counter example */
816         map = isl_map_read_from_str(ctx,
817                 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
818                         "i2 = 1 and j2 = j or "
819                         "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
820         map = isl_map_transitive_closure(map, &exact);
821         assert(exact);
822         isl_map_free(map);
823
824         map = isl_map_read_from_str(ctx,
825                 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
826                         "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
827                         "i2 = i + 1 and 3 <= j2 - j <= 4 and "
828                         "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
829         map = isl_map_transitive_closure(map, &exact);
830         assert(exact);
831         isl_map_free(map);
832
833         /* Kelly et al 1996, fig 12 */
834         map = isl_map_read_from_str(ctx,
835                 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
836                         "1 <= i,j,j+1 <= n or "
837                         "j = n and j2 = 1 and i2 = i + 1 and "
838                         "1 <= i,i+1 <= n }", -1);
839         map = isl_map_transitive_closure(map, &exact);
840         assert(exact);
841         map2 = isl_map_read_from_str(ctx,
842                 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
843                         "1 <= i <= n and i = i2 or "
844                         "1 <= i < i2 <= n and 1 <= j <= n and "
845                         "1 <= j2 <= n }", -1);
846         assert(isl_map_is_equal(map, map2));
847         isl_map_free(map2);
848         isl_map_free(map);
849
850         /* Omega's closure4 */
851         map = isl_map_read_from_str(ctx,
852                 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
853                         "1 <= x,y <= 10 or "
854                         "x2 = x + 1 and y2 = y and "
855                         "1 <= x <= 20 && 5 <= y <= 15 }", -1);
856         map = isl_map_transitive_closure(map, &exact);
857         assert(exact);
858         isl_map_free(map);
859
860         map = isl_map_read_from_str(ctx,
861                 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
862         map = isl_map_transitive_closure(map, &exact);
863         assert(!exact);
864         map2 = isl_map_read_from_str(ctx,
865                 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
866         assert(isl_map_is_equal(map, map2));
867         isl_map_free(map);
868         isl_map_free(map2);
869 }
870
871 int main()
872 {
873         struct isl_ctx *ctx;
874
875         srcdir = getenv("srcdir");
876         assert(srcdir);
877
878         ctx = isl_ctx_alloc();
879         test_read(ctx);
880         test_construction(ctx);
881         test_dim(ctx);
882         test_div(ctx);
883         test_application(ctx);
884         test_affine_hull(ctx);
885         test_convex_hull(ctx);
886         test_gist(ctx);
887         test_coalesce(ctx);
888         test_closure(ctx);
889         isl_ctx_free(ctx);
890         return 0;
891 }