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