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