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