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