isl_qpolynomial_substitute_equalities: ignore constant when removing factors
[isl.git] / isl_test.c
blobc2f0285bdc4a01fc53359ec19e01a6b33ddb3368
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(struct isl_ctx *ctx)
33 isl_map *map, *map2;
34 const char *str;
36 str = "{ [i] -> [-i] }";
37 map = isl_map_read_from_str(ctx, str, -1);
38 assert(map);
39 isl_map_free(map);
41 str = "{ A[i] -> L[([i/3])] }";
42 map = isl_map_read_from_str(ctx, str, -1);
43 assert(map);
44 isl_map_free(map);
46 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
48 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
49 map = isl_map_read_from_str(ctx, str, -1);
50 str = "{ [new, old] -> [o0, o1] : "
51 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
52 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
53 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
54 map2 = isl_map_read_from_str(ctx, str, -1);
55 assert(isl_map_is_equal(map, map2));
56 isl_map_free(map);
57 isl_map_free(map2);
60 void test_read(struct isl_ctx *ctx)
62 char filename[PATH_MAX];
63 FILE *input;
64 int n;
65 struct isl_basic_set *bset1, *bset2;
66 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
68 n = snprintf(filename, sizeof(filename),
69 "%s/test_inputs/set.omega", srcdir);
70 assert(n < sizeof(filename));
71 input = fopen(filename, "r");
72 assert(input);
74 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
75 bset2 = isl_basic_set_read_from_str(ctx, str, 0);
77 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
79 isl_basic_set_free(bset1);
80 isl_basic_set_free(bset2);
82 fclose(input);
85 void test_bounded(struct isl_ctx *ctx)
87 isl_set *set;
88 int bounded;
90 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }", -1);
91 bounded = isl_set_is_bounded(set);
92 assert(bounded);
93 isl_set_free(set);
95 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }", -1);
96 bounded = isl_set_is_bounded(set);
97 assert(!bounded);
98 isl_set_free(set);
100 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }", -1);
101 bounded = isl_set_is_bounded(set);
102 assert(!bounded);
103 isl_set_free(set);
106 /* Construct the basic set { [i] : 5 <= i <= N } */
107 void test_construction(struct isl_ctx *ctx)
109 isl_int v;
110 struct isl_dim *dim;
111 struct isl_basic_set *bset;
112 struct isl_constraint *c;
114 isl_int_init(v);
116 dim = isl_dim_set_alloc(ctx, 1, 1);
117 bset = isl_basic_set_universe(dim);
119 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
120 isl_int_set_si(v, -1);
121 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
122 isl_int_set_si(v, 1);
123 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
124 bset = isl_basic_set_add_constraint(bset, c);
126 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
127 isl_int_set_si(v, 1);
128 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
129 isl_int_set_si(v, -5);
130 isl_constraint_set_constant(c, v);
131 bset = isl_basic_set_add_constraint(bset, c);
133 isl_basic_set_free(bset);
135 isl_int_clear(v);
138 void test_dim(struct isl_ctx *ctx)
140 const char *str;
141 isl_map *map1, *map2;
143 map1 = isl_map_read_from_str(ctx,
144 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
145 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
146 map2 = isl_map_read_from_str(ctx,
147 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
148 assert(isl_map_is_equal(map1, map2));
149 isl_map_free(map2);
151 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
152 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }", -1);
153 assert(isl_map_is_equal(map1, map2));
155 isl_map_free(map1);
156 isl_map_free(map2);
158 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
159 map1 = isl_map_read_from_str(ctx, str, -1);
160 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
161 map2 = isl_map_read_from_str(ctx, str, -1);
162 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
163 assert(isl_map_is_equal(map1, map2));
165 isl_map_free(map1);
166 isl_map_free(map2);
169 void test_div(struct isl_ctx *ctx)
171 isl_int v;
172 int pos;
173 struct isl_dim *dim;
174 struct isl_div *div;
175 struct isl_basic_set *bset;
176 struct isl_constraint *c;
178 isl_int_init(v);
180 /* test 1 */
181 dim = isl_dim_set_alloc(ctx, 0, 1);
182 bset = isl_basic_set_universe(dim);
184 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
185 isl_int_set_si(v, -1);
186 isl_constraint_set_constant(c, v);
187 isl_int_set_si(v, 1);
188 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
189 div = isl_div_alloc(isl_basic_set_get_dim(bset));
190 c = isl_constraint_add_div(c, div, &pos);
191 isl_int_set_si(v, 3);
192 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
193 bset = isl_basic_set_add_constraint(bset, c);
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 assert(bset && bset->n_div == 1);
207 isl_basic_set_free(bset);
209 /* test 2 */
210 dim = isl_dim_set_alloc(ctx, 0, 1);
211 bset = isl_basic_set_universe(dim);
213 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
214 isl_int_set_si(v, 1);
215 isl_constraint_set_constant(c, v);
216 isl_int_set_si(v, -1);
217 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
218 div = isl_div_alloc(isl_basic_set_get_dim(bset));
219 c = isl_constraint_add_div(c, div, &pos);
220 isl_int_set_si(v, 3);
221 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
222 bset = isl_basic_set_add_constraint(bset, c);
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 assert(bset && bset->n_div == 1);
236 isl_basic_set_free(bset);
238 /* test 3 */
239 dim = isl_dim_set_alloc(ctx, 0, 1);
240 bset = isl_basic_set_universe(dim);
242 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
243 isl_int_set_si(v, 1);
244 isl_constraint_set_constant(c, v);
245 isl_int_set_si(v, -1);
246 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
247 div = isl_div_alloc(isl_basic_set_get_dim(bset));
248 c = isl_constraint_add_div(c, div, &pos);
249 isl_int_set_si(v, 3);
250 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
251 bset = isl_basic_set_add_constraint(bset, c);
253 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
254 isl_int_set_si(v, -3);
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, 4);
261 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
262 bset = isl_basic_set_add_constraint(bset, c);
264 assert(bset && bset->n_div == 1);
265 isl_basic_set_free(bset);
267 /* test 4 */
268 dim = isl_dim_set_alloc(ctx, 0, 1);
269 bset = isl_basic_set_universe(dim);
271 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
272 isl_int_set_si(v, 2);
273 isl_constraint_set_constant(c, v);
274 isl_int_set_si(v, -1);
275 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
276 div = isl_div_alloc(isl_basic_set_get_dim(bset));
277 c = isl_constraint_add_div(c, div, &pos);
278 isl_int_set_si(v, 3);
279 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
280 bset = isl_basic_set_add_constraint(bset, c);
282 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
283 isl_int_set_si(v, -1);
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, 6);
290 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
291 bset = isl_basic_set_add_constraint(bset, c);
293 assert(isl_basic_set_is_empty(bset));
294 isl_basic_set_free(bset);
296 /* test 5 */
297 dim = isl_dim_set_alloc(ctx, 0, 2);
298 bset = isl_basic_set_universe(dim);
300 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
301 isl_int_set_si(v, -1);
302 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
303 div = isl_div_alloc(isl_basic_set_get_dim(bset));
304 c = isl_constraint_add_div(c, div, &pos);
305 isl_int_set_si(v, 3);
306 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
307 bset = isl_basic_set_add_constraint(bset, c);
309 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
310 isl_int_set_si(v, 1);
311 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
312 isl_int_set_si(v, -3);
313 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
314 bset = isl_basic_set_add_constraint(bset, c);
316 assert(bset && bset->n_div == 0);
317 isl_basic_set_free(bset);
319 /* test 6 */
320 dim = isl_dim_set_alloc(ctx, 0, 2);
321 bset = isl_basic_set_universe(dim);
323 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
324 isl_int_set_si(v, -1);
325 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
326 div = isl_div_alloc(isl_basic_set_get_dim(bset));
327 c = isl_constraint_add_div(c, div, &pos);
328 isl_int_set_si(v, 6);
329 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
330 bset = isl_basic_set_add_constraint(bset, c);
332 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
333 isl_int_set_si(v, 1);
334 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
335 isl_int_set_si(v, -3);
336 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
337 bset = isl_basic_set_add_constraint(bset, c);
339 assert(bset && bset->n_div == 1);
340 isl_basic_set_free(bset);
342 /* test 7 */
343 /* This test is a bit tricky. We set up an equality
344 * a + 3b + 3c = 6 e0
345 * Normalization of divs creates _two_ divs
346 * a = 3 e0
347 * c - b - e0 = 2 e1
348 * Afterwards e0 is removed again because it has coefficient -1
349 * and we end up with the original equality and div again.
350 * Perhaps we can avoid the introduction of this temporary div.
352 dim = isl_dim_set_alloc(ctx, 0, 3);
353 bset = isl_basic_set_universe(dim);
355 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
356 isl_int_set_si(v, -1);
357 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
358 isl_int_set_si(v, -3);
359 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
360 isl_int_set_si(v, -3);
361 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
362 div = isl_div_alloc(isl_basic_set_get_dim(bset));
363 c = isl_constraint_add_div(c, div, &pos);
364 isl_int_set_si(v, 6);
365 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
366 bset = isl_basic_set_add_constraint(bset, c);
368 /* Test disabled for now */
370 assert(bset && bset->n_div == 1);
372 isl_basic_set_free(bset);
374 /* test 8 */
375 dim = isl_dim_set_alloc(ctx, 0, 4);
376 bset = isl_basic_set_universe(dim);
378 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
379 isl_int_set_si(v, -1);
380 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
381 isl_int_set_si(v, -3);
382 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
383 isl_int_set_si(v, -3);
384 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
385 div = isl_div_alloc(isl_basic_set_get_dim(bset));
386 c = isl_constraint_add_div(c, div, &pos);
387 isl_int_set_si(v, 6);
388 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
389 bset = isl_basic_set_add_constraint(bset, c);
391 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
392 isl_int_set_si(v, -1);
393 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
394 isl_int_set_si(v, 1);
395 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
396 isl_int_set_si(v, 1);
397 isl_constraint_set_constant(c, v);
398 bset = isl_basic_set_add_constraint(bset, c);
400 /* Test disabled for now */
402 assert(bset && bset->n_div == 1);
404 isl_basic_set_free(bset);
406 /* test 9 */
407 dim = isl_dim_set_alloc(ctx, 0, 2);
408 bset = isl_basic_set_universe(dim);
410 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
411 isl_int_set_si(v, 1);
412 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
413 isl_int_set_si(v, -1);
414 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
415 div = isl_div_alloc(isl_basic_set_get_dim(bset));
416 c = isl_constraint_add_div(c, div, &pos);
417 isl_int_set_si(v, -2);
418 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
419 bset = isl_basic_set_add_constraint(bset, c);
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 div = isl_div_alloc(isl_basic_set_get_dim(bset));
425 c = isl_constraint_add_div(c, div, &pos);
426 isl_int_set_si(v, 3);
427 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
428 isl_int_set_si(v, 2);
429 isl_constraint_set_constant(c, v);
430 bset = isl_basic_set_add_constraint(bset, c);
432 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
434 assert(!isl_basic_set_is_empty(bset));
436 isl_basic_set_free(bset);
438 /* test 10 */
439 dim = isl_dim_set_alloc(ctx, 0, 2);
440 bset = isl_basic_set_universe(dim);
442 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
443 isl_int_set_si(v, 1);
444 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
445 div = isl_div_alloc(isl_basic_set_get_dim(bset));
446 c = isl_constraint_add_div(c, div, &pos);
447 isl_int_set_si(v, -2);
448 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
449 bset = isl_basic_set_add_constraint(bset, c);
451 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
453 isl_basic_set_free(bset);
455 isl_int_clear(v);
458 void test_application_case(struct isl_ctx *ctx, const char *name)
460 char filename[PATH_MAX];
461 FILE *input;
462 int n;
463 struct isl_basic_set *bset1, *bset2;
464 struct isl_basic_map *bmap;
466 n = snprintf(filename, sizeof(filename),
467 "%s/test_inputs/%s.omega", srcdir, name);
468 assert(n < sizeof(filename));
469 input = fopen(filename, "r");
470 assert(input);
472 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
473 bmap = isl_basic_map_read_from_file(ctx, input, 0);
475 bset1 = isl_basic_set_apply(bset1, bmap);
477 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
479 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
481 isl_basic_set_free(bset1);
482 isl_basic_set_free(bset2);
484 fclose(input);
487 void test_application(struct isl_ctx *ctx)
489 test_application_case(ctx, "application");
490 test_application_case(ctx, "application2");
493 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
495 char filename[PATH_MAX];
496 FILE *input;
497 int n;
498 struct isl_basic_set *bset1, *bset2;
500 n = snprintf(filename, sizeof(filename),
501 "%s/test_inputs/%s.polylib", srcdir, name);
502 assert(n < sizeof(filename));
503 input = fopen(filename, "r");
504 assert(input);
506 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
507 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
509 bset1 = isl_basic_set_affine_hull(bset1);
511 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
513 isl_basic_set_free(bset1);
514 isl_basic_set_free(bset2);
516 fclose(input);
519 void test_affine_hull(struct isl_ctx *ctx)
521 test_affine_hull_case(ctx, "affine2");
522 test_affine_hull_case(ctx, "affine");
523 test_affine_hull_case(ctx, "affine3");
526 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
528 char filename[PATH_MAX];
529 FILE *input;
530 int n;
531 struct isl_basic_set *bset1, *bset2;
532 struct isl_set *set;
534 n = snprintf(filename, sizeof(filename),
535 "%s/test_inputs/%s.polylib", srcdir, name);
536 assert(n < sizeof(filename));
537 input = fopen(filename, "r");
538 assert(input);
540 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
541 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
543 set = isl_basic_set_union(bset1, bset2);
544 bset1 = isl_set_convex_hull(set);
546 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
548 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
550 isl_basic_set_free(bset1);
551 isl_basic_set_free(bset2);
553 fclose(input);
556 void test_convex_hull(struct isl_ctx *ctx)
558 const char *str1, *str2;
559 isl_set *set1, *set2;
561 test_convex_hull_case(ctx, "convex0");
562 test_convex_hull_case(ctx, "convex1");
563 test_convex_hull_case(ctx, "convex2");
564 test_convex_hull_case(ctx, "convex3");
565 test_convex_hull_case(ctx, "convex4");
566 test_convex_hull_case(ctx, "convex5");
567 test_convex_hull_case(ctx, "convex6");
568 test_convex_hull_case(ctx, "convex7");
569 test_convex_hull_case(ctx, "convex8");
570 test_convex_hull_case(ctx, "convex9");
571 test_convex_hull_case(ctx, "convex10");
572 test_convex_hull_case(ctx, "convex11");
573 test_convex_hull_case(ctx, "convex12");
574 test_convex_hull_case(ctx, "convex13");
575 test_convex_hull_case(ctx, "convex14");
576 test_convex_hull_case(ctx, "convex15");
578 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
579 "(i0 = 1 and i1 = 0 and i2 = 1) or "
580 "(i0 = 0 and i1 = 0 and i2 = 0) }";
581 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
582 set1 = isl_set_read_from_str(ctx, str1, -1);
583 set2 = isl_set_read_from_str(ctx, str2, -1);
584 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
585 assert(isl_set_is_equal(set1, set2));
586 isl_set_free(set1);
587 isl_set_free(set2);
590 void test_gist_case(struct isl_ctx *ctx, const char *name)
592 char filename[PATH_MAX];
593 FILE *input;
594 int n;
595 struct isl_basic_set *bset1, *bset2;
597 n = snprintf(filename, sizeof(filename),
598 "%s/test_inputs/%s.polylib", srcdir, name);
599 assert(n < sizeof(filename));
600 input = fopen(filename, "r");
601 assert(input);
603 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
604 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
606 bset1 = isl_basic_set_gist(bset1, bset2);
608 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
610 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
612 isl_basic_set_free(bset1);
613 isl_basic_set_free(bset2);
615 fclose(input);
618 void test_gist(struct isl_ctx *ctx)
620 test_gist_case(ctx, "gist1");
623 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
625 isl_set *set, *set2;
627 set = isl_set_read_from_str(ctx, str, -1);
628 set = isl_set_coalesce(set);
629 set2 = isl_set_read_from_str(ctx, str, -1);
630 assert(isl_set_is_equal(set, set2));
631 if (check_one)
632 assert(set && set->n == 1);
633 isl_set_free(set);
634 isl_set_free(set2);
637 void test_coalesce(struct isl_ctx *ctx)
639 const char *str;
640 struct isl_set *set, *set2;
641 struct isl_map *map, *map2;
643 set = isl_set_read_from_str(ctx,
644 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
645 "y >= x & x >= 2 & 5 >= y }", -1);
646 set = isl_set_coalesce(set);
647 assert(set && set->n == 1);
648 isl_set_free(set);
650 set = isl_set_read_from_str(ctx,
651 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
652 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
653 set = isl_set_coalesce(set);
654 assert(set && set->n == 1);
655 isl_set_free(set);
657 set = isl_set_read_from_str(ctx,
658 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
659 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
660 set = isl_set_coalesce(set);
661 assert(set && set->n == 2);
662 isl_set_free(set);
664 set = isl_set_read_from_str(ctx,
665 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
666 "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
667 set = isl_set_coalesce(set);
668 assert(set && set->n == 1);
669 isl_set_free(set);
671 set = isl_set_read_from_str(ctx,
672 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
673 "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
674 set = isl_set_coalesce(set);
675 assert(set && set->n == 2);
676 isl_set_free(set);
678 set = isl_set_read_from_str(ctx,
679 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
680 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
681 set = isl_set_coalesce(set);
682 assert(set && set->n == 2);
683 isl_set_free(set);
685 set = isl_set_read_from_str(ctx,
686 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
687 "y >= 0 & x = 6 & y <= 6}", -1);
688 set = isl_set_coalesce(set);
689 assert(set && set->n == 1);
690 isl_set_free(set);
692 set = isl_set_read_from_str(ctx,
693 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
694 "y >= 0 & x = 7 & y <= 6}", -1);
695 set = isl_set_coalesce(set);
696 assert(set && set->n == 2);
697 isl_set_free(set);
699 set = isl_set_read_from_str(ctx,
700 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
701 "y >= 0 & x = 6 & y <= 5}", -1);
702 set = isl_set_coalesce(set);
703 assert(set && set->n == 1);
704 set2 = isl_set_read_from_str(ctx,
705 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
706 "y >= 0 & x = 6 & y <= 5}", -1);
707 assert(isl_set_is_equal(set, set2));
708 isl_set_free(set);
709 isl_set_free(set2);
711 set = isl_set_read_from_str(ctx,
712 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
713 "y >= 0 & x = 6 & y <= 7}", -1);
714 set = isl_set_coalesce(set);
715 assert(set && set->n == 2);
716 isl_set_free(set);
718 set = isl_set_read_from_str(ctx,
719 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
720 set = isl_set_coalesce(set);
721 assert(set && set->n == 1);
722 set2 = isl_set_read_from_str(ctx,
723 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
724 assert(isl_set_is_equal(set, set2));
725 isl_set_free(set);
726 isl_set_free(set2);
728 set = isl_set_read_from_str(ctx,
729 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
730 set = isl_set_coalesce(set);
731 set2 = isl_set_read_from_str(ctx,
732 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
733 assert(isl_set_is_equal(set, set2));
734 isl_set_free(set);
735 isl_set_free(set2);
737 set = isl_set_read_from_str(ctx,
738 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
739 "2 <= i and i <= n }", -1);
740 set = isl_set_coalesce(set);
741 assert(set && set->n == 1);
742 set2 = isl_set_read_from_str(ctx,
743 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
744 "2 <= i and i <= n }", -1);
745 assert(isl_set_is_equal(set, set2));
746 isl_set_free(set);
747 isl_set_free(set2);
749 map = isl_map_read_from_str(ctx,
750 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
751 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
752 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
753 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
754 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
755 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
756 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
757 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
758 "[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 2e0 >= 3 + n and e0 <= -4 + n and "
762 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
763 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
764 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
765 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
766 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
767 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
768 map = isl_map_coalesce(map);
769 map2 = isl_map_read_from_str(ctx,
770 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
771 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
772 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
773 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
774 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
775 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
776 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
777 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
778 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
779 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
780 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
781 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
782 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
783 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
784 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
785 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
786 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
787 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
788 assert(isl_map_is_equal(map, map2));
789 isl_map_free(map);
790 isl_map_free(map2);
792 str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
793 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
794 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
795 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
796 map = isl_map_read_from_str(ctx, str, -1);
797 map = isl_map_coalesce(map);
798 map2 = isl_map_read_from_str(ctx, str, -1);
799 assert(isl_map_is_equal(map, map2));
800 isl_map_free(map);
801 isl_map_free(map2);
803 str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
804 "[o0, o1, o2, o3, o4, o5, o6] : "
805 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
806 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
807 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
808 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
809 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
810 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
811 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
812 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
813 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
814 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
815 "o6 >= i3 + i6 - o3 and M >= 0 and "
816 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
817 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
818 map = isl_map_read_from_str(ctx, str, -1);
819 map = isl_map_coalesce(map);
820 map2 = isl_map_read_from_str(ctx, str, -1);
821 assert(isl_map_is_equal(map, map2));
822 isl_map_free(map);
823 isl_map_free(map2);
825 str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
826 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
827 "(o0 = 0 and M >= 2 and N >= 3) or "
828 "(M = 0 and o0 = 0 and N >= 3) }";
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 = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
837 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
838 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
839 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
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 test_coalesce_set(ctx,
848 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
849 "(i1 = M and M >= 1) }", 0);
850 test_coalesce_set(ctx,
851 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
852 test_coalesce_set(ctx,
853 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
854 "(y = 3 and x = 1) }", 1);
855 test_coalesce_set(ctx,
856 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
857 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
858 "i1 <= M and i3 <= M and i4 <= M) or "
859 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
860 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
861 "i4 <= -1 + M) }", 1);
862 test_coalesce_set(ctx,
863 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
864 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
865 test_coalesce_set(ctx,
866 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
867 "-x - y + 1 >= 0 and -3 <= z <= 3;"
868 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
869 "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
870 test_coalesce_set(ctx,
871 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
872 test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
873 test_coalesce_set(ctx,
874 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
875 test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
878 void test_closure(struct isl_ctx *ctx)
880 const char *str;
881 isl_set *dom;
882 isl_map *up, *right;
883 isl_map *map, *map2;
884 int exact;
886 /* COCOA example 1 */
887 map = isl_map_read_from_str(ctx,
888 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
889 "1 <= i and i < n and 1 <= j and j < n or "
890 "i2 = i + 1 and j2 = j - 1 and "
891 "1 <= i and i < n and 2 <= j and j <= n }", -1);
892 map = isl_map_power(map, 1, &exact);
893 assert(exact);
894 isl_map_free(map);
896 /* COCOA example 1 */
897 map = isl_map_read_from_str(ctx,
898 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
899 "1 <= i and i < n and 1 <= j and j < n or "
900 "i2 = i + 1 and j2 = j - 1 and "
901 "1 <= i and i < n and 2 <= j and j <= n }", -1);
902 map = isl_map_transitive_closure(map, &exact);
903 assert(exact);
904 map2 = isl_map_read_from_str(ctx,
905 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
906 "1 <= i and i < n and 1 <= j and j <= n and "
907 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
908 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
909 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
910 assert(isl_map_is_equal(map, map2));
911 isl_map_free(map2);
912 isl_map_free(map);
914 map = isl_map_read_from_str(ctx,
915 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
916 " 0 <= y and y <= n }", -1);
917 map = isl_map_transitive_closure(map, &exact);
918 map2 = isl_map_read_from_str(ctx,
919 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
920 " 0 <= y and y <= n }", -1);
921 assert(isl_map_is_equal(map, map2));
922 isl_map_free(map2);
923 isl_map_free(map);
925 /* COCOA example 2 */
926 map = isl_map_read_from_str(ctx,
927 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
928 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
929 "i2 = i + 2 and j2 = j - 2 and "
930 "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
931 map = isl_map_transitive_closure(map, &exact);
932 assert(exact);
933 map2 = isl_map_read_from_str(ctx,
934 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
935 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
936 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
937 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
938 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
939 assert(isl_map_is_equal(map, map2));
940 isl_map_free(map);
941 isl_map_free(map2);
943 /* COCOA Fig.2 left */
944 map = isl_map_read_from_str(ctx,
945 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
946 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
947 "j <= n or "
948 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
949 "j <= 2 i - 3 and j <= n - 2 or "
950 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
951 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
952 map = isl_map_transitive_closure(map, &exact);
953 assert(exact);
954 isl_map_free(map);
956 /* COCOA Fig.2 right */
957 map = isl_map_read_from_str(ctx,
958 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
959 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
960 "j <= n or "
961 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
962 "j <= 2 i - 4 and j <= n - 3 or "
963 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
964 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
965 map = isl_map_power(map, 1, &exact);
966 assert(exact);
967 isl_map_free(map);
969 /* COCOA Fig.2 right */
970 map = isl_map_read_from_str(ctx,
971 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
972 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
973 "j <= n or "
974 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
975 "j <= 2 i - 4 and j <= n - 3 or "
976 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
977 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
978 map = isl_map_transitive_closure(map, &exact);
979 assert(exact);
980 map2 = isl_map_read_from_str(ctx,
981 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
982 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
983 "j <= n and 3 + i + 2 j <= 3 n and "
984 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
985 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
986 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
987 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
988 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
989 assert(isl_map_is_equal(map, map2));
990 isl_map_free(map2);
991 isl_map_free(map);
993 /* COCOA Fig.1 right */
994 dom = isl_set_read_from_str(ctx,
995 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
996 "2 x - 3 y + 3 >= 0 }", -1);
997 right = isl_map_read_from_str(ctx,
998 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
999 up = isl_map_read_from_str(ctx,
1000 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
1001 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1002 right = isl_map_intersect_range(right, isl_set_copy(dom));
1003 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1004 up = isl_map_intersect_range(up, dom);
1005 map = isl_map_union(up, right);
1006 map = isl_map_transitive_closure(map, &exact);
1007 assert(exact);
1008 map2 = isl_map_read_from_str(ctx,
1009 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1010 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }", -1);
1011 assert(isl_map_is_equal(map, map2));
1012 isl_map_free(map2);
1013 isl_map_free(map);
1015 /* COCOA Theorem 1 counter example */
1016 map = isl_map_read_from_str(ctx,
1017 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1018 "i2 = 1 and j2 = j or "
1019 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
1020 map = isl_map_transitive_closure(map, &exact);
1021 assert(exact);
1022 isl_map_free(map);
1024 map = isl_map_read_from_str(ctx,
1025 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1026 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1027 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1028 "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
1029 map = isl_map_transitive_closure(map, &exact);
1030 assert(exact);
1031 isl_map_free(map);
1033 /* Kelly et al 1996, fig 12 */
1034 map = isl_map_read_from_str(ctx,
1035 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1036 "1 <= i,j,j+1 <= n or "
1037 "j = n and j2 = 1 and i2 = i + 1 and "
1038 "1 <= i,i+1 <= n }", -1);
1039 map = isl_map_transitive_closure(map, &exact);
1040 assert(exact);
1041 map2 = isl_map_read_from_str(ctx,
1042 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1043 "1 <= i <= n and i = i2 or "
1044 "1 <= i < i2 <= n and 1 <= j <= n and "
1045 "1 <= j2 <= n }", -1);
1046 assert(isl_map_is_equal(map, map2));
1047 isl_map_free(map2);
1048 isl_map_free(map);
1050 /* Omega's closure4 */
1051 map = isl_map_read_from_str(ctx,
1052 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1053 "1 <= x,y <= 10 or "
1054 "x2 = x + 1 and y2 = y and "
1055 "1 <= x <= 20 && 5 <= y <= 15 }", -1);
1056 map = isl_map_transitive_closure(map, &exact);
1057 assert(exact);
1058 isl_map_free(map);
1060 map = isl_map_read_from_str(ctx,
1061 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
1062 map = isl_map_transitive_closure(map, &exact);
1063 assert(!exact);
1064 map2 = isl_map_read_from_str(ctx,
1065 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
1066 assert(isl_map_is_equal(map, map2));
1067 isl_map_free(map);
1068 isl_map_free(map2);
1070 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1071 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1072 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1073 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1074 map = isl_map_read_from_str(ctx, str, -1);
1075 map = isl_map_transitive_closure(map, &exact);
1076 assert(exact);
1077 map2 = isl_map_read_from_str(ctx, str, -1);
1078 assert(isl_map_is_equal(map, map2));
1079 isl_map_free(map);
1080 isl_map_free(map2);
1082 str = "{[0] -> [1]; [2] -> [3]}";
1083 map = isl_map_read_from_str(ctx, str, -1);
1084 map = isl_map_transitive_closure(map, &exact);
1085 assert(exact);
1086 map2 = isl_map_read_from_str(ctx, str, -1);
1087 assert(isl_map_is_equal(map, map2));
1088 isl_map_free(map);
1089 isl_map_free(map2);
1092 void test_lex(struct isl_ctx *ctx)
1094 isl_dim *dim;
1095 isl_map *map;
1097 dim = isl_dim_alloc(ctx, 0, 0, 0);
1098 map = isl_map_lex_le(dim);
1099 assert(!isl_map_is_empty(map));
1100 isl_map_free(map);
1103 void test_lexmin(struct isl_ctx *ctx)
1105 const char *str;
1106 isl_map *map;
1107 isl_set *set;
1108 isl_set *set2;
1110 str = "[p0, p1] -> { [] -> [] : "
1111 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1112 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1113 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1114 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1115 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1116 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1117 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1118 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1119 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1120 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1121 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1122 map = isl_map_read_from_str(ctx, str, -1);
1123 map = isl_map_lexmin(map);
1124 isl_map_free(map);
1126 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1127 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1128 set = isl_set_read_from_str(ctx, str, -1);
1129 set = isl_set_lexmax(set);
1130 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1131 set2 = isl_set_read_from_str(ctx, str, -1);
1132 set = isl_set_intersect(set, set2);
1133 assert(!isl_set_is_empty(set));
1134 isl_set_free(set);
1137 struct must_may {
1138 isl_map *must;
1139 isl_map *may;
1142 static int collect_must_may(__isl_take isl_map *dep, int must,
1143 void *dep_user, void *user)
1145 struct must_may *mm = (struct must_may *)user;
1147 if (must)
1148 mm->must = isl_map_union(mm->must, dep);
1149 else
1150 mm->may = isl_map_union(mm->may, dep);
1152 return 0;
1155 static int common_space(void *first, void *second)
1157 int depth = *(int *)first;
1158 return 2 * depth;
1161 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1163 isl_map *map2;
1164 int equal;
1166 if (!map)
1167 return -1;
1169 map2 = isl_map_read_from_str(map->ctx, str, -1);
1170 equal = isl_map_is_equal(map, map2);
1171 isl_map_free(map2);
1173 return equal;
1176 void test_dep(struct isl_ctx *ctx)
1178 const char *str;
1179 isl_dim *dim;
1180 isl_map *map;
1181 isl_access_info *ai;
1182 isl_flow *flow;
1183 int depth;
1184 struct must_may mm;
1186 depth = 3;
1188 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1189 map = isl_map_read_from_str(ctx, str, -1);
1190 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1192 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1193 map = isl_map_read_from_str(ctx, str, -1);
1194 ai = isl_access_info_add_source(ai, map, 1, &depth);
1196 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1197 map = isl_map_read_from_str(ctx, str, -1);
1198 ai = isl_access_info_add_source(ai, map, 1, &depth);
1200 flow = isl_access_info_compute_flow(ai);
1201 dim = isl_dim_alloc(ctx, 0, 3, 3);
1202 mm.must = isl_map_empty(isl_dim_copy(dim));
1203 mm.may = isl_map_empty(dim);
1205 isl_flow_foreach(flow, collect_must_may, &mm);
1207 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1208 " [1,10,0] -> [2,5,0] }";
1209 assert(map_is_equal(mm.must, str));
1210 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1211 assert(map_is_equal(mm.may, str));
1213 isl_map_free(mm.must);
1214 isl_map_free(mm.may);
1215 isl_flow_free(flow);
1218 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1219 map = isl_map_read_from_str(ctx, str, -1);
1220 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1222 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1223 map = isl_map_read_from_str(ctx, str, -1);
1224 ai = isl_access_info_add_source(ai, map, 1, &depth);
1226 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1227 map = isl_map_read_from_str(ctx, str, -1);
1228 ai = isl_access_info_add_source(ai, map, 0, &depth);
1230 flow = isl_access_info_compute_flow(ai);
1231 dim = isl_dim_alloc(ctx, 0, 3, 3);
1232 mm.must = isl_map_empty(isl_dim_copy(dim));
1233 mm.may = isl_map_empty(dim);
1235 isl_flow_foreach(flow, collect_must_may, &mm);
1237 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1238 assert(map_is_equal(mm.must, str));
1239 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1240 assert(map_is_equal(mm.may, str));
1242 isl_map_free(mm.must);
1243 isl_map_free(mm.may);
1244 isl_flow_free(flow);
1247 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1248 map = isl_map_read_from_str(ctx, str, -1);
1249 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1251 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1252 map = isl_map_read_from_str(ctx, str, -1);
1253 ai = isl_access_info_add_source(ai, map, 0, &depth);
1255 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1256 map = isl_map_read_from_str(ctx, str, -1);
1257 ai = isl_access_info_add_source(ai, map, 0, &depth);
1259 flow = isl_access_info_compute_flow(ai);
1260 dim = isl_dim_alloc(ctx, 0, 3, 3);
1261 mm.must = isl_map_empty(isl_dim_copy(dim));
1262 mm.may = isl_map_empty(dim);
1264 isl_flow_foreach(flow, collect_must_may, &mm);
1266 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1267 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1268 assert(map_is_equal(mm.may, str));
1269 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1270 assert(map_is_equal(mm.must, str));
1272 isl_map_free(mm.must);
1273 isl_map_free(mm.may);
1274 isl_flow_free(flow);
1277 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1278 map = isl_map_read_from_str(ctx, str, -1);
1279 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1281 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1282 map = isl_map_read_from_str(ctx, str, -1);
1283 ai = isl_access_info_add_source(ai, map, 0, &depth);
1285 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1286 map = isl_map_read_from_str(ctx, str, -1);
1287 ai = isl_access_info_add_source(ai, map, 0, &depth);
1289 flow = isl_access_info_compute_flow(ai);
1290 dim = isl_dim_alloc(ctx, 0, 3, 3);
1291 mm.must = isl_map_empty(isl_dim_copy(dim));
1292 mm.may = isl_map_empty(dim);
1294 isl_flow_foreach(flow, collect_must_may, &mm);
1296 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1297 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1298 assert(map_is_equal(mm.may, str));
1299 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1300 assert(map_is_equal(mm.must, str));
1302 isl_map_free(mm.must);
1303 isl_map_free(mm.may);
1304 isl_flow_free(flow);
1307 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1308 map = isl_map_read_from_str(ctx, str, -1);
1309 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1311 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1312 map = isl_map_read_from_str(ctx, str, -1);
1313 ai = isl_access_info_add_source(ai, map, 0, &depth);
1315 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1316 map = isl_map_read_from_str(ctx, str, -1);
1317 ai = isl_access_info_add_source(ai, map, 0, &depth);
1319 flow = isl_access_info_compute_flow(ai);
1320 dim = isl_dim_alloc(ctx, 0, 3, 3);
1321 mm.must = isl_map_empty(isl_dim_copy(dim));
1322 mm.may = isl_map_empty(dim);
1324 isl_flow_foreach(flow, collect_must_may, &mm);
1326 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1327 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1328 assert(map_is_equal(mm.may, str));
1329 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1330 assert(map_is_equal(mm.must, str));
1332 isl_map_free(mm.must);
1333 isl_map_free(mm.may);
1334 isl_flow_free(flow);
1337 depth = 5;
1339 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1340 map = isl_map_read_from_str(ctx, str, -1);
1341 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1343 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1344 map = isl_map_read_from_str(ctx, str, -1);
1345 ai = isl_access_info_add_source(ai, map, 1, &depth);
1347 flow = isl_access_info_compute_flow(ai);
1348 dim = isl_dim_alloc(ctx, 0, 5, 5);
1349 mm.must = isl_map_empty(isl_dim_copy(dim));
1350 mm.may = isl_map_empty(dim);
1352 isl_flow_foreach(flow, collect_must_may, &mm);
1354 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1355 assert(map_is_equal(mm.must, str));
1356 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1357 assert(map_is_equal(mm.may, str));
1359 isl_map_free(mm.must);
1360 isl_map_free(mm.may);
1361 isl_flow_free(flow);
1364 void test_sv(struct isl_ctx *ctx)
1366 const char *str;
1367 isl_map *map;
1369 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1370 map = isl_map_read_from_str(ctx, str, -1);
1371 assert(isl_map_is_single_valued(map));
1372 isl_map_free(map);
1374 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1375 map = isl_map_read_from_str(ctx, str, -1);
1376 assert(!isl_map_is_single_valued(map));
1377 isl_map_free(map);
1380 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1382 isl_map *map;
1384 map = isl_map_read_from_str(ctx, str, -1);
1385 if (bijective)
1386 assert(isl_map_is_bijective(map));
1387 else
1388 assert(!isl_map_is_bijective(map));
1389 isl_map_free(map);
1392 void test_bijective(struct isl_ctx *ctx)
1394 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1395 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1396 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1397 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1398 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1399 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1400 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1401 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1402 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1403 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1404 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1405 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1406 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1409 void test_pwqp(struct isl_ctx *ctx)
1411 const char *str;
1412 isl_set *set;
1413 isl_pw_qpolynomial *pwqp1, *pwqp2;
1415 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1416 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1418 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1419 isl_dim_set, 1, 1);
1421 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1422 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1424 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1426 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1428 isl_pw_qpolynomial_free(pwqp1);
1430 str = "{ [i] -> i }";
1431 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1432 str = "{ [k] : exists a : k = 2a }";
1433 set = isl_set_read_from_str(ctx, str, 0);
1434 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1435 str = "{ [i] -> i }";
1436 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1438 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1440 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1442 isl_pw_qpolynomial_free(pwqp1);
1445 void test_split_periods(isl_ctx *ctx)
1447 const char *str;
1448 isl_pw_qpolynomial *pwqp;
1450 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1451 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1452 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1453 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1455 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1456 assert(pwqp);
1458 isl_pw_qpolynomial_free(pwqp);
1461 void test_union(isl_ctx *ctx)
1463 const char *str;
1464 isl_union_set *uset;
1465 isl_union_map *umap1, *umap2;
1467 str = "{ [i] : 0 <= i <= 1 }";
1468 uset = isl_union_set_from_set(isl_set_read_from_str(ctx, str, -1));
1469 str = "{ [1] -> [0] }";
1470 umap1 = isl_union_map_from_map(isl_map_read_from_str(ctx, str, -1));
1472 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset), uset);
1473 assert(isl_union_map_is_equal(umap1, umap2));
1475 isl_union_map_free(umap1);
1476 isl_union_map_free(umap2);
1479 int main()
1481 struct isl_ctx *ctx;
1483 srcdir = getenv("srcdir");
1484 assert(srcdir);
1486 ctx = isl_ctx_alloc();
1487 test_union(ctx);
1488 test_split_periods(ctx);
1489 test_parse(ctx);
1490 test_pwqp(ctx);
1491 test_lex(ctx);
1492 test_sv(ctx);
1493 test_bijective(ctx);
1494 test_dep(ctx);
1495 test_read(ctx);
1496 test_bounded(ctx);
1497 test_construction(ctx);
1498 test_dim(ctx);
1499 test_div(ctx);
1500 test_application(ctx);
1501 test_affine_hull(ctx);
1502 test_convex_hull(ctx);
1503 test_gist(ctx);
1504 test_coalesce(ctx);
1505 test_closure(ctx);
1506 test_lexmin(ctx);
1507 isl_ctx_free(ctx);
1508 return 0;