c6cd583a3c58ef3a426cf290976e5b848a1c49bf
[platform/upstream/isl.git] / isl_test.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
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/aff.h>
16 #include <isl/set.h>
17 #include <isl/flow.h>
18 #include <isl/constraint.h>
19 #include <isl/polynomial.h>
20 #include <isl/union_map.h>
21 #include <isl_factorization.h>
22 #include <isl/schedule.h>
23 #include <isl_options_private.h>
24 #include <isl/vertices.h>
25
26 static char *srcdir;
27
28 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
29         char *filename;
30         int length;
31         char *pattern = "%s/test_inputs/%s.%s";
32
33         length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
34                 + strlen(suffix) + 1;
35         filename = isl_alloc_array(ctx, char, length);
36
37         if (!filename)
38                 return NULL;
39
40         sprintf(filename, pattern, srcdir, name, suffix);
41
42         return filename;
43 }
44
45 void test_parse_map(isl_ctx *ctx, const char *str)
46 {
47         isl_map *map;
48
49         map = isl_map_read_from_str(ctx, str);
50         assert(map);
51         isl_map_free(map);
52 }
53
54 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
55 {
56         isl_map *map, *map2;
57         int equal;
58
59         map = isl_map_read_from_str(ctx, str);
60         map2 = isl_map_read_from_str(ctx, str2);
61         equal = isl_map_is_equal(map, map2);
62         isl_map_free(map);
63         isl_map_free(map2);
64
65         if (equal < 0)
66                 return -1;
67         if (!equal)
68                 isl_die(ctx, isl_error_unknown, "maps not equal",
69                         return -1);
70
71         return 0;
72 }
73
74 void test_parse_pwqp(isl_ctx *ctx, const char *str)
75 {
76         isl_pw_qpolynomial *pwqp;
77
78         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
79         assert(pwqp);
80         isl_pw_qpolynomial_free(pwqp);
81 }
82
83 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
84 {
85         isl_pw_aff *pwaff;
86
87         pwaff = isl_pw_aff_read_from_str(ctx, str);
88         assert(pwaff);
89         isl_pw_aff_free(pwaff);
90 }
91
92 int test_parse(struct isl_ctx *ctx)
93 {
94         isl_map *map, *map2;
95         const char *str, *str2;
96
97         str = "{ [i] -> [-i] }";
98         map = isl_map_read_from_str(ctx, str);
99         assert(map);
100         isl_map_free(map);
101
102         str = "{ A[i] -> L[([i/3])] }";
103         map = isl_map_read_from_str(ctx, str);
104         assert(map);
105         isl_map_free(map);
106
107         test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
108         test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
109                                 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
110
111         str = "{ [x,y]  : [([x/2]+y)/3] >= 1 }";
112         str2 = "{ [x, y] : 2y >= 6 - x }";
113         if (test_parse_map_equal(ctx, str, str2) < 0)
114                 return -1;
115
116         if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
117                                       "{ [x,y] : x <= y, 2*y + 3 }") < 0)
118                 return -1;
119         str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
120         if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
121                                         str) < 0)
122                 return -1;
123
124         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
125         map = isl_map_read_from_str(ctx, str);
126         str = "{ [new, old] -> [o0, o1] : "
127                "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
128                "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
129                "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
130         map2 = isl_map_read_from_str(ctx, str);
131         assert(isl_map_is_equal(map, map2));
132         isl_map_free(map);
133         isl_map_free(map2);
134
135         str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
136         map = isl_map_read_from_str(ctx, str);
137         str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
138         map2 = isl_map_read_from_str(ctx, str);
139         assert(isl_map_is_equal(map, map2));
140         isl_map_free(map);
141         isl_map_free(map2);
142
143         str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
144         str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
145         if (test_parse_map_equal(ctx, str, str2) < 0)
146                 return -1;
147
148         str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
149         str2 = "{ [i,j] -> [min(i,j)] }";
150         if (test_parse_map_equal(ctx, str, str2) < 0)
151                 return -1;
152
153         str = "{ [i,j] : i != j }";
154         str2 = "{ [i,j] : i < j or i > j }";
155         if (test_parse_map_equal(ctx, str, str2) < 0)
156                 return -1;
157
158         str = "{ [i,j] : (i+1)*2 >= j }";
159         str2 = "{ [i, j] : j <= 2 + 2i }";
160         if (test_parse_map_equal(ctx, str, str2) < 0)
161                 return -1;
162
163         str = "{ [i] -> [i > 0 ? 4 : 5] }";
164         str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
165         if (test_parse_map_equal(ctx, str, str2) < 0)
166                 return -1;
167
168         str = "[N=2,M] -> { [i=[(M+N)/4]] }";
169         str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
170         if (test_parse_map_equal(ctx, str, str2) < 0)
171                 return -1;
172
173         str = "{ [x] : x >= 0 }";
174         str2 = "{ [x] : x-0 >= 0 }";
175         if (test_parse_map_equal(ctx, str, str2) < 0)
176                 return -1;
177
178         str = "{ [i] : ((i > 10)) }";
179         str2 = "{ [i] : i >= 11 }";
180         if (test_parse_map_equal(ctx, str, str2) < 0)
181                 return -1;
182
183         str = "{ [i] -> [0] }";
184         str2 = "{ [i] -> [0 * i] }";
185         if (test_parse_map_equal(ctx, str, str2) < 0)
186                 return -1;
187
188         test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
189         test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
190         test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
191         test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
192
193         return 0;
194 }
195
196 void test_read(struct isl_ctx *ctx)
197 {
198         char *filename;
199         FILE *input;
200         struct isl_basic_set *bset1, *bset2;
201         const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
202
203         filename = get_filename(ctx, "set", "omega");
204         assert(filename);
205         input = fopen(filename, "r");
206         assert(input);
207
208         bset1 = isl_basic_set_read_from_file(ctx, input);
209         bset2 = isl_basic_set_read_from_str(ctx, str);
210
211         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
212
213         isl_basic_set_free(bset1);
214         isl_basic_set_free(bset2);
215         free(filename);
216
217         fclose(input);
218 }
219
220 void test_bounded(struct isl_ctx *ctx)
221 {
222         isl_set *set;
223         int bounded;
224
225         set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
226         bounded = isl_set_is_bounded(set);
227         assert(bounded);
228         isl_set_free(set);
229
230         set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
231         bounded = isl_set_is_bounded(set);
232         assert(!bounded);
233         isl_set_free(set);
234
235         set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
236         bounded = isl_set_is_bounded(set);
237         assert(!bounded);
238         isl_set_free(set);
239 }
240
241 /* Construct the basic set { [i] : 5 <= i <= N } */
242 void test_construction(struct isl_ctx *ctx)
243 {
244         isl_int v;
245         isl_space *dim;
246         isl_local_space *ls;
247         struct isl_basic_set *bset;
248         struct isl_constraint *c;
249
250         isl_int_init(v);
251
252         dim = isl_space_set_alloc(ctx, 1, 1);
253         bset = isl_basic_set_universe(isl_space_copy(dim));
254         ls = isl_local_space_from_space(dim);
255
256         c = isl_inequality_alloc(isl_local_space_copy(ls));
257         isl_int_set_si(v, -1);
258         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
259         isl_int_set_si(v, 1);
260         isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
261         bset = isl_basic_set_add_constraint(bset, c);
262
263         c = isl_inequality_alloc(isl_local_space_copy(ls));
264         isl_int_set_si(v, 1);
265         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
266         isl_int_set_si(v, -5);
267         isl_constraint_set_constant(c, v);
268         bset = isl_basic_set_add_constraint(bset, c);
269
270         isl_local_space_free(ls);
271         isl_basic_set_free(bset);
272
273         isl_int_clear(v);
274 }
275
276 void test_dim(struct isl_ctx *ctx)
277 {
278         const char *str;
279         isl_map *map1, *map2;
280
281         map1 = isl_map_read_from_str(ctx,
282             "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
283         map1 = isl_map_add_dims(map1, isl_dim_in, 1);
284         map2 = isl_map_read_from_str(ctx,
285             "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
286         assert(isl_map_is_equal(map1, map2));
287         isl_map_free(map2);
288
289         map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
290         map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
291         assert(isl_map_is_equal(map1, map2));
292
293         isl_map_free(map1);
294         isl_map_free(map2);
295
296         str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
297         map1 = isl_map_read_from_str(ctx, str);
298         str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
299         map2 = isl_map_read_from_str(ctx, str);
300         map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
301         assert(isl_map_is_equal(map1, map2));
302
303         isl_map_free(map1);
304         isl_map_free(map2);
305 }
306
307 void test_div(struct isl_ctx *ctx)
308 {
309         isl_int v;
310         isl_space *dim;
311         isl_local_space *ls;
312         struct isl_basic_set *bset;
313         struct isl_constraint *c;
314
315         isl_int_init(v);
316
317         /* test 1 */
318         dim = isl_space_set_alloc(ctx, 0, 3);
319         bset = isl_basic_set_universe(isl_space_copy(dim));
320         ls = isl_local_space_from_space(dim);
321
322         c = isl_equality_alloc(isl_local_space_copy(ls));
323         isl_int_set_si(v, -1);
324         isl_constraint_set_constant(c, v);
325         isl_int_set_si(v, 1);
326         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
327         isl_int_set_si(v, 3);
328         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
329         bset = isl_basic_set_add_constraint(bset, c);
330
331         c = isl_equality_alloc(isl_local_space_copy(ls));
332         isl_int_set_si(v, 1);
333         isl_constraint_set_constant(c, v);
334         isl_int_set_si(v, -1);
335         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
336         isl_int_set_si(v, 3);
337         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
338         bset = isl_basic_set_add_constraint(bset, c);
339
340         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
341
342         assert(bset && bset->n_div == 1);
343         isl_local_space_free(ls);
344         isl_basic_set_free(bset);
345
346         /* test 2 */
347         dim = isl_space_set_alloc(ctx, 0, 3);
348         bset = isl_basic_set_universe(isl_space_copy(dim));
349         ls = isl_local_space_from_space(dim);
350
351         c = isl_equality_alloc(isl_local_space_copy(ls));
352         isl_int_set_si(v, 1);
353         isl_constraint_set_constant(c, v);
354         isl_int_set_si(v, -1);
355         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
356         isl_int_set_si(v, 3);
357         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
358         bset = isl_basic_set_add_constraint(bset, c);
359
360         c = isl_equality_alloc(isl_local_space_copy(ls));
361         isl_int_set_si(v, -1);
362         isl_constraint_set_constant(c, v);
363         isl_int_set_si(v, 1);
364         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
365         isl_int_set_si(v, 3);
366         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
367         bset = isl_basic_set_add_constraint(bset, c);
368
369         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
370
371         assert(bset && bset->n_div == 1);
372         isl_local_space_free(ls);
373         isl_basic_set_free(bset);
374
375         /* test 3 */
376         dim = isl_space_set_alloc(ctx, 0, 3);
377         bset = isl_basic_set_universe(isl_space_copy(dim));
378         ls = isl_local_space_from_space(dim);
379
380         c = isl_equality_alloc(isl_local_space_copy(ls));
381         isl_int_set_si(v, 1);
382         isl_constraint_set_constant(c, v);
383         isl_int_set_si(v, -1);
384         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
385         isl_int_set_si(v, 3);
386         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
387         bset = isl_basic_set_add_constraint(bset, c);
388
389         c = isl_equality_alloc(isl_local_space_copy(ls));
390         isl_int_set_si(v, -3);
391         isl_constraint_set_constant(c, v);
392         isl_int_set_si(v, 1);
393         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
394         isl_int_set_si(v, 4);
395         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
396         bset = isl_basic_set_add_constraint(bset, c);
397
398         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
399
400         assert(bset && bset->n_div == 1);
401         isl_local_space_free(ls);
402         isl_basic_set_free(bset);
403
404         /* test 4 */
405         dim = isl_space_set_alloc(ctx, 0, 3);
406         bset = isl_basic_set_universe(isl_space_copy(dim));
407         ls = isl_local_space_from_space(dim);
408
409         c = isl_equality_alloc(isl_local_space_copy(ls));
410         isl_int_set_si(v, 2);
411         isl_constraint_set_constant(c, v);
412         isl_int_set_si(v, -1);
413         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
414         isl_int_set_si(v, 3);
415         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
416         bset = isl_basic_set_add_constraint(bset, c);
417
418         c = isl_equality_alloc(isl_local_space_copy(ls));
419         isl_int_set_si(v, -1);
420         isl_constraint_set_constant(c, v);
421         isl_int_set_si(v, 1);
422         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
423         isl_int_set_si(v, 6);
424         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
425         bset = isl_basic_set_add_constraint(bset, c);
426
427         bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
428
429         assert(isl_basic_set_is_empty(bset));
430         isl_local_space_free(ls);
431         isl_basic_set_free(bset);
432
433         /* test 5 */
434         dim = isl_space_set_alloc(ctx, 0, 3);
435         bset = isl_basic_set_universe(isl_space_copy(dim));
436         ls = isl_local_space_from_space(dim);
437
438         c = isl_equality_alloc(isl_local_space_copy(ls));
439         isl_int_set_si(v, -1);
440         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
441         isl_int_set_si(v, 3);
442         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
443         bset = isl_basic_set_add_constraint(bset, c);
444
445         c = isl_equality_alloc(isl_local_space_copy(ls));
446         isl_int_set_si(v, 1);
447         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
448         isl_int_set_si(v, -3);
449         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
450         bset = isl_basic_set_add_constraint(bset, c);
451
452         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
453
454         assert(bset && bset->n_div == 0);
455         isl_basic_set_free(bset);
456         isl_local_space_free(ls);
457
458         /* test 6 */
459         dim = isl_space_set_alloc(ctx, 0, 3);
460         bset = isl_basic_set_universe(isl_space_copy(dim));
461         ls = isl_local_space_from_space(dim);
462
463         c = isl_equality_alloc(isl_local_space_copy(ls));
464         isl_int_set_si(v, -1);
465         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
466         isl_int_set_si(v, 6);
467         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
468         bset = isl_basic_set_add_constraint(bset, c);
469
470         c = isl_equality_alloc(isl_local_space_copy(ls));
471         isl_int_set_si(v, 1);
472         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
473         isl_int_set_si(v, -3);
474         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
475         bset = isl_basic_set_add_constraint(bset, c);
476
477         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
478
479         assert(bset && bset->n_div == 1);
480         isl_basic_set_free(bset);
481         isl_local_space_free(ls);
482
483         /* test 7 */
484         /* This test is a bit tricky.  We set up an equality
485          *              a + 3b + 3c = 6 e0
486          * Normalization of divs creates _two_ divs
487          *              a = 3 e0
488          *              c - b - e0 = 2 e1
489          * Afterwards e0 is removed again because it has coefficient -1
490          * and we end up with the original equality and div again.
491          * Perhaps we can avoid the introduction of this temporary div.
492          */
493         dim = isl_space_set_alloc(ctx, 0, 4);
494         bset = isl_basic_set_universe(isl_space_copy(dim));
495         ls = isl_local_space_from_space(dim);
496
497         c = isl_equality_alloc(isl_local_space_copy(ls));
498         isl_int_set_si(v, -1);
499         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
500         isl_int_set_si(v, -3);
501         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
502         isl_int_set_si(v, -3);
503         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
504         isl_int_set_si(v, 6);
505         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
506         bset = isl_basic_set_add_constraint(bset, c);
507
508         bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
509
510         /* Test disabled for now */
511         /*
512         assert(bset && bset->n_div == 1);
513         */
514         isl_local_space_free(ls);
515         isl_basic_set_free(bset);
516
517         /* test 8 */
518         dim = isl_space_set_alloc(ctx, 0, 5);
519         bset = isl_basic_set_universe(isl_space_copy(dim));
520         ls = isl_local_space_from_space(dim);
521
522         c = isl_equality_alloc(isl_local_space_copy(ls));
523         isl_int_set_si(v, -1);
524         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
525         isl_int_set_si(v, -3);
526         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
527         isl_int_set_si(v, -3);
528         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
529         isl_int_set_si(v, 6);
530         isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
531         bset = isl_basic_set_add_constraint(bset, c);
532
533         c = isl_equality_alloc(isl_local_space_copy(ls));
534         isl_int_set_si(v, -1);
535         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
536         isl_int_set_si(v, 1);
537         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
538         isl_int_set_si(v, 1);
539         isl_constraint_set_constant(c, v);
540         bset = isl_basic_set_add_constraint(bset, c);
541
542         bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
543
544         /* Test disabled for now */
545         /*
546         assert(bset && bset->n_div == 1);
547         */
548         isl_local_space_free(ls);
549         isl_basic_set_free(bset);
550
551         /* test 9 */
552         dim = isl_space_set_alloc(ctx, 0, 4);
553         bset = isl_basic_set_universe(isl_space_copy(dim));
554         ls = isl_local_space_from_space(dim);
555
556         c = isl_equality_alloc(isl_local_space_copy(ls));
557         isl_int_set_si(v, 1);
558         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
559         isl_int_set_si(v, -1);
560         isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
561         isl_int_set_si(v, -2);
562         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
563         bset = isl_basic_set_add_constraint(bset, c);
564
565         c = isl_equality_alloc(isl_local_space_copy(ls));
566         isl_int_set_si(v, -1);
567         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
568         isl_int_set_si(v, 3);
569         isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
570         isl_int_set_si(v, 2);
571         isl_constraint_set_constant(c, v);
572         bset = isl_basic_set_add_constraint(bset, c);
573
574         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
575
576         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
577
578         assert(!isl_basic_set_is_empty(bset));
579
580         isl_local_space_free(ls);
581         isl_basic_set_free(bset);
582
583         /* test 10 */
584         dim = isl_space_set_alloc(ctx, 0, 3);
585         bset = isl_basic_set_universe(isl_space_copy(dim));
586         ls = isl_local_space_from_space(dim);
587
588         c = isl_equality_alloc(isl_local_space_copy(ls));
589         isl_int_set_si(v, 1);
590         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
591         isl_int_set_si(v, -2);
592         isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
593         bset = isl_basic_set_add_constraint(bset, c);
594
595         bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
596
597         bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
598
599         isl_local_space_free(ls);
600         isl_basic_set_free(bset);
601
602         isl_int_clear(v);
603 }
604
605 void test_application_case(struct isl_ctx *ctx, const char *name)
606 {
607         char *filename;
608         FILE *input;
609         struct isl_basic_set *bset1, *bset2;
610         struct isl_basic_map *bmap;
611
612         filename = get_filename(ctx, name, "omega");
613         assert(filename);
614         input = fopen(filename, "r");
615         assert(input);
616
617         bset1 = isl_basic_set_read_from_file(ctx, input);
618         bmap = isl_basic_map_read_from_file(ctx, input);
619
620         bset1 = isl_basic_set_apply(bset1, bmap);
621
622         bset2 = isl_basic_set_read_from_file(ctx, input);
623
624         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
625
626         isl_basic_set_free(bset1);
627         isl_basic_set_free(bset2);
628         free(filename);
629
630         fclose(input);
631 }
632
633 void test_application(struct isl_ctx *ctx)
634 {
635         test_application_case(ctx, "application");
636         test_application_case(ctx, "application2");
637 }
638
639 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
640 {
641         char *filename;
642         FILE *input;
643         struct isl_basic_set *bset1, *bset2;
644
645         filename = get_filename(ctx, name, "polylib");
646         assert(filename);
647         input = fopen(filename, "r");
648         assert(input);
649
650         bset1 = isl_basic_set_read_from_file(ctx, input);
651         bset2 = isl_basic_set_read_from_file(ctx, input);
652
653         bset1 = isl_basic_set_affine_hull(bset1);
654
655         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
656
657         isl_basic_set_free(bset1);
658         isl_basic_set_free(bset2);
659         free(filename);
660
661         fclose(input);
662 }
663
664 int test_affine_hull(struct isl_ctx *ctx)
665 {
666         const char *str;
667         isl_set *set;
668         isl_basic_set *bset;
669         int n;
670
671         test_affine_hull_case(ctx, "affine2");
672         test_affine_hull_case(ctx, "affine");
673         test_affine_hull_case(ctx, "affine3");
674
675         str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
676                         "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
677                         "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
678                         "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
679         set = isl_set_read_from_str(ctx, str);
680         bset = isl_set_affine_hull(set);
681         n = isl_basic_set_dim(bset, isl_dim_div);
682         isl_basic_set_free(bset);
683         if (n != 0)
684                 isl_die(ctx, isl_error_unknown, "not expecting any divs",
685                         return -1);
686
687         return 0;
688 }
689
690 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
691 {
692         char *filename;
693         FILE *input;
694         struct isl_basic_set *bset1, *bset2;
695         struct isl_set *set;
696
697         filename = get_filename(ctx, name, "polylib");
698         assert(filename);
699         input = fopen(filename, "r");
700         assert(input);
701
702         bset1 = isl_basic_set_read_from_file(ctx, input);
703         bset2 = isl_basic_set_read_from_file(ctx, input);
704
705         set = isl_basic_set_union(bset1, bset2);
706         bset1 = isl_set_convex_hull(set);
707
708         bset2 = isl_basic_set_read_from_file(ctx, input);
709
710         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
711
712         isl_basic_set_free(bset1);
713         isl_basic_set_free(bset2);
714         free(filename);
715
716         fclose(input);
717 }
718
719 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
720 {
721         const char *str1, *str2;
722         isl_set *set1, *set2;
723         int orig_convex = ctx->opt->convex;
724         ctx->opt->convex = convex;
725
726         test_convex_hull_case(ctx, "convex0");
727         test_convex_hull_case(ctx, "convex1");
728         test_convex_hull_case(ctx, "convex2");
729         test_convex_hull_case(ctx, "convex3");
730         test_convex_hull_case(ctx, "convex4");
731         test_convex_hull_case(ctx, "convex5");
732         test_convex_hull_case(ctx, "convex6");
733         test_convex_hull_case(ctx, "convex7");
734         test_convex_hull_case(ctx, "convex8");
735         test_convex_hull_case(ctx, "convex9");
736         test_convex_hull_case(ctx, "convex10");
737         test_convex_hull_case(ctx, "convex11");
738         test_convex_hull_case(ctx, "convex12");
739         test_convex_hull_case(ctx, "convex13");
740         test_convex_hull_case(ctx, "convex14");
741         test_convex_hull_case(ctx, "convex15");
742
743         str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
744                "(i0 = 1 and i1 = 0 and i2 = 1) or "
745                "(i0 = 0 and i1 = 0 and i2 = 0) }";
746         str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
747         set1 = isl_set_read_from_str(ctx, str1);
748         set2 = isl_set_read_from_str(ctx, str2);
749         set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
750         assert(isl_set_is_equal(set1, set2));
751         isl_set_free(set1);
752         isl_set_free(set2);
753
754         ctx->opt->convex = orig_convex;
755 }
756
757 void test_convex_hull(struct isl_ctx *ctx)
758 {
759         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
760         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
761 }
762
763 void test_gist_case(struct isl_ctx *ctx, const char *name)
764 {
765         char *filename;
766         FILE *input;
767         struct isl_basic_set *bset1, *bset2;
768
769         filename = get_filename(ctx, name, "polylib");
770         assert(filename);
771         input = fopen(filename, "r");
772         assert(input);
773
774         bset1 = isl_basic_set_read_from_file(ctx, input);
775         bset2 = isl_basic_set_read_from_file(ctx, input);
776
777         bset1 = isl_basic_set_gist(bset1, bset2);
778
779         bset2 = isl_basic_set_read_from_file(ctx, input);
780
781         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
782
783         isl_basic_set_free(bset1);
784         isl_basic_set_free(bset2);
785         free(filename);
786
787         fclose(input);
788 }
789
790 void test_gist(struct isl_ctx *ctx)
791 {
792         const char *str;
793         isl_basic_set *bset1, *bset2;
794
795         test_gist_case(ctx, "gist1");
796
797         str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
798             "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
799             "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
800             "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
801             "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
802             "16e0 >= 16 + 16p6 + 15p10 and  p2 <= 15 and p3 >= 0 and "
803             "p3 <= 31 and  p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
804             "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
805             "p10 <= 15 and p10 <= -1 + p0 - p6) }";
806         bset1 = isl_basic_set_read_from_str(ctx, str);
807         str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
808             "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
809             "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
810             "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
811             "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
812             "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
813             "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
814             "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
815         bset2 = isl_basic_set_read_from_str(ctx, str);
816         bset1 = isl_basic_set_gist(bset1, bset2);
817         assert(bset1 && bset1->n_div == 0);
818         isl_basic_set_free(bset1);
819 }
820
821 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
822 {
823         isl_set *set, *set2;
824         int equal;
825         int one;
826
827         set = isl_set_read_from_str(ctx, str);
828         set = isl_set_coalesce(set);
829         set2 = isl_set_read_from_str(ctx, str);
830         equal = isl_set_is_equal(set, set2);
831         one = set && set->n == 1;
832         isl_set_free(set);
833         isl_set_free(set2);
834
835         if (equal < 0)
836                 return -1;
837         if (!equal)
838                 isl_die(ctx, isl_error_unknown,
839                         "coalesced set not equal to input", return -1);
840         if (check_one && !one)
841                 isl_die(ctx, isl_error_unknown,
842                         "coalesced set should not be a union", return -1);
843
844         return 0;
845 }
846
847 int test_coalesce(struct isl_ctx *ctx)
848 {
849         const char *str;
850         struct isl_set *set, *set2;
851         struct isl_map *map, *map2;
852
853         set = isl_set_read_from_str(ctx,
854                 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
855                        "y >= x & x >= 2 & 5 >= y }");
856         set = isl_set_coalesce(set);
857         assert(set && set->n == 1);
858         isl_set_free(set);
859
860         set = isl_set_read_from_str(ctx,
861                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
862                        "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}");
863         set = isl_set_coalesce(set);
864         assert(set && set->n == 1);
865         isl_set_free(set);
866
867         set = isl_set_read_from_str(ctx,
868                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
869                        "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}");
870         set = isl_set_coalesce(set);
871         assert(set && set->n == 2);
872         isl_set_free(set);
873
874         set = isl_set_read_from_str(ctx,
875                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
876                        "y >= 0 & x >= 6 & x <= 10 & y <= x}");
877         set = isl_set_coalesce(set);
878         assert(set && set->n == 1);
879         isl_set_free(set);
880
881         set = isl_set_read_from_str(ctx,
882                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
883                        "y >= 0 & x >= 7 & x <= 10 & y <= x}");
884         set = isl_set_coalesce(set);
885         assert(set && set->n == 2);
886         isl_set_free(set);
887
888         set = isl_set_read_from_str(ctx,
889                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
890                        "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}");
891         set = isl_set_coalesce(set);
892         assert(set && set->n == 2);
893         isl_set_free(set);
894
895         set = isl_set_read_from_str(ctx,
896                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
897                        "y >= 0 & x = 6 & y <= 6}");
898         set = isl_set_coalesce(set);
899         assert(set && set->n == 1);
900         isl_set_free(set);
901
902         set = isl_set_read_from_str(ctx,
903                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
904                        "y >= 0 & x = 7 & y <= 6}");
905         set = isl_set_coalesce(set);
906         assert(set && set->n == 2);
907         isl_set_free(set);
908
909         set = isl_set_read_from_str(ctx,
910                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
911                        "y >= 0 & x = 6 & y <= 5}");
912         set = isl_set_coalesce(set);
913         assert(set && set->n == 1);
914         set2 = isl_set_read_from_str(ctx,
915                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
916                        "y >= 0 & x = 6 & y <= 5}");
917         assert(isl_set_is_equal(set, set2));
918         isl_set_free(set);
919         isl_set_free(set2);
920
921         set = isl_set_read_from_str(ctx,
922                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
923                        "y >= 0 & x = 6 & y <= 7}");
924         set = isl_set_coalesce(set);
925         assert(set && set->n == 2);
926         isl_set_free(set);
927
928         set = isl_set_read_from_str(ctx,
929                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
930         set = isl_set_coalesce(set);
931         assert(set && set->n == 1);
932         set2 = isl_set_read_from_str(ctx,
933                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
934         assert(isl_set_is_equal(set, set2));
935         isl_set_free(set);
936         isl_set_free(set2);
937
938         set = isl_set_read_from_str(ctx,
939                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
940         set = isl_set_coalesce(set);
941         set2 = isl_set_read_from_str(ctx,
942                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
943         assert(isl_set_is_equal(set, set2));
944         isl_set_free(set);
945         isl_set_free(set2);
946
947         set = isl_set_read_from_str(ctx,
948                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
949                                 "2 <= i and i <= n }");
950         set = isl_set_coalesce(set);
951         assert(set && set->n == 1);
952         set2 = isl_set_read_from_str(ctx,
953                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
954                                 "2 <= i and i <= n }");
955         assert(isl_set_is_equal(set, set2));
956         isl_set_free(set);
957         isl_set_free(set2);
958
959         map = isl_map_read_from_str(ctx,
960                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
961                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
962                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
963                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
964                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
965                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
966                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
967                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
968                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
969                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
970                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
971                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
972                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
973                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
974                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
975                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
976                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
977                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
978         map = isl_map_coalesce(map);
979         map2 = isl_map_read_from_str(ctx,
980                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
981                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
982                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
983                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
984                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
985                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
986                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
987                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
988                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
989                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
990                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
991                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
992                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
993                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
994                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
995                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
996                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
997                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
998         assert(isl_map_is_equal(map, map2));
999         isl_map_free(map);
1000         isl_map_free(map2);
1001
1002         str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1003               "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1004               "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1005               "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
1006         map = isl_map_read_from_str(ctx, str);
1007         map = isl_map_coalesce(map);
1008         map2 = isl_map_read_from_str(ctx, str);
1009         assert(isl_map_is_equal(map, map2));
1010         isl_map_free(map);
1011         isl_map_free(map2);
1012
1013         str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
1014           "[o0, o1, o2, o3, o4, o5, o6] : "
1015           "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1016           "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1017           "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1018           "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1019           "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1020           "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1021           "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1022           "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1023           "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1024           "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1025           "o6 >= i3 + i6 - o3 and M >= 0 and "
1026           "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1027           "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
1028         map = isl_map_read_from_str(ctx, str);
1029         map = isl_map_coalesce(map);
1030         map2 = isl_map_read_from_str(ctx, str);
1031         assert(isl_map_is_equal(map, map2));
1032         isl_map_free(map);
1033         isl_map_free(map2);
1034
1035         str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1036                 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1037                 "(o0 = 0 and M >= 2 and N >= 3) or "
1038                 "(M = 0 and o0 = 0 and N >= 3) }";
1039         map = isl_map_read_from_str(ctx, str);
1040         map = isl_map_coalesce(map);
1041         map2 = isl_map_read_from_str(ctx, str);
1042         assert(isl_map_is_equal(map, map2));
1043         isl_map_free(map);
1044         isl_map_free(map2);
1045
1046         str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1047                 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1048                 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1049                 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
1050         map = isl_map_read_from_str(ctx, str);
1051         map = isl_map_coalesce(map);
1052         map2 = isl_map_read_from_str(ctx, str);
1053         assert(isl_map_is_equal(map, map2));
1054         isl_map_free(map);
1055         isl_map_free(map2);
1056
1057         test_coalesce_set(ctx,
1058                 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
1059                                 "(i1 = M and M >= 1) }", 0);
1060         test_coalesce_set(ctx,
1061                 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
1062         test_coalesce_set(ctx,
1063                 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1064                 "(y = 3 and x = 1) }", 1);
1065         test_coalesce_set(ctx,
1066                 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1067                 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1068                 "i1 <= M and i3 <= M and i4 <= M) or "
1069                 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1070                 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1071                 "i4 <= -1 + M) }", 1);
1072         test_coalesce_set(ctx,
1073                 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1074                 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
1075         test_coalesce_set(ctx,
1076                 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1077                         "-x - y + 1 >= 0 and -3 <= z <= 3;"
1078                 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1079                         "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
1080         if (test_coalesce_set(ctx,
1081                 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1082                 return -1;
1083         if (test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0) < 0)
1084                 return -1;
1085         if (test_coalesce_set(ctx,
1086                 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1087                 return -1;
1088         if (test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1) < 0)
1089                 return -1;
1090         if (test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1) < 0)
1091                 return -1;
1092         if (test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0) < 0)
1093                 return -1;
1094         if (test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1) < 0)
1095                 return -1;
1096         if (test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0) < 0)
1097                 return -1;
1098         if (test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1099                 return -1;
1100         if (test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1101                 return -1;
1102         if (test_coalesce_set(ctx, "{ [a, b] : exists e : 2e = a and "
1103                     "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }", 0) < 0)
1104                 return -1;
1105         return 0;
1106 }
1107
1108 void test_closure(struct isl_ctx *ctx)
1109 {
1110         const char *str;
1111         isl_set *dom;
1112         isl_map *up, *right;
1113         isl_map *map, *map2;
1114         int exact;
1115
1116         /* COCOA example 1 */
1117         map = isl_map_read_from_str(ctx,
1118                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1119                         "1 <= i and i < n and 1 <= j and j < n or "
1120                         "i2 = i + 1 and j2 = j - 1 and "
1121                         "1 <= i and i < n and 2 <= j and j <= n }");
1122         map = isl_map_power(map, &exact);
1123         assert(exact);
1124         isl_map_free(map);
1125
1126         /* COCOA example 1 */
1127         map = isl_map_read_from_str(ctx,
1128                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1129                         "1 <= i and i < n and 1 <= j and j < n or "
1130                         "i2 = i + 1 and j2 = j - 1 and "
1131                         "1 <= i and i < n and 2 <= j and j <= n }");
1132         map = isl_map_transitive_closure(map, &exact);
1133         assert(exact);
1134         map2 = isl_map_read_from_str(ctx,
1135                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1136                         "1 <= i and i < n and 1 <= j and j <= n and "
1137                         "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1138                         "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1139                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1140         assert(isl_map_is_equal(map, map2));
1141         isl_map_free(map2);
1142         isl_map_free(map);
1143
1144         map = isl_map_read_from_str(ctx,
1145                 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1146                                      " 0 <= y and y <= n }");
1147         map = isl_map_transitive_closure(map, &exact);
1148         map2 = isl_map_read_from_str(ctx,
1149                 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1150                                      " 0 <= y and y <= n }");
1151         assert(isl_map_is_equal(map, map2));
1152         isl_map_free(map2);
1153         isl_map_free(map);
1154
1155         /* COCOA example 2 */
1156         map = isl_map_read_from_str(ctx,
1157                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1158                         "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1159                         "i2 = i + 2 and j2 = j - 2 and "
1160                         "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1161         map = isl_map_transitive_closure(map, &exact);
1162         assert(exact);
1163         map2 = isl_map_read_from_str(ctx,
1164                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1165                         "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1166                         "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1167                         "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1168                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1169         assert(isl_map_is_equal(map, map2));
1170         isl_map_free(map);
1171         isl_map_free(map2);
1172
1173         /* COCOA Fig.2 left */
1174         map = isl_map_read_from_str(ctx,
1175                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1176                         "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1177                         "j <= n or "
1178                         "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1179                         "j <= 2 i - 3 and j <= n - 2 or "
1180                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1181                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1182         map = isl_map_transitive_closure(map, &exact);
1183         assert(exact);
1184         isl_map_free(map);
1185
1186         /* COCOA Fig.2 right */
1187         map = isl_map_read_from_str(ctx,
1188                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1189                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1190                         "j <= n or "
1191                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1192                         "j <= 2 i - 4 and j <= n - 3 or "
1193                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1194                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1195         map = isl_map_power(map, &exact);
1196         assert(exact);
1197         isl_map_free(map);
1198
1199         /* COCOA Fig.2 right */
1200         map = isl_map_read_from_str(ctx,
1201                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1202                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1203                         "j <= n or "
1204                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1205                         "j <= 2 i - 4 and j <= n - 3 or "
1206                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1207                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1208         map = isl_map_transitive_closure(map, &exact);
1209         assert(exact);
1210         map2 = isl_map_read_from_str(ctx,
1211                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1212                         "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1213                         "j <= n and 3 + i + 2 j <= 3 n and "
1214                         "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1215                         "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1216                         "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1217                         "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1218                         "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1219         assert(isl_map_is_equal(map, map2));
1220         isl_map_free(map2);
1221         isl_map_free(map);
1222
1223         /* COCOA Fig.1 right */
1224         dom = isl_set_read_from_str(ctx,
1225                 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1226                         "2 x - 3 y + 3 >= 0 }");
1227         right = isl_map_read_from_str(ctx,
1228                 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1229         up = isl_map_read_from_str(ctx,
1230                 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1231         right = isl_map_intersect_domain(right, isl_set_copy(dom));
1232         right = isl_map_intersect_range(right, isl_set_copy(dom));
1233         up = isl_map_intersect_domain(up, isl_set_copy(dom));
1234         up = isl_map_intersect_range(up, dom);
1235         map = isl_map_union(up, right);
1236         map = isl_map_transitive_closure(map, &exact);
1237         assert(exact);
1238         map2 = isl_map_read_from_str(ctx,
1239                 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1240                 "  [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1241         assert(isl_map_is_equal(map, map2));
1242         isl_map_free(map2);
1243         isl_map_free(map);
1244
1245         /* COCOA Theorem 1 counter example */
1246         map = isl_map_read_from_str(ctx,
1247                 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1248                         "i2 = 1 and j2 = j or "
1249                         "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1250         map = isl_map_transitive_closure(map, &exact);
1251         assert(exact);
1252         isl_map_free(map);
1253
1254         map = isl_map_read_from_str(ctx,
1255                 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1256                         "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1257                         "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1258                         "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1259         map = isl_map_transitive_closure(map, &exact);
1260         assert(exact);
1261         isl_map_free(map);
1262
1263         /* Kelly et al 1996, fig 12 */
1264         map = isl_map_read_from_str(ctx,
1265                 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1266                         "1 <= i,j,j+1 <= n or "
1267                         "j = n and j2 = 1 and i2 = i + 1 and "
1268                         "1 <= i,i+1 <= n }");
1269         map = isl_map_transitive_closure(map, &exact);
1270         assert(exact);
1271         map2 = isl_map_read_from_str(ctx,
1272                 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1273                         "1 <= i <= n and i = i2 or "
1274                         "1 <= i < i2 <= n and 1 <= j <= n and "
1275                         "1 <= j2 <= n }");
1276         assert(isl_map_is_equal(map, map2));
1277         isl_map_free(map2);
1278         isl_map_free(map);
1279
1280         /* Omega's closure4 */
1281         map = isl_map_read_from_str(ctx,
1282                 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1283                         "1 <= x,y <= 10 or "
1284                         "x2 = x + 1 and y2 = y and "
1285                         "1 <= x <= 20 && 5 <= y <= 15 }");
1286         map = isl_map_transitive_closure(map, &exact);
1287         assert(exact);
1288         isl_map_free(map);
1289
1290         map = isl_map_read_from_str(ctx,
1291                 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1292         map = isl_map_transitive_closure(map, &exact);
1293         assert(!exact);
1294         map2 = isl_map_read_from_str(ctx,
1295                 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1296         assert(isl_map_is_equal(map, map2));
1297         isl_map_free(map);
1298         isl_map_free(map2);
1299
1300         str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1301             "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1302             "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1303             "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1304         map = isl_map_read_from_str(ctx, str);
1305         map = isl_map_transitive_closure(map, &exact);
1306         assert(exact);
1307         map2 = isl_map_read_from_str(ctx, str);
1308         assert(isl_map_is_equal(map, map2));
1309         isl_map_free(map);
1310         isl_map_free(map2);
1311
1312         str = "{[0] -> [1]; [2] -> [3]}";
1313         map = isl_map_read_from_str(ctx, str);
1314         map = isl_map_transitive_closure(map, &exact);
1315         assert(exact);
1316         map2 = isl_map_read_from_str(ctx, str);
1317         assert(isl_map_is_equal(map, map2));
1318         isl_map_free(map);
1319         isl_map_free(map2);
1320
1321         str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1322             "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1323             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1324             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1325             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1326             "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1327             "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1328             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1329             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1330             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1331             "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1332             "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1333             "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1334             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1335             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1336             "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1337             "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1338             "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1339             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1340             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1341         map = isl_map_read_from_str(ctx, str);
1342         map = isl_map_transitive_closure(map, NULL);
1343         assert(map);
1344         isl_map_free(map);
1345 }
1346
1347 void test_lex(struct isl_ctx *ctx)
1348 {
1349         isl_space *dim;
1350         isl_map *map;
1351
1352         dim = isl_space_set_alloc(ctx, 0, 0);
1353         map = isl_map_lex_le(dim);
1354         assert(!isl_map_is_empty(map));
1355         isl_map_free(map);
1356 }
1357
1358 void test_lexmin(struct isl_ctx *ctx)
1359 {
1360         const char *str;
1361         isl_basic_map *bmap;
1362         isl_map *map, *map2;
1363         isl_set *set;
1364         isl_set *set2;
1365         isl_pw_multi_aff *pma;
1366
1367         str = "[p0, p1] -> { [] -> [] : "
1368             "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1369             "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1370             "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1371             "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1372             "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1373             "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1374             "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1375             "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1376             "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1377             "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1378             "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1379         map = isl_map_read_from_str(ctx, str);
1380         map = isl_map_lexmin(map);
1381         isl_map_free(map);
1382
1383         str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1384             "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1385         set = isl_set_read_from_str(ctx, str);
1386         set = isl_set_lexmax(set);
1387         str = "[C] -> { [obj,a,b,c] : C = 8 }";
1388         set2 = isl_set_read_from_str(ctx, str);
1389         set = isl_set_intersect(set, set2);
1390         assert(!isl_set_is_empty(set));
1391         isl_set_free(set);
1392
1393         str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1394         map = isl_map_read_from_str(ctx, str);
1395         map = isl_map_lexmin(map);
1396         str = "{ [x] -> [5] : 6 <= x <= 8; "
1397                 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1398         map2 = isl_map_read_from_str(ctx, str);
1399         assert(isl_map_is_equal(map, map2));
1400         isl_map_free(map);
1401         isl_map_free(map2);
1402
1403         str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1404         map = isl_map_read_from_str(ctx, str);
1405         map2 = isl_map_copy(map);
1406         map = isl_map_lexmin(map);
1407         assert(isl_map_is_equal(map, map2));
1408         isl_map_free(map);
1409         isl_map_free(map2);
1410
1411         str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1412         map = isl_map_read_from_str(ctx, str);
1413         map = isl_map_lexmin(map);
1414         str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1415                 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1416                 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1417                 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1418         map2 = isl_map_read_from_str(ctx, str);
1419         assert(isl_map_is_equal(map, map2));
1420         isl_map_free(map);
1421         isl_map_free(map2);
1422
1423         str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1424                                 " 8i' <= i and 8i' >= -7 + i }";
1425         bmap = isl_basic_map_read_from_str(ctx, str);
1426         pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1427         map2 = isl_map_from_pw_multi_aff(pma);
1428         map = isl_map_from_basic_map(bmap);
1429         assert(isl_map_is_equal(map, map2));
1430         isl_map_free(map);
1431         isl_map_free(map2);
1432
1433         str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1434         map = isl_map_read_from_str(ctx, str);
1435         map = isl_map_lexmin(map);
1436         str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1437         map2 = isl_map_read_from_str(ctx, str);
1438         assert(isl_map_is_equal(map, map2));
1439         isl_map_free(map);
1440         isl_map_free(map2);
1441 }
1442
1443 struct must_may {
1444         isl_map *must;
1445         isl_map *may;
1446 };
1447
1448 static int collect_must_may(__isl_take isl_map *dep, int must,
1449         void *dep_user, void *user)
1450 {
1451         struct must_may *mm = (struct must_may *)user;
1452
1453         if (must)
1454                 mm->must = isl_map_union(mm->must, dep);
1455         else
1456                 mm->may = isl_map_union(mm->may, dep);
1457
1458         return 0;
1459 }
1460
1461 static int common_space(void *first, void *second)
1462 {
1463         int depth = *(int *)first;
1464         return 2 * depth;
1465 }
1466
1467 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1468 {
1469         isl_map *map2;
1470         int equal;
1471
1472         if (!map)
1473                 return -1;
1474
1475         map2 = isl_map_read_from_str(map->ctx, str);
1476         equal = isl_map_is_equal(map, map2);
1477         isl_map_free(map2);
1478
1479         return equal;
1480 }
1481
1482 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1483 {
1484         int equal;
1485
1486         equal = map_is_equal(map, str);
1487         if (equal < 0)
1488                 return -1;
1489         if (!equal)
1490                 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1491                         "result not as expected", return -1);
1492         return 0;
1493 }
1494
1495 void test_dep(struct isl_ctx *ctx)
1496 {
1497         const char *str;
1498         isl_space *dim;
1499         isl_map *map;
1500         isl_access_info *ai;
1501         isl_flow *flow;
1502         int depth;
1503         struct must_may mm;
1504
1505         depth = 3;
1506
1507         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1508         map = isl_map_read_from_str(ctx, str);
1509         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1510
1511         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1512         map = isl_map_read_from_str(ctx, str);
1513         ai = isl_access_info_add_source(ai, map, 1, &depth);
1514
1515         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1516         map = isl_map_read_from_str(ctx, str);
1517         ai = isl_access_info_add_source(ai, map, 1, &depth);
1518
1519         flow = isl_access_info_compute_flow(ai);
1520         dim = isl_space_alloc(ctx, 0, 3, 3);
1521         mm.must = isl_map_empty(isl_space_copy(dim));
1522         mm.may = isl_map_empty(dim);
1523
1524         isl_flow_foreach(flow, collect_must_may, &mm);
1525
1526         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1527               "  [1,10,0] -> [2,5,0] }";
1528         assert(map_is_equal(mm.must, str));
1529         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1530         assert(map_is_equal(mm.may, str));
1531
1532         isl_map_free(mm.must);
1533         isl_map_free(mm.may);
1534         isl_flow_free(flow);
1535
1536
1537         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1538         map = isl_map_read_from_str(ctx, str);
1539         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1540
1541         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1542         map = isl_map_read_from_str(ctx, str);
1543         ai = isl_access_info_add_source(ai, map, 1, &depth);
1544
1545         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1546         map = isl_map_read_from_str(ctx, str);
1547         ai = isl_access_info_add_source(ai, map, 0, &depth);
1548
1549         flow = isl_access_info_compute_flow(ai);
1550         dim = isl_space_alloc(ctx, 0, 3, 3);
1551         mm.must = isl_map_empty(isl_space_copy(dim));
1552         mm.may = isl_map_empty(dim);
1553
1554         isl_flow_foreach(flow, collect_must_may, &mm);
1555
1556         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1557         assert(map_is_equal(mm.must, str));
1558         str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1559         assert(map_is_equal(mm.may, str));
1560
1561         isl_map_free(mm.must);
1562         isl_map_free(mm.may);
1563         isl_flow_free(flow);
1564
1565
1566         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1567         map = isl_map_read_from_str(ctx, str);
1568         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1569
1570         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1571         map = isl_map_read_from_str(ctx, str);
1572         ai = isl_access_info_add_source(ai, map, 0, &depth);
1573
1574         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1575         map = isl_map_read_from_str(ctx, str);
1576         ai = isl_access_info_add_source(ai, map, 0, &depth);
1577
1578         flow = isl_access_info_compute_flow(ai);
1579         dim = isl_space_alloc(ctx, 0, 3, 3);
1580         mm.must = isl_map_empty(isl_space_copy(dim));
1581         mm.may = isl_map_empty(dim);
1582
1583         isl_flow_foreach(flow, collect_must_may, &mm);
1584
1585         str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1586               "  [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1587         assert(map_is_equal(mm.may, str));
1588         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1589         assert(map_is_equal(mm.must, str));
1590
1591         isl_map_free(mm.must);
1592         isl_map_free(mm.may);
1593         isl_flow_free(flow);
1594
1595
1596         str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1597         map = isl_map_read_from_str(ctx, str);
1598         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1599
1600         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1601         map = isl_map_read_from_str(ctx, str);
1602         ai = isl_access_info_add_source(ai, map, 0, &depth);
1603
1604         str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1605         map = isl_map_read_from_str(ctx, str);
1606         ai = isl_access_info_add_source(ai, map, 0, &depth);
1607
1608         flow = isl_access_info_compute_flow(ai);
1609         dim = isl_space_alloc(ctx, 0, 3, 3);
1610         mm.must = isl_map_empty(isl_space_copy(dim));
1611         mm.may = isl_map_empty(dim);
1612
1613         isl_flow_foreach(flow, collect_must_may, &mm);
1614
1615         str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1616               "  [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1617         assert(map_is_equal(mm.may, str));
1618         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1619         assert(map_is_equal(mm.must, str));
1620
1621         isl_map_free(mm.must);
1622         isl_map_free(mm.may);
1623         isl_flow_free(flow);
1624
1625
1626         str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1627         map = isl_map_read_from_str(ctx, str);
1628         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1629
1630         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1631         map = isl_map_read_from_str(ctx, str);
1632         ai = isl_access_info_add_source(ai, map, 0, &depth);
1633
1634         str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1635         map = isl_map_read_from_str(ctx, str);
1636         ai = isl_access_info_add_source(ai, map, 0, &depth);
1637
1638         flow = isl_access_info_compute_flow(ai);
1639         dim = isl_space_alloc(ctx, 0, 3, 3);
1640         mm.must = isl_map_empty(isl_space_copy(dim));
1641         mm.may = isl_map_empty(dim);
1642
1643         isl_flow_foreach(flow, collect_must_may, &mm);
1644
1645         str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1646               "  [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1647         assert(map_is_equal(mm.may, str));
1648         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1649         assert(map_is_equal(mm.must, str));
1650
1651         isl_map_free(mm.must);
1652         isl_map_free(mm.may);
1653         isl_flow_free(flow);
1654
1655
1656         depth = 5;
1657
1658         str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1659         map = isl_map_read_from_str(ctx, str);
1660         ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1661
1662         str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1663         map = isl_map_read_from_str(ctx, str);
1664         ai = isl_access_info_add_source(ai, map, 1, &depth);
1665
1666         flow = isl_access_info_compute_flow(ai);
1667         dim = isl_space_alloc(ctx, 0, 5, 5);
1668         mm.must = isl_map_empty(isl_space_copy(dim));
1669         mm.may = isl_map_empty(dim);
1670
1671         isl_flow_foreach(flow, collect_must_may, &mm);
1672
1673         str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1674         assert(map_is_equal(mm.must, str));
1675         str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1676         assert(map_is_equal(mm.may, str));
1677
1678         isl_map_free(mm.must);
1679         isl_map_free(mm.may);
1680         isl_flow_free(flow);
1681 }
1682
1683 int test_sv(isl_ctx *ctx)
1684 {
1685         const char *str;
1686         isl_map *map;
1687         isl_union_map *umap;
1688         int sv;
1689
1690         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1691         map = isl_map_read_from_str(ctx, str);
1692         sv = isl_map_is_single_valued(map);
1693         isl_map_free(map);
1694         if (sv < 0)
1695                 return -1;
1696         if (!sv)
1697                 isl_die(ctx, isl_error_internal,
1698                         "map not detected as single valued", return -1);
1699
1700         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1701         map = isl_map_read_from_str(ctx, str);
1702         sv = isl_map_is_single_valued(map);
1703         isl_map_free(map);
1704         if (sv < 0)
1705                 return -1;
1706         if (sv)
1707                 isl_die(ctx, isl_error_internal,
1708                         "map detected as single valued", return -1);
1709
1710         str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1711         umap = isl_union_map_read_from_str(ctx, str);
1712         sv = isl_union_map_is_single_valued(umap);
1713         isl_union_map_free(umap);
1714         if (sv < 0)
1715                 return -1;
1716         if (!sv)
1717                 isl_die(ctx, isl_error_internal,
1718                         "map not detected as single valued", return -1);
1719
1720         str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1721         umap = isl_union_map_read_from_str(ctx, str);
1722         sv = isl_union_map_is_single_valued(umap);
1723         isl_union_map_free(umap);
1724         if (sv < 0)
1725                 return -1;
1726         if (sv)
1727                 isl_die(ctx, isl_error_internal,
1728                         "map detected as single valued", return -1);
1729
1730         return 0;
1731 }
1732
1733 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1734 {
1735         isl_map *map;
1736
1737         map = isl_map_read_from_str(ctx, str);
1738         if (bijective)
1739                 assert(isl_map_is_bijective(map));
1740         else
1741                 assert(!isl_map_is_bijective(map));
1742         isl_map_free(map);
1743 }
1744
1745 void test_bijective(struct isl_ctx *ctx)
1746 {
1747         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1748         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1749         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1750         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1751         test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1752         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1753         test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1754         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1755         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1756         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1757         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1758         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1759         test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1760 }
1761
1762 void test_pwqp(struct isl_ctx *ctx)
1763 {
1764         const char *str;
1765         isl_set *set;
1766         isl_pw_qpolynomial *pwqp1, *pwqp2;
1767
1768         str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1769         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1770
1771         pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1772                                                 isl_dim_in, 1, 1);
1773
1774         str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1775         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1776
1777         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1778
1779         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1780
1781         isl_pw_qpolynomial_free(pwqp1);
1782
1783         str = "{ [i] -> i }";
1784         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1785         str = "{ [k] : exists a : k = 2a }";
1786         set = isl_set_read_from_str(ctx, str);
1787         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1788         str = "{ [i] -> i }";
1789         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1790
1791         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1792
1793         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1794
1795         isl_pw_qpolynomial_free(pwqp1);
1796
1797         str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1798         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1799         str = "{ [10] }";
1800         set = isl_set_read_from_str(ctx, str);
1801         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1802         str = "{ [i] -> 16 }";
1803         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1804
1805         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1806
1807         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1808
1809         isl_pw_qpolynomial_free(pwqp1);
1810
1811         str = "{ [i] -> ([(i)/2]) }";
1812         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1813         str = "{ [k] : exists a : k = 2a+1 }";
1814         set = isl_set_read_from_str(ctx, str);
1815         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1816         str = "{ [i] -> -1/2 + 1/2 * i }";
1817         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1818
1819         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1820
1821         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1822
1823         isl_pw_qpolynomial_free(pwqp1);
1824
1825         str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1826         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1827         str = "{ [i] -> ([(2 * [i/2])/5]) }";
1828         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1829
1830         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1831
1832         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1833
1834         isl_pw_qpolynomial_free(pwqp1);
1835
1836         str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1837         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1838         str = "{ [x] -> x }";
1839         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1840
1841         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1842
1843         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1844
1845         isl_pw_qpolynomial_free(pwqp1);
1846
1847         str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1848         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1849         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1850         pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1851         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1852         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1853         isl_pw_qpolynomial_free(pwqp1);
1854 }
1855
1856 void test_split_periods(isl_ctx *ctx)
1857 {
1858         const char *str;
1859         isl_pw_qpolynomial *pwqp;
1860
1861         str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1862                 "U + 2V + 3 >= 0 and - U -2V  >= 0 and - U + 10 >= 0 and "
1863                 "U  >= 0; [U,V] -> U^2 : U >= 100 }";
1864         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1865
1866         pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1867         assert(pwqp);
1868
1869         isl_pw_qpolynomial_free(pwqp);
1870 }
1871
1872 void test_union(isl_ctx *ctx)
1873 {
1874         const char *str;
1875         isl_union_set *uset1, *uset2;
1876         isl_union_map *umap1, *umap2;
1877
1878         str = "{ [i] : 0 <= i <= 1 }";
1879         uset1 = isl_union_set_read_from_str(ctx, str);
1880         str = "{ [1] -> [0] }";
1881         umap1 = isl_union_map_read_from_str(ctx, str);
1882
1883         umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1884         assert(isl_union_map_is_equal(umap1, umap2));
1885
1886         isl_union_map_free(umap1);
1887         isl_union_map_free(umap2);
1888
1889         str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1890         umap1 = isl_union_map_read_from_str(ctx, str);
1891         str = "{ A[i]; B[i] }";
1892         uset1 = isl_union_set_read_from_str(ctx, str);
1893
1894         uset2 = isl_union_map_domain(umap1);
1895
1896         assert(isl_union_set_is_equal(uset1, uset2));
1897
1898         isl_union_set_free(uset1);
1899         isl_union_set_free(uset2);
1900 }
1901
1902 void test_bound(isl_ctx *ctx)
1903 {
1904         const char *str;
1905         isl_pw_qpolynomial *pwqp;
1906         isl_pw_qpolynomial_fold *pwf;
1907
1908         str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1909         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1910         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1911         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1912         isl_pw_qpolynomial_fold_free(pwf);
1913
1914         str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1915         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1916         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1917         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1918         isl_pw_qpolynomial_fold_free(pwf);
1919 }
1920
1921 void test_lift(isl_ctx *ctx)
1922 {
1923         const char *str;
1924         isl_basic_map *bmap;
1925         isl_basic_set *bset;
1926
1927         str = "{ [i0] : exists e0 : i0 = 4e0 }";
1928         bset = isl_basic_set_read_from_str(ctx, str);
1929         bset = isl_basic_set_lift(bset);
1930         bmap = isl_basic_map_from_range(bset);
1931         bset = isl_basic_map_domain(bmap);
1932         isl_basic_set_free(bset);
1933 }
1934
1935 void test_subset(isl_ctx *ctx)
1936 {
1937         const char *str;
1938         isl_set *set1, *set2;
1939
1940         str = "{ [112, 0] }";
1941         set1 = isl_set_read_from_str(ctx, str);
1942         str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1943                 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1944                 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1945         set2 = isl_set_read_from_str(ctx, str);
1946         assert(isl_set_is_subset(set1, set2));
1947         isl_set_free(set1);
1948         isl_set_free(set2);
1949 }
1950
1951 void test_factorize(isl_ctx *ctx)
1952 {
1953         const char *str;
1954         isl_basic_set *bset;
1955         isl_factorizer *f;
1956
1957         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1958             "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1959             "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1960             "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1961             "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1962             "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1963             "3i5 >= -2i0 - i2 + 3i4 }";
1964         bset = isl_basic_set_read_from_str(ctx, str);
1965         f = isl_basic_set_factorizer(bset);
1966         assert(f);
1967         isl_basic_set_free(bset);
1968         isl_factorizer_free(f);
1969
1970         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1971             "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
1972             "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
1973             "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
1974             "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
1975             "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
1976             "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
1977             "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
1978             "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
1979             "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
1980         bset = isl_basic_set_read_from_str(ctx, str);
1981         f = isl_basic_set_factorizer(bset);
1982         assert(f);
1983         isl_basic_set_free(bset);
1984         isl_factorizer_free(f);
1985 }
1986
1987 static int check_injective(__isl_take isl_map *map, void *user)
1988 {
1989         int *injective = user;
1990
1991         *injective = isl_map_is_injective(map);
1992         isl_map_free(map);
1993
1994         if (*injective < 0 || !*injective)
1995                 return -1;
1996
1997         return 0;
1998 }
1999
2000 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2001         const char *r, const char *s, int tilable, int parallel)
2002 {
2003         int i;
2004         isl_union_set *D;
2005         isl_union_map *W, *R, *S;
2006         isl_union_map *empty;
2007         isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2008         isl_union_map *validity, *proximity;
2009         isl_union_map *schedule;
2010         isl_union_map *test;
2011         isl_union_set *delta;
2012         isl_union_set *domain;
2013         isl_set *delta_set;
2014         isl_set *slice;
2015         isl_set *origin;
2016         isl_schedule *sched;
2017         int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2018
2019         D = isl_union_set_read_from_str(ctx, d);
2020         W = isl_union_map_read_from_str(ctx, w);
2021         R = isl_union_map_read_from_str(ctx, r);
2022         S = isl_union_map_read_from_str(ctx, s);
2023
2024         W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2025         R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2026
2027         empty = isl_union_map_empty(isl_union_map_get_space(S));
2028         isl_union_map_compute_flow(isl_union_map_copy(R),
2029                                    isl_union_map_copy(W), empty,
2030                                    isl_union_map_copy(S),
2031                                    &dep_raw, NULL, NULL, NULL);
2032         isl_union_map_compute_flow(isl_union_map_copy(W),
2033                                    isl_union_map_copy(W),
2034                                    isl_union_map_copy(R),
2035                                    isl_union_map_copy(S),
2036                                    &dep_waw, &dep_war, NULL, NULL);
2037
2038         dep = isl_union_map_union(dep_waw, dep_war);
2039         dep = isl_union_map_union(dep, dep_raw);
2040         validity = isl_union_map_copy(dep);
2041         proximity = isl_union_map_copy(dep);
2042
2043         sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
2044                                                validity, proximity);
2045         schedule = isl_schedule_get_map(sched);
2046         isl_schedule_free(sched);
2047         isl_union_map_free(W);
2048         isl_union_map_free(R);
2049         isl_union_map_free(S);
2050
2051         is_injection = 1;
2052         isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2053
2054         domain = isl_union_map_domain(isl_union_map_copy(schedule));
2055         is_complete = isl_union_set_is_subset(D, domain);
2056         isl_union_set_free(D);
2057         isl_union_set_free(domain);
2058
2059         test = isl_union_map_reverse(isl_union_map_copy(schedule));
2060         test = isl_union_map_apply_range(test, dep);
2061         test = isl_union_map_apply_range(test, schedule);
2062
2063         delta = isl_union_map_deltas(test);
2064         if (isl_union_set_n_set(delta) == 0) {
2065                 is_tilable = 1;
2066                 is_parallel = 1;
2067                 is_nonneg = 1;
2068                 isl_union_set_free(delta);
2069         } else {
2070                 delta_set = isl_set_from_union_set(delta);
2071
2072                 slice = isl_set_universe(isl_set_get_space(delta_set));
2073                 for (i = 0; i < tilable; ++i)
2074                         slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2075                 is_tilable = isl_set_is_subset(delta_set, slice);
2076                 isl_set_free(slice);
2077
2078                 slice = isl_set_universe(isl_set_get_space(delta_set));
2079                 for (i = 0; i < parallel; ++i)
2080                         slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2081                 is_parallel = isl_set_is_subset(delta_set, slice);
2082                 isl_set_free(slice);
2083
2084                 origin = isl_set_universe(isl_set_get_space(delta_set));
2085                 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2086                         origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2087
2088                 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2089                 delta_set = isl_set_lexmin(delta_set);
2090
2091                 is_nonneg = isl_set_is_equal(delta_set, origin);
2092
2093                 isl_set_free(origin);
2094                 isl_set_free(delta_set);
2095         }
2096
2097         if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2098             is_injection < 0 || is_complete < 0)
2099                 return -1;
2100         if (!is_complete)
2101                 isl_die(ctx, isl_error_unknown,
2102                         "generated schedule incomplete", return -1);
2103         if (!is_injection)
2104                 isl_die(ctx, isl_error_unknown,
2105                         "generated schedule not injective on each statement",
2106                         return -1);
2107         if (!is_nonneg)
2108                 isl_die(ctx, isl_error_unknown,
2109                         "negative dependences in generated schedule",
2110                         return -1);
2111         if (!is_tilable)
2112                 isl_die(ctx, isl_error_unknown,
2113                         "generated schedule not as tilable as expected",
2114                         return -1);
2115         if (!is_parallel)
2116                 isl_die(ctx, isl_error_unknown,
2117                         "generated schedule not as parallel as expected",
2118                         return -1);
2119
2120         return 0;
2121 }
2122
2123 int test_special_schedule(isl_ctx *ctx, const char *domain,
2124         const char *validity, const char *proximity, const char *expected_sched)
2125 {
2126         isl_union_set *dom;
2127         isl_union_map *dep;
2128         isl_union_map *prox;
2129         isl_union_map *sched1, *sched2;
2130         isl_schedule *schedule;
2131         int equal;
2132
2133         dom = isl_union_set_read_from_str(ctx, domain);
2134         dep = isl_union_map_read_from_str(ctx, validity);
2135         prox = isl_union_map_read_from_str(ctx, proximity);
2136         schedule = isl_union_set_compute_schedule(dom, dep, prox);
2137         sched1 = isl_schedule_get_map(schedule);
2138         isl_schedule_free(schedule);
2139
2140         sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2141
2142         equal = isl_union_map_is_equal(sched1, sched2);
2143         isl_union_map_free(sched1);
2144         isl_union_map_free(sched2);
2145
2146         if (equal < 0)
2147                 return -1;
2148         if (!equal)
2149                 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2150                         return -1);
2151
2152         return 0;
2153 }
2154
2155 int test_schedule(isl_ctx *ctx)
2156 {
2157         const char *D, *W, *R, *V, *P, *S;
2158
2159         /* Jacobi */
2160         D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2161         W = "{ S1[t,i] -> a[t,i] }";
2162         R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2163                 "S1[t,i] -> a[t-1,i+1] }";
2164         S = "{ S1[t,i] -> [t,i] }";
2165         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2166                 return -1;
2167
2168         /* Fig. 5 of CC2008 */
2169         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2170                                 "j <= -1 + N }";
2171         W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2172                                 "j >= 2 and j <= -1 + N }";
2173         R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2174                                 "j >= 2 and j <= -1 + N; "
2175                     "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2176                                 "j >= 2 and j <= -1 + N }";
2177         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2178         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2179                 return -1;
2180
2181         D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2182         W = "{ S1[i] -> a[i] }";
2183         R = "{ S2[i] -> a[i+1] }";
2184         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2185         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2186                 return -1;
2187
2188         D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2189         W = "{ S1[i] -> a[i] }";
2190         R = "{ S2[i] -> a[9-i] }";
2191         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2192         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2193                 return -1;
2194
2195         D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2196         W = "{ S1[i] -> a[i] }";
2197         R = "[N] -> { S2[i] -> a[N-1-i] }";
2198         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2199         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2200                 return -1;
2201         
2202         D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2203         W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2204         R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2205         S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2206         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2207                 return -1;
2208
2209         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2210         W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2211         R = "{ S2[i,j] -> a[i-1,j] }";
2212         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2213         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2214                 return -1;
2215
2216         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2217         W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2218         R = "{ S2[i,j] -> a[i,j-1] }";
2219         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2220         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2221                 return -1;
2222
2223         D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2224         W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2225                     "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2226         R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2227         S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2228                     "S_0[] -> [0, 0, 0] }";
2229         if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2230                 return -1;
2231         ctx->opt->schedule_parametric = 0;
2232         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2233                 return -1;
2234         ctx->opt->schedule_parametric = 1;
2235
2236         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2237                     "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2238         W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2239         R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2240                     "S4[i] -> a[i,N] }";
2241         S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2242                 "S4[i] -> [4,i,0] }";
2243         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2244                 return -1;
2245
2246         D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2247         W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2248                                         "j <= N }";
2249         R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2250                                         "j <= N; "
2251                     "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2252                                         "j <= N }";
2253         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2254         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2255                 return -1;
2256
2257         D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2258                     " S_2[t] : t >= 0 and t <= -1 + N; "
2259                     " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2260                                 "i <= -1 + N }";
2261         W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2262                     " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2263                     " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2264                                                 "i >= 0 and i <= -1 + N }";
2265         R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2266                                             "i >= 0 and i <= -1 + N; "
2267                     " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2268         S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2269                     " S_0[t] -> [0, t, 0] }";
2270
2271         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2272                 return -1;
2273         ctx->opt->schedule_parametric = 0;
2274         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2275                 return -1;
2276         ctx->opt->schedule_parametric = 1;
2277
2278         D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2279         S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2280         if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2281                 return -1;
2282
2283         D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2284             "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2285         W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2286                                             "j >= 0 and j <= -1 + N; "
2287                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2288         R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2289                                             "j >= 0 and j <= -1 + N; "
2290                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2291         S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2292         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2293                 return -1;
2294
2295         D = "{ S_0[i] : i >= 0 }";
2296         W = "{ S_0[i] -> a[i] : i >= 0 }";
2297         R = "{ S_0[i] -> a[0] : i >= 0 }";
2298         S = "{ S_0[i] -> [0, i, 0] }";
2299         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2300                 return -1;
2301
2302         D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2303         W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2304         R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2305         S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2306         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2307                 return -1;
2308
2309         D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2310                                 "k <= -1 + n and k >= 0 }";
2311         W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "                                                  "k <= -1 + n and k >= 0 }";
2312         R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2313                                         "k <= -1 + n and k >= 0; "
2314                     "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2315                                         "k <= -1 + n and k >= 0; "
2316                     "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2317                                         "k <= -1 + n and k >= 0 }";
2318         S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2319         ctx->opt->schedule_outer_zero_distance = 1;
2320         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2321                 return -1;
2322         ctx->opt->schedule_outer_zero_distance = 0;
2323
2324         D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2325                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2326                 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2327              "Stmt_for_body24[i0, i1, 1, 0]:"
2328                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2329              "Stmt_for_body7[i0, i1, i2]:"
2330                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2331                 "i2 <= 7 }";
2332
2333         V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2334                 "Stmt_for_body24[1, i1, i2, i3]:"
2335                 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2336                 "i2 >= 1;"
2337              "Stmt_for_body24[0, i1, i2, i3] -> "
2338                 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2339                 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2340                 "i3 >= 0;"
2341               "Stmt_for_body24[0, i1, i2, i3] ->"
2342                 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2343                 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2344               "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2345                 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2346                 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2347                 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2348               "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2349                 "i1 <= 6 and i1 >= 0;"
2350               "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2351               "Stmt_for_body7[i0, i1, i2] -> "
2352                 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2353                 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2354                 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2355               "Stmt_for_body7[i0, i1, i2] -> "
2356                 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2357                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2358                 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2359         P = V;
2360         S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2361                 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2362             "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2363
2364         if (test_special_schedule(ctx, D, V, P, S) < 0)
2365                 return -1;
2366
2367         D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2368         V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2369                                            "j >= 1 and j <= 7;"
2370                 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2371                                              "j >= 1 and j <= 8 }";
2372         P = "{ }";
2373         S = "{ S_0[i, j] -> [i + j, j] }";
2374         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2375         if (test_special_schedule(ctx, D, V, P, S) < 0)
2376                 return -1;
2377         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2378
2379         /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2380         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2381                                  "j >= 0 and j <= -1 + i }";
2382         V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2383                                         "i <= -1 + N and j >= 0;"
2384                      "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2385                                         "i <= -2 + N }";
2386         P = "{ }";
2387         S = "{ S_0[i, j] -> [i, j] }";
2388         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2389         if (test_special_schedule(ctx, D, V, P, S) < 0)
2390                 return -1;
2391         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2392
2393         /* Test both algorithms on a case with only proximity dependences. */
2394         D = "{ S[i,j] : 0 <= i <= 10 }";
2395         V = "{ }";
2396         P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2397         S = "{ S[i, j] -> [j, i] }";
2398         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2399         if (test_special_schedule(ctx, D, V, P, S) < 0)
2400                 return -1;
2401         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2402         return test_special_schedule(ctx, D, V, P, S);
2403 }
2404
2405 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2406 {
2407         isl_union_map *umap;
2408         int test;
2409
2410         umap = isl_union_map_read_from_str(ctx, str);
2411         test = isl_union_map_plain_is_injective(umap);
2412         isl_union_map_free(umap);
2413         if (test < 0)
2414                 return -1;
2415         if (test == injective)
2416                 return 0;
2417         if (injective)
2418                 isl_die(ctx, isl_error_unknown,
2419                         "map not detected as injective", return -1);
2420         else
2421                 isl_die(ctx, isl_error_unknown,
2422                         "map detected as injective", return -1);
2423 }
2424
2425 int test_injective(isl_ctx *ctx)
2426 {
2427         const char *str;
2428
2429         if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2430                 return -1;
2431         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2432                 return -1;
2433         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2434                 return -1;
2435         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2436                 return -1;
2437         if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2438                 return -1;
2439         if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2440                 return -1;
2441         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2442                 return -1;
2443         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2444                 return -1;
2445
2446         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2447         if (test_plain_injective(ctx, str, 1))
2448                 return -1;
2449         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2450         if (test_plain_injective(ctx, str, 0))
2451                 return -1;
2452
2453         return 0;
2454 }
2455
2456 int test_aff(isl_ctx *ctx)
2457 {
2458         const char *str;
2459         isl_set *set;
2460         isl_space *dim;
2461         isl_local_space *ls;
2462         isl_aff *aff;
2463         int zero;
2464
2465         dim = isl_space_set_alloc(ctx, 0, 1);
2466         ls = isl_local_space_from_space(dim);
2467         aff = isl_aff_zero_on_domain(ls);
2468
2469         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2470         aff = isl_aff_scale_down_ui(aff, 3);
2471         aff = isl_aff_floor(aff);
2472         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2473         aff = isl_aff_scale_down_ui(aff, 2);
2474         aff = isl_aff_floor(aff);
2475         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2476
2477         str = "{ [10] }";
2478         set = isl_set_read_from_str(ctx, str);
2479         aff = isl_aff_gist(aff, set);
2480
2481         aff = isl_aff_add_constant_si(aff, -16);
2482         zero = isl_aff_plain_is_zero(aff);
2483         isl_aff_free(aff);
2484
2485         if (zero < 0)
2486                 return -1;
2487         if (!zero)
2488                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2489
2490         return 0;
2491 }
2492
2493 int test_dim_max(isl_ctx *ctx)
2494 {
2495         int equal;
2496         const char *str;
2497         isl_set *set1, *set2;
2498         isl_set *set;
2499         isl_map *map;
2500         isl_pw_aff *pwaff;
2501
2502         str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2503         set = isl_set_read_from_str(ctx, str);
2504         pwaff = isl_set_dim_max(set, 0);
2505         set1 = isl_set_from_pw_aff(pwaff);
2506         str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2507         set2 = isl_set_read_from_str(ctx, str);
2508         equal = isl_set_is_equal(set1, set2);
2509         isl_set_free(set1);
2510         isl_set_free(set2);
2511         if (equal < 0)
2512                 return -1;
2513         if (!equal)
2514                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2515
2516         str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2517         set = isl_set_read_from_str(ctx, str);
2518         pwaff = isl_set_dim_max(set, 0);
2519         set1 = isl_set_from_pw_aff(pwaff);
2520         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2521         set2 = isl_set_read_from_str(ctx, str);
2522         equal = isl_set_is_equal(set1, set2);
2523         isl_set_free(set1);
2524         isl_set_free(set2);
2525         if (equal < 0)
2526                 return -1;
2527         if (!equal)
2528                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2529
2530         str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2531         set = isl_set_read_from_str(ctx, str);
2532         pwaff = isl_set_dim_max(set, 0);
2533         set1 = isl_set_from_pw_aff(pwaff);
2534         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2535         set2 = isl_set_read_from_str(ctx, str);
2536         equal = isl_set_is_equal(set1, set2);
2537         isl_set_free(set1);
2538         isl_set_free(set2);
2539         if (equal < 0)
2540                 return -1;
2541         if (!equal)
2542                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2543
2544         str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2545                         "0 <= i < N and 0 <= j < M }";
2546         map = isl_map_read_from_str(ctx, str);
2547         set = isl_map_range(map);
2548
2549         pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2550         set1 = isl_set_from_pw_aff(pwaff);
2551         str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2552         set2 = isl_set_read_from_str(ctx, str);
2553         equal = isl_set_is_equal(set1, set2);
2554         isl_set_free(set1);
2555         isl_set_free(set2);
2556
2557         pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2558         set1 = isl_set_from_pw_aff(pwaff);
2559         str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2560         set2 = isl_set_read_from_str(ctx, str);
2561         if (equal >= 0 && equal)
2562                 equal = isl_set_is_equal(set1, set2);
2563         isl_set_free(set1);
2564         isl_set_free(set2);
2565
2566         isl_set_free(set);
2567
2568         if (equal < 0)
2569                 return -1;
2570         if (!equal)
2571                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2572
2573         return 0;
2574 }
2575
2576 int test_product(isl_ctx *ctx)
2577 {
2578         const char *str;
2579         isl_set *set;
2580         isl_union_set *uset1, *uset2;
2581         int ok;
2582
2583         str = "{ A[i] }";
2584         set = isl_set_read_from_str(ctx, str);
2585         set = isl_set_product(set, isl_set_copy(set));
2586         ok = isl_set_is_wrapping(set);
2587         isl_set_free(set);
2588         if (ok < 0)
2589                 return -1;
2590         if (!ok)
2591                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2592
2593         str = "{ [] }";
2594         uset1 = isl_union_set_read_from_str(ctx, str);
2595         uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
2596         str = "{ [[] -> []] }";
2597         uset2 = isl_union_set_read_from_str(ctx, str);
2598         ok = isl_union_set_is_equal(uset1, uset2);
2599         isl_union_set_free(uset1);
2600         isl_union_set_free(uset2);
2601         if (ok < 0)
2602                 return -1;
2603         if (!ok)
2604                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2605
2606         return 0;
2607 }
2608
2609 int test_equal(isl_ctx *ctx)
2610 {
2611         const char *str;
2612         isl_set *set, *set2;
2613         int equal;
2614
2615         str = "{ S_6[i] }";
2616         set = isl_set_read_from_str(ctx, str);
2617         str = "{ S_7[i] }";
2618         set2 = isl_set_read_from_str(ctx, str);
2619         equal = isl_set_is_equal(set, set2);
2620         isl_set_free(set);
2621         isl_set_free(set2);
2622         if (equal < 0)
2623                 return -1;
2624         if (equal)
2625                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2626
2627         return 0;
2628 }
2629
2630 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2631         enum isl_dim_type type, unsigned pos, int fixed)
2632 {
2633         int test;
2634
2635         test = isl_map_plain_is_fixed(map, type, pos, NULL);
2636         isl_map_free(map);
2637         if (test < 0)
2638                 return -1;
2639         if (test == fixed)
2640                 return 0;
2641         if (fixed)
2642                 isl_die(ctx, isl_error_unknown,
2643                         "map not detected as fixed", return -1);
2644         else
2645                 isl_die(ctx, isl_error_unknown,
2646                         "map detected as fixed", return -1);
2647 }
2648
2649 int test_fixed(isl_ctx *ctx)
2650 {
2651         const char *str;
2652         isl_map *map;
2653
2654         str = "{ [i] -> [i] }";
2655         map = isl_map_read_from_str(ctx, str);
2656         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2657                 return -1;
2658         str = "{ [i] -> [1] }";
2659         map = isl_map_read_from_str(ctx, str);
2660         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2661                 return -1;
2662         str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2663         map = isl_map_read_from_str(ctx, str);
2664         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2665                 return -1;
2666         map = isl_map_read_from_str(ctx, str);
2667         map = isl_map_neg(map);
2668         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2669                 return -1;
2670
2671         return 0;
2672 }
2673
2674 int test_vertices(isl_ctx *ctx)
2675 {
2676         const char *str;
2677         isl_basic_set *bset;
2678         isl_vertices *vertices;
2679
2680         str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
2681         bset = isl_basic_set_read_from_str(ctx, str);
2682         vertices = isl_basic_set_compute_vertices(bset);
2683         isl_basic_set_free(bset);
2684         isl_vertices_free(vertices);
2685         if (!vertices)
2686                 return -1;
2687
2688         str = "{ A[t, i] : t = 14 and i = 1 }";
2689         bset = isl_basic_set_read_from_str(ctx, str);
2690         vertices = isl_basic_set_compute_vertices(bset);
2691         isl_basic_set_free(bset);
2692         isl_vertices_free(vertices);
2693         if (!vertices)
2694                 return -1;
2695
2696         return 0;
2697 }
2698
2699 int test_union_pw(isl_ctx *ctx)
2700 {
2701         int equal;
2702         const char *str;
2703         isl_union_set *uset;
2704         isl_union_pw_qpolynomial *upwqp1, *upwqp2;
2705
2706         str = "{ [x] -> x^2 }";
2707         upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
2708         upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
2709         uset = isl_union_pw_qpolynomial_domain(upwqp1);
2710         upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
2711         upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
2712         equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
2713         isl_union_pw_qpolynomial_free(upwqp1);
2714         isl_union_pw_qpolynomial_free(upwqp2);
2715         if (equal < 0)
2716                 return -1;
2717         if (!equal)
2718                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2719
2720         return 0;
2721 }
2722
2723 int test_output(isl_ctx *ctx)
2724 {
2725         char *s;
2726         const char *str;
2727         isl_pw_aff *pa;
2728         isl_printer *p;
2729         int equal;
2730
2731         str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
2732         pa = isl_pw_aff_read_from_str(ctx, str);
2733
2734         p = isl_printer_to_str(ctx);
2735         p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2736         p = isl_printer_print_pw_aff(p, pa);
2737         s = isl_printer_get_str(p);
2738         isl_printer_free(p);
2739         isl_pw_aff_free(pa);
2740         equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
2741         free(s);
2742         if (equal < 0)
2743                 return -1;
2744         if (!equal)
2745                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2746
2747         return 0;
2748 }
2749
2750 int test_sample(isl_ctx *ctx)
2751 {
2752         const char *str;
2753         isl_basic_set *bset1, *bset2;
2754         int empty, subset;
2755
2756         str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
2757             "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
2758             "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
2759             "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
2760             "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
2761             "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
2762             "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
2763             "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
2764             "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
2765             "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
2766             "3i >= 1 + 2b - 2c + e + 2f + 3g and "
2767             "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
2768                 "d - 1073741821e and "
2769             "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
2770             "3j >= 1 - a + b + 2e and "
2771             "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
2772             "3i <= 4 - a + 4b - e and "
2773             "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
2774             "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
2775             "c <= -1 + a and 3i >= -2 - a + 3e and "
2776             "1073741822e <= 1073741823 - a + 1073741822b + c and "
2777             "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
2778             "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
2779             "1073741823e >= 1 + 1073741823b - d and "
2780             "3i >= 1073741823b + c - 1073741823e - f and "
2781             "3i >= 1 + 2b + e + 3g }";
2782         bset1 = isl_basic_set_read_from_str(ctx, str);
2783         bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
2784         empty = isl_basic_set_is_empty(bset2);
2785         subset = isl_basic_set_is_subset(bset2, bset1);
2786         isl_basic_set_free(bset1);
2787         isl_basic_set_free(bset2);
2788         if (empty)
2789                 isl_die(ctx, isl_error_unknown, "point not found", return -1);
2790         if (!subset)
2791                 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
2792
2793         return 0;
2794 }
2795
2796 int test_fixed_power(isl_ctx *ctx)
2797 {
2798         const char *str;
2799         isl_map *map;
2800         isl_int exp;
2801         int equal;
2802
2803         isl_int_init(exp);
2804         str = "{ [i] -> [i + 1] }";
2805         map = isl_map_read_from_str(ctx, str);
2806         isl_int_set_si(exp, 23);
2807         map = isl_map_fixed_power(map, exp);
2808         equal = map_check_equal(map, "{ [i] -> [i + 23] }");
2809         isl_int_clear(exp);
2810         isl_map_free(map);
2811         if (equal < 0)
2812                 return -1;
2813
2814         return 0;
2815 }
2816
2817 int main()
2818 {
2819         struct isl_ctx *ctx;
2820
2821         srcdir = getenv("srcdir");
2822         assert(srcdir);
2823
2824         ctx = isl_ctx_alloc();
2825         if (test_fixed_power(ctx) < 0)
2826                 goto error;
2827         if (test_sample(ctx) < 0)
2828                 goto error;
2829         if (test_output(ctx) < 0)
2830                 goto error;
2831         if (test_vertices(ctx) < 0)
2832                 goto error;
2833         if (test_fixed(ctx) < 0)
2834                 goto error;
2835         if (test_equal(ctx) < 0)
2836                 goto error;
2837         if (test_product(ctx) < 0)
2838                 goto error;
2839         if (test_dim_max(ctx) < 0)
2840                 goto error;
2841         if (test_aff(ctx) < 0)
2842                 goto error;
2843         if (test_injective(ctx) < 0)
2844                 goto error;
2845         if (test_schedule(ctx) < 0)
2846                 goto error;
2847         if (test_union_pw(ctx) < 0)
2848                 goto error;
2849         test_factorize(ctx);
2850         test_subset(ctx);
2851         test_lift(ctx);
2852         test_bound(ctx);
2853         test_union(ctx);
2854         test_split_periods(ctx);
2855         if (test_parse(ctx) < 0)
2856                 goto error;
2857         test_pwqp(ctx);
2858         test_lex(ctx);
2859         if (test_sv(ctx) < 0)
2860                 goto error;
2861         test_bijective(ctx);
2862         test_dep(ctx);
2863         test_read(ctx);
2864         test_bounded(ctx);
2865         test_construction(ctx);
2866         test_dim(ctx);
2867         test_div(ctx);
2868         test_application(ctx);
2869         if (test_affine_hull(ctx) < 0)
2870                 goto error;
2871         test_convex_hull(ctx);
2872         test_gist(ctx);
2873         if (test_coalesce(ctx) < 0)
2874                 goto error;
2875         test_closure(ctx);
2876         test_lexmin(ctx);
2877         isl_ctx_free(ctx);
2878         return 0;
2879 error:
2880         isl_ctx_free(ctx);
2881         return -1;
2882 }