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