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