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