add isl_set_flatten
[isl.git] / isl_test.c
blobb73b642d78de2341f67f9102367065c12439b2de
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
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>
19 static char *srcdir;
21 void test_parse_map(isl_ctx *ctx, const char *str)
23 isl_map *map;
25 map = isl_map_read_from_str(ctx, str, -1);
26 assert(map);
27 isl_map_free(map);
30 void test_parse(struct isl_ctx *ctx)
32 isl_map *map;
33 const char *str;
35 str = "{ [i] -> [-i] }";
36 map = isl_map_read_from_str(ctx, str, -1);
37 assert(map);
38 isl_map_free(map);
40 str = "{ A[i] -> L[([i/3])] }";
41 map = isl_map_read_from_str(ctx, str, -1);
42 assert(map);
43 isl_map_free(map);
45 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
48 void test_read(struct isl_ctx *ctx)
50 char filename[PATH_MAX];
51 FILE *input;
52 int n;
53 struct isl_basic_set *bset1, *bset2;
54 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
56 n = snprintf(filename, sizeof(filename),
57 "%s/test_inputs/set.omega", srcdir);
58 assert(n < sizeof(filename));
59 input = fopen(filename, "r");
60 assert(input);
62 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
63 bset2 = isl_basic_set_read_from_str(ctx, str, 0);
65 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
67 isl_basic_set_free(bset1);
68 isl_basic_set_free(bset2);
70 fclose(input);
73 void test_bounded(struct isl_ctx *ctx)
75 isl_set *set;
76 int bounded;
78 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }", -1);
79 bounded = isl_set_is_bounded(set);
80 assert(bounded);
81 isl_set_free(set);
83 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }", -1);
84 bounded = isl_set_is_bounded(set);
85 assert(!bounded);
86 isl_set_free(set);
88 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }", -1);
89 bounded = isl_set_is_bounded(set);
90 assert(!bounded);
91 isl_set_free(set);
94 /* Construct the basic set { [i] : 5 <= i <= N } */
95 void test_construction(struct isl_ctx *ctx)
97 isl_int v;
98 struct isl_dim *dim;
99 struct isl_basic_set *bset;
100 struct isl_constraint *c;
102 isl_int_init(v);
104 dim = isl_dim_set_alloc(ctx, 1, 1);
105 bset = isl_basic_set_universe(dim);
107 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
108 isl_int_set_si(v, -1);
109 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
110 isl_int_set_si(v, 1);
111 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
112 bset = isl_basic_set_add_constraint(bset, c);
114 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
115 isl_int_set_si(v, 1);
116 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
117 isl_int_set_si(v, -5);
118 isl_constraint_set_constant(c, v);
119 bset = isl_basic_set_add_constraint(bset, c);
121 isl_basic_set_free(bset);
123 isl_int_clear(v);
126 void test_dim(struct isl_ctx *ctx)
128 const char *str;
129 isl_map *map1, *map2;
131 map1 = isl_map_read_from_str(ctx,
132 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
133 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
134 map2 = isl_map_read_from_str(ctx,
135 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
136 assert(isl_map_is_equal(map1, map2));
137 isl_map_free(map2);
139 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
140 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }", -1);
141 assert(isl_map_is_equal(map1, map2));
143 isl_map_free(map1);
144 isl_map_free(map2);
146 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
147 map1 = isl_map_read_from_str(ctx, str, -1);
148 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
149 map2 = isl_map_read_from_str(ctx, str, -1);
150 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
151 assert(isl_map_is_equal(map1, map2));
153 isl_map_free(map1);
154 isl_map_free(map2);
157 void test_div(struct isl_ctx *ctx)
159 isl_int v;
160 int pos;
161 struct isl_dim *dim;
162 struct isl_div *div;
163 struct isl_basic_set *bset;
164 struct isl_constraint *c;
166 isl_int_init(v);
168 /* test 1 */
169 dim = isl_dim_set_alloc(ctx, 0, 1);
170 bset = isl_basic_set_universe(dim);
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);
183 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
184 isl_int_set_si(v, 1);
185 isl_constraint_set_constant(c, v);
186 isl_int_set_si(v, -1);
187 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
188 div = isl_div_alloc(isl_basic_set_get_dim(bset));
189 c = isl_constraint_add_div(c, div, &pos);
190 isl_int_set_si(v, 3);
191 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
192 bset = isl_basic_set_add_constraint(bset, c);
194 assert(bset && bset->n_div == 1);
195 isl_basic_set_free(bset);
197 /* test 2 */
198 dim = isl_dim_set_alloc(ctx, 0, 1);
199 bset = isl_basic_set_universe(dim);
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);
212 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
213 isl_int_set_si(v, -1);
214 isl_constraint_set_constant(c, v);
215 isl_int_set_si(v, 1);
216 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
217 div = isl_div_alloc(isl_basic_set_get_dim(bset));
218 c = isl_constraint_add_div(c, div, &pos);
219 isl_int_set_si(v, 3);
220 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
221 bset = isl_basic_set_add_constraint(bset, c);
223 assert(bset && bset->n_div == 1);
224 isl_basic_set_free(bset);
226 /* test 3 */
227 dim = isl_dim_set_alloc(ctx, 0, 1);
228 bset = isl_basic_set_universe(dim);
230 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
231 isl_int_set_si(v, 1);
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, 3);
238 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
239 bset = isl_basic_set_add_constraint(bset, c);
241 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
242 isl_int_set_si(v, -3);
243 isl_constraint_set_constant(c, v);
244 isl_int_set_si(v, 1);
245 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
246 div = isl_div_alloc(isl_basic_set_get_dim(bset));
247 c = isl_constraint_add_div(c, div, &pos);
248 isl_int_set_si(v, 4);
249 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
250 bset = isl_basic_set_add_constraint(bset, c);
252 assert(bset && bset->n_div == 1);
253 isl_basic_set_free(bset);
255 /* test 4 */
256 dim = isl_dim_set_alloc(ctx, 0, 1);
257 bset = isl_basic_set_universe(dim);
259 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
260 isl_int_set_si(v, 2);
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, 3);
267 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
268 bset = isl_basic_set_add_constraint(bset, c);
270 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
271 isl_int_set_si(v, -1);
272 isl_constraint_set_constant(c, v);
273 isl_int_set_si(v, 1);
274 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
275 div = isl_div_alloc(isl_basic_set_get_dim(bset));
276 c = isl_constraint_add_div(c, div, &pos);
277 isl_int_set_si(v, 6);
278 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
279 bset = isl_basic_set_add_constraint(bset, c);
281 assert(isl_basic_set_is_empty(bset));
282 isl_basic_set_free(bset);
284 /* test 5 */
285 dim = isl_dim_set_alloc(ctx, 0, 2);
286 bset = isl_basic_set_universe(dim);
288 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
289 isl_int_set_si(v, -1);
290 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
291 div = isl_div_alloc(isl_basic_set_get_dim(bset));
292 c = isl_constraint_add_div(c, div, &pos);
293 isl_int_set_si(v, 3);
294 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
295 bset = isl_basic_set_add_constraint(bset, c);
297 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
298 isl_int_set_si(v, 1);
299 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
300 isl_int_set_si(v, -3);
301 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
302 bset = isl_basic_set_add_constraint(bset, c);
304 assert(bset && bset->n_div == 0);
305 isl_basic_set_free(bset);
307 /* test 6 */
308 dim = isl_dim_set_alloc(ctx, 0, 2);
309 bset = isl_basic_set_universe(dim);
311 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
312 isl_int_set_si(v, -1);
313 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
314 div = isl_div_alloc(isl_basic_set_get_dim(bset));
315 c = isl_constraint_add_div(c, div, &pos);
316 isl_int_set_si(v, 6);
317 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
318 bset = isl_basic_set_add_constraint(bset, c);
320 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
321 isl_int_set_si(v, 1);
322 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
323 isl_int_set_si(v, -3);
324 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
325 bset = isl_basic_set_add_constraint(bset, c);
327 assert(bset && bset->n_div == 1);
328 isl_basic_set_free(bset);
330 /* test 7 */
331 /* This test is a bit tricky. We set up an equality
332 * a + 3b + 3c = 6 e0
333 * Normalization of divs creates _two_ divs
334 * a = 3 e0
335 * c - b - e0 = 2 e1
336 * Afterwards e0 is removed again because it has coefficient -1
337 * and we end up with the original equality and div again.
338 * Perhaps we can avoid the introduction of this temporary div.
340 dim = isl_dim_set_alloc(ctx, 0, 3);
341 bset = isl_basic_set_universe(dim);
343 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
344 isl_int_set_si(v, -1);
345 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
346 isl_int_set_si(v, -3);
347 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
348 isl_int_set_si(v, -3);
349 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
350 div = isl_div_alloc(isl_basic_set_get_dim(bset));
351 c = isl_constraint_add_div(c, div, &pos);
352 isl_int_set_si(v, 6);
353 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
354 bset = isl_basic_set_add_constraint(bset, c);
356 /* Test disabled for now */
358 assert(bset && bset->n_div == 1);
360 isl_basic_set_free(bset);
362 /* test 8 */
363 dim = isl_dim_set_alloc(ctx, 0, 4);
364 bset = isl_basic_set_universe(dim);
366 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
367 isl_int_set_si(v, -1);
368 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
369 isl_int_set_si(v, -3);
370 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
371 isl_int_set_si(v, -3);
372 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
373 div = isl_div_alloc(isl_basic_set_get_dim(bset));
374 c = isl_constraint_add_div(c, div, &pos);
375 isl_int_set_si(v, 6);
376 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
377 bset = isl_basic_set_add_constraint(bset, c);
379 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
380 isl_int_set_si(v, -1);
381 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
382 isl_int_set_si(v, 1);
383 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
384 isl_int_set_si(v, 1);
385 isl_constraint_set_constant(c, v);
386 bset = isl_basic_set_add_constraint(bset, c);
388 /* Test disabled for now */
390 assert(bset && bset->n_div == 1);
392 isl_basic_set_free(bset);
394 /* test 9 */
395 dim = isl_dim_set_alloc(ctx, 0, 2);
396 bset = isl_basic_set_universe(dim);
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 isl_int_set_si(v, -1);
402 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
403 div = isl_div_alloc(isl_basic_set_get_dim(bset));
404 c = isl_constraint_add_div(c, div, &pos);
405 isl_int_set_si(v, -2);
406 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
407 bset = isl_basic_set_add_constraint(bset, c);
409 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
410 isl_int_set_si(v, -1);
411 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
412 div = isl_div_alloc(isl_basic_set_get_dim(bset));
413 c = isl_constraint_add_div(c, div, &pos);
414 isl_int_set_si(v, 3);
415 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
416 isl_int_set_si(v, 2);
417 isl_constraint_set_constant(c, v);
418 bset = isl_basic_set_add_constraint(bset, c);
420 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
422 assert(!isl_basic_set_is_empty(bset));
424 isl_basic_set_free(bset);
426 /* test 10 */
427 dim = isl_dim_set_alloc(ctx, 0, 2);
428 bset = isl_basic_set_universe(dim);
430 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
431 isl_int_set_si(v, 1);
432 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
433 div = isl_div_alloc(isl_basic_set_get_dim(bset));
434 c = isl_constraint_add_div(c, div, &pos);
435 isl_int_set_si(v, -2);
436 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
437 bset = isl_basic_set_add_constraint(bset, c);
439 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
441 isl_basic_set_free(bset);
443 isl_int_clear(v);
446 void test_application_case(struct isl_ctx *ctx, const char *name)
448 char filename[PATH_MAX];
449 FILE *input;
450 int n;
451 struct isl_basic_set *bset1, *bset2;
452 struct isl_basic_map *bmap;
454 n = snprintf(filename, sizeof(filename),
455 "%s/test_inputs/%s.omega", srcdir, name);
456 assert(n < sizeof(filename));
457 input = fopen(filename, "r");
458 assert(input);
460 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
461 bmap = isl_basic_map_read_from_file(ctx, input, 0);
463 bset1 = isl_basic_set_apply(bset1, bmap);
465 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
467 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
469 isl_basic_set_free(bset1);
470 isl_basic_set_free(bset2);
472 fclose(input);
475 void test_application(struct isl_ctx *ctx)
477 test_application_case(ctx, "application");
478 test_application_case(ctx, "application2");
481 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
483 char filename[PATH_MAX];
484 FILE *input;
485 int n;
486 struct isl_basic_set *bset1, *bset2;
488 n = snprintf(filename, sizeof(filename),
489 "%s/test_inputs/%s.polylib", srcdir, name);
490 assert(n < sizeof(filename));
491 input = fopen(filename, "r");
492 assert(input);
494 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
495 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
497 bset1 = isl_basic_set_affine_hull(bset1);
499 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
501 isl_basic_set_free(bset1);
502 isl_basic_set_free(bset2);
504 fclose(input);
507 void test_affine_hull(struct isl_ctx *ctx)
509 test_affine_hull_case(ctx, "affine2");
510 test_affine_hull_case(ctx, "affine");
511 test_affine_hull_case(ctx, "affine3");
514 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
516 char filename[PATH_MAX];
517 FILE *input;
518 int n;
519 struct isl_basic_set *bset1, *bset2;
520 struct isl_set *set;
522 n = snprintf(filename, sizeof(filename),
523 "%s/test_inputs/%s.polylib", srcdir, name);
524 assert(n < sizeof(filename));
525 input = fopen(filename, "r");
526 assert(input);
528 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
529 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
531 set = isl_basic_set_union(bset1, bset2);
532 bset1 = isl_set_convex_hull(set);
534 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
536 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
538 isl_basic_set_free(bset1);
539 isl_basic_set_free(bset2);
541 fclose(input);
544 void test_convex_hull(struct isl_ctx *ctx)
546 const char *str1, *str2;
547 isl_set *set1, *set2;
549 test_convex_hull_case(ctx, "convex0");
550 test_convex_hull_case(ctx, "convex1");
551 test_convex_hull_case(ctx, "convex2");
552 test_convex_hull_case(ctx, "convex3");
553 test_convex_hull_case(ctx, "convex4");
554 test_convex_hull_case(ctx, "convex5");
555 test_convex_hull_case(ctx, "convex6");
556 test_convex_hull_case(ctx, "convex7");
557 test_convex_hull_case(ctx, "convex8");
558 test_convex_hull_case(ctx, "convex9");
559 test_convex_hull_case(ctx, "convex10");
560 test_convex_hull_case(ctx, "convex11");
561 test_convex_hull_case(ctx, "convex12");
562 test_convex_hull_case(ctx, "convex13");
563 test_convex_hull_case(ctx, "convex14");
564 test_convex_hull_case(ctx, "convex15");
566 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
567 "(i0 = 1 and i1 = 0 and i2 = 1) or "
568 "(i0 = 0 and i1 = 0 and i2 = 0) }";
569 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
570 set1 = isl_set_read_from_str(ctx, str1, -1);
571 set2 = isl_set_read_from_str(ctx, str2, -1);
572 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
573 assert(isl_set_is_equal(set1, set2));
574 isl_set_free(set1);
575 isl_set_free(set2);
578 void test_gist_case(struct isl_ctx *ctx, const char *name)
580 char filename[PATH_MAX];
581 FILE *input;
582 int n;
583 struct isl_basic_set *bset1, *bset2;
585 n = snprintf(filename, sizeof(filename),
586 "%s/test_inputs/%s.polylib", srcdir, name);
587 assert(n < sizeof(filename));
588 input = fopen(filename, "r");
589 assert(input);
591 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
592 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
594 bset1 = isl_basic_set_gist(bset1, bset2);
596 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
598 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
600 isl_basic_set_free(bset1);
601 isl_basic_set_free(bset2);
603 fclose(input);
606 void test_gist(struct isl_ctx *ctx)
608 test_gist_case(ctx, "gist1");
611 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
613 isl_set *set, *set2;
615 set = isl_set_read_from_str(ctx, str, -1);
616 set = isl_set_coalesce(set);
617 set2 = isl_set_read_from_str(ctx, str, -1);
618 assert(isl_set_is_equal(set, set2));
619 if (check_one)
620 assert(set && set->n == 1);
621 isl_set_free(set);
622 isl_set_free(set2);
625 void test_coalesce(struct isl_ctx *ctx)
627 const char *str;
628 struct isl_set *set, *set2;
629 struct isl_map *map, *map2;
631 set = isl_set_read_from_str(ctx,
632 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
633 "y >= x & x >= 2 & 5 >= y }", -1);
634 set = isl_set_coalesce(set);
635 assert(set && set->n == 1);
636 isl_set_free(set);
638 set = isl_set_read_from_str(ctx,
639 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
640 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
641 set = isl_set_coalesce(set);
642 assert(set && set->n == 1);
643 isl_set_free(set);
645 set = isl_set_read_from_str(ctx,
646 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
647 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
648 set = isl_set_coalesce(set);
649 assert(set && set->n == 2);
650 isl_set_free(set);
652 set = isl_set_read_from_str(ctx,
653 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
654 "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
655 set = isl_set_coalesce(set);
656 assert(set && set->n == 1);
657 isl_set_free(set);
659 set = isl_set_read_from_str(ctx,
660 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
661 "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
662 set = isl_set_coalesce(set);
663 assert(set && set->n == 2);
664 isl_set_free(set);
666 set = isl_set_read_from_str(ctx,
667 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
668 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
669 set = isl_set_coalesce(set);
670 assert(set && set->n == 2);
671 isl_set_free(set);
673 set = isl_set_read_from_str(ctx,
674 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
675 "y >= 0 & x = 6 & y <= 6}", -1);
676 set = isl_set_coalesce(set);
677 assert(set && set->n == 1);
678 isl_set_free(set);
680 set = isl_set_read_from_str(ctx,
681 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
682 "y >= 0 & x = 7 & y <= 6}", -1);
683 set = isl_set_coalesce(set);
684 assert(set && set->n == 2);
685 isl_set_free(set);
687 set = isl_set_read_from_str(ctx,
688 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
689 "y >= 0 & x = 6 & y <= 5}", -1);
690 set = isl_set_coalesce(set);
691 assert(set && set->n == 1);
692 set2 = isl_set_read_from_str(ctx,
693 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
694 "y >= 0 & x = 6 & y <= 5}", -1);
695 assert(isl_set_is_equal(set, set2));
696 isl_set_free(set);
697 isl_set_free(set2);
699 set = isl_set_read_from_str(ctx,
700 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
701 "y >= 0 & x = 6 & y <= 7}", -1);
702 set = isl_set_coalesce(set);
703 assert(set && set->n == 2);
704 isl_set_free(set);
706 set = isl_set_read_from_str(ctx,
707 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
708 set = isl_set_coalesce(set);
709 assert(set && set->n == 1);
710 set2 = isl_set_read_from_str(ctx,
711 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
712 assert(isl_set_is_equal(set, set2));
713 isl_set_free(set);
714 isl_set_free(set2);
716 set = isl_set_read_from_str(ctx,
717 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
718 set = isl_set_coalesce(set);
719 set2 = isl_set_read_from_str(ctx,
720 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
721 assert(isl_set_is_equal(set, set2));
722 isl_set_free(set);
723 isl_set_free(set2);
725 set = isl_set_read_from_str(ctx,
726 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
727 "2 <= i and i <= n }", -1);
728 set = isl_set_coalesce(set);
729 assert(set && set->n == 1);
730 set2 = isl_set_read_from_str(ctx,
731 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
732 "2 <= i and i <= n }", -1);
733 assert(isl_set_is_equal(set, set2));
734 isl_set_free(set);
735 isl_set_free(set2);
737 map = isl_map_read_from_str(ctx,
738 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
739 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
740 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
741 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
742 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
743 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
744 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
745 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
746 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
747 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
748 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
749 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
750 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
751 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
752 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
753 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
754 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
755 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
756 map = isl_map_coalesce(map);
757 map2 = isl_map_read_from_str(ctx,
758 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
759 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
760 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
761 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
762 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
763 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
764 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
765 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
766 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
767 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
768 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
769 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
770 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
771 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
772 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
773 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
774 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
775 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
776 assert(isl_map_is_equal(map, map2));
777 isl_map_free(map);
778 isl_map_free(map2);
780 str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
781 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
782 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
783 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
784 map = isl_map_read_from_str(ctx, str, -1);
785 map = isl_map_coalesce(map);
786 map2 = isl_map_read_from_str(ctx, str, -1);
787 assert(isl_map_is_equal(map, map2));
788 isl_map_free(map);
789 isl_map_free(map2);
791 str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
792 "[o0, o1, o2, o3, o4, o5, o6] : "
793 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
794 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
795 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
796 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
797 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
798 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
799 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
800 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
801 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
802 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
803 "o6 >= i3 + i6 - o3 and M >= 0 and "
804 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
805 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
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);
813 str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
814 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
815 "(o0 = 0 and M >= 2 and N >= 3) or "
816 "(M = 0 and o0 = 0 and N >= 3) }";
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);
824 str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
825 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
826 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
827 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
828 map = isl_map_read_from_str(ctx, str, -1);
829 map = isl_map_coalesce(map);
830 map2 = isl_map_read_from_str(ctx, str, -1);
831 assert(isl_map_is_equal(map, map2));
832 isl_map_free(map);
833 isl_map_free(map2);
835 test_coalesce_set(ctx,
836 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
837 "(i1 = M and M >= 1) }", 0);
838 test_coalesce_set(ctx,
839 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
840 test_coalesce_set(ctx,
841 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
842 "(y = 3 and x = 1) }", 1);
843 test_coalesce_set(ctx,
844 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
845 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
846 "i1 <= M and i3 <= M and i4 <= M) or "
847 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
848 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
849 "i4 <= -1 + M) }", 1);
850 test_coalesce_set(ctx,
851 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
852 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
853 test_coalesce_set(ctx,
854 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
855 "-x - y + 1 >= 0 and -3 <= z <= 3;"
856 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
857 "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
858 test_coalesce_set(ctx,
859 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
860 test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
861 test_coalesce_set(ctx,
862 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
863 test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
866 void test_closure(struct isl_ctx *ctx)
868 const char *str;
869 isl_set *dom;
870 isl_map *up, *right;
871 isl_map *map, *map2;
872 int exact;
874 /* COCOA example 1 */
875 map = isl_map_read_from_str(ctx,
876 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
877 "1 <= i and i < n and 1 <= j and j < n or "
878 "i2 = i + 1 and j2 = j - 1 and "
879 "1 <= i and i < n and 2 <= j and j <= n }", -1);
880 map = isl_map_power(map, 1, &exact);
881 assert(exact);
882 isl_map_free(map);
884 /* COCOA example 1 */
885 map = isl_map_read_from_str(ctx,
886 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
887 "1 <= i and i < n and 1 <= j and j < n or "
888 "i2 = i + 1 and j2 = j - 1 and "
889 "1 <= i and i < n and 2 <= j and j <= n }", -1);
890 map = isl_map_transitive_closure(map, &exact);
891 assert(exact);
892 map2 = isl_map_read_from_str(ctx,
893 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
894 "1 <= i and i < n and 1 <= j and j <= n and "
895 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
896 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
897 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
898 assert(isl_map_is_equal(map, map2));
899 isl_map_free(map2);
900 isl_map_free(map);
902 map = isl_map_read_from_str(ctx,
903 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
904 " 0 <= y and y <= n }", -1);
905 map = isl_map_transitive_closure(map, &exact);
906 map2 = isl_map_read_from_str(ctx,
907 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
908 " 0 <= y and y <= n }", -1);
909 assert(isl_map_is_equal(map, map2));
910 isl_map_free(map2);
911 isl_map_free(map);
913 /* COCOA example 2 */
914 map = isl_map_read_from_str(ctx,
915 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
916 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
917 "i2 = i + 2 and j2 = j - 2 and "
918 "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
919 map = isl_map_transitive_closure(map, &exact);
920 assert(exact);
921 map2 = isl_map_read_from_str(ctx,
922 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
923 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
924 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
925 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
926 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
927 assert(isl_map_is_equal(map, map2));
928 isl_map_free(map);
929 isl_map_free(map2);
931 /* COCOA Fig.2 left */
932 map = isl_map_read_from_str(ctx,
933 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
934 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
935 "j <= n or "
936 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
937 "j <= 2 i - 3 and j <= n - 2 or "
938 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
939 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
940 map = isl_map_transitive_closure(map, &exact);
941 assert(exact);
942 isl_map_free(map);
944 /* COCOA Fig.2 right */
945 map = isl_map_read_from_str(ctx,
946 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
947 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
948 "j <= n or "
949 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
950 "j <= 2 i - 4 and j <= n - 3 or "
951 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
952 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
953 map = isl_map_power(map, 1, &exact);
954 assert(exact);
955 isl_map_free(map);
957 /* COCOA Fig.2 right */
958 map = isl_map_read_from_str(ctx,
959 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
960 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
961 "j <= n or "
962 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
963 "j <= 2 i - 4 and j <= n - 3 or "
964 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
965 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
966 map = isl_map_transitive_closure(map, &exact);
967 assert(exact);
968 map2 = isl_map_read_from_str(ctx,
969 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
970 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
971 "j <= n and 3 + i + 2 j <= 3 n and "
972 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
973 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
974 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
975 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
976 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
977 assert(isl_map_is_equal(map, map2));
978 isl_map_free(map2);
979 isl_map_free(map);
981 /* COCOA Fig.1 right */
982 dom = isl_set_read_from_str(ctx,
983 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
984 "2 x - 3 y + 3 >= 0 }", -1);
985 right = isl_map_read_from_str(ctx,
986 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
987 up = isl_map_read_from_str(ctx,
988 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
989 right = isl_map_intersect_domain(right, isl_set_copy(dom));
990 right = isl_map_intersect_range(right, isl_set_copy(dom));
991 up = isl_map_intersect_domain(up, isl_set_copy(dom));
992 up = isl_map_intersect_range(up, dom);
993 map = isl_map_union(up, right);
994 map = isl_map_transitive_closure(map, &exact);
995 assert(exact);
996 map2 = isl_map_read_from_str(ctx,
997 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
998 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }", -1);
999 assert(isl_map_is_equal(map, map2));
1000 isl_map_free(map2);
1001 isl_map_free(map);
1003 /* COCOA Theorem 1 counter example */
1004 map = isl_map_read_from_str(ctx,
1005 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1006 "i2 = 1 and j2 = j or "
1007 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
1008 map = isl_map_transitive_closure(map, &exact);
1009 assert(exact);
1010 isl_map_free(map);
1012 map = isl_map_read_from_str(ctx,
1013 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1014 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1015 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1016 "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
1017 map = isl_map_transitive_closure(map, &exact);
1018 assert(exact);
1019 isl_map_free(map);
1021 /* Kelly et al 1996, fig 12 */
1022 map = isl_map_read_from_str(ctx,
1023 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1024 "1 <= i,j,j+1 <= n or "
1025 "j = n and j2 = 1 and i2 = i + 1 and "
1026 "1 <= i,i+1 <= n }", -1);
1027 map = isl_map_transitive_closure(map, &exact);
1028 assert(exact);
1029 map2 = isl_map_read_from_str(ctx,
1030 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1031 "1 <= i <= n and i = i2 or "
1032 "1 <= i < i2 <= n and 1 <= j <= n and "
1033 "1 <= j2 <= n }", -1);
1034 assert(isl_map_is_equal(map, map2));
1035 isl_map_free(map2);
1036 isl_map_free(map);
1038 /* Omega's closure4 */
1039 map = isl_map_read_from_str(ctx,
1040 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1041 "1 <= x,y <= 10 or "
1042 "x2 = x + 1 and y2 = y and "
1043 "1 <= x <= 20 && 5 <= y <= 15 }", -1);
1044 map = isl_map_transitive_closure(map, &exact);
1045 assert(exact);
1046 isl_map_free(map);
1048 map = isl_map_read_from_str(ctx,
1049 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
1050 map = isl_map_transitive_closure(map, &exact);
1051 assert(!exact);
1052 map2 = isl_map_read_from_str(ctx,
1053 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
1054 assert(isl_map_is_equal(map, map2));
1055 isl_map_free(map);
1056 isl_map_free(map2);
1058 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1059 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1060 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1061 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1062 map = isl_map_read_from_str(ctx, str, -1);
1063 map = isl_map_transitive_closure(map, &exact);
1064 assert(exact);
1065 map2 = isl_map_read_from_str(ctx, str, -1);
1066 assert(isl_map_is_equal(map, map2));
1067 isl_map_free(map);
1068 isl_map_free(map2);
1070 str = "{[0] -> [1]; [2] -> [3]}";
1071 map = isl_map_read_from_str(ctx, str, -1);
1072 map = isl_map_transitive_closure(map, &exact);
1073 assert(exact);
1074 map2 = isl_map_read_from_str(ctx, str, -1);
1075 assert(isl_map_is_equal(map, map2));
1076 isl_map_free(map);
1077 isl_map_free(map2);
1080 void test_lex(struct isl_ctx *ctx)
1082 isl_dim *dim;
1083 isl_map *map;
1085 dim = isl_dim_alloc(ctx, 0, 0, 0);
1086 map = isl_map_lex_le(dim);
1087 assert(!isl_map_is_empty(map));
1088 isl_map_free(map);
1091 void test_lexmin(struct isl_ctx *ctx)
1093 const char *str;
1094 isl_map *map;
1095 isl_set *set;
1096 isl_set *set2;
1098 str = "[p0, p1] -> { [] -> [] : "
1099 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1100 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1101 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1102 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1103 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1104 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1105 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1106 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1107 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1108 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1109 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1110 map = isl_map_read_from_str(ctx, str, -1);
1111 map = isl_map_lexmin(map);
1112 isl_map_free(map);
1114 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1115 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1116 set = isl_set_read_from_str(ctx, str, -1);
1117 set = isl_set_lexmax(set);
1118 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1119 set2 = isl_set_read_from_str(ctx, str, -1);
1120 set = isl_set_intersect(set, set2);
1121 assert(!isl_set_is_empty(set));
1122 isl_set_free(set);
1125 struct must_may {
1126 isl_map *must;
1127 isl_map *may;
1130 static int collect_must_may(__isl_take isl_map *dep, int must,
1131 void *dep_user, void *user)
1133 struct must_may *mm = (struct must_may *)user;
1135 if (must)
1136 mm->must = isl_map_union(mm->must, dep);
1137 else
1138 mm->may = isl_map_union(mm->may, dep);
1140 return 0;
1143 static int common_space(void *first, void *second)
1145 int depth = *(int *)first;
1146 return 2 * depth;
1149 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1151 isl_map *map2;
1152 int equal;
1154 if (!map)
1155 return -1;
1157 map2 = isl_map_read_from_str(map->ctx, str, -1);
1158 equal = isl_map_is_equal(map, map2);
1159 isl_map_free(map2);
1161 return equal;
1164 void test_dep(struct isl_ctx *ctx)
1166 const char *str;
1167 isl_dim *dim;
1168 isl_map *map;
1169 isl_access_info *ai;
1170 isl_flow *flow;
1171 int depth;
1172 struct must_may mm;
1174 depth = 3;
1176 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1177 map = isl_map_read_from_str(ctx, str, -1);
1178 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1180 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1181 map = isl_map_read_from_str(ctx, str, -1);
1182 ai = isl_access_info_add_source(ai, map, 1, &depth);
1184 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1185 map = isl_map_read_from_str(ctx, str, -1);
1186 ai = isl_access_info_add_source(ai, map, 1, &depth);
1188 flow = isl_access_info_compute_flow(ai);
1189 dim = isl_dim_alloc(ctx, 0, 3, 3);
1190 mm.must = isl_map_empty(isl_dim_copy(dim));
1191 mm.may = isl_map_empty(dim);
1193 isl_flow_foreach(flow, collect_must_may, &mm);
1195 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1196 " [1,10,0] -> [2,5,0] }";
1197 assert(map_is_equal(mm.must, str));
1198 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1199 assert(map_is_equal(mm.may, str));
1201 isl_map_free(mm.must);
1202 isl_map_free(mm.may);
1203 isl_flow_free(flow);
1206 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1207 map = isl_map_read_from_str(ctx, str, -1);
1208 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1210 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1211 map = isl_map_read_from_str(ctx, str, -1);
1212 ai = isl_access_info_add_source(ai, map, 1, &depth);
1214 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1215 map = isl_map_read_from_str(ctx, str, -1);
1216 ai = isl_access_info_add_source(ai, map, 0, &depth);
1218 flow = isl_access_info_compute_flow(ai);
1219 dim = isl_dim_alloc(ctx, 0, 3, 3);
1220 mm.must = isl_map_empty(isl_dim_copy(dim));
1221 mm.may = isl_map_empty(dim);
1223 isl_flow_foreach(flow, collect_must_may, &mm);
1225 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1226 assert(map_is_equal(mm.must, str));
1227 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1228 assert(map_is_equal(mm.may, str));
1230 isl_map_free(mm.must);
1231 isl_map_free(mm.may);
1232 isl_flow_free(flow);
1235 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1236 map = isl_map_read_from_str(ctx, str, -1);
1237 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1239 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1240 map = isl_map_read_from_str(ctx, str, -1);
1241 ai = isl_access_info_add_source(ai, map, 0, &depth);
1243 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1244 map = isl_map_read_from_str(ctx, str, -1);
1245 ai = isl_access_info_add_source(ai, map, 0, &depth);
1247 flow = isl_access_info_compute_flow(ai);
1248 dim = isl_dim_alloc(ctx, 0, 3, 3);
1249 mm.must = isl_map_empty(isl_dim_copy(dim));
1250 mm.may = isl_map_empty(dim);
1252 isl_flow_foreach(flow, collect_must_may, &mm);
1254 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1255 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1256 assert(map_is_equal(mm.may, str));
1257 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1258 assert(map_is_equal(mm.must, str));
1260 isl_map_free(mm.must);
1261 isl_map_free(mm.may);
1262 isl_flow_free(flow);
1265 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1266 map = isl_map_read_from_str(ctx, str, -1);
1267 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1269 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1270 map = isl_map_read_from_str(ctx, str, -1);
1271 ai = isl_access_info_add_source(ai, map, 0, &depth);
1273 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1274 map = isl_map_read_from_str(ctx, str, -1);
1275 ai = isl_access_info_add_source(ai, map, 0, &depth);
1277 flow = isl_access_info_compute_flow(ai);
1278 dim = isl_dim_alloc(ctx, 0, 3, 3);
1279 mm.must = isl_map_empty(isl_dim_copy(dim));
1280 mm.may = isl_map_empty(dim);
1282 isl_flow_foreach(flow, collect_must_may, &mm);
1284 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1285 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1286 assert(map_is_equal(mm.may, str));
1287 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1288 assert(map_is_equal(mm.must, str));
1290 isl_map_free(mm.must);
1291 isl_map_free(mm.may);
1292 isl_flow_free(flow);
1295 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1296 map = isl_map_read_from_str(ctx, str, -1);
1297 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1299 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1300 map = isl_map_read_from_str(ctx, str, -1);
1301 ai = isl_access_info_add_source(ai, map, 0, &depth);
1303 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1304 map = isl_map_read_from_str(ctx, str, -1);
1305 ai = isl_access_info_add_source(ai, map, 0, &depth);
1307 flow = isl_access_info_compute_flow(ai);
1308 dim = isl_dim_alloc(ctx, 0, 3, 3);
1309 mm.must = isl_map_empty(isl_dim_copy(dim));
1310 mm.may = isl_map_empty(dim);
1312 isl_flow_foreach(flow, collect_must_may, &mm);
1314 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1315 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1316 assert(map_is_equal(mm.may, str));
1317 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1318 assert(map_is_equal(mm.must, str));
1320 isl_map_free(mm.must);
1321 isl_map_free(mm.may);
1322 isl_flow_free(flow);
1325 depth = 5;
1327 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1328 map = isl_map_read_from_str(ctx, str, -1);
1329 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1331 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1332 map = isl_map_read_from_str(ctx, str, -1);
1333 ai = isl_access_info_add_source(ai, map, 1, &depth);
1335 flow = isl_access_info_compute_flow(ai);
1336 dim = isl_dim_alloc(ctx, 0, 5, 5);
1337 mm.must = isl_map_empty(isl_dim_copy(dim));
1338 mm.may = isl_map_empty(dim);
1340 isl_flow_foreach(flow, collect_must_may, &mm);
1342 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1343 assert(map_is_equal(mm.must, str));
1344 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1345 assert(map_is_equal(mm.may, str));
1347 isl_map_free(mm.must);
1348 isl_map_free(mm.may);
1349 isl_flow_free(flow);
1352 void test_sv(struct isl_ctx *ctx)
1354 const char *str;
1355 isl_map *map;
1357 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1358 map = isl_map_read_from_str(ctx, str, -1);
1359 assert(isl_map_is_single_valued(map));
1360 isl_map_free(map);
1362 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1363 map = isl_map_read_from_str(ctx, str, -1);
1364 assert(!isl_map_is_single_valued(map));
1365 isl_map_free(map);
1368 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1370 isl_map *map;
1372 map = isl_map_read_from_str(ctx, str, -1);
1373 if (bijective)
1374 assert(isl_map_is_bijective(map));
1375 else
1376 assert(!isl_map_is_bijective(map));
1377 isl_map_free(map);
1380 void test_bijective(struct isl_ctx *ctx)
1382 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1383 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1384 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1385 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1386 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1387 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1388 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1389 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1390 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1391 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1392 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1393 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1394 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1397 void test_pwqp(struct isl_ctx *ctx)
1399 const char *str;
1400 isl_pw_qpolynomial *pwqp1, *pwqp2;
1402 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1403 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1405 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1406 isl_dim_set, 1, 1);
1408 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1409 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1411 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1413 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1415 isl_pw_qpolynomial_free(pwqp1);
1418 void test_split_periods(isl_ctx *ctx)
1420 const char *str;
1421 isl_pw_qpolynomial *pwqp;
1423 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1424 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1425 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1426 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1428 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1429 assert(pwqp);
1431 isl_pw_qpolynomial_free(pwqp);
1434 int main()
1436 struct isl_ctx *ctx;
1438 srcdir = getenv("srcdir");
1439 assert(srcdir);
1441 ctx = isl_ctx_alloc();
1442 test_split_periods(ctx);
1443 test_parse(ctx);
1444 test_pwqp(ctx);
1445 test_lex(ctx);
1446 test_sv(ctx);
1447 test_bijective(ctx);
1448 test_dep(ctx);
1449 test_read(ctx);
1450 test_bounded(ctx);
1451 test_construction(ctx);
1452 test_dim(ctx);
1453 test_div(ctx);
1454 test_application(ctx);
1455 test_affine_hull(ctx);
1456 test_convex_hull(ctx);
1457 test_gist(ctx);
1458 test_coalesce(ctx);
1459 test_closure(ctx);
1460 test_lexmin(ctx);
1461 isl_ctx_free(ctx);
1462 return 0;