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