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