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