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