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