fb76444c00e56483deb099e347c2717733578ee4
[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_unbounded_wrapping(isl_ctx *ctx)
852 {
853         int r = 0;
854         int bounded;
855
856         bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
857         isl_options_set_coalesce_bounded_wrapping(ctx, 0);
858
859         if (test_coalesce_set(ctx,
860                 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
861                         "-x - y + 1 >= 0 and -3 <= z <= 3;"
862                 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
863                         "x-z + 20 >= 0 and x+z + 20 >= 0 and "
864                         "-10 <= y <= 0}", 1) < 0)
865                 goto error;
866         if (test_coalesce_set(ctx,
867                 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
868                 goto error;
869         if (test_coalesce_set(ctx,
870                 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
871                 goto error;
872
873         if (0) {
874 error:
875                 r = -1;
876         }
877
878         isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
879
880         return r;
881 }
882
883 int test_coalesce(struct isl_ctx *ctx)
884 {
885         const char *str;
886         struct isl_set *set, *set2;
887         struct isl_map *map, *map2;
888
889         set = isl_set_read_from_str(ctx,
890                 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
891                        "y >= x & x >= 2 & 5 >= y }");
892         set = isl_set_coalesce(set);
893         assert(set && set->n == 1);
894         isl_set_free(set);
895
896         set = isl_set_read_from_str(ctx,
897                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
898                        "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}");
899         set = isl_set_coalesce(set);
900         assert(set && set->n == 1);
901         isl_set_free(set);
902
903         set = isl_set_read_from_str(ctx,
904                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
905                        "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}");
906         set = isl_set_coalesce(set);
907         assert(set && set->n == 2);
908         isl_set_free(set);
909
910         set = isl_set_read_from_str(ctx,
911                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
912                        "y >= 0 & x >= 6 & x <= 10 & y <= x}");
913         set = isl_set_coalesce(set);
914         assert(set && set->n == 1);
915         isl_set_free(set);
916
917         set = isl_set_read_from_str(ctx,
918                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
919                        "y >= 0 & x >= 7 & x <= 10 & y <= x}");
920         set = isl_set_coalesce(set);
921         assert(set && set->n == 2);
922         isl_set_free(set);
923
924         set = isl_set_read_from_str(ctx,
925                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
926                        "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}");
927         set = isl_set_coalesce(set);
928         assert(set && set->n == 2);
929         isl_set_free(set);
930
931         set = isl_set_read_from_str(ctx,
932                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
933                        "y >= 0 & x = 6 & y <= 6}");
934         set = isl_set_coalesce(set);
935         assert(set && set->n == 1);
936         isl_set_free(set);
937
938         set = isl_set_read_from_str(ctx,
939                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
940                        "y >= 0 & x = 7 & y <= 6}");
941         set = isl_set_coalesce(set);
942         assert(set && set->n == 2);
943         isl_set_free(set);
944
945         set = isl_set_read_from_str(ctx,
946                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
947                        "y >= 0 & x = 6 & y <= 5}");
948         set = isl_set_coalesce(set);
949         assert(set && set->n == 1);
950         set2 = isl_set_read_from_str(ctx,
951                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
952                        "y >= 0 & x = 6 & y <= 5}");
953         assert(isl_set_is_equal(set, set2));
954         isl_set_free(set);
955         isl_set_free(set2);
956
957         set = isl_set_read_from_str(ctx,
958                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
959                        "y >= 0 & x = 6 & y <= 7}");
960         set = isl_set_coalesce(set);
961         assert(set && set->n == 2);
962         isl_set_free(set);
963
964         set = isl_set_read_from_str(ctx,
965                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
966         set = isl_set_coalesce(set);
967         assert(set && set->n == 1);
968         set2 = isl_set_read_from_str(ctx,
969                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
970         assert(isl_set_is_equal(set, set2));
971         isl_set_free(set);
972         isl_set_free(set2);
973
974         set = isl_set_read_from_str(ctx,
975                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
976         set = isl_set_coalesce(set);
977         set2 = isl_set_read_from_str(ctx,
978                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
979         assert(isl_set_is_equal(set, set2));
980         isl_set_free(set);
981         isl_set_free(set2);
982
983         set = isl_set_read_from_str(ctx,
984                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
985                                 "2 <= i and i <= n }");
986         set = isl_set_coalesce(set);
987         assert(set && set->n == 1);
988         set2 = isl_set_read_from_str(ctx,
989                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
990                                 "2 <= i and i <= n }");
991         assert(isl_set_is_equal(set, set2));
992         isl_set_free(set);
993         isl_set_free(set2);
994
995         map = isl_map_read_from_str(ctx,
996                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
997                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
998                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
999                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1000                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1001                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1002                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1003                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1004                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1005                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1006                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1007                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1008                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1009                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1010                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1011                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1012                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1013                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
1014         map = isl_map_coalesce(map);
1015         map2 = isl_map_read_from_str(ctx,
1016                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1017                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1018                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1019                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1020                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1021                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1022                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1023                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1024                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1025                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1026                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1027                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1028                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1029                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1030                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1031                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1032                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1033                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
1034         assert(isl_map_is_equal(map, map2));
1035         isl_map_free(map);
1036         isl_map_free(map2);
1037
1038         str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1039               "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1040               "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1041               "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
1042         map = isl_map_read_from_str(ctx, str);
1043         map = isl_map_coalesce(map);
1044         map2 = isl_map_read_from_str(ctx, str);
1045         assert(isl_map_is_equal(map, map2));
1046         isl_map_free(map);
1047         isl_map_free(map2);
1048
1049         str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
1050           "[o0, o1, o2, o3, o4, o5, o6] : "
1051           "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1052           "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1053           "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1054           "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1055           "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1056           "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1057           "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1058           "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1059           "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1060           "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1061           "o6 >= i3 + i6 - o3 and M >= 0 and "
1062           "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1063           "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
1064         map = isl_map_read_from_str(ctx, str);
1065         map = isl_map_coalesce(map);
1066         map2 = isl_map_read_from_str(ctx, str);
1067         assert(isl_map_is_equal(map, map2));
1068         isl_map_free(map);
1069         isl_map_free(map2);
1070
1071         str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1072                 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1073                 "(o0 = 0 and M >= 2 and N >= 3) or "
1074                 "(M = 0 and o0 = 0 and N >= 3) }";
1075         map = isl_map_read_from_str(ctx, str);
1076         map = isl_map_coalesce(map);
1077         map2 = isl_map_read_from_str(ctx, str);
1078         assert(isl_map_is_equal(map, map2));
1079         isl_map_free(map);
1080         isl_map_free(map2);
1081
1082         str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1083                 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1084                 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1085                 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
1086         map = isl_map_read_from_str(ctx, str);
1087         map = isl_map_coalesce(map);
1088         map2 = isl_map_read_from_str(ctx, str);
1089         assert(isl_map_is_equal(map, map2));
1090         isl_map_free(map);
1091         isl_map_free(map2);
1092
1093         test_coalesce_set(ctx,
1094                 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
1095                                 "(i1 = M and M >= 1) }", 0);
1096         test_coalesce_set(ctx,
1097                 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
1098         test_coalesce_set(ctx,
1099                 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1100                 "(y = 3 and x = 1) }", 1);
1101         test_coalesce_set(ctx,
1102                 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1103                 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1104                 "i1 <= M and i3 <= M and i4 <= M) or "
1105                 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1106                 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1107                 "i4 <= -1 + M) }", 1);
1108         test_coalesce_set(ctx,
1109                 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1110                 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
1111         if (test_coalesce_unbounded_wrapping(ctx) < 0)
1112                 return -1;
1113         if (test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0) < 0)
1114                 return -1;
1115         if (test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1) < 0)
1116                 return -1;
1117         if (test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1) < 0)
1118                 return -1;
1119         if (test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0) < 0)
1120                 return -1;
1121         if (test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1) < 0)
1122                 return -1;
1123         if (test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0) < 0)
1124                 return -1;
1125         if (test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1126                 return -1;
1127         if (test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1128                 return -1;
1129         if (test_coalesce_set(ctx, "{ [a, b] : exists e : 2e = a and "
1130                     "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }", 0) < 0)
1131                 return -1;
1132         if (test_coalesce_set(ctx,
1133                 "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1134                         "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1135                         "j >= 1 and j' <= i + j - i' and i >= 1; "
1136                 "[1, 1, 1, 1] }", 0) < 0)
1137                 return -1;
1138         if (test_coalesce_set(ctx, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1139                                      "[i,j] : exists a : j = 3a }", 1) < 0)
1140                 return -1;
1141         return 0;
1142 }
1143
1144 void test_closure(struct isl_ctx *ctx)
1145 {
1146         const char *str;
1147         isl_set *dom;
1148         isl_map *up, *right;
1149         isl_map *map, *map2;
1150         int exact;
1151
1152         /* COCOA example 1 */
1153         map = isl_map_read_from_str(ctx,
1154                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1155                         "1 <= i and i < n and 1 <= j and j < n or "
1156                         "i2 = i + 1 and j2 = j - 1 and "
1157                         "1 <= i and i < n and 2 <= j and j <= n }");
1158         map = isl_map_power(map, &exact);
1159         assert(exact);
1160         isl_map_free(map);
1161
1162         /* COCOA example 1 */
1163         map = isl_map_read_from_str(ctx,
1164                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1165                         "1 <= i and i < n and 1 <= j and j < n or "
1166                         "i2 = i + 1 and j2 = j - 1 and "
1167                         "1 <= i and i < n and 2 <= j and j <= n }");
1168         map = isl_map_transitive_closure(map, &exact);
1169         assert(exact);
1170         map2 = isl_map_read_from_str(ctx,
1171                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1172                         "1 <= i and i < n and 1 <= j and j <= n and "
1173                         "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1174                         "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1175                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1176         assert(isl_map_is_equal(map, map2));
1177         isl_map_free(map2);
1178         isl_map_free(map);
1179
1180         map = isl_map_read_from_str(ctx,
1181                 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1182                                      " 0 <= y and y <= n }");
1183         map = isl_map_transitive_closure(map, &exact);
1184         map2 = isl_map_read_from_str(ctx,
1185                 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1186                                      " 0 <= y and y <= n }");
1187         assert(isl_map_is_equal(map, map2));
1188         isl_map_free(map2);
1189         isl_map_free(map);
1190
1191         /* COCOA example 2 */
1192         map = isl_map_read_from_str(ctx,
1193                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1194                         "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1195                         "i2 = i + 2 and j2 = j - 2 and "
1196                         "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1197         map = isl_map_transitive_closure(map, &exact);
1198         assert(exact);
1199         map2 = isl_map_read_from_str(ctx,
1200                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1201                         "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1202                         "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1203                         "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1204                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1205         assert(isl_map_is_equal(map, map2));
1206         isl_map_free(map);
1207         isl_map_free(map2);
1208
1209         /* COCOA Fig.2 left */
1210         map = isl_map_read_from_str(ctx,
1211                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1212                         "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1213                         "j <= n or "
1214                         "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1215                         "j <= 2 i - 3 and j <= n - 2 or "
1216                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1217                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1218         map = isl_map_transitive_closure(map, &exact);
1219         assert(exact);
1220         isl_map_free(map);
1221
1222         /* COCOA Fig.2 right */
1223         map = isl_map_read_from_str(ctx,
1224                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1225                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1226                         "j <= n or "
1227                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1228                         "j <= 2 i - 4 and j <= n - 3 or "
1229                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1230                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1231         map = isl_map_power(map, &exact);
1232         assert(exact);
1233         isl_map_free(map);
1234
1235         /* COCOA Fig.2 right */
1236         map = isl_map_read_from_str(ctx,
1237                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1238                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1239                         "j <= n or "
1240                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1241                         "j <= 2 i - 4 and j <= n - 3 or "
1242                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1243                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1244         map = isl_map_transitive_closure(map, &exact);
1245         assert(exact);
1246         map2 = isl_map_read_from_str(ctx,
1247                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1248                         "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1249                         "j <= n and 3 + i + 2 j <= 3 n and "
1250                         "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1251                         "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1252                         "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1253                         "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1254                         "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1255         assert(isl_map_is_equal(map, map2));
1256         isl_map_free(map2);
1257         isl_map_free(map);
1258
1259         /* COCOA Fig.1 right */
1260         dom = isl_set_read_from_str(ctx,
1261                 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1262                         "2 x - 3 y + 3 >= 0 }");
1263         right = isl_map_read_from_str(ctx,
1264                 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1265         up = isl_map_read_from_str(ctx,
1266                 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1267         right = isl_map_intersect_domain(right, isl_set_copy(dom));
1268         right = isl_map_intersect_range(right, isl_set_copy(dom));
1269         up = isl_map_intersect_domain(up, isl_set_copy(dom));
1270         up = isl_map_intersect_range(up, dom);
1271         map = isl_map_union(up, right);
1272         map = isl_map_transitive_closure(map, &exact);
1273         assert(exact);
1274         map2 = isl_map_read_from_str(ctx,
1275                 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1276                 "  [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1277         assert(isl_map_is_equal(map, map2));
1278         isl_map_free(map2);
1279         isl_map_free(map);
1280
1281         /* COCOA Theorem 1 counter example */
1282         map = isl_map_read_from_str(ctx,
1283                 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1284                         "i2 = 1 and j2 = j or "
1285                         "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1286         map = isl_map_transitive_closure(map, &exact);
1287         assert(exact);
1288         isl_map_free(map);
1289
1290         map = isl_map_read_from_str(ctx,
1291                 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1292                         "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1293                         "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1294                         "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1295         map = isl_map_transitive_closure(map, &exact);
1296         assert(exact);
1297         isl_map_free(map);
1298
1299         /* Kelly et al 1996, fig 12 */
1300         map = isl_map_read_from_str(ctx,
1301                 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1302                         "1 <= i,j,j+1 <= n or "
1303                         "j = n and j2 = 1 and i2 = i + 1 and "
1304                         "1 <= i,i+1 <= n }");
1305         map = isl_map_transitive_closure(map, &exact);
1306         assert(exact);
1307         map2 = isl_map_read_from_str(ctx,
1308                 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1309                         "1 <= i <= n and i = i2 or "
1310                         "1 <= i < i2 <= n and 1 <= j <= n and "
1311                         "1 <= j2 <= n }");
1312         assert(isl_map_is_equal(map, map2));
1313         isl_map_free(map2);
1314         isl_map_free(map);
1315
1316         /* Omega's closure4 */
1317         map = isl_map_read_from_str(ctx,
1318                 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1319                         "1 <= x,y <= 10 or "
1320                         "x2 = x + 1 and y2 = y and "
1321                         "1 <= x <= 20 && 5 <= y <= 15 }");
1322         map = isl_map_transitive_closure(map, &exact);
1323         assert(exact);
1324         isl_map_free(map);
1325
1326         map = isl_map_read_from_str(ctx,
1327                 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1328         map = isl_map_transitive_closure(map, &exact);
1329         assert(!exact);
1330         map2 = isl_map_read_from_str(ctx,
1331                 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1332         assert(isl_map_is_equal(map, map2));
1333         isl_map_free(map);
1334         isl_map_free(map2);
1335
1336         str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1337             "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1338             "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1339             "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1340         map = isl_map_read_from_str(ctx, str);
1341         map = isl_map_transitive_closure(map, &exact);
1342         assert(exact);
1343         map2 = isl_map_read_from_str(ctx, str);
1344         assert(isl_map_is_equal(map, map2));
1345         isl_map_free(map);
1346         isl_map_free(map2);
1347
1348         str = "{[0] -> [1]; [2] -> [3]}";
1349         map = isl_map_read_from_str(ctx, str);
1350         map = isl_map_transitive_closure(map, &exact);
1351         assert(exact);
1352         map2 = isl_map_read_from_str(ctx, str);
1353         assert(isl_map_is_equal(map, map2));
1354         isl_map_free(map);
1355         isl_map_free(map2);
1356
1357         str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1358             "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1359             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1360             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1361             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1362             "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1363             "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1364             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1365             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1366             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1367             "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1368             "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1369             "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1370             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1371             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1372             "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1373             "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1374             "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1375             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1376             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1377         map = isl_map_read_from_str(ctx, str);
1378         map = isl_map_transitive_closure(map, NULL);
1379         assert(map);
1380         isl_map_free(map);
1381 }
1382
1383 void test_lex(struct isl_ctx *ctx)
1384 {
1385         isl_space *dim;
1386         isl_map *map;
1387
1388         dim = isl_space_set_alloc(ctx, 0, 0);
1389         map = isl_map_lex_le(dim);
1390         assert(!isl_map_is_empty(map));
1391         isl_map_free(map);
1392 }
1393
1394 void test_lexmin(struct isl_ctx *ctx)
1395 {
1396         const char *str;
1397         isl_basic_map *bmap;
1398         isl_map *map, *map2;
1399         isl_set *set;
1400         isl_set *set2;
1401         isl_pw_multi_aff *pma;
1402
1403         str = "[p0, p1] -> { [] -> [] : "
1404             "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1405             "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1406             "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1407             "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1408             "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1409             "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1410             "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1411             "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1412             "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1413             "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1414             "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1415         map = isl_map_read_from_str(ctx, str);
1416         map = isl_map_lexmin(map);
1417         isl_map_free(map);
1418
1419         str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1420             "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1421         set = isl_set_read_from_str(ctx, str);
1422         set = isl_set_lexmax(set);
1423         str = "[C] -> { [obj,a,b,c] : C = 8 }";
1424         set2 = isl_set_read_from_str(ctx, str);
1425         set = isl_set_intersect(set, set2);
1426         assert(!isl_set_is_empty(set));
1427         isl_set_free(set);
1428
1429         str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1430         map = isl_map_read_from_str(ctx, str);
1431         map = isl_map_lexmin(map);
1432         str = "{ [x] -> [5] : 6 <= x <= 8; "
1433                 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1434         map2 = isl_map_read_from_str(ctx, str);
1435         assert(isl_map_is_equal(map, map2));
1436         isl_map_free(map);
1437         isl_map_free(map2);
1438
1439         str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1440         map = isl_map_read_from_str(ctx, str);
1441         map2 = isl_map_copy(map);
1442         map = isl_map_lexmin(map);
1443         assert(isl_map_is_equal(map, map2));
1444         isl_map_free(map);
1445         isl_map_free(map2);
1446
1447         str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1448         map = isl_map_read_from_str(ctx, str);
1449         map = isl_map_lexmin(map);
1450         str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1451                 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1452                 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1453                 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1454         map2 = isl_map_read_from_str(ctx, str);
1455         assert(isl_map_is_equal(map, map2));
1456         isl_map_free(map);
1457         isl_map_free(map2);
1458
1459         str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1460                                 " 8i' <= i and 8i' >= -7 + i }";
1461         bmap = isl_basic_map_read_from_str(ctx, str);
1462         pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1463         map2 = isl_map_from_pw_multi_aff(pma);
1464         map = isl_map_from_basic_map(bmap);
1465         assert(isl_map_is_equal(map, map2));
1466         isl_map_free(map);
1467         isl_map_free(map2);
1468
1469         str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1470         map = isl_map_read_from_str(ctx, str);
1471         map = isl_map_lexmin(map);
1472         str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1473         map2 = isl_map_read_from_str(ctx, str);
1474         assert(isl_map_is_equal(map, map2));
1475         isl_map_free(map);
1476         isl_map_free(map2);
1477 }
1478
1479 struct must_may {
1480         isl_map *must;
1481         isl_map *may;
1482 };
1483
1484 static int collect_must_may(__isl_take isl_map *dep, int must,
1485         void *dep_user, void *user)
1486 {
1487         struct must_may *mm = (struct must_may *)user;
1488
1489         if (must)
1490                 mm->must = isl_map_union(mm->must, dep);
1491         else
1492                 mm->may = isl_map_union(mm->may, dep);
1493
1494         return 0;
1495 }
1496
1497 static int common_space(void *first, void *second)
1498 {
1499         int depth = *(int *)first;
1500         return 2 * depth;
1501 }
1502
1503 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1504 {
1505         isl_map *map2;
1506         int equal;
1507
1508         if (!map)
1509                 return -1;
1510
1511         map2 = isl_map_read_from_str(map->ctx, str);
1512         equal = isl_map_is_equal(map, map2);
1513         isl_map_free(map2);
1514
1515         return equal;
1516 }
1517
1518 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1519 {
1520         int equal;
1521
1522         equal = map_is_equal(map, str);
1523         if (equal < 0)
1524                 return -1;
1525         if (!equal)
1526                 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1527                         "result not as expected", return -1);
1528         return 0;
1529 }
1530
1531 void test_dep(struct isl_ctx *ctx)
1532 {
1533         const char *str;
1534         isl_space *dim;
1535         isl_map *map;
1536         isl_access_info *ai;
1537         isl_flow *flow;
1538         int depth;
1539         struct must_may mm;
1540
1541         depth = 3;
1542
1543         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1544         map = isl_map_read_from_str(ctx, str);
1545         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1546
1547         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1548         map = isl_map_read_from_str(ctx, str);
1549         ai = isl_access_info_add_source(ai, map, 1, &depth);
1550
1551         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1552         map = isl_map_read_from_str(ctx, str);
1553         ai = isl_access_info_add_source(ai, map, 1, &depth);
1554
1555         flow = isl_access_info_compute_flow(ai);
1556         dim = isl_space_alloc(ctx, 0, 3, 3);
1557         mm.must = isl_map_empty(isl_space_copy(dim));
1558         mm.may = isl_map_empty(dim);
1559
1560         isl_flow_foreach(flow, collect_must_may, &mm);
1561
1562         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1563               "  [1,10,0] -> [2,5,0] }";
1564         assert(map_is_equal(mm.must, str));
1565         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1566         assert(map_is_equal(mm.may, str));
1567
1568         isl_map_free(mm.must);
1569         isl_map_free(mm.may);
1570         isl_flow_free(flow);
1571
1572
1573         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1574         map = isl_map_read_from_str(ctx, str);
1575         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1576
1577         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1578         map = isl_map_read_from_str(ctx, str);
1579         ai = isl_access_info_add_source(ai, map, 1, &depth);
1580
1581         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1582         map = isl_map_read_from_str(ctx, str);
1583         ai = isl_access_info_add_source(ai, map, 0, &depth);
1584
1585         flow = isl_access_info_compute_flow(ai);
1586         dim = isl_space_alloc(ctx, 0, 3, 3);
1587         mm.must = isl_map_empty(isl_space_copy(dim));
1588         mm.may = isl_map_empty(dim);
1589
1590         isl_flow_foreach(flow, collect_must_may, &mm);
1591
1592         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1593         assert(map_is_equal(mm.must, str));
1594         str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1595         assert(map_is_equal(mm.may, str));
1596
1597         isl_map_free(mm.must);
1598         isl_map_free(mm.may);
1599         isl_flow_free(flow);
1600
1601
1602         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1603         map = isl_map_read_from_str(ctx, str);
1604         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1605
1606         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1607         map = isl_map_read_from_str(ctx, str);
1608         ai = isl_access_info_add_source(ai, map, 0, &depth);
1609
1610         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1611         map = isl_map_read_from_str(ctx, str);
1612         ai = isl_access_info_add_source(ai, map, 0, &depth);
1613
1614         flow = isl_access_info_compute_flow(ai);
1615         dim = isl_space_alloc(ctx, 0, 3, 3);
1616         mm.must = isl_map_empty(isl_space_copy(dim));
1617         mm.may = isl_map_empty(dim);
1618
1619         isl_flow_foreach(flow, collect_must_may, &mm);
1620
1621         str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1622               "  [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1623         assert(map_is_equal(mm.may, str));
1624         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1625         assert(map_is_equal(mm.must, str));
1626
1627         isl_map_free(mm.must);
1628         isl_map_free(mm.may);
1629         isl_flow_free(flow);
1630
1631
1632         str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1633         map = isl_map_read_from_str(ctx, str);
1634         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1635
1636         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1637         map = isl_map_read_from_str(ctx, str);
1638         ai = isl_access_info_add_source(ai, map, 0, &depth);
1639
1640         str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1641         map = isl_map_read_from_str(ctx, str);
1642         ai = isl_access_info_add_source(ai, map, 0, &depth);
1643
1644         flow = isl_access_info_compute_flow(ai);
1645         dim = isl_space_alloc(ctx, 0, 3, 3);
1646         mm.must = isl_map_empty(isl_space_copy(dim));
1647         mm.may = isl_map_empty(dim);
1648
1649         isl_flow_foreach(flow, collect_must_may, &mm);
1650
1651         str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1652               "  [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1653         assert(map_is_equal(mm.may, str));
1654         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1655         assert(map_is_equal(mm.must, str));
1656
1657         isl_map_free(mm.must);
1658         isl_map_free(mm.may);
1659         isl_flow_free(flow);
1660
1661
1662         str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1663         map = isl_map_read_from_str(ctx, str);
1664         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1665
1666         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1667         map = isl_map_read_from_str(ctx, str);
1668         ai = isl_access_info_add_source(ai, map, 0, &depth);
1669
1670         str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1671         map = isl_map_read_from_str(ctx, str);
1672         ai = isl_access_info_add_source(ai, map, 0, &depth);
1673
1674         flow = isl_access_info_compute_flow(ai);
1675         dim = isl_space_alloc(ctx, 0, 3, 3);
1676         mm.must = isl_map_empty(isl_space_copy(dim));
1677         mm.may = isl_map_empty(dim);
1678
1679         isl_flow_foreach(flow, collect_must_may, &mm);
1680
1681         str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1682               "  [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1683         assert(map_is_equal(mm.may, str));
1684         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1685         assert(map_is_equal(mm.must, str));
1686
1687         isl_map_free(mm.must);
1688         isl_map_free(mm.may);
1689         isl_flow_free(flow);
1690
1691
1692         depth = 5;
1693
1694         str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1695         map = isl_map_read_from_str(ctx, str);
1696         ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1697
1698         str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1699         map = isl_map_read_from_str(ctx, str);
1700         ai = isl_access_info_add_source(ai, map, 1, &depth);
1701
1702         flow = isl_access_info_compute_flow(ai);
1703         dim = isl_space_alloc(ctx, 0, 5, 5);
1704         mm.must = isl_map_empty(isl_space_copy(dim));
1705         mm.may = isl_map_empty(dim);
1706
1707         isl_flow_foreach(flow, collect_must_may, &mm);
1708
1709         str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1710         assert(map_is_equal(mm.must, str));
1711         str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1712         assert(map_is_equal(mm.may, str));
1713
1714         isl_map_free(mm.must);
1715         isl_map_free(mm.may);
1716         isl_flow_free(flow);
1717 }
1718
1719 int test_sv(isl_ctx *ctx)
1720 {
1721         const char *str;
1722         isl_map *map;
1723         isl_union_map *umap;
1724         int sv;
1725
1726         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1727         map = isl_map_read_from_str(ctx, str);
1728         sv = isl_map_is_single_valued(map);
1729         isl_map_free(map);
1730         if (sv < 0)
1731                 return -1;
1732         if (!sv)
1733                 isl_die(ctx, isl_error_internal,
1734                         "map not detected as single valued", return -1);
1735
1736         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1737         map = isl_map_read_from_str(ctx, str);
1738         sv = isl_map_is_single_valued(map);
1739         isl_map_free(map);
1740         if (sv < 0)
1741                 return -1;
1742         if (sv)
1743                 isl_die(ctx, isl_error_internal,
1744                         "map detected as single valued", return -1);
1745
1746         str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1747         umap = isl_union_map_read_from_str(ctx, str);
1748         sv = isl_union_map_is_single_valued(umap);
1749         isl_union_map_free(umap);
1750         if (sv < 0)
1751                 return -1;
1752         if (!sv)
1753                 isl_die(ctx, isl_error_internal,
1754                         "map not detected as single valued", return -1);
1755
1756         str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1757         umap = isl_union_map_read_from_str(ctx, str);
1758         sv = isl_union_map_is_single_valued(umap);
1759         isl_union_map_free(umap);
1760         if (sv < 0)
1761                 return -1;
1762         if (sv)
1763                 isl_die(ctx, isl_error_internal,
1764                         "map detected as single valued", return -1);
1765
1766         return 0;
1767 }
1768
1769 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1770 {
1771         isl_map *map;
1772
1773         map = isl_map_read_from_str(ctx, str);
1774         if (bijective)
1775                 assert(isl_map_is_bijective(map));
1776         else
1777                 assert(!isl_map_is_bijective(map));
1778         isl_map_free(map);
1779 }
1780
1781 void test_bijective(struct isl_ctx *ctx)
1782 {
1783         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1784         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1785         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1786         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1787         test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1788         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1789         test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1790         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1791         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1792         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1793         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1794         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1795         test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1796 }
1797
1798 void test_pwqp(struct isl_ctx *ctx)
1799 {
1800         const char *str;
1801         isl_set *set;
1802         isl_pw_qpolynomial *pwqp1, *pwqp2;
1803
1804         str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1805         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1806
1807         pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1808                                                 isl_dim_in, 1, 1);
1809
1810         str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1811         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1812
1813         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1814
1815         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1816
1817         isl_pw_qpolynomial_free(pwqp1);
1818
1819         str = "{ [i] -> i }";
1820         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1821         str = "{ [k] : exists a : k = 2a }";
1822         set = isl_set_read_from_str(ctx, str);
1823         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1824         str = "{ [i] -> i }";
1825         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1826
1827         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1828
1829         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1830
1831         isl_pw_qpolynomial_free(pwqp1);
1832
1833         str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1834         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1835         str = "{ [10] }";
1836         set = isl_set_read_from_str(ctx, str);
1837         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1838         str = "{ [i] -> 16 }";
1839         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1840
1841         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1842
1843         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1844
1845         isl_pw_qpolynomial_free(pwqp1);
1846
1847         str = "{ [i] -> ([(i)/2]) }";
1848         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1849         str = "{ [k] : exists a : k = 2a+1 }";
1850         set = isl_set_read_from_str(ctx, str);
1851         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1852         str = "{ [i] -> -1/2 + 1/2 * i }";
1853         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1854
1855         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1856
1857         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1858
1859         isl_pw_qpolynomial_free(pwqp1);
1860
1861         str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1862         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1863         str = "{ [i] -> ([(2 * [i/2])/5]) }";
1864         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1865
1866         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1867
1868         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1869
1870         isl_pw_qpolynomial_free(pwqp1);
1871
1872         str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1873         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1874         str = "{ [x] -> x }";
1875         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1876
1877         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1878
1879         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1880
1881         isl_pw_qpolynomial_free(pwqp1);
1882
1883         str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1884         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1885         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1886         pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1887         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1888         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1889         isl_pw_qpolynomial_free(pwqp1);
1890 }
1891
1892 void test_split_periods(isl_ctx *ctx)
1893 {
1894         const char *str;
1895         isl_pw_qpolynomial *pwqp;
1896
1897         str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1898                 "U + 2V + 3 >= 0 and - U -2V  >= 0 and - U + 10 >= 0 and "
1899                 "U  >= 0; [U,V] -> U^2 : U >= 100 }";
1900         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1901
1902         pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1903         assert(pwqp);
1904
1905         isl_pw_qpolynomial_free(pwqp);
1906 }
1907
1908 void test_union(isl_ctx *ctx)
1909 {
1910         const char *str;
1911         isl_union_set *uset1, *uset2;
1912         isl_union_map *umap1, *umap2;
1913
1914         str = "{ [i] : 0 <= i <= 1 }";
1915         uset1 = isl_union_set_read_from_str(ctx, str);
1916         str = "{ [1] -> [0] }";
1917         umap1 = isl_union_map_read_from_str(ctx, str);
1918
1919         umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1920         assert(isl_union_map_is_equal(umap1, umap2));
1921
1922         isl_union_map_free(umap1);
1923         isl_union_map_free(umap2);
1924
1925         str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1926         umap1 = isl_union_map_read_from_str(ctx, str);
1927         str = "{ A[i]; B[i] }";
1928         uset1 = isl_union_set_read_from_str(ctx, str);
1929
1930         uset2 = isl_union_map_domain(umap1);
1931
1932         assert(isl_union_set_is_equal(uset1, uset2));
1933
1934         isl_union_set_free(uset1);
1935         isl_union_set_free(uset2);
1936 }
1937
1938 void test_bound(isl_ctx *ctx)
1939 {
1940         const char *str;
1941         isl_pw_qpolynomial *pwqp;
1942         isl_pw_qpolynomial_fold *pwf;
1943
1944         str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1945         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1946         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1947         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1948         isl_pw_qpolynomial_fold_free(pwf);
1949
1950         str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1951         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1952         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1953         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1954         isl_pw_qpolynomial_fold_free(pwf);
1955 }
1956
1957 void test_lift(isl_ctx *ctx)
1958 {
1959         const char *str;
1960         isl_basic_map *bmap;
1961         isl_basic_set *bset;
1962
1963         str = "{ [i0] : exists e0 : i0 = 4e0 }";
1964         bset = isl_basic_set_read_from_str(ctx, str);
1965         bset = isl_basic_set_lift(bset);
1966         bmap = isl_basic_map_from_range(bset);
1967         bset = isl_basic_map_domain(bmap);
1968         isl_basic_set_free(bset);
1969 }
1970
1971 void test_subset(isl_ctx *ctx)
1972 {
1973         const char *str;
1974         isl_set *set1, *set2;
1975
1976         str = "{ [112, 0] }";
1977         set1 = isl_set_read_from_str(ctx, str);
1978         str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1979                 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1980                 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1981         set2 = isl_set_read_from_str(ctx, str);
1982         assert(isl_set_is_subset(set1, set2));
1983         isl_set_free(set1);
1984         isl_set_free(set2);
1985 }
1986
1987 void test_factorize(isl_ctx *ctx)
1988 {
1989         const char *str;
1990         isl_basic_set *bset;
1991         isl_factorizer *f;
1992
1993         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1994             "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1995             "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1996             "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1997             "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1998             "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1999             "3i5 >= -2i0 - i2 + 3i4 }";
2000         bset = isl_basic_set_read_from_str(ctx, str);
2001         f = isl_basic_set_factorizer(bset);
2002         assert(f);
2003         isl_basic_set_free(bset);
2004         isl_factorizer_free(f);
2005
2006         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2007             "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2008             "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2009             "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2010             "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2011             "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2012             "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2013             "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2014             "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2015             "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2016         bset = isl_basic_set_read_from_str(ctx, str);
2017         f = isl_basic_set_factorizer(bset);
2018         assert(f);
2019         isl_basic_set_free(bset);
2020         isl_factorizer_free(f);
2021 }
2022
2023 static int check_injective(__isl_take isl_map *map, void *user)
2024 {
2025         int *injective = user;
2026
2027         *injective = isl_map_is_injective(map);
2028         isl_map_free(map);
2029
2030         if (*injective < 0 || !*injective)
2031                 return -1;
2032
2033         return 0;
2034 }
2035
2036 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2037         const char *r, const char *s, int tilable, int parallel)
2038 {
2039         int i;
2040         isl_union_set *D;
2041         isl_union_map *W, *R, *S;
2042         isl_union_map *empty;
2043         isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2044         isl_union_map *validity, *proximity;
2045         isl_union_map *schedule;
2046         isl_union_map *test;
2047         isl_union_set *delta;
2048         isl_union_set *domain;
2049         isl_set *delta_set;
2050         isl_set *slice;
2051         isl_set *origin;
2052         isl_schedule *sched;
2053         int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2054
2055         D = isl_union_set_read_from_str(ctx, d);
2056         W = isl_union_map_read_from_str(ctx, w);
2057         R = isl_union_map_read_from_str(ctx, r);
2058         S = isl_union_map_read_from_str(ctx, s);
2059
2060         W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2061         R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2062
2063         empty = isl_union_map_empty(isl_union_map_get_space(S));
2064         isl_union_map_compute_flow(isl_union_map_copy(R),
2065                                    isl_union_map_copy(W), empty,
2066                                    isl_union_map_copy(S),
2067                                    &dep_raw, NULL, NULL, NULL);
2068         isl_union_map_compute_flow(isl_union_map_copy(W),
2069                                    isl_union_map_copy(W),
2070                                    isl_union_map_copy(R),
2071                                    isl_union_map_copy(S),
2072                                    &dep_waw, &dep_war, NULL, NULL);
2073
2074         dep = isl_union_map_union(dep_waw, dep_war);
2075         dep = isl_union_map_union(dep, dep_raw);
2076         validity = isl_union_map_copy(dep);
2077         proximity = isl_union_map_copy(dep);
2078
2079         sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
2080                                                validity, proximity);
2081         schedule = isl_schedule_get_map(sched);
2082         isl_schedule_free(sched);
2083         isl_union_map_free(W);
2084         isl_union_map_free(R);
2085         isl_union_map_free(S);
2086
2087         is_injection = 1;
2088         isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2089
2090         domain = isl_union_map_domain(isl_union_map_copy(schedule));
2091         is_complete = isl_union_set_is_subset(D, domain);
2092         isl_union_set_free(D);
2093         isl_union_set_free(domain);
2094
2095         test = isl_union_map_reverse(isl_union_map_copy(schedule));
2096         test = isl_union_map_apply_range(test, dep);
2097         test = isl_union_map_apply_range(test, schedule);
2098
2099         delta = isl_union_map_deltas(test);
2100         if (isl_union_set_n_set(delta) == 0) {
2101                 is_tilable = 1;
2102                 is_parallel = 1;
2103                 is_nonneg = 1;
2104                 isl_union_set_free(delta);
2105         } else {
2106                 delta_set = isl_set_from_union_set(delta);
2107
2108                 slice = isl_set_universe(isl_set_get_space(delta_set));
2109                 for (i = 0; i < tilable; ++i)
2110                         slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2111                 is_tilable = isl_set_is_subset(delta_set, slice);
2112                 isl_set_free(slice);
2113
2114                 slice = isl_set_universe(isl_set_get_space(delta_set));
2115                 for (i = 0; i < parallel; ++i)
2116                         slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2117                 is_parallel = isl_set_is_subset(delta_set, slice);
2118                 isl_set_free(slice);
2119
2120                 origin = isl_set_universe(isl_set_get_space(delta_set));
2121                 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2122                         origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2123
2124                 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2125                 delta_set = isl_set_lexmin(delta_set);
2126
2127                 is_nonneg = isl_set_is_equal(delta_set, origin);
2128
2129                 isl_set_free(origin);
2130                 isl_set_free(delta_set);
2131         }
2132
2133         if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2134             is_injection < 0 || is_complete < 0)
2135                 return -1;
2136         if (!is_complete)
2137                 isl_die(ctx, isl_error_unknown,
2138                         "generated schedule incomplete", return -1);
2139         if (!is_injection)
2140                 isl_die(ctx, isl_error_unknown,
2141                         "generated schedule not injective on each statement",
2142                         return -1);
2143         if (!is_nonneg)
2144                 isl_die(ctx, isl_error_unknown,
2145                         "negative dependences in generated schedule",
2146                         return -1);
2147         if (!is_tilable)
2148                 isl_die(ctx, isl_error_unknown,
2149                         "generated schedule not as tilable as expected",
2150                         return -1);
2151         if (!is_parallel)
2152                 isl_die(ctx, isl_error_unknown,
2153                         "generated schedule not as parallel as expected",
2154                         return -1);
2155
2156         return 0;
2157 }
2158
2159 int test_special_schedule(isl_ctx *ctx, const char *domain,
2160         const char *validity, const char *proximity, const char *expected_sched)
2161 {
2162         isl_union_set *dom;
2163         isl_union_map *dep;
2164         isl_union_map *prox;
2165         isl_union_map *sched1, *sched2;
2166         isl_schedule *schedule;
2167         int equal;
2168
2169         dom = isl_union_set_read_from_str(ctx, domain);
2170         dep = isl_union_map_read_from_str(ctx, validity);
2171         prox = isl_union_map_read_from_str(ctx, proximity);
2172         schedule = isl_union_set_compute_schedule(dom, dep, prox);
2173         sched1 = isl_schedule_get_map(schedule);
2174         isl_schedule_free(schedule);
2175
2176         sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2177
2178         equal = isl_union_map_is_equal(sched1, sched2);
2179         isl_union_map_free(sched1);
2180         isl_union_map_free(sched2);
2181
2182         if (equal < 0)
2183                 return -1;
2184         if (!equal)
2185                 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2186                         return -1);
2187
2188         return 0;
2189 }
2190
2191 int test_schedule(isl_ctx *ctx)
2192 {
2193         const char *D, *W, *R, *V, *P, *S;
2194
2195         /* Jacobi */
2196         D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2197         W = "{ S1[t,i] -> a[t,i] }";
2198         R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2199                 "S1[t,i] -> a[t-1,i+1] }";
2200         S = "{ S1[t,i] -> [t,i] }";
2201         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2202                 return -1;
2203
2204         /* Fig. 5 of CC2008 */
2205         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2206                                 "j <= -1 + N }";
2207         W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2208                                 "j >= 2 and j <= -1 + N }";
2209         R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2210                                 "j >= 2 and j <= -1 + N; "
2211                     "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2212                                 "j >= 2 and j <= -1 + N }";
2213         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2214         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2215                 return -1;
2216
2217         D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2218         W = "{ S1[i] -> a[i] }";
2219         R = "{ S2[i] -> a[i+1] }";
2220         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2221         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2222                 return -1;
2223
2224         D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2225         W = "{ S1[i] -> a[i] }";
2226         R = "{ S2[i] -> a[9-i] }";
2227         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2228         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2229                 return -1;
2230
2231         D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2232         W = "{ S1[i] -> a[i] }";
2233         R = "[N] -> { S2[i] -> a[N-1-i] }";
2234         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2235         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2236                 return -1;
2237         
2238         D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2239         W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2240         R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2241         S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2242         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2243                 return -1;
2244
2245         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2246         W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2247         R = "{ S2[i,j] -> a[i-1,j] }";
2248         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2249         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2250                 return -1;
2251
2252         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2253         W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2254         R = "{ S2[i,j] -> a[i,j-1] }";
2255         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2256         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2257                 return -1;
2258
2259         D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2260         W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2261                     "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2262         R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2263         S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2264                     "S_0[] -> [0, 0, 0] }";
2265         if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2266                 return -1;
2267         ctx->opt->schedule_parametric = 0;
2268         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2269                 return -1;
2270         ctx->opt->schedule_parametric = 1;
2271
2272         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2273                     "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2274         W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2275         R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2276                     "S4[i] -> a[i,N] }";
2277         S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2278                 "S4[i] -> [4,i,0] }";
2279         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2280                 return -1;
2281
2282         D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2283         W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2284                                         "j <= N }";
2285         R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2286                                         "j <= N; "
2287                     "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2288                                         "j <= N }";
2289         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2290         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2291                 return -1;
2292
2293         D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2294                     " S_2[t] : t >= 0 and t <= -1 + N; "
2295                     " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2296                                 "i <= -1 + N }";
2297         W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2298                     " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2299                     " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2300                                                 "i >= 0 and i <= -1 + N }";
2301         R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2302                                             "i >= 0 and i <= -1 + N; "
2303                     " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2304         S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2305                     " S_0[t] -> [0, t, 0] }";
2306
2307         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2308                 return -1;
2309         ctx->opt->schedule_parametric = 0;
2310         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2311                 return -1;
2312         ctx->opt->schedule_parametric = 1;
2313
2314         D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2315         S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2316         if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2317                 return -1;
2318
2319         D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2320             "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2321         W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2322                                             "j >= 0 and j <= -1 + N; "
2323                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2324         R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2325                                             "j >= 0 and j <= -1 + N; "
2326                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2327         S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2328         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2329                 return -1;
2330
2331         D = "{ S_0[i] : i >= 0 }";
2332         W = "{ S_0[i] -> a[i] : i >= 0 }";
2333         R = "{ S_0[i] -> a[0] : i >= 0 }";
2334         S = "{ S_0[i] -> [0, i, 0] }";
2335         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2336                 return -1;
2337
2338         D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2339         W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2340         R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2341         S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2342         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2343                 return -1;
2344
2345         D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2346                                 "k <= -1 + n and k >= 0 }";
2347         W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "                                                  "k <= -1 + n and k >= 0 }";
2348         R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2349                                         "k <= -1 + n and k >= 0; "
2350                     "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2351                                         "k <= -1 + n and k >= 0; "
2352                     "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2353                                         "k <= -1 + n and k >= 0 }";
2354         S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2355         ctx->opt->schedule_outer_zero_distance = 1;
2356         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2357                 return -1;
2358         ctx->opt->schedule_outer_zero_distance = 0;
2359
2360         D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2361                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2362                 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2363              "Stmt_for_body24[i0, i1, 1, 0]:"
2364                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2365              "Stmt_for_body7[i0, i1, i2]:"
2366                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2367                 "i2 <= 7 }";
2368
2369         V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2370                 "Stmt_for_body24[1, i1, i2, i3]:"
2371                 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2372                 "i2 >= 1;"
2373              "Stmt_for_body24[0, i1, i2, i3] -> "
2374                 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2375                 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2376                 "i3 >= 0;"
2377               "Stmt_for_body24[0, i1, i2, i3] ->"
2378                 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2379                 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2380               "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2381                 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2382                 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2383                 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2384               "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2385                 "i1 <= 6 and i1 >= 0;"
2386               "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2387               "Stmt_for_body7[i0, i1, i2] -> "
2388                 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2389                 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2390                 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2391               "Stmt_for_body7[i0, i1, i2] -> "
2392                 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2393                 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2394                 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2395         P = V;
2396         S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2397                 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2398             "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2399
2400         if (test_special_schedule(ctx, D, V, P, S) < 0)
2401                 return -1;
2402
2403         D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2404         V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2405                                            "j >= 1 and j <= 7;"
2406                 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2407                                              "j >= 1 and j <= 8 }";
2408         P = "{ }";
2409         S = "{ S_0[i, j] -> [i + j, j] }";
2410         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2411         if (test_special_schedule(ctx, D, V, P, S) < 0)
2412                 return -1;
2413         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2414
2415         /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2416         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2417                                  "j >= 0 and j <= -1 + i }";
2418         V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2419                                         "i <= -1 + N and j >= 0;"
2420                      "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2421                                         "i <= -2 + N }";
2422         P = "{ }";
2423         S = "{ S_0[i, j] -> [i, j] }";
2424         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2425         if (test_special_schedule(ctx, D, V, P, S) < 0)
2426                 return -1;
2427         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2428
2429         /* Test both algorithms on a case with only proximity dependences. */
2430         D = "{ S[i,j] : 0 <= i <= 10 }";
2431         V = "{ }";
2432         P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2433         S = "{ S[i, j] -> [j, i] }";
2434         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2435         if (test_special_schedule(ctx, D, V, P, S) < 0)
2436                 return -1;
2437         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2438         return test_special_schedule(ctx, D, V, P, S);
2439 }
2440
2441 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2442 {
2443         isl_union_map *umap;
2444         int test;
2445
2446         umap = isl_union_map_read_from_str(ctx, str);
2447         test = isl_union_map_plain_is_injective(umap);
2448         isl_union_map_free(umap);
2449         if (test < 0)
2450                 return -1;
2451         if (test == injective)
2452                 return 0;
2453         if (injective)
2454                 isl_die(ctx, isl_error_unknown,
2455                         "map not detected as injective", return -1);
2456         else
2457                 isl_die(ctx, isl_error_unknown,
2458                         "map detected as injective", return -1);
2459 }
2460
2461 int test_injective(isl_ctx *ctx)
2462 {
2463         const char *str;
2464
2465         if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2466                 return -1;
2467         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2468                 return -1;
2469         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2470                 return -1;
2471         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2472                 return -1;
2473         if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2474                 return -1;
2475         if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2476                 return -1;
2477         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2478                 return -1;
2479         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2480                 return -1;
2481
2482         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2483         if (test_plain_injective(ctx, str, 1))
2484                 return -1;
2485         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2486         if (test_plain_injective(ctx, str, 0))
2487                 return -1;
2488
2489         return 0;
2490 }
2491
2492 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
2493 {
2494         isl_aff *aff2;
2495         int equal;
2496
2497         if (!aff)
2498                 return -1;
2499
2500         aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
2501         equal = isl_aff_plain_is_equal(aff, aff2);
2502         isl_aff_free(aff2);
2503
2504         return equal;
2505 }
2506
2507 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
2508 {
2509         int equal;
2510
2511         equal = aff_plain_is_equal(aff, str);
2512         if (equal < 0)
2513                 return -1;
2514         if (!equal)
2515                 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
2516                         "result not as expected", return -1);
2517         return 0;
2518 }
2519
2520 int test_aff(isl_ctx *ctx)
2521 {
2522         const char *str;
2523         isl_set *set;
2524         isl_space *space;
2525         isl_local_space *ls;
2526         isl_aff *aff;
2527         int zero, equal;
2528
2529         space = isl_space_set_alloc(ctx, 0, 1);
2530         ls = isl_local_space_from_space(space);
2531         aff = isl_aff_zero_on_domain(ls);
2532
2533         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2534         aff = isl_aff_scale_down_ui(aff, 3);
2535         aff = isl_aff_floor(aff);
2536         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2537         aff = isl_aff_scale_down_ui(aff, 2);
2538         aff = isl_aff_floor(aff);
2539         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2540
2541         str = "{ [10] }";
2542         set = isl_set_read_from_str(ctx, str);
2543         aff = isl_aff_gist(aff, set);
2544
2545         aff = isl_aff_add_constant_si(aff, -16);
2546         zero = isl_aff_plain_is_zero(aff);
2547         isl_aff_free(aff);
2548
2549         if (zero < 0)
2550                 return -1;
2551         if (!zero)
2552                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2553
2554         aff = isl_aff_read_from_str(ctx, "{ [-1] }");
2555         aff = isl_aff_scale_down_ui(aff, 64);
2556         aff = isl_aff_floor(aff);
2557         equal = aff_check_plain_equal(aff, "{ [-1] }");
2558         isl_aff_free(aff);
2559         if (equal < 0)
2560                 return -1;
2561
2562         return 0;
2563 }
2564
2565 int test_dim_max(isl_ctx *ctx)
2566 {
2567         int equal;
2568         const char *str;
2569         isl_set *set1, *set2;
2570         isl_set *set;
2571         isl_map *map;
2572         isl_pw_aff *pwaff;
2573
2574         str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2575         set = isl_set_read_from_str(ctx, str);
2576         pwaff = isl_set_dim_max(set, 0);
2577         set1 = isl_set_from_pw_aff(pwaff);
2578         str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2579         set2 = isl_set_read_from_str(ctx, str);
2580         equal = isl_set_is_equal(set1, set2);
2581         isl_set_free(set1);
2582         isl_set_free(set2);
2583         if (equal < 0)
2584                 return -1;
2585         if (!equal)
2586                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2587
2588         str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2589         set = isl_set_read_from_str(ctx, str);
2590         pwaff = isl_set_dim_max(set, 0);
2591         set1 = isl_set_from_pw_aff(pwaff);
2592         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2593         set2 = isl_set_read_from_str(ctx, str);
2594         equal = isl_set_is_equal(set1, set2);
2595         isl_set_free(set1);
2596         isl_set_free(set2);
2597         if (equal < 0)
2598                 return -1;
2599         if (!equal)
2600                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2601
2602         str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2603         set = isl_set_read_from_str(ctx, str);
2604         pwaff = isl_set_dim_max(set, 0);
2605         set1 = isl_set_from_pw_aff(pwaff);
2606         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2607         set2 = isl_set_read_from_str(ctx, str);
2608         equal = isl_set_is_equal(set1, set2);
2609         isl_set_free(set1);
2610         isl_set_free(set2);
2611         if (equal < 0)
2612                 return -1;
2613         if (!equal)
2614                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2615
2616         str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2617                         "0 <= i < N and 0 <= j < M }";
2618         map = isl_map_read_from_str(ctx, str);
2619         set = isl_map_range(map);
2620
2621         pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2622         set1 = isl_set_from_pw_aff(pwaff);
2623         str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2624         set2 = isl_set_read_from_str(ctx, str);
2625         equal = isl_set_is_equal(set1, set2);
2626         isl_set_free(set1);
2627         isl_set_free(set2);
2628
2629         pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2630         set1 = isl_set_from_pw_aff(pwaff);
2631         str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2632         set2 = isl_set_read_from_str(ctx, str);
2633         if (equal >= 0 && equal)
2634                 equal = isl_set_is_equal(set1, set2);
2635         isl_set_free(set1);
2636         isl_set_free(set2);
2637
2638         isl_set_free(set);
2639
2640         if (equal < 0)
2641                 return -1;
2642         if (!equal)
2643                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2644
2645         return 0;
2646 }
2647
2648 int test_product(isl_ctx *ctx)
2649 {
2650         const char *str;
2651         isl_set *set;
2652         isl_union_set *uset1, *uset2;
2653         int ok;
2654
2655         str = "{ A[i] }";
2656         set = isl_set_read_from_str(ctx, str);
2657         set = isl_set_product(set, isl_set_copy(set));
2658         ok = isl_set_is_wrapping(set);
2659         isl_set_free(set);
2660         if (ok < 0)
2661                 return -1;
2662         if (!ok)
2663                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2664
2665         str = "{ [] }";
2666         uset1 = isl_union_set_read_from_str(ctx, str);
2667         uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
2668         str = "{ [[] -> []] }";
2669         uset2 = isl_union_set_read_from_str(ctx, str);
2670         ok = isl_union_set_is_equal(uset1, uset2);
2671         isl_union_set_free(uset1);
2672         isl_union_set_free(uset2);
2673         if (ok < 0)
2674                 return -1;
2675         if (!ok)
2676                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2677
2678         return 0;
2679 }
2680
2681 int test_equal(isl_ctx *ctx)
2682 {
2683         const char *str;
2684         isl_set *set, *set2;
2685         int equal;
2686
2687         str = "{ S_6[i] }";
2688         set = isl_set_read_from_str(ctx, str);
2689         str = "{ S_7[i] }";
2690         set2 = isl_set_read_from_str(ctx, str);
2691         equal = isl_set_is_equal(set, set2);
2692         isl_set_free(set);
2693         isl_set_free(set2);
2694         if (equal < 0)
2695                 return -1;
2696         if (equal)
2697                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2698
2699         return 0;
2700 }
2701
2702 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2703         enum isl_dim_type type, unsigned pos, int fixed)
2704 {
2705         int test;
2706
2707         test = isl_map_plain_is_fixed(map, type, pos, NULL);
2708         isl_map_free(map);
2709         if (test < 0)
2710                 return -1;
2711         if (test == fixed)
2712                 return 0;
2713         if (fixed)
2714                 isl_die(ctx, isl_error_unknown,
2715                         "map not detected as fixed", return -1);
2716         else
2717                 isl_die(ctx, isl_error_unknown,
2718                         "map detected as fixed", return -1);
2719 }
2720
2721 int test_fixed(isl_ctx *ctx)
2722 {
2723         const char *str;
2724         isl_map *map;
2725
2726         str = "{ [i] -> [i] }";
2727         map = isl_map_read_from_str(ctx, str);
2728         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2729                 return -1;
2730         str = "{ [i] -> [1] }";
2731         map = isl_map_read_from_str(ctx, str);
2732         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2733                 return -1;
2734         str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2735         map = isl_map_read_from_str(ctx, str);
2736         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2737                 return -1;
2738         map = isl_map_read_from_str(ctx, str);
2739         map = isl_map_neg(map);
2740         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2741                 return -1;
2742
2743         return 0;
2744 }
2745
2746 int test_vertices(isl_ctx *ctx)
2747 {
2748         const char *str;
2749         isl_basic_set *bset;
2750         isl_vertices *vertices;
2751
2752         str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
2753         bset = isl_basic_set_read_from_str(ctx, str);
2754         vertices = isl_basic_set_compute_vertices(bset);
2755         isl_basic_set_free(bset);
2756         isl_vertices_free(vertices);
2757         if (!vertices)
2758                 return -1;
2759
2760         str = "{ A[t, i] : t = 14 and i = 1 }";
2761         bset = isl_basic_set_read_from_str(ctx, str);
2762         vertices = isl_basic_set_compute_vertices(bset);
2763         isl_basic_set_free(bset);
2764         isl_vertices_free(vertices);
2765         if (!vertices)
2766                 return -1;
2767
2768         return 0;
2769 }
2770
2771 int test_union_pw(isl_ctx *ctx)
2772 {
2773         int equal;
2774         const char *str;
2775         isl_union_set *uset;
2776         isl_union_pw_qpolynomial *upwqp1, *upwqp2;
2777
2778         str = "{ [x] -> x^2 }";
2779         upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
2780         upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
2781         uset = isl_union_pw_qpolynomial_domain(upwqp1);
2782         upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
2783         upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
2784         equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
2785         isl_union_pw_qpolynomial_free(upwqp1);
2786         isl_union_pw_qpolynomial_free(upwqp2);
2787         if (equal < 0)
2788                 return -1;
2789         if (!equal)
2790                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2791
2792         return 0;
2793 }
2794
2795 int test_output(isl_ctx *ctx)
2796 {
2797         char *s;
2798         const char *str;
2799         isl_pw_aff *pa;
2800         isl_printer *p;
2801         int equal;
2802
2803         str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
2804         pa = isl_pw_aff_read_from_str(ctx, str);
2805
2806         p = isl_printer_to_str(ctx);
2807         p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2808         p = isl_printer_print_pw_aff(p, pa);
2809         s = isl_printer_get_str(p);
2810         isl_printer_free(p);
2811         isl_pw_aff_free(pa);
2812         equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
2813         free(s);
2814         if (equal < 0)
2815                 return -1;
2816         if (!equal)
2817                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2818
2819         return 0;
2820 }
2821
2822 int test_sample(isl_ctx *ctx)
2823 {
2824         const char *str;
2825         isl_basic_set *bset1, *bset2;
2826         int empty, subset;
2827
2828         str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
2829             "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
2830             "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
2831             "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
2832             "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
2833             "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
2834             "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
2835             "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
2836             "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
2837             "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
2838             "3i >= 1 + 2b - 2c + e + 2f + 3g and "
2839             "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
2840                 "d - 1073741821e and "
2841             "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
2842             "3j >= 1 - a + b + 2e and "
2843             "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
2844             "3i <= 4 - a + 4b - e and "
2845             "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
2846             "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
2847             "c <= -1 + a and 3i >= -2 - a + 3e and "
2848             "1073741822e <= 1073741823 - a + 1073741822b + c and "
2849             "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
2850             "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
2851             "1073741823e >= 1 + 1073741823b - d and "
2852             "3i >= 1073741823b + c - 1073741823e - f and "
2853             "3i >= 1 + 2b + e + 3g }";
2854         bset1 = isl_basic_set_read_from_str(ctx, str);
2855         bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
2856         empty = isl_basic_set_is_empty(bset2);
2857         subset = isl_basic_set_is_subset(bset2, bset1);
2858         isl_basic_set_free(bset1);
2859         isl_basic_set_free(bset2);
2860         if (empty)
2861                 isl_die(ctx, isl_error_unknown, "point not found", return -1);
2862         if (!subset)
2863                 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
2864
2865         return 0;
2866 }
2867
2868 int test_fixed_power(isl_ctx *ctx)
2869 {
2870         const char *str;
2871         isl_map *map;
2872         isl_int exp;
2873         int equal;
2874
2875         isl_int_init(exp);
2876         str = "{ [i] -> [i + 1] }";
2877         map = isl_map_read_from_str(ctx, str);
2878         isl_int_set_si(exp, 23);
2879         map = isl_map_fixed_power(map, exp);
2880         equal = map_check_equal(map, "{ [i] -> [i + 23] }");
2881         isl_int_clear(exp);
2882         isl_map_free(map);
2883         if (equal < 0)
2884                 return -1;
2885
2886         return 0;
2887 }
2888
2889 int test_slice(isl_ctx *ctx)
2890 {
2891         const char *str;
2892         isl_map *map;
2893         int equal;
2894
2895         str = "{ [i] -> [j] }";
2896         map = isl_map_read_from_str(ctx, str);
2897         map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
2898         equal = map_check_equal(map, "{ [i] -> [i] }");
2899         isl_map_free(map);
2900         if (equal < 0)
2901                 return -1;
2902
2903         str = "{ [i] -> [j] }";
2904         map = isl_map_read_from_str(ctx, str);
2905         map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
2906         equal = map_check_equal(map, "{ [i] -> [j] }");
2907         isl_map_free(map);
2908         if (equal < 0)
2909                 return -1;
2910
2911         str = "{ [i] -> [j] }";
2912         map = isl_map_read_from_str(ctx, str);
2913         map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
2914         equal = map_check_equal(map, "{ [i] -> [-i] }");
2915         isl_map_free(map);
2916         if (equal < 0)
2917                 return -1;
2918
2919         str = "{ [i] -> [j] }";
2920         map = isl_map_read_from_str(ctx, str);
2921         map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
2922         equal = map_check_equal(map, "{ [0] -> [j] }");
2923         isl_map_free(map);
2924         if (equal < 0)
2925                 return -1;
2926
2927         str = "{ [i] -> [j] }";
2928         map = isl_map_read_from_str(ctx, str);
2929         map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
2930         equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
2931         isl_map_free(map);
2932         if (equal < 0)
2933                 return -1;
2934
2935         str = "{ [i] -> [j] }";
2936         map = isl_map_read_from_str(ctx, str);
2937         map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
2938         equal = map_check_equal(map, "{ [i] -> [j] : false }");
2939         isl_map_free(map);
2940         if (equal < 0)
2941                 return -1;
2942
2943         return 0;
2944 }
2945
2946 int main()
2947 {
2948         struct isl_ctx *ctx;
2949
2950         srcdir = getenv("srcdir");
2951         assert(srcdir);
2952
2953         ctx = isl_ctx_alloc();
2954         if (test_slice(ctx) < 0)
2955                 goto error;
2956         if (test_fixed_power(ctx) < 0)
2957                 goto error;
2958         if (test_sample(ctx) < 0)
2959                 goto error;
2960         if (test_output(ctx) < 0)
2961                 goto error;
2962         if (test_vertices(ctx) < 0)
2963                 goto error;
2964         if (test_fixed(ctx) < 0)
2965                 goto error;
2966         if (test_equal(ctx) < 0)
2967                 goto error;
2968         if (test_product(ctx) < 0)
2969                 goto error;
2970         if (test_dim_max(ctx) < 0)
2971                 goto error;
2972         if (test_aff(ctx) < 0)
2973                 goto error;
2974         if (test_injective(ctx) < 0)
2975                 goto error;
2976         if (test_schedule(ctx) < 0)
2977                 goto error;
2978         if (test_union_pw(ctx) < 0)
2979                 goto error;
2980         test_factorize(ctx);
2981         test_subset(ctx);
2982         test_lift(ctx);
2983         test_bound(ctx);
2984         test_union(ctx);
2985         test_split_periods(ctx);
2986         if (test_parse(ctx) < 0)
2987                 goto error;
2988         test_pwqp(ctx);
2989         test_lex(ctx);
2990         if (test_sv(ctx) < 0)
2991                 goto error;
2992         test_bijective(ctx);
2993         test_dep(ctx);
2994         test_read(ctx);
2995         test_bounded(ctx);
2996         test_construction(ctx);
2997         test_dim(ctx);
2998         test_div(ctx);
2999         test_application(ctx);
3000         if (test_affine_hull(ctx) < 0)
3001                 goto error;
3002         test_convex_hull(ctx);
3003         test_gist(ctx);
3004         if (test_coalesce(ctx) < 0)
3005                 goto error;
3006         test_closure(ctx);
3007         test_lexmin(ctx);
3008         isl_ctx_free(ctx);
3009         return 0;
3010 error:
3011         isl_ctx_free(ctx);
3012         return -1;
3013 }