isl_reordering_extend_dim: don't mess with copies of input
[isl.git] / isl_test.c
blob7e2227949ce94e4e67ee540d877e682f2a204b47
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_private.h>
14 #include <isl_map_private.h>
15 #include <isl/set.h>
16 #include <isl/flow.h>
17 #include <isl/constraint.h>
18 #include <isl/polynomial.h>
19 #include <isl/union_map.h>
20 #include <isl_factorization.h>
22 static char *srcdir;
24 void test_parse_map(isl_ctx *ctx, const char *str)
26 isl_map *map;
28 map = isl_map_read_from_str(ctx, str, -1);
29 assert(map);
30 isl_map_free(map);
33 void test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
35 isl_map *map, *map2;
37 map = isl_map_read_from_str(ctx, str, -1);
38 map2 = isl_map_read_from_str(ctx, str2, -1);
39 assert(map && map2 && isl_map_is_equal(map, map2));
40 isl_map_free(map);
41 isl_map_free(map2);
44 void test_parse_pwqp(isl_ctx *ctx, const char *str)
46 isl_pw_qpolynomial *pwqp;
48 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
49 assert(pwqp);
50 isl_pw_qpolynomial_free(pwqp);
53 void test_parse(struct isl_ctx *ctx)
55 isl_map *map, *map2;
56 const char *str, *str2;
58 str = "{ [i] -> [-i] }";
59 map = isl_map_read_from_str(ctx, str, -1);
60 assert(map);
61 isl_map_free(map);
63 str = "{ A[i] -> L[([i/3])] }";
64 map = isl_map_read_from_str(ctx, str, -1);
65 assert(map);
66 isl_map_free(map);
68 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
69 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
70 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
72 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
73 str2 = "{ [x, y] : 2y >= 6 - x }";
74 test_parse_map_equal(ctx, str, str2);
76 test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
77 "{ [x,y] : x <= y, 2*y + 3 }");
78 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
79 test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }", str);
81 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
82 map = isl_map_read_from_str(ctx, str, -1);
83 str = "{ [new, old] -> [o0, o1] : "
84 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
85 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
86 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
87 map2 = isl_map_read_from_str(ctx, str, -1);
88 assert(isl_map_is_equal(map, map2));
89 isl_map_free(map);
90 isl_map_free(map2);
92 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
93 map = isl_map_read_from_str(ctx, str, -1);
94 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
95 map2 = isl_map_read_from_str(ctx, str, -1);
96 assert(isl_map_is_equal(map, map2));
97 isl_map_free(map);
98 isl_map_free(map2);
100 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
101 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
102 test_parse_map_equal(ctx, str, str2);
104 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
105 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
108 void test_read(struct isl_ctx *ctx)
110 char filename[PATH_MAX];
111 FILE *input;
112 int n;
113 struct isl_basic_set *bset1, *bset2;
114 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
116 n = snprintf(filename, sizeof(filename),
117 "%s/test_inputs/set.omega", srcdir);
118 assert(n < sizeof(filename));
119 input = fopen(filename, "r");
120 assert(input);
122 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
123 bset2 = isl_basic_set_read_from_str(ctx, str, 0);
125 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
127 isl_basic_set_free(bset1);
128 isl_basic_set_free(bset2);
130 fclose(input);
133 void test_bounded(struct isl_ctx *ctx)
135 isl_set *set;
136 int bounded;
138 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }", -1);
139 bounded = isl_set_is_bounded(set);
140 assert(bounded);
141 isl_set_free(set);
143 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }", -1);
144 bounded = isl_set_is_bounded(set);
145 assert(!bounded);
146 isl_set_free(set);
148 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }", -1);
149 bounded = isl_set_is_bounded(set);
150 assert(!bounded);
151 isl_set_free(set);
154 /* Construct the basic set { [i] : 5 <= i <= N } */
155 void test_construction(struct isl_ctx *ctx)
157 isl_int v;
158 struct isl_dim *dim;
159 struct isl_basic_set *bset;
160 struct isl_constraint *c;
162 isl_int_init(v);
164 dim = isl_dim_set_alloc(ctx, 1, 1);
165 bset = isl_basic_set_universe(dim);
167 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
168 isl_int_set_si(v, -1);
169 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
170 isl_int_set_si(v, 1);
171 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
172 bset = isl_basic_set_add_constraint(bset, c);
174 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
175 isl_int_set_si(v, 1);
176 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
177 isl_int_set_si(v, -5);
178 isl_constraint_set_constant(c, v);
179 bset = isl_basic_set_add_constraint(bset, c);
181 isl_basic_set_free(bset);
183 isl_int_clear(v);
186 void test_dim(struct isl_ctx *ctx)
188 const char *str;
189 isl_map *map1, *map2;
191 map1 = isl_map_read_from_str(ctx,
192 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
193 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
194 map2 = isl_map_read_from_str(ctx,
195 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
196 assert(isl_map_is_equal(map1, map2));
197 isl_map_free(map2);
199 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
200 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }", -1);
201 assert(isl_map_is_equal(map1, map2));
203 isl_map_free(map1);
204 isl_map_free(map2);
206 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
207 map1 = isl_map_read_from_str(ctx, str, -1);
208 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
209 map2 = isl_map_read_from_str(ctx, str, -1);
210 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
211 assert(isl_map_is_equal(map1, map2));
213 isl_map_free(map1);
214 isl_map_free(map2);
217 void test_div(struct isl_ctx *ctx)
219 isl_int v;
220 int pos;
221 struct isl_dim *dim;
222 struct isl_div *div;
223 struct isl_basic_set *bset;
224 struct isl_constraint *c;
226 isl_int_init(v);
228 /* test 1 */
229 dim = isl_dim_set_alloc(ctx, 0, 1);
230 bset = isl_basic_set_universe(dim);
232 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
233 isl_int_set_si(v, -1);
234 isl_constraint_set_constant(c, v);
235 isl_int_set_si(v, 1);
236 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
237 div = isl_div_alloc(isl_basic_set_get_dim(bset));
238 c = isl_constraint_add_div(c, div, &pos);
239 isl_int_set_si(v, 3);
240 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
241 bset = isl_basic_set_add_constraint(bset, c);
243 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
244 isl_int_set_si(v, 1);
245 isl_constraint_set_constant(c, v);
246 isl_int_set_si(v, -1);
247 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
248 div = isl_div_alloc(isl_basic_set_get_dim(bset));
249 c = isl_constraint_add_div(c, div, &pos);
250 isl_int_set_si(v, 3);
251 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
252 bset = isl_basic_set_add_constraint(bset, c);
254 assert(bset && bset->n_div == 1);
255 isl_basic_set_free(bset);
257 /* test 2 */
258 dim = isl_dim_set_alloc(ctx, 0, 1);
259 bset = isl_basic_set_universe(dim);
261 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
262 isl_int_set_si(v, 1);
263 isl_constraint_set_constant(c, v);
264 isl_int_set_si(v, -1);
265 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
266 div = isl_div_alloc(isl_basic_set_get_dim(bset));
267 c = isl_constraint_add_div(c, div, &pos);
268 isl_int_set_si(v, 3);
269 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
270 bset = isl_basic_set_add_constraint(bset, c);
272 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
273 isl_int_set_si(v, -1);
274 isl_constraint_set_constant(c, v);
275 isl_int_set_si(v, 1);
276 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
277 div = isl_div_alloc(isl_basic_set_get_dim(bset));
278 c = isl_constraint_add_div(c, div, &pos);
279 isl_int_set_si(v, 3);
280 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
281 bset = isl_basic_set_add_constraint(bset, c);
283 assert(bset && bset->n_div == 1);
284 isl_basic_set_free(bset);
286 /* test 3 */
287 dim = isl_dim_set_alloc(ctx, 0, 1);
288 bset = isl_basic_set_universe(dim);
290 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
291 isl_int_set_si(v, 1);
292 isl_constraint_set_constant(c, v);
293 isl_int_set_si(v, -1);
294 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
295 div = isl_div_alloc(isl_basic_set_get_dim(bset));
296 c = isl_constraint_add_div(c, div, &pos);
297 isl_int_set_si(v, 3);
298 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
299 bset = isl_basic_set_add_constraint(bset, c);
301 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
302 isl_int_set_si(v, -3);
303 isl_constraint_set_constant(c, v);
304 isl_int_set_si(v, 1);
305 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
306 div = isl_div_alloc(isl_basic_set_get_dim(bset));
307 c = isl_constraint_add_div(c, div, &pos);
308 isl_int_set_si(v, 4);
309 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
310 bset = isl_basic_set_add_constraint(bset, c);
312 assert(bset && bset->n_div == 1);
313 isl_basic_set_free(bset);
315 /* test 4 */
316 dim = isl_dim_set_alloc(ctx, 0, 1);
317 bset = isl_basic_set_universe(dim);
319 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
320 isl_int_set_si(v, 2);
321 isl_constraint_set_constant(c, v);
322 isl_int_set_si(v, -1);
323 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
324 div = isl_div_alloc(isl_basic_set_get_dim(bset));
325 c = isl_constraint_add_div(c, div, &pos);
326 isl_int_set_si(v, 3);
327 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
328 bset = isl_basic_set_add_constraint(bset, c);
330 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
331 isl_int_set_si(v, -1);
332 isl_constraint_set_constant(c, v);
333 isl_int_set_si(v, 1);
334 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
335 div = isl_div_alloc(isl_basic_set_get_dim(bset));
336 c = isl_constraint_add_div(c, div, &pos);
337 isl_int_set_si(v, 6);
338 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
339 bset = isl_basic_set_add_constraint(bset, c);
341 assert(isl_basic_set_is_empty(bset));
342 isl_basic_set_free(bset);
344 /* test 5 */
345 dim = isl_dim_set_alloc(ctx, 0, 2);
346 bset = isl_basic_set_universe(dim);
348 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
349 isl_int_set_si(v, -1);
350 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
351 div = isl_div_alloc(isl_basic_set_get_dim(bset));
352 c = isl_constraint_add_div(c, div, &pos);
353 isl_int_set_si(v, 3);
354 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
355 bset = isl_basic_set_add_constraint(bset, c);
357 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
358 isl_int_set_si(v, 1);
359 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
360 isl_int_set_si(v, -3);
361 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
362 bset = isl_basic_set_add_constraint(bset, c);
364 assert(bset && bset->n_div == 0);
365 isl_basic_set_free(bset);
367 /* test 6 */
368 dim = isl_dim_set_alloc(ctx, 0, 2);
369 bset = isl_basic_set_universe(dim);
371 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
372 isl_int_set_si(v, -1);
373 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
374 div = isl_div_alloc(isl_basic_set_get_dim(bset));
375 c = isl_constraint_add_div(c, div, &pos);
376 isl_int_set_si(v, 6);
377 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
378 bset = isl_basic_set_add_constraint(bset, c);
380 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
381 isl_int_set_si(v, 1);
382 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
383 isl_int_set_si(v, -3);
384 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
385 bset = isl_basic_set_add_constraint(bset, c);
387 assert(bset && bset->n_div == 1);
388 isl_basic_set_free(bset);
390 /* test 7 */
391 /* This test is a bit tricky. We set up an equality
392 * a + 3b + 3c = 6 e0
393 * Normalization of divs creates _two_ divs
394 * a = 3 e0
395 * c - b - e0 = 2 e1
396 * Afterwards e0 is removed again because it has coefficient -1
397 * and we end up with the original equality and div again.
398 * Perhaps we can avoid the introduction of this temporary div.
400 dim = isl_dim_set_alloc(ctx, 0, 3);
401 bset = isl_basic_set_universe(dim);
403 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
404 isl_int_set_si(v, -1);
405 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
406 isl_int_set_si(v, -3);
407 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
408 isl_int_set_si(v, -3);
409 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
410 div = isl_div_alloc(isl_basic_set_get_dim(bset));
411 c = isl_constraint_add_div(c, div, &pos);
412 isl_int_set_si(v, 6);
413 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
414 bset = isl_basic_set_add_constraint(bset, c);
416 /* Test disabled for now */
418 assert(bset && bset->n_div == 1);
420 isl_basic_set_free(bset);
422 /* test 8 */
423 dim = isl_dim_set_alloc(ctx, 0, 4);
424 bset = isl_basic_set_universe(dim);
426 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
427 isl_int_set_si(v, -1);
428 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
429 isl_int_set_si(v, -3);
430 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
431 isl_int_set_si(v, -3);
432 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
433 div = isl_div_alloc(isl_basic_set_get_dim(bset));
434 c = isl_constraint_add_div(c, div, &pos);
435 isl_int_set_si(v, 6);
436 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
437 bset = isl_basic_set_add_constraint(bset, c);
439 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
440 isl_int_set_si(v, -1);
441 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
442 isl_int_set_si(v, 1);
443 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
444 isl_int_set_si(v, 1);
445 isl_constraint_set_constant(c, v);
446 bset = isl_basic_set_add_constraint(bset, c);
448 /* Test disabled for now */
450 assert(bset && bset->n_div == 1);
452 isl_basic_set_free(bset);
454 /* test 9 */
455 dim = isl_dim_set_alloc(ctx, 0, 2);
456 bset = isl_basic_set_universe(dim);
458 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
459 isl_int_set_si(v, 1);
460 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
461 isl_int_set_si(v, -1);
462 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
463 div = isl_div_alloc(isl_basic_set_get_dim(bset));
464 c = isl_constraint_add_div(c, div, &pos);
465 isl_int_set_si(v, -2);
466 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
467 bset = isl_basic_set_add_constraint(bset, c);
469 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
470 isl_int_set_si(v, -1);
471 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
472 div = isl_div_alloc(isl_basic_set_get_dim(bset));
473 c = isl_constraint_add_div(c, div, &pos);
474 isl_int_set_si(v, 3);
475 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
476 isl_int_set_si(v, 2);
477 isl_constraint_set_constant(c, v);
478 bset = isl_basic_set_add_constraint(bset, c);
480 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
482 assert(!isl_basic_set_is_empty(bset));
484 isl_basic_set_free(bset);
486 /* test 10 */
487 dim = isl_dim_set_alloc(ctx, 0, 2);
488 bset = isl_basic_set_universe(dim);
490 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
491 isl_int_set_si(v, 1);
492 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
493 div = isl_div_alloc(isl_basic_set_get_dim(bset));
494 c = isl_constraint_add_div(c, div, &pos);
495 isl_int_set_si(v, -2);
496 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
497 bset = isl_basic_set_add_constraint(bset, c);
499 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
501 isl_basic_set_free(bset);
503 isl_int_clear(v);
506 void test_application_case(struct isl_ctx *ctx, const char *name)
508 char filename[PATH_MAX];
509 FILE *input;
510 int n;
511 struct isl_basic_set *bset1, *bset2;
512 struct isl_basic_map *bmap;
514 n = snprintf(filename, sizeof(filename),
515 "%s/test_inputs/%s.omega", srcdir, name);
516 assert(n < sizeof(filename));
517 input = fopen(filename, "r");
518 assert(input);
520 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
521 bmap = isl_basic_map_read_from_file(ctx, input, 0);
523 bset1 = isl_basic_set_apply(bset1, bmap);
525 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
527 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
529 isl_basic_set_free(bset1);
530 isl_basic_set_free(bset2);
532 fclose(input);
535 void test_application(struct isl_ctx *ctx)
537 test_application_case(ctx, "application");
538 test_application_case(ctx, "application2");
541 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
543 char filename[PATH_MAX];
544 FILE *input;
545 int n;
546 struct isl_basic_set *bset1, *bset2;
548 n = snprintf(filename, sizeof(filename),
549 "%s/test_inputs/%s.polylib", srcdir, name);
550 assert(n < sizeof(filename));
551 input = fopen(filename, "r");
552 assert(input);
554 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
555 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
557 bset1 = isl_basic_set_affine_hull(bset1);
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_affine_hull(struct isl_ctx *ctx)
569 test_affine_hull_case(ctx, "affine2");
570 test_affine_hull_case(ctx, "affine");
571 test_affine_hull_case(ctx, "affine3");
574 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
576 char filename[PATH_MAX];
577 FILE *input;
578 int n;
579 struct isl_basic_set *bset1, *bset2;
580 struct isl_set *set;
582 n = snprintf(filename, sizeof(filename),
583 "%s/test_inputs/%s.polylib", srcdir, name);
584 assert(n < sizeof(filename));
585 input = fopen(filename, "r");
586 assert(input);
588 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
589 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
591 set = isl_basic_set_union(bset1, bset2);
592 bset1 = isl_set_convex_hull(set);
594 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
596 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
598 isl_basic_set_free(bset1);
599 isl_basic_set_free(bset2);
601 fclose(input);
604 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
606 const char *str1, *str2;
607 isl_set *set1, *set2;
608 int orig_convex = ctx->opt->convex;
609 ctx->opt->convex = convex;
611 test_convex_hull_case(ctx, "convex0");
612 test_convex_hull_case(ctx, "convex1");
613 test_convex_hull_case(ctx, "convex2");
614 test_convex_hull_case(ctx, "convex3");
615 test_convex_hull_case(ctx, "convex4");
616 test_convex_hull_case(ctx, "convex5");
617 test_convex_hull_case(ctx, "convex6");
618 test_convex_hull_case(ctx, "convex7");
619 test_convex_hull_case(ctx, "convex8");
620 test_convex_hull_case(ctx, "convex9");
621 test_convex_hull_case(ctx, "convex10");
622 test_convex_hull_case(ctx, "convex11");
623 test_convex_hull_case(ctx, "convex12");
624 test_convex_hull_case(ctx, "convex13");
625 test_convex_hull_case(ctx, "convex14");
626 test_convex_hull_case(ctx, "convex15");
628 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
629 "(i0 = 1 and i1 = 0 and i2 = 1) or "
630 "(i0 = 0 and i1 = 0 and i2 = 0) }";
631 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
632 set1 = isl_set_read_from_str(ctx, str1, -1);
633 set2 = isl_set_read_from_str(ctx, str2, -1);
634 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
635 assert(isl_set_is_equal(set1, set2));
636 isl_set_free(set1);
637 isl_set_free(set2);
639 ctx->opt->convex = orig_convex;
642 void test_convex_hull(struct isl_ctx *ctx)
644 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
645 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
648 void test_gist_case(struct isl_ctx *ctx, const char *name)
650 char filename[PATH_MAX];
651 FILE *input;
652 int n;
653 struct isl_basic_set *bset1, *bset2;
655 n = snprintf(filename, sizeof(filename),
656 "%s/test_inputs/%s.polylib", srcdir, name);
657 assert(n < sizeof(filename));
658 input = fopen(filename, "r");
659 assert(input);
661 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
662 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
664 bset1 = isl_basic_set_gist(bset1, bset2);
666 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
668 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
670 isl_basic_set_free(bset1);
671 isl_basic_set_free(bset2);
673 fclose(input);
676 void test_gist(struct isl_ctx *ctx)
678 const char *str;
679 isl_basic_set *bset1, *bset2;
681 test_gist_case(ctx, "gist1");
683 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
684 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
685 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
686 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
687 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
688 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
689 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
690 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
691 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
692 bset1 = isl_basic_set_read_from_str(ctx, str, -1);
693 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
694 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
695 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
696 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
697 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
698 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
699 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
700 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
701 bset2 = isl_basic_set_read_from_str(ctx, str, -1);
702 bset1 = isl_basic_set_gist(bset1, bset2);
703 assert(bset1 && bset1->n_div == 0);
704 isl_basic_set_free(bset1);
707 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
709 isl_set *set, *set2;
711 set = isl_set_read_from_str(ctx, str, -1);
712 set = isl_set_coalesce(set);
713 set2 = isl_set_read_from_str(ctx, str, -1);
714 assert(isl_set_is_equal(set, set2));
715 if (check_one)
716 assert(set && set->n == 1);
717 isl_set_free(set);
718 isl_set_free(set2);
721 void test_coalesce(struct isl_ctx *ctx)
723 const char *str;
724 struct isl_set *set, *set2;
725 struct isl_map *map, *map2;
727 set = isl_set_read_from_str(ctx,
728 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
729 "y >= x & x >= 2 & 5 >= y }", -1);
730 set = isl_set_coalesce(set);
731 assert(set && set->n == 1);
732 isl_set_free(set);
734 set = isl_set_read_from_str(ctx,
735 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
736 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
737 set = isl_set_coalesce(set);
738 assert(set && set->n == 1);
739 isl_set_free(set);
741 set = isl_set_read_from_str(ctx,
742 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
743 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
744 set = isl_set_coalesce(set);
745 assert(set && set->n == 2);
746 isl_set_free(set);
748 set = isl_set_read_from_str(ctx,
749 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
750 "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
751 set = isl_set_coalesce(set);
752 assert(set && set->n == 1);
753 isl_set_free(set);
755 set = isl_set_read_from_str(ctx,
756 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
757 "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
758 set = isl_set_coalesce(set);
759 assert(set && set->n == 2);
760 isl_set_free(set);
762 set = isl_set_read_from_str(ctx,
763 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
764 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
765 set = isl_set_coalesce(set);
766 assert(set && set->n == 2);
767 isl_set_free(set);
769 set = isl_set_read_from_str(ctx,
770 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
771 "y >= 0 & x = 6 & y <= 6}", -1);
772 set = isl_set_coalesce(set);
773 assert(set && set->n == 1);
774 isl_set_free(set);
776 set = isl_set_read_from_str(ctx,
777 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
778 "y >= 0 & x = 7 & y <= 6}", -1);
779 set = isl_set_coalesce(set);
780 assert(set && set->n == 2);
781 isl_set_free(set);
783 set = isl_set_read_from_str(ctx,
784 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
785 "y >= 0 & x = 6 & y <= 5}", -1);
786 set = isl_set_coalesce(set);
787 assert(set && set->n == 1);
788 set2 = isl_set_read_from_str(ctx,
789 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
790 "y >= 0 & x = 6 & y <= 5}", -1);
791 assert(isl_set_is_equal(set, set2));
792 isl_set_free(set);
793 isl_set_free(set2);
795 set = isl_set_read_from_str(ctx,
796 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
797 "y >= 0 & x = 6 & y <= 7}", -1);
798 set = isl_set_coalesce(set);
799 assert(set && set->n == 2);
800 isl_set_free(set);
802 set = isl_set_read_from_str(ctx,
803 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
804 set = isl_set_coalesce(set);
805 assert(set && set->n == 1);
806 set2 = isl_set_read_from_str(ctx,
807 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
808 assert(isl_set_is_equal(set, set2));
809 isl_set_free(set);
810 isl_set_free(set2);
812 set = isl_set_read_from_str(ctx,
813 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
814 set = isl_set_coalesce(set);
815 set2 = isl_set_read_from_str(ctx,
816 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
817 assert(isl_set_is_equal(set, set2));
818 isl_set_free(set);
819 isl_set_free(set2);
821 set = isl_set_read_from_str(ctx,
822 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
823 "2 <= i and i <= n }", -1);
824 set = isl_set_coalesce(set);
825 assert(set && set->n == 1);
826 set2 = isl_set_read_from_str(ctx,
827 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
828 "2 <= i and i <= n }", -1);
829 assert(isl_set_is_equal(set, set2));
830 isl_set_free(set);
831 isl_set_free(set2);
833 map = isl_map_read_from_str(ctx,
834 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
835 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
836 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
837 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
838 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
839 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
840 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
841 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
842 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
843 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
844 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
845 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
846 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
847 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
848 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
849 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
850 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
851 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
852 map = isl_map_coalesce(map);
853 map2 = isl_map_read_from_str(ctx,
854 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
855 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
856 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
857 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
858 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
859 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
860 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
861 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
862 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
863 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
864 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
865 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
866 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
867 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
868 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
869 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
870 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
871 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
872 assert(isl_map_is_equal(map, map2));
873 isl_map_free(map);
874 isl_map_free(map2);
876 str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
877 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
878 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
879 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
880 map = isl_map_read_from_str(ctx, str, -1);
881 map = isl_map_coalesce(map);
882 map2 = isl_map_read_from_str(ctx, str, -1);
883 assert(isl_map_is_equal(map, map2));
884 isl_map_free(map);
885 isl_map_free(map2);
887 str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
888 "[o0, o1, o2, o3, o4, o5, o6] : "
889 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
890 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
891 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
892 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
893 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
894 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
895 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
896 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
897 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
898 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
899 "o6 >= i3 + i6 - o3 and M >= 0 and "
900 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
901 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
902 map = isl_map_read_from_str(ctx, str, -1);
903 map = isl_map_coalesce(map);
904 map2 = isl_map_read_from_str(ctx, str, -1);
905 assert(isl_map_is_equal(map, map2));
906 isl_map_free(map);
907 isl_map_free(map2);
909 str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
910 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
911 "(o0 = 0 and M >= 2 and N >= 3) or "
912 "(M = 0 and o0 = 0 and N >= 3) }";
913 map = isl_map_read_from_str(ctx, str, -1);
914 map = isl_map_coalesce(map);
915 map2 = isl_map_read_from_str(ctx, str, -1);
916 assert(isl_map_is_equal(map, map2));
917 isl_map_free(map);
918 isl_map_free(map2);
920 str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
921 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
922 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
923 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
924 map = isl_map_read_from_str(ctx, str, -1);
925 map = isl_map_coalesce(map);
926 map2 = isl_map_read_from_str(ctx, str, -1);
927 assert(isl_map_is_equal(map, map2));
928 isl_map_free(map);
929 isl_map_free(map2);
931 test_coalesce_set(ctx,
932 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
933 "(i1 = M and M >= 1) }", 0);
934 test_coalesce_set(ctx,
935 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
936 test_coalesce_set(ctx,
937 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
938 "(y = 3 and x = 1) }", 1);
939 test_coalesce_set(ctx,
940 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
941 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
942 "i1 <= M and i3 <= M and i4 <= M) or "
943 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
944 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
945 "i4 <= -1 + M) }", 1);
946 test_coalesce_set(ctx,
947 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
948 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
949 test_coalesce_set(ctx,
950 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
951 "-x - y + 1 >= 0 and -3 <= z <= 3;"
952 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
953 "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
954 test_coalesce_set(ctx,
955 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
956 test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
957 test_coalesce_set(ctx,
958 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
959 test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
960 test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1);
961 test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0);
962 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1);
963 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0);
964 test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0);
965 test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0);
968 void test_closure(struct isl_ctx *ctx)
970 const char *str;
971 isl_set *dom;
972 isl_map *up, *right;
973 isl_map *map, *map2;
974 int exact;
976 /* COCOA example 1 */
977 map = isl_map_read_from_str(ctx,
978 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
979 "1 <= i and i < n and 1 <= j and j < n or "
980 "i2 = i + 1 and j2 = j - 1 and "
981 "1 <= i and i < n and 2 <= j and j <= n }", -1);
982 map = isl_map_power(map, &exact);
983 assert(exact);
984 isl_map_free(map);
986 /* COCOA example 1 */
987 map = isl_map_read_from_str(ctx,
988 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
989 "1 <= i and i < n and 1 <= j and j < n or "
990 "i2 = i + 1 and j2 = j - 1 and "
991 "1 <= i and i < n and 2 <= j and j <= n }", -1);
992 map = isl_map_transitive_closure(map, &exact);
993 assert(exact);
994 map2 = isl_map_read_from_str(ctx,
995 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
996 "1 <= i and i < n and 1 <= j and j <= n and "
997 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
998 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
999 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
1000 assert(isl_map_is_equal(map, map2));
1001 isl_map_free(map2);
1002 isl_map_free(map);
1004 map = isl_map_read_from_str(ctx,
1005 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1006 " 0 <= y and y <= n }", -1);
1007 map = isl_map_transitive_closure(map, &exact);
1008 map2 = isl_map_read_from_str(ctx,
1009 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1010 " 0 <= y and y <= n }", -1);
1011 assert(isl_map_is_equal(map, map2));
1012 isl_map_free(map2);
1013 isl_map_free(map);
1015 /* COCOA example 2 */
1016 map = isl_map_read_from_str(ctx,
1017 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1018 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1019 "i2 = i + 2 and j2 = j - 2 and "
1020 "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
1021 map = isl_map_transitive_closure(map, &exact);
1022 assert(exact);
1023 map2 = isl_map_read_from_str(ctx,
1024 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1025 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1026 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1027 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1028 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
1029 assert(isl_map_is_equal(map, map2));
1030 isl_map_free(map);
1031 isl_map_free(map2);
1033 /* COCOA Fig.2 left */
1034 map = isl_map_read_from_str(ctx,
1035 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1036 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1037 "j <= n or "
1038 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1039 "j <= 2 i - 3 and j <= n - 2 or "
1040 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1041 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
1042 map = isl_map_transitive_closure(map, &exact);
1043 assert(exact);
1044 isl_map_free(map);
1046 /* COCOA Fig.2 right */
1047 map = isl_map_read_from_str(ctx,
1048 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1049 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1050 "j <= n or "
1051 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1052 "j <= 2 i - 4 and j <= n - 3 or "
1053 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1054 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
1055 map = isl_map_power(map, &exact);
1056 assert(exact);
1057 isl_map_free(map);
1059 /* COCOA Fig.2 right */
1060 map = isl_map_read_from_str(ctx,
1061 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1062 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1063 "j <= n or "
1064 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1065 "j <= 2 i - 4 and j <= n - 3 or "
1066 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1067 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
1068 map = isl_map_transitive_closure(map, &exact);
1069 assert(exact);
1070 map2 = isl_map_read_from_str(ctx,
1071 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1072 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1073 "j <= n and 3 + i + 2 j <= 3 n and "
1074 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1075 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1076 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1077 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1078 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
1079 assert(isl_map_is_equal(map, map2));
1080 isl_map_free(map2);
1081 isl_map_free(map);
1083 /* COCOA Fig.1 right */
1084 dom = isl_set_read_from_str(ctx,
1085 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1086 "2 x - 3 y + 3 >= 0 }", -1);
1087 right = isl_map_read_from_str(ctx,
1088 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
1089 up = isl_map_read_from_str(ctx,
1090 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
1091 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1092 right = isl_map_intersect_range(right, isl_set_copy(dom));
1093 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1094 up = isl_map_intersect_range(up, dom);
1095 map = isl_map_union(up, right);
1096 map = isl_map_transitive_closure(map, &exact);
1097 assert(exact);
1098 map2 = isl_map_read_from_str(ctx,
1099 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1100 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }", -1);
1101 assert(isl_map_is_equal(map, map2));
1102 isl_map_free(map2);
1103 isl_map_free(map);
1105 /* COCOA Theorem 1 counter example */
1106 map = isl_map_read_from_str(ctx,
1107 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1108 "i2 = 1 and j2 = j or "
1109 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
1110 map = isl_map_transitive_closure(map, &exact);
1111 assert(exact);
1112 isl_map_free(map);
1114 map = isl_map_read_from_str(ctx,
1115 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1116 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1117 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1118 "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
1119 map = isl_map_transitive_closure(map, &exact);
1120 assert(exact);
1121 isl_map_free(map);
1123 /* Kelly et al 1996, fig 12 */
1124 map = isl_map_read_from_str(ctx,
1125 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1126 "1 <= i,j,j+1 <= n or "
1127 "j = n and j2 = 1 and i2 = i + 1 and "
1128 "1 <= i,i+1 <= n }", -1);
1129 map = isl_map_transitive_closure(map, &exact);
1130 assert(exact);
1131 map2 = isl_map_read_from_str(ctx,
1132 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1133 "1 <= i <= n and i = i2 or "
1134 "1 <= i < i2 <= n and 1 <= j <= n and "
1135 "1 <= j2 <= n }", -1);
1136 assert(isl_map_is_equal(map, map2));
1137 isl_map_free(map2);
1138 isl_map_free(map);
1140 /* Omega's closure4 */
1141 map = isl_map_read_from_str(ctx,
1142 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1143 "1 <= x,y <= 10 or "
1144 "x2 = x + 1 and y2 = y and "
1145 "1 <= x <= 20 && 5 <= y <= 15 }", -1);
1146 map = isl_map_transitive_closure(map, &exact);
1147 assert(exact);
1148 isl_map_free(map);
1150 map = isl_map_read_from_str(ctx,
1151 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
1152 map = isl_map_transitive_closure(map, &exact);
1153 assert(!exact);
1154 map2 = isl_map_read_from_str(ctx,
1155 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
1156 assert(isl_map_is_equal(map, map2));
1157 isl_map_free(map);
1158 isl_map_free(map2);
1160 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1161 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1162 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1163 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1164 map = isl_map_read_from_str(ctx, str, -1);
1165 map = isl_map_transitive_closure(map, &exact);
1166 assert(exact);
1167 map2 = isl_map_read_from_str(ctx, str, -1);
1168 assert(isl_map_is_equal(map, map2));
1169 isl_map_free(map);
1170 isl_map_free(map2);
1172 str = "{[0] -> [1]; [2] -> [3]}";
1173 map = isl_map_read_from_str(ctx, str, -1);
1174 map = isl_map_transitive_closure(map, &exact);
1175 assert(exact);
1176 map2 = isl_map_read_from_str(ctx, str, -1);
1177 assert(isl_map_is_equal(map, map2));
1178 isl_map_free(map);
1179 isl_map_free(map2);
1181 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1182 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1183 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1184 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1185 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1186 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1187 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1188 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1189 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1190 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1191 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1192 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1193 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1194 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1195 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1196 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1197 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1198 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1199 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1200 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1201 map = isl_map_read_from_str(ctx, str, -1);
1202 map = isl_map_transitive_closure(map, NULL);
1203 assert(map);
1204 isl_map_free(map);
1207 void test_lex(struct isl_ctx *ctx)
1209 isl_dim *dim;
1210 isl_map *map;
1212 dim = isl_dim_alloc(ctx, 0, 0, 0);
1213 map = isl_map_lex_le(dim);
1214 assert(!isl_map_is_empty(map));
1215 isl_map_free(map);
1218 void test_lexmin(struct isl_ctx *ctx)
1220 const char *str;
1221 isl_map *map, *map2;
1222 isl_set *set;
1223 isl_set *set2;
1225 str = "[p0, p1] -> { [] -> [] : "
1226 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1227 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1228 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1229 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1230 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1231 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1232 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1233 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1234 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1235 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1236 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1237 map = isl_map_read_from_str(ctx, str, -1);
1238 map = isl_map_lexmin(map);
1239 isl_map_free(map);
1241 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1242 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1243 set = isl_set_read_from_str(ctx, str, -1);
1244 set = isl_set_lexmax(set);
1245 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1246 set2 = isl_set_read_from_str(ctx, str, -1);
1247 set = isl_set_intersect(set, set2);
1248 assert(!isl_set_is_empty(set));
1249 isl_set_free(set);
1251 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1252 map = isl_map_read_from_str(ctx, str, -1);
1253 map = isl_map_lexmin(map);
1254 str = "{ [x] -> [5] : 6 <= x <= 8; "
1255 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1256 map2 = isl_map_read_from_str(ctx, str, -1);
1257 assert(isl_map_is_equal(map, map2));
1258 isl_map_free(map);
1259 isl_map_free(map2);
1261 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1262 map = isl_map_read_from_str(ctx, str, -1);
1263 map2 = isl_map_copy(map);
1264 map = isl_map_lexmin(map);
1265 assert(isl_map_is_equal(map, map2));
1266 isl_map_free(map);
1267 isl_map_free(map2);
1269 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1270 map = isl_map_read_from_str(ctx, str, -1);
1271 map = isl_map_lexmin(map);
1272 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1273 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1274 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1275 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1276 map2 = isl_map_read_from_str(ctx, str, -1);
1277 assert(isl_map_is_equal(map, map2));
1278 isl_map_free(map);
1279 isl_map_free(map2);
1282 struct must_may {
1283 isl_map *must;
1284 isl_map *may;
1287 static int collect_must_may(__isl_take isl_map *dep, int must,
1288 void *dep_user, void *user)
1290 struct must_may *mm = (struct must_may *)user;
1292 if (must)
1293 mm->must = isl_map_union(mm->must, dep);
1294 else
1295 mm->may = isl_map_union(mm->may, dep);
1297 return 0;
1300 static int common_space(void *first, void *second)
1302 int depth = *(int *)first;
1303 return 2 * depth;
1306 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1308 isl_map *map2;
1309 int equal;
1311 if (!map)
1312 return -1;
1314 map2 = isl_map_read_from_str(map->ctx, str, -1);
1315 equal = isl_map_is_equal(map, map2);
1316 isl_map_free(map2);
1318 return equal;
1321 void test_dep(struct isl_ctx *ctx)
1323 const char *str;
1324 isl_dim *dim;
1325 isl_map *map;
1326 isl_access_info *ai;
1327 isl_flow *flow;
1328 int depth;
1329 struct must_may mm;
1331 depth = 3;
1333 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1334 map = isl_map_read_from_str(ctx, str, -1);
1335 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1337 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1338 map = isl_map_read_from_str(ctx, str, -1);
1339 ai = isl_access_info_add_source(ai, map, 1, &depth);
1341 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1342 map = isl_map_read_from_str(ctx, str, -1);
1343 ai = isl_access_info_add_source(ai, map, 1, &depth);
1345 flow = isl_access_info_compute_flow(ai);
1346 dim = isl_dim_alloc(ctx, 0, 3, 3);
1347 mm.must = isl_map_empty(isl_dim_copy(dim));
1348 mm.may = isl_map_empty(dim);
1350 isl_flow_foreach(flow, collect_must_may, &mm);
1352 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1353 " [1,10,0] -> [2,5,0] }";
1354 assert(map_is_equal(mm.must, str));
1355 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1356 assert(map_is_equal(mm.may, str));
1358 isl_map_free(mm.must);
1359 isl_map_free(mm.may);
1360 isl_flow_free(flow);
1363 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1364 map = isl_map_read_from_str(ctx, str, -1);
1365 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1367 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1368 map = isl_map_read_from_str(ctx, str, -1);
1369 ai = isl_access_info_add_source(ai, map, 1, &depth);
1371 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1372 map = isl_map_read_from_str(ctx, str, -1);
1373 ai = isl_access_info_add_source(ai, map, 0, &depth);
1375 flow = isl_access_info_compute_flow(ai);
1376 dim = isl_dim_alloc(ctx, 0, 3, 3);
1377 mm.must = isl_map_empty(isl_dim_copy(dim));
1378 mm.may = isl_map_empty(dim);
1380 isl_flow_foreach(flow, collect_must_may, &mm);
1382 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1383 assert(map_is_equal(mm.must, str));
1384 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1385 assert(map_is_equal(mm.may, str));
1387 isl_map_free(mm.must);
1388 isl_map_free(mm.may);
1389 isl_flow_free(flow);
1392 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1393 map = isl_map_read_from_str(ctx, str, -1);
1394 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1396 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1397 map = isl_map_read_from_str(ctx, str, -1);
1398 ai = isl_access_info_add_source(ai, map, 0, &depth);
1400 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1401 map = isl_map_read_from_str(ctx, str, -1);
1402 ai = isl_access_info_add_source(ai, map, 0, &depth);
1404 flow = isl_access_info_compute_flow(ai);
1405 dim = isl_dim_alloc(ctx, 0, 3, 3);
1406 mm.must = isl_map_empty(isl_dim_copy(dim));
1407 mm.may = isl_map_empty(dim);
1409 isl_flow_foreach(flow, collect_must_may, &mm);
1411 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1412 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1413 assert(map_is_equal(mm.may, str));
1414 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1415 assert(map_is_equal(mm.must, str));
1417 isl_map_free(mm.must);
1418 isl_map_free(mm.may);
1419 isl_flow_free(flow);
1422 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1423 map = isl_map_read_from_str(ctx, str, -1);
1424 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1426 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1427 map = isl_map_read_from_str(ctx, str, -1);
1428 ai = isl_access_info_add_source(ai, map, 0, &depth);
1430 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1431 map = isl_map_read_from_str(ctx, str, -1);
1432 ai = isl_access_info_add_source(ai, map, 0, &depth);
1434 flow = isl_access_info_compute_flow(ai);
1435 dim = isl_dim_alloc(ctx, 0, 3, 3);
1436 mm.must = isl_map_empty(isl_dim_copy(dim));
1437 mm.may = isl_map_empty(dim);
1439 isl_flow_foreach(flow, collect_must_may, &mm);
1441 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1442 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1443 assert(map_is_equal(mm.may, str));
1444 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1445 assert(map_is_equal(mm.must, str));
1447 isl_map_free(mm.must);
1448 isl_map_free(mm.may);
1449 isl_flow_free(flow);
1452 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1453 map = isl_map_read_from_str(ctx, str, -1);
1454 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1456 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1457 map = isl_map_read_from_str(ctx, str, -1);
1458 ai = isl_access_info_add_source(ai, map, 0, &depth);
1460 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1461 map = isl_map_read_from_str(ctx, str, -1);
1462 ai = isl_access_info_add_source(ai, map, 0, &depth);
1464 flow = isl_access_info_compute_flow(ai);
1465 dim = isl_dim_alloc(ctx, 0, 3, 3);
1466 mm.must = isl_map_empty(isl_dim_copy(dim));
1467 mm.may = isl_map_empty(dim);
1469 isl_flow_foreach(flow, collect_must_may, &mm);
1471 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1472 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1473 assert(map_is_equal(mm.may, str));
1474 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1475 assert(map_is_equal(mm.must, str));
1477 isl_map_free(mm.must);
1478 isl_map_free(mm.may);
1479 isl_flow_free(flow);
1482 depth = 5;
1484 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1485 map = isl_map_read_from_str(ctx, str, -1);
1486 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1488 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1489 map = isl_map_read_from_str(ctx, str, -1);
1490 ai = isl_access_info_add_source(ai, map, 1, &depth);
1492 flow = isl_access_info_compute_flow(ai);
1493 dim = isl_dim_alloc(ctx, 0, 5, 5);
1494 mm.must = isl_map_empty(isl_dim_copy(dim));
1495 mm.may = isl_map_empty(dim);
1497 isl_flow_foreach(flow, collect_must_may, &mm);
1499 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1500 assert(map_is_equal(mm.must, str));
1501 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1502 assert(map_is_equal(mm.may, str));
1504 isl_map_free(mm.must);
1505 isl_map_free(mm.may);
1506 isl_flow_free(flow);
1509 void test_sv(struct isl_ctx *ctx)
1511 const char *str;
1512 isl_map *map;
1514 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1515 map = isl_map_read_from_str(ctx, str, -1);
1516 assert(isl_map_is_single_valued(map));
1517 isl_map_free(map);
1519 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1520 map = isl_map_read_from_str(ctx, str, -1);
1521 assert(!isl_map_is_single_valued(map));
1522 isl_map_free(map);
1525 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1527 isl_map *map;
1529 map = isl_map_read_from_str(ctx, str, -1);
1530 if (bijective)
1531 assert(isl_map_is_bijective(map));
1532 else
1533 assert(!isl_map_is_bijective(map));
1534 isl_map_free(map);
1537 void test_bijective(struct isl_ctx *ctx)
1539 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1540 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1541 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1542 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1543 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1544 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1545 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1546 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1547 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1548 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1549 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1550 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1551 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1554 void test_pwqp(struct isl_ctx *ctx)
1556 const char *str;
1557 isl_set *set;
1558 isl_pw_qpolynomial *pwqp1, *pwqp2;
1560 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1561 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1563 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1564 isl_dim_set, 1, 1);
1566 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1567 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1569 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1571 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1573 isl_pw_qpolynomial_free(pwqp1);
1575 str = "{ [i] -> i }";
1576 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1577 str = "{ [k] : exists a : k = 2a }";
1578 set = isl_set_read_from_str(ctx, str, 0);
1579 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1580 str = "{ [i] -> i }";
1581 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1583 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1585 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1587 isl_pw_qpolynomial_free(pwqp1);
1589 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1590 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1591 str = "{ [10] }";
1592 set = isl_set_read_from_str(ctx, str, 0);
1593 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1594 str = "{ [i] -> 16 }";
1595 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1597 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1599 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1601 isl_pw_qpolynomial_free(pwqp1);
1603 str = "{ [i] -> ([(i)/2]) }";
1604 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1605 str = "{ [k] : exists a : k = 2a+1 }";
1606 set = isl_set_read_from_str(ctx, str, 0);
1607 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1608 str = "{ [i] -> -1/2 + 1/2 * i }";
1609 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1611 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1613 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1615 isl_pw_qpolynomial_free(pwqp1);
1617 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1618 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1619 str = "{ [i] -> ([(2 * [i/2])/5]) }";
1620 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1622 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1624 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1626 isl_pw_qpolynomial_free(pwqp1);
1628 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1629 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1630 str = "{ [x] -> x }";
1631 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1633 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1635 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1637 isl_pw_qpolynomial_free(pwqp1);
1639 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1640 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1641 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1642 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1643 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1644 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1645 isl_pw_qpolynomial_free(pwqp1);
1648 void test_split_periods(isl_ctx *ctx)
1650 const char *str;
1651 isl_pw_qpolynomial *pwqp;
1653 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1654 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1655 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1656 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1658 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1659 assert(pwqp);
1661 isl_pw_qpolynomial_free(pwqp);
1664 void test_union(isl_ctx *ctx)
1666 const char *str;
1667 isl_union_set *uset1, *uset2;
1668 isl_union_map *umap1, *umap2;
1670 str = "{ [i] : 0 <= i <= 1 }";
1671 uset1 = isl_union_set_read_from_str(ctx, str);
1672 str = "{ [1] -> [0] }";
1673 umap1 = isl_union_map_read_from_str(ctx, str);
1675 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1676 assert(isl_union_map_is_equal(umap1, umap2));
1678 isl_union_map_free(umap1);
1679 isl_union_map_free(umap2);
1681 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1682 umap1 = isl_union_map_read_from_str(ctx, str);
1683 str = "{ A[i]; B[i] }";
1684 uset1 = isl_union_set_read_from_str(ctx, str);
1686 uset2 = isl_union_map_domain(umap1);
1688 assert(isl_union_set_is_equal(uset1, uset2));
1690 isl_union_set_free(uset1);
1691 isl_union_set_free(uset2);
1694 void test_bound(isl_ctx *ctx)
1696 const char *str;
1697 isl_pw_qpolynomial *pwqp;
1698 isl_pw_qpolynomial_fold *pwf;
1700 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1701 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1702 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1703 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 4);
1704 isl_pw_qpolynomial_fold_free(pwf);
1706 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1707 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1708 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1709 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 1);
1710 isl_pw_qpolynomial_fold_free(pwf);
1713 void test_lift(isl_ctx *ctx)
1715 const char *str;
1716 isl_basic_map *bmap;
1717 isl_basic_set *bset;
1719 str = "{ [i0] : exists e0 : i0 = 4e0 }";
1720 bset = isl_basic_set_read_from_str(ctx, str, 0);
1721 bset = isl_basic_set_lift(bset);
1722 bmap = isl_basic_map_from_range(bset);
1723 bset = isl_basic_map_domain(bmap);
1724 isl_basic_set_free(bset);
1727 void test_subset(isl_ctx *ctx)
1729 const char *str;
1730 isl_set *set1, *set2;
1732 str = "{ [112, 0] }";
1733 set1 = isl_set_read_from_str(ctx, str, 0);
1734 str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1735 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1736 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1737 set2 = isl_set_read_from_str(ctx, str, 0);
1738 assert(isl_set_is_subset(set1, set2));
1739 isl_set_free(set1);
1740 isl_set_free(set2);
1743 void test_factorize(isl_ctx *ctx)
1745 const char *str;
1746 isl_basic_set *bset;
1747 isl_factorizer *f;
1749 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1750 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1751 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1752 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1753 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1754 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1755 "3i5 >= -2i0 - i2 + 3i4 }";
1756 bset = isl_basic_set_read_from_str(ctx, str, 0);
1757 f = isl_basic_set_factorizer(bset);
1758 assert(f);
1759 isl_basic_set_free(bset);
1760 isl_factorizer_free(f);
1762 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1763 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
1764 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
1765 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
1766 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
1767 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
1768 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
1769 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
1770 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
1771 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
1772 bset = isl_basic_set_read_from_str(ctx, str, 0);
1773 f = isl_basic_set_factorizer(bset);
1774 assert(f);
1775 isl_basic_set_free(bset);
1776 isl_factorizer_free(f);
1779 int main()
1781 struct isl_ctx *ctx;
1783 srcdir = getenv("srcdir");
1784 assert(srcdir);
1786 ctx = isl_ctx_alloc();
1787 test_factorize(ctx);
1788 test_subset(ctx);
1789 test_lift(ctx);
1790 test_bound(ctx);
1791 test_union(ctx);
1792 test_split_periods(ctx);
1793 test_parse(ctx);
1794 test_pwqp(ctx);
1795 test_lex(ctx);
1796 test_sv(ctx);
1797 test_bijective(ctx);
1798 test_dep(ctx);
1799 test_read(ctx);
1800 test_bounded(ctx);
1801 test_construction(ctx);
1802 test_dim(ctx);
1803 test_div(ctx);
1804 test_application(ctx);
1805 test_affine_hull(ctx);
1806 test_convex_hull(ctx);
1807 test_gist(ctx);
1808 test_coalesce(ctx);
1809 test_closure(ctx);
1810 test_lexmin(ctx);
1811 isl_ctx_free(ctx);
1812 return 0;