isl_union_set_compute_schedule: fix computation of maximal number of rows
[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 void test_affine_hull(struct isl_ctx *ctx)
641 {
642         test_affine_hull_case(ctx, "affine2");
643         test_affine_hull_case(ctx, "affine");
644         test_affine_hull_case(ctx, "affine3");
645 }
646
647 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
648 {
649         char *filename;
650         FILE *input;
651         struct isl_basic_set *bset1, *bset2;
652         struct isl_set *set;
653
654         filename = get_filename(ctx, name, "polylib");
655         assert(filename);
656         input = fopen(filename, "r");
657         assert(input);
658
659         bset1 = isl_basic_set_read_from_file(ctx, input);
660         bset2 = isl_basic_set_read_from_file(ctx, input);
661
662         set = isl_basic_set_union(bset1, bset2);
663         bset1 = isl_set_convex_hull(set);
664
665         bset2 = isl_basic_set_read_from_file(ctx, input);
666
667         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
668
669         isl_basic_set_free(bset1);
670         isl_basic_set_free(bset2);
671         free(filename);
672
673         fclose(input);
674 }
675
676 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
677 {
678         const char *str1, *str2;
679         isl_set *set1, *set2;
680         int orig_convex = ctx->opt->convex;
681         ctx->opt->convex = convex;
682
683         test_convex_hull_case(ctx, "convex0");
684         test_convex_hull_case(ctx, "convex1");
685         test_convex_hull_case(ctx, "convex2");
686         test_convex_hull_case(ctx, "convex3");
687         test_convex_hull_case(ctx, "convex4");
688         test_convex_hull_case(ctx, "convex5");
689         test_convex_hull_case(ctx, "convex6");
690         test_convex_hull_case(ctx, "convex7");
691         test_convex_hull_case(ctx, "convex8");
692         test_convex_hull_case(ctx, "convex9");
693         test_convex_hull_case(ctx, "convex10");
694         test_convex_hull_case(ctx, "convex11");
695         test_convex_hull_case(ctx, "convex12");
696         test_convex_hull_case(ctx, "convex13");
697         test_convex_hull_case(ctx, "convex14");
698         test_convex_hull_case(ctx, "convex15");
699
700         str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
701                "(i0 = 1 and i1 = 0 and i2 = 1) or "
702                "(i0 = 0 and i1 = 0 and i2 = 0) }";
703         str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
704         set1 = isl_set_read_from_str(ctx, str1);
705         set2 = isl_set_read_from_str(ctx, str2);
706         set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
707         assert(isl_set_is_equal(set1, set2));
708         isl_set_free(set1);
709         isl_set_free(set2);
710
711         ctx->opt->convex = orig_convex;
712 }
713
714 void test_convex_hull(struct isl_ctx *ctx)
715 {
716         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
717         test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
718 }
719
720 void test_gist_case(struct isl_ctx *ctx, const char *name)
721 {
722         char *filename;
723         FILE *input;
724         struct isl_basic_set *bset1, *bset2;
725
726         filename = get_filename(ctx, name, "polylib");
727         assert(filename);
728         input = fopen(filename, "r");
729         assert(input);
730
731         bset1 = isl_basic_set_read_from_file(ctx, input);
732         bset2 = isl_basic_set_read_from_file(ctx, input);
733
734         bset1 = isl_basic_set_gist(bset1, bset2);
735
736         bset2 = isl_basic_set_read_from_file(ctx, input);
737
738         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
739
740         isl_basic_set_free(bset1);
741         isl_basic_set_free(bset2);
742         free(filename);
743
744         fclose(input);
745 }
746
747 void test_gist(struct isl_ctx *ctx)
748 {
749         const char *str;
750         isl_basic_set *bset1, *bset2;
751
752         test_gist_case(ctx, "gist1");
753
754         str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
755             "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
756             "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
757             "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
758             "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
759             "16e0 >= 16 + 16p6 + 15p10 and  p2 <= 15 and p3 >= 0 and "
760             "p3 <= 31 and  p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
761             "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
762             "p10 <= 15 and p10 <= -1 + p0 - p6) }";
763         bset1 = isl_basic_set_read_from_str(ctx, str);
764         str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
765             "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
766             "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
767             "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
768             "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
769             "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
770             "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
771             "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
772         bset2 = isl_basic_set_read_from_str(ctx, str);
773         bset1 = isl_basic_set_gist(bset1, bset2);
774         assert(bset1 && bset1->n_div == 0);
775         isl_basic_set_free(bset1);
776 }
777
778 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
779 {
780         isl_set *set, *set2;
781         int equal;
782         int one;
783
784         set = isl_set_read_from_str(ctx, str);
785         set = isl_set_coalesce(set);
786         set2 = isl_set_read_from_str(ctx, str);
787         equal = isl_set_is_equal(set, set2);
788         one = set && set->n == 1;
789         isl_set_free(set);
790         isl_set_free(set2);
791
792         if (equal < 0)
793                 return -1;
794         if (!equal)
795                 isl_die(ctx, isl_error_unknown,
796                         "coalesced set not equal to input", return -1);
797         if (check_one && !one)
798                 isl_die(ctx, isl_error_unknown,
799                         "coalesced set should not be a union", return -1);
800
801         return 0;
802 }
803
804 int test_coalesce(struct isl_ctx *ctx)
805 {
806         const char *str;
807         struct isl_set *set, *set2;
808         struct isl_map *map, *map2;
809
810         set = isl_set_read_from_str(ctx,
811                 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
812                        "y >= x & x >= 2 & 5 >= y }");
813         set = isl_set_coalesce(set);
814         assert(set && set->n == 1);
815         isl_set_free(set);
816
817         set = isl_set_read_from_str(ctx,
818                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
819                        "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}");
820         set = isl_set_coalesce(set);
821         assert(set && set->n == 1);
822         isl_set_free(set);
823
824         set = isl_set_read_from_str(ctx,
825                 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
826                        "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}");
827         set = isl_set_coalesce(set);
828         assert(set && set->n == 2);
829         isl_set_free(set);
830
831         set = isl_set_read_from_str(ctx,
832                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
833                        "y >= 0 & x >= 6 & x <= 10 & y <= x}");
834         set = isl_set_coalesce(set);
835         assert(set && set->n == 1);
836         isl_set_free(set);
837
838         set = isl_set_read_from_str(ctx,
839                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
840                        "y >= 0 & x >= 7 & x <= 10 & y <= x}");
841         set = isl_set_coalesce(set);
842         assert(set && set->n == 2);
843         isl_set_free(set);
844
845         set = isl_set_read_from_str(ctx,
846                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
847                        "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}");
848         set = isl_set_coalesce(set);
849         assert(set && set->n == 2);
850         isl_set_free(set);
851
852         set = isl_set_read_from_str(ctx,
853                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
854                        "y >= 0 & x = 6 & y <= 6}");
855         set = isl_set_coalesce(set);
856         assert(set && set->n == 1);
857         isl_set_free(set);
858
859         set = isl_set_read_from_str(ctx,
860                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
861                        "y >= 0 & x = 7 & y <= 6}");
862         set = isl_set_coalesce(set);
863         assert(set && set->n == 2);
864         isl_set_free(set);
865
866         set = isl_set_read_from_str(ctx,
867                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
868                        "y >= 0 & x = 6 & y <= 5}");
869         set = isl_set_coalesce(set);
870         assert(set && set->n == 1);
871         set2 = isl_set_read_from_str(ctx,
872                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
873                        "y >= 0 & x = 6 & y <= 5}");
874         assert(isl_set_is_equal(set, set2));
875         isl_set_free(set);
876         isl_set_free(set2);
877
878         set = isl_set_read_from_str(ctx,
879                 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
880                        "y >= 0 & x = 6 & y <= 7}");
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                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
887         set = isl_set_coalesce(set);
888         assert(set && set->n == 1);
889         set2 = isl_set_read_from_str(ctx,
890                 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
891         assert(isl_set_is_equal(set, set2));
892         isl_set_free(set);
893         isl_set_free(set2);
894
895         set = isl_set_read_from_str(ctx,
896                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
897         set = isl_set_coalesce(set);
898         set2 = isl_set_read_from_str(ctx,
899                 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
900         assert(isl_set_is_equal(set, set2));
901         isl_set_free(set);
902         isl_set_free(set2);
903
904         set = isl_set_read_from_str(ctx,
905                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
906                                 "2 <= i and i <= n }");
907         set = isl_set_coalesce(set);
908         assert(set && set->n == 1);
909         set2 = isl_set_read_from_str(ctx,
910                 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
911                                 "2 <= i and i <= n }");
912         assert(isl_set_is_equal(set, set2));
913         isl_set_free(set);
914         isl_set_free(set2);
915
916         map = isl_map_read_from_str(ctx,
917                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
918                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
919                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
920                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
921                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
922                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
923                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
924                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
925                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
926                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
927                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
928                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
929                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
930                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
931                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
932                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
933                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
934                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
935         map = isl_map_coalesce(map);
936         map2 = isl_map_read_from_str(ctx,
937                 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
938                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
939                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
940                 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
941                 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
942                 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
943                 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
944                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
945                 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
946                 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
947                 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
948                 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
949                 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
950                 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
951                 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
952                 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
953                 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
954                 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
955         assert(isl_map_is_equal(map, map2));
956         isl_map_free(map);
957         isl_map_free(map2);
958
959         str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
960               "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
961               "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
962               "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
963         map = isl_map_read_from_str(ctx, str);
964         map = isl_map_coalesce(map);
965         map2 = isl_map_read_from_str(ctx, str);
966         assert(isl_map_is_equal(map, map2));
967         isl_map_free(map);
968         isl_map_free(map2);
969
970         str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
971           "[o0, o1, o2, o3, o4, o5, o6] : "
972           "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
973           "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
974           "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
975           "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
976           "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
977           "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
978           "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
979           "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
980           "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
981           "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
982           "o6 >= i3 + i6 - o3 and M >= 0 and "
983           "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
984           "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
985         map = isl_map_read_from_str(ctx, str);
986         map = isl_map_coalesce(map);
987         map2 = isl_map_read_from_str(ctx, str);
988         assert(isl_map_is_equal(map, map2));
989         isl_map_free(map);
990         isl_map_free(map2);
991
992         str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
993                 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
994                 "(o0 = 0 and M >= 2 and N >= 3) or "
995                 "(M = 0 and o0 = 0 and N >= 3) }";
996         map = isl_map_read_from_str(ctx, str);
997         map = isl_map_coalesce(map);
998         map2 = isl_map_read_from_str(ctx, str);
999         assert(isl_map_is_equal(map, map2));
1000         isl_map_free(map);
1001         isl_map_free(map2);
1002
1003         str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1004                 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1005                 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1006                 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
1007         map = isl_map_read_from_str(ctx, str);
1008         map = isl_map_coalesce(map);
1009         map2 = isl_map_read_from_str(ctx, str);
1010         assert(isl_map_is_equal(map, map2));
1011         isl_map_free(map);
1012         isl_map_free(map2);
1013
1014         test_coalesce_set(ctx,
1015                 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
1016                                 "(i1 = M and M >= 1) }", 0);
1017         test_coalesce_set(ctx,
1018                 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
1019         test_coalesce_set(ctx,
1020                 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1021                 "(y = 3 and x = 1) }", 1);
1022         test_coalesce_set(ctx,
1023                 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1024                 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1025                 "i1 <= M and i3 <= M and i4 <= M) or "
1026                 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1027                 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1028                 "i4 <= -1 + M) }", 1);
1029         test_coalesce_set(ctx,
1030                 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1031                 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
1032         test_coalesce_set(ctx,
1033                 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1034                         "-x - y + 1 >= 0 and -3 <= z <= 3;"
1035                 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1036                         "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
1037         if (test_coalesce_set(ctx,
1038                 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1039                 return -1;
1040         if (test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0) < 0)
1041                 return -1;
1042         if (test_coalesce_set(ctx,
1043                 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1044                 return -1;
1045         if (test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1) < 0)
1046                 return -1;
1047         if (test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1) < 0)
1048                 return -1;
1049         if (test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0) < 0)
1050                 return -1;
1051         if (test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1) < 0)
1052                 return -1;
1053         if (test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0) < 0)
1054                 return -1;
1055         if (test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1056                 return -1;
1057         if (test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1058                 return -1;
1059         if (test_coalesce_set(ctx, "{ [a, b] : exists e : 2e = a and "
1060                     "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }", 0) < 0)
1061                 return -1;
1062         if (test_coalesce_set(ctx,
1063                 "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1064                         "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1065                         "j >= 1 and j' <= i + j - i' and i >= 1; "
1066                 "[1, 1, 1, 1] }", 0) < 0)
1067                 return -1;
1068         return 0;
1069 }
1070
1071 void test_closure(struct isl_ctx *ctx)
1072 {
1073         const char *str;
1074         isl_set *dom;
1075         isl_map *up, *right;
1076         isl_map *map, *map2;
1077         int exact;
1078
1079         /* COCOA example 1 */
1080         map = isl_map_read_from_str(ctx,
1081                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1082                         "1 <= i and i < n and 1 <= j and j < n or "
1083                         "i2 = i + 1 and j2 = j - 1 and "
1084                         "1 <= i and i < n and 2 <= j and j <= n }");
1085         map = isl_map_power(map, &exact);
1086         assert(exact);
1087         isl_map_free(map);
1088
1089         /* COCOA example 1 */
1090         map = isl_map_read_from_str(ctx,
1091                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1092                         "1 <= i and i < n and 1 <= j and j < n or "
1093                         "i2 = i + 1 and j2 = j - 1 and "
1094                         "1 <= i and i < n and 2 <= j and j <= n }");
1095         map = isl_map_transitive_closure(map, &exact);
1096         assert(exact);
1097         map2 = isl_map_read_from_str(ctx,
1098                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1099                         "1 <= i and i < n and 1 <= j and j <= n and "
1100                         "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1101                         "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1102                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1103         assert(isl_map_is_equal(map, map2));
1104         isl_map_free(map2);
1105         isl_map_free(map);
1106
1107         map = isl_map_read_from_str(ctx,
1108                 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1109                                      " 0 <= y and y <= n }");
1110         map = isl_map_transitive_closure(map, &exact);
1111         map2 = isl_map_read_from_str(ctx,
1112                 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1113                                      " 0 <= y and y <= n }");
1114         assert(isl_map_is_equal(map, map2));
1115         isl_map_free(map2);
1116         isl_map_free(map);
1117
1118         /* COCOA example 2 */
1119         map = isl_map_read_from_str(ctx,
1120                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1121                         "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1122                         "i2 = i + 2 and j2 = j - 2 and "
1123                         "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1124         map = isl_map_transitive_closure(map, &exact);
1125         assert(exact);
1126         map2 = isl_map_read_from_str(ctx,
1127                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1128                         "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1129                         "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1130                         "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1131                         "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1132         assert(isl_map_is_equal(map, map2));
1133         isl_map_free(map);
1134         isl_map_free(map2);
1135
1136         /* COCOA Fig.2 left */
1137         map = isl_map_read_from_str(ctx,
1138                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1139                         "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1140                         "j <= n or "
1141                         "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1142                         "j <= 2 i - 3 and j <= n - 2 or "
1143                         "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1144                         "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1145         map = isl_map_transitive_closure(map, &exact);
1146         assert(exact);
1147         isl_map_free(map);
1148
1149         /* COCOA Fig.2 right */
1150         map = isl_map_read_from_str(ctx,
1151                 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1152                         "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1153                         "j <= n or "
1154                         "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1155                         "j <= 2 i - 4 and j <= n - 3 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_power(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_transitive_closure(map, &exact);
1172         assert(exact);
1173         map2 = isl_map_read_from_str(ctx,
1174                 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1175                         "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1176                         "j <= n and 3 + i + 2 j <= 3 n and "
1177                         "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1178                         "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1179                         "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1180                         "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1181                         "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1182         assert(isl_map_is_equal(map, map2));
1183         isl_map_free(map2);
1184         isl_map_free(map);
1185
1186         /* COCOA Fig.1 right */
1187         dom = isl_set_read_from_str(ctx,
1188                 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1189                         "2 x - 3 y + 3 >= 0 }");
1190         right = isl_map_read_from_str(ctx,
1191                 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1192         up = isl_map_read_from_str(ctx,
1193                 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1194         right = isl_map_intersect_domain(right, isl_set_copy(dom));
1195         right = isl_map_intersect_range(right, isl_set_copy(dom));
1196         up = isl_map_intersect_domain(up, isl_set_copy(dom));
1197         up = isl_map_intersect_range(up, dom);
1198         map = isl_map_union(up, right);
1199         map = isl_map_transitive_closure(map, &exact);
1200         assert(exact);
1201         map2 = isl_map_read_from_str(ctx,
1202                 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1203                 "  [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1204         assert(isl_map_is_equal(map, map2));
1205         isl_map_free(map2);
1206         isl_map_free(map);
1207
1208         /* COCOA Theorem 1 counter example */
1209         map = isl_map_read_from_str(ctx,
1210                 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1211                         "i2 = 1 and j2 = j or "
1212                         "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1213         map = isl_map_transitive_closure(map, &exact);
1214         assert(exact);
1215         isl_map_free(map);
1216
1217         map = isl_map_read_from_str(ctx,
1218                 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1219                         "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1220                         "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1221                         "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1222         map = isl_map_transitive_closure(map, &exact);
1223         assert(exact);
1224         isl_map_free(map);
1225
1226         /* Kelly et al 1996, fig 12 */
1227         map = isl_map_read_from_str(ctx,
1228                 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1229                         "1 <= i,j,j+1 <= n or "
1230                         "j = n and j2 = 1 and i2 = i + 1 and "
1231                         "1 <= i,i+1 <= n }");
1232         map = isl_map_transitive_closure(map, &exact);
1233         assert(exact);
1234         map2 = isl_map_read_from_str(ctx,
1235                 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1236                         "1 <= i <= n and i = i2 or "
1237                         "1 <= i < i2 <= n and 1 <= j <= n and "
1238                         "1 <= j2 <= n }");
1239         assert(isl_map_is_equal(map, map2));
1240         isl_map_free(map2);
1241         isl_map_free(map);
1242
1243         /* Omega's closure4 */
1244         map = isl_map_read_from_str(ctx,
1245                 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1246                         "1 <= x,y <= 10 or "
1247                         "x2 = x + 1 and y2 = y and "
1248                         "1 <= x <= 20 && 5 <= y <= 15 }");
1249         map = isl_map_transitive_closure(map, &exact);
1250         assert(exact);
1251         isl_map_free(map);
1252
1253         map = isl_map_read_from_str(ctx,
1254                 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1255         map = isl_map_transitive_closure(map, &exact);
1256         assert(!exact);
1257         map2 = isl_map_read_from_str(ctx,
1258                 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1259         assert(isl_map_is_equal(map, map2));
1260         isl_map_free(map);
1261         isl_map_free(map2);
1262
1263         str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1264             "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1265             "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1266             "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1267         map = isl_map_read_from_str(ctx, str);
1268         map = isl_map_transitive_closure(map, &exact);
1269         assert(exact);
1270         map2 = isl_map_read_from_str(ctx, str);
1271         assert(isl_map_is_equal(map, map2));
1272         isl_map_free(map);
1273         isl_map_free(map2);
1274
1275         str = "{[0] -> [1]; [2] -> [3]}";
1276         map = isl_map_read_from_str(ctx, str);
1277         map = isl_map_transitive_closure(map, &exact);
1278         assert(exact);
1279         map2 = isl_map_read_from_str(ctx, str);
1280         assert(isl_map_is_equal(map, map2));
1281         isl_map_free(map);
1282         isl_map_free(map2);
1283
1284         str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1285             "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1286             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1287             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1288             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1289             "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1290             "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1291             "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1292             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1293             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1294             "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1295             "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1296             "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1297             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1298             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1299             "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1300             "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1301             "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1302             "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1303             "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1304         map = isl_map_read_from_str(ctx, str);
1305         map = isl_map_transitive_closure(map, NULL);
1306         assert(map);
1307         isl_map_free(map);
1308 }
1309
1310 void test_lex(struct isl_ctx *ctx)
1311 {
1312         isl_space *dim;
1313         isl_map *map;
1314
1315         dim = isl_space_set_alloc(ctx, 0, 0);
1316         map = isl_map_lex_le(dim);
1317         assert(!isl_map_is_empty(map));
1318         isl_map_free(map);
1319 }
1320
1321 void test_lexmin(struct isl_ctx *ctx)
1322 {
1323         const char *str;
1324         isl_basic_map *bmap;
1325         isl_map *map, *map2;
1326         isl_set *set;
1327         isl_set *set2;
1328         isl_pw_multi_aff *pma;
1329
1330         str = "[p0, p1] -> { [] -> [] : "
1331             "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1332             "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1333             "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1334             "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1335             "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1336             "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1337             "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1338             "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1339             "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1340             "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1341             "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1342         map = isl_map_read_from_str(ctx, str);
1343         map = isl_map_lexmin(map);
1344         isl_map_free(map);
1345
1346         str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1347             "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1348         set = isl_set_read_from_str(ctx, str);
1349         set = isl_set_lexmax(set);
1350         str = "[C] -> { [obj,a,b,c] : C = 8 }";
1351         set2 = isl_set_read_from_str(ctx, str);
1352         set = isl_set_intersect(set, set2);
1353         assert(!isl_set_is_empty(set));
1354         isl_set_free(set);
1355
1356         str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1357         map = isl_map_read_from_str(ctx, str);
1358         map = isl_map_lexmin(map);
1359         str = "{ [x] -> [5] : 6 <= x <= 8; "
1360                 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1361         map2 = isl_map_read_from_str(ctx, str);
1362         assert(isl_map_is_equal(map, map2));
1363         isl_map_free(map);
1364         isl_map_free(map2);
1365
1366         str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1367         map = isl_map_read_from_str(ctx, str);
1368         map2 = isl_map_copy(map);
1369         map = isl_map_lexmin(map);
1370         assert(isl_map_is_equal(map, map2));
1371         isl_map_free(map);
1372         isl_map_free(map2);
1373
1374         str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1375         map = isl_map_read_from_str(ctx, str);
1376         map = isl_map_lexmin(map);
1377         str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1378                 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1379                 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1380                 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1381         map2 = isl_map_read_from_str(ctx, str);
1382         assert(isl_map_is_equal(map, map2));
1383         isl_map_free(map);
1384         isl_map_free(map2);
1385
1386         str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1387                                 " 8i' <= i and 8i' >= -7 + i }";
1388         bmap = isl_basic_map_read_from_str(ctx, str);
1389         pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1390         map2 = isl_map_from_pw_multi_aff(pma);
1391         map = isl_map_from_basic_map(bmap);
1392         assert(isl_map_is_equal(map, map2));
1393         isl_map_free(map);
1394         isl_map_free(map2);
1395
1396         str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1397         map = isl_map_read_from_str(ctx, str);
1398         map = isl_map_lexmin(map);
1399         str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1400         map2 = isl_map_read_from_str(ctx, str);
1401         assert(isl_map_is_equal(map, map2));
1402         isl_map_free(map);
1403         isl_map_free(map2);
1404 }
1405
1406 struct must_may {
1407         isl_map *must;
1408         isl_map *may;
1409 };
1410
1411 static int collect_must_may(__isl_take isl_map *dep, int must,
1412         void *dep_user, void *user)
1413 {
1414         struct must_may *mm = (struct must_may *)user;
1415
1416         if (must)
1417                 mm->must = isl_map_union(mm->must, dep);
1418         else
1419                 mm->may = isl_map_union(mm->may, dep);
1420
1421         return 0;
1422 }
1423
1424 static int common_space(void *first, void *second)
1425 {
1426         int depth = *(int *)first;
1427         return 2 * depth;
1428 }
1429
1430 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1431 {
1432         isl_map *map2;
1433         int equal;
1434
1435         if (!map)
1436                 return -1;
1437
1438         map2 = isl_map_read_from_str(map->ctx, str);
1439         equal = isl_map_is_equal(map, map2);
1440         isl_map_free(map2);
1441
1442         return equal;
1443 }
1444
1445 void test_dep(struct isl_ctx *ctx)
1446 {
1447         const char *str;
1448         isl_space *dim;
1449         isl_map *map;
1450         isl_access_info *ai;
1451         isl_flow *flow;
1452         int depth;
1453         struct must_may mm;
1454
1455         depth = 3;
1456
1457         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1458         map = isl_map_read_from_str(ctx, str);
1459         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1460
1461         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1462         map = isl_map_read_from_str(ctx, str);
1463         ai = isl_access_info_add_source(ai, map, 1, &depth);
1464
1465         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1466         map = isl_map_read_from_str(ctx, str);
1467         ai = isl_access_info_add_source(ai, map, 1, &depth);
1468
1469         flow = isl_access_info_compute_flow(ai);
1470         dim = isl_space_alloc(ctx, 0, 3, 3);
1471         mm.must = isl_map_empty(isl_space_copy(dim));
1472         mm.may = isl_map_empty(dim);
1473
1474         isl_flow_foreach(flow, collect_must_may, &mm);
1475
1476         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1477               "  [1,10,0] -> [2,5,0] }";
1478         assert(map_is_equal(mm.must, str));
1479         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1480         assert(map_is_equal(mm.may, str));
1481
1482         isl_map_free(mm.must);
1483         isl_map_free(mm.may);
1484         isl_flow_free(flow);
1485
1486
1487         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1488         map = isl_map_read_from_str(ctx, str);
1489         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1490
1491         str = "{ [0,i,0] -> [i] : 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         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1496         map = isl_map_read_from_str(ctx, str);
1497         ai = isl_access_info_add_source(ai, map, 0, &depth);
1498
1499         flow = isl_access_info_compute_flow(ai);
1500         dim = isl_space_alloc(ctx, 0, 3, 3);
1501         mm.must = isl_map_empty(isl_space_copy(dim));
1502         mm.may = isl_map_empty(dim);
1503
1504         isl_flow_foreach(flow, collect_must_may, &mm);
1505
1506         str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1507         assert(map_is_equal(mm.must, str));
1508         str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1509         assert(map_is_equal(mm.may, str));
1510
1511         isl_map_free(mm.must);
1512         isl_map_free(mm.may);
1513         isl_flow_free(flow);
1514
1515
1516         str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1517         map = isl_map_read_from_str(ctx, str);
1518         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1519
1520         str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1521         map = isl_map_read_from_str(ctx, str);
1522         ai = isl_access_info_add_source(ai, map, 0, &depth);
1523
1524         str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1525         map = isl_map_read_from_str(ctx, str);
1526         ai = isl_access_info_add_source(ai, map, 0, &depth);
1527
1528         flow = isl_access_info_compute_flow(ai);
1529         dim = isl_space_alloc(ctx, 0, 3, 3);
1530         mm.must = isl_map_empty(isl_space_copy(dim));
1531         mm.may = isl_map_empty(dim);
1532
1533         isl_flow_foreach(flow, collect_must_may, &mm);
1534
1535         str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1536               "  [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1537         assert(map_is_equal(mm.may, str));
1538         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1539         assert(map_is_equal(mm.must, str));
1540
1541         isl_map_free(mm.must);
1542         isl_map_free(mm.may);
1543         isl_flow_free(flow);
1544
1545
1546         str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1547         map = isl_map_read_from_str(ctx, str);
1548         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1549
1550         str = "{ [0,i,0] -> [i] : 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         str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1555         map = isl_map_read_from_str(ctx, str);
1556         ai = isl_access_info_add_source(ai, map, 0, &depth);
1557
1558         flow = isl_access_info_compute_flow(ai);
1559         dim = isl_space_alloc(ctx, 0, 3, 3);
1560         mm.must = isl_map_empty(isl_space_copy(dim));
1561         mm.may = isl_map_empty(dim);
1562
1563         isl_flow_foreach(flow, collect_must_may, &mm);
1564
1565         str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1566               "  [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1567         assert(map_is_equal(mm.may, str));
1568         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1569         assert(map_is_equal(mm.must, str));
1570
1571         isl_map_free(mm.must);
1572         isl_map_free(mm.may);
1573         isl_flow_free(flow);
1574
1575
1576         str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1577         map = isl_map_read_from_str(ctx, str);
1578         ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1579
1580         str = "{ [0,i,0] -> [i] : 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         str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1585         map = isl_map_read_from_str(ctx, str);
1586         ai = isl_access_info_add_source(ai, map, 0, &depth);
1587
1588         flow = isl_access_info_compute_flow(ai);
1589         dim = isl_space_alloc(ctx, 0, 3, 3);
1590         mm.must = isl_map_empty(isl_space_copy(dim));
1591         mm.may = isl_map_empty(dim);
1592
1593         isl_flow_foreach(flow, collect_must_may, &mm);
1594
1595         str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1596               "  [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1597         assert(map_is_equal(mm.may, str));
1598         str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1599         assert(map_is_equal(mm.must, str));
1600
1601         isl_map_free(mm.must);
1602         isl_map_free(mm.may);
1603         isl_flow_free(flow);
1604
1605
1606         depth = 5;
1607
1608         str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1609         map = isl_map_read_from_str(ctx, str);
1610         ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1611
1612         str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1613         map = isl_map_read_from_str(ctx, str);
1614         ai = isl_access_info_add_source(ai, map, 1, &depth);
1615
1616         flow = isl_access_info_compute_flow(ai);
1617         dim = isl_space_alloc(ctx, 0, 5, 5);
1618         mm.must = isl_map_empty(isl_space_copy(dim));
1619         mm.may = isl_map_empty(dim);
1620
1621         isl_flow_foreach(flow, collect_must_may, &mm);
1622
1623         str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1624         assert(map_is_equal(mm.must, str));
1625         str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1626         assert(map_is_equal(mm.may, str));
1627
1628         isl_map_free(mm.must);
1629         isl_map_free(mm.may);
1630         isl_flow_free(flow);
1631 }
1632
1633 int test_sv(isl_ctx *ctx)
1634 {
1635         const char *str;
1636         isl_map *map;
1637         isl_union_map *umap;
1638         int sv;
1639
1640         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1641         map = isl_map_read_from_str(ctx, str);
1642         sv = isl_map_is_single_valued(map);
1643         isl_map_free(map);
1644         if (sv < 0)
1645                 return -1;
1646         if (!sv)
1647                 isl_die(ctx, isl_error_internal,
1648                         "map not detected as single valued", return -1);
1649
1650         str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1651         map = isl_map_read_from_str(ctx, str);
1652         sv = isl_map_is_single_valued(map);
1653         isl_map_free(map);
1654         if (sv < 0)
1655                 return -1;
1656         if (sv)
1657                 isl_die(ctx, isl_error_internal,
1658                         "map detected as single valued", return -1);
1659
1660         str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1661         umap = isl_union_map_read_from_str(ctx, str);
1662         sv = isl_union_map_is_single_valued(umap);
1663         isl_union_map_free(umap);
1664         if (sv < 0)
1665                 return -1;
1666         if (!sv)
1667                 isl_die(ctx, isl_error_internal,
1668                         "map not detected as single valued", return -1);
1669
1670         str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1671         umap = isl_union_map_read_from_str(ctx, str);
1672         sv = isl_union_map_is_single_valued(umap);
1673         isl_union_map_free(umap);
1674         if (sv < 0)
1675                 return -1;
1676         if (sv)
1677                 isl_die(ctx, isl_error_internal,
1678                         "map detected as single valued", return -1);
1679
1680         return 0;
1681 }
1682
1683 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1684 {
1685         isl_map *map;
1686
1687         map = isl_map_read_from_str(ctx, str);
1688         if (bijective)
1689                 assert(isl_map_is_bijective(map));
1690         else
1691                 assert(!isl_map_is_bijective(map));
1692         isl_map_free(map);
1693 }
1694
1695 void test_bijective(struct isl_ctx *ctx)
1696 {
1697         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1698         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1699         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1700         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1701         test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1702         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1703         test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1704         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1705         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1706         test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1707         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1708         test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1709         test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1710 }
1711
1712 void test_pwqp(struct isl_ctx *ctx)
1713 {
1714         const char *str;
1715         isl_set *set;
1716         isl_pw_qpolynomial *pwqp1, *pwqp2;
1717
1718         str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1719         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1720
1721         pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1722                                                 isl_dim_in, 1, 1);
1723
1724         str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1725         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1726
1727         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1728
1729         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1730
1731         isl_pw_qpolynomial_free(pwqp1);
1732
1733         str = "{ [i] -> i }";
1734         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1735         str = "{ [k] : exists a : k = 2a }";
1736         set = isl_set_read_from_str(ctx, str);
1737         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1738         str = "{ [i] -> i }";
1739         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1740
1741         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1742
1743         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1744
1745         isl_pw_qpolynomial_free(pwqp1);
1746
1747         str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1748         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1749         str = "{ [10] }";
1750         set = isl_set_read_from_str(ctx, str);
1751         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1752         str = "{ [i] -> 16 }";
1753         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1754
1755         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1756
1757         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1758
1759         isl_pw_qpolynomial_free(pwqp1);
1760
1761         str = "{ [i] -> ([(i)/2]) }";
1762         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1763         str = "{ [k] : exists a : k = 2a+1 }";
1764         set = isl_set_read_from_str(ctx, str);
1765         pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1766         str = "{ [i] -> -1/2 + 1/2 * i }";
1767         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1768
1769         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1770
1771         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1772
1773         isl_pw_qpolynomial_free(pwqp1);
1774
1775         str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1776         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1777         str = "{ [i] -> ([(2 * [i/2])/5]) }";
1778         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1779
1780         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1781
1782         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1783
1784         isl_pw_qpolynomial_free(pwqp1);
1785
1786         str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1787         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1788         str = "{ [x] -> x }";
1789         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1790
1791         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1792
1793         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1794
1795         isl_pw_qpolynomial_free(pwqp1);
1796
1797         str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1798         pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1799         pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1800         pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1801         pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1802         assert(isl_pw_qpolynomial_is_zero(pwqp1));
1803         isl_pw_qpolynomial_free(pwqp1);
1804 }
1805
1806 void test_split_periods(isl_ctx *ctx)
1807 {
1808         const char *str;
1809         isl_pw_qpolynomial *pwqp;
1810
1811         str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1812                 "U + 2V + 3 >= 0 and - U -2V  >= 0 and - U + 10 >= 0 and "
1813                 "U  >= 0; [U,V] -> U^2 : U >= 100 }";
1814         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1815
1816         pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1817         assert(pwqp);
1818
1819         isl_pw_qpolynomial_free(pwqp);
1820 }
1821
1822 void test_union(isl_ctx *ctx)
1823 {
1824         const char *str;
1825         isl_union_set *uset1, *uset2;
1826         isl_union_map *umap1, *umap2;
1827
1828         str = "{ [i] : 0 <= i <= 1 }";
1829         uset1 = isl_union_set_read_from_str(ctx, str);
1830         str = "{ [1] -> [0] }";
1831         umap1 = isl_union_map_read_from_str(ctx, str);
1832
1833         umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1834         assert(isl_union_map_is_equal(umap1, umap2));
1835
1836         isl_union_map_free(umap1);
1837         isl_union_map_free(umap2);
1838
1839         str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1840         umap1 = isl_union_map_read_from_str(ctx, str);
1841         str = "{ A[i]; B[i] }";
1842         uset1 = isl_union_set_read_from_str(ctx, str);
1843
1844         uset2 = isl_union_map_domain(umap1);
1845
1846         assert(isl_union_set_is_equal(uset1, uset2));
1847
1848         isl_union_set_free(uset1);
1849         isl_union_set_free(uset2);
1850 }
1851
1852 void test_bound(isl_ctx *ctx)
1853 {
1854         const char *str;
1855         isl_pw_qpolynomial *pwqp;
1856         isl_pw_qpolynomial_fold *pwf;
1857
1858         str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1859         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1860         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1861         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1862         isl_pw_qpolynomial_fold_free(pwf);
1863
1864         str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1865         pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1866         pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1867         assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1868         isl_pw_qpolynomial_fold_free(pwf);
1869 }
1870
1871 void test_lift(isl_ctx *ctx)
1872 {
1873         const char *str;
1874         isl_basic_map *bmap;
1875         isl_basic_set *bset;
1876
1877         str = "{ [i0] : exists e0 : i0 = 4e0 }";
1878         bset = isl_basic_set_read_from_str(ctx, str);
1879         bset = isl_basic_set_lift(bset);
1880         bmap = isl_basic_map_from_range(bset);
1881         bset = isl_basic_map_domain(bmap);
1882         isl_basic_set_free(bset);
1883 }
1884
1885 void test_subset(isl_ctx *ctx)
1886 {
1887         const char *str;
1888         isl_set *set1, *set2;
1889
1890         str = "{ [112, 0] }";
1891         set1 = isl_set_read_from_str(ctx, str);
1892         str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1893                 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1894                 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1895         set2 = isl_set_read_from_str(ctx, str);
1896         assert(isl_set_is_subset(set1, set2));
1897         isl_set_free(set1);
1898         isl_set_free(set2);
1899 }
1900
1901 void test_factorize(isl_ctx *ctx)
1902 {
1903         const char *str;
1904         isl_basic_set *bset;
1905         isl_factorizer *f;
1906
1907         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1908             "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1909             "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1910             "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1911             "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1912             "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1913             "3i5 >= -2i0 - i2 + 3i4 }";
1914         bset = isl_basic_set_read_from_str(ctx, str);
1915         f = isl_basic_set_factorizer(bset);
1916         assert(f);
1917         isl_basic_set_free(bset);
1918         isl_factorizer_free(f);
1919
1920         str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1921             "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
1922             "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
1923             "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
1924             "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
1925             "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
1926             "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
1927             "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
1928             "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
1929             "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
1930         bset = isl_basic_set_read_from_str(ctx, str);
1931         f = isl_basic_set_factorizer(bset);
1932         assert(f);
1933         isl_basic_set_free(bset);
1934         isl_factorizer_free(f);
1935 }
1936
1937 static int check_injective(__isl_take isl_map *map, void *user)
1938 {
1939         int *injective = user;
1940
1941         *injective = isl_map_is_injective(map);
1942         isl_map_free(map);
1943
1944         if (*injective < 0 || !*injective)
1945                 return -1;
1946
1947         return 0;
1948 }
1949
1950 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
1951         const char *r, const char *s, int tilable, int parallel)
1952 {
1953         int i;
1954         isl_union_set *D;
1955         isl_union_map *W, *R, *S;
1956         isl_union_map *empty;
1957         isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
1958         isl_union_map *validity, *proximity;
1959         isl_union_map *schedule;
1960         isl_union_map *test;
1961         isl_union_set *delta;
1962         isl_union_set *domain;
1963         isl_set *delta_set;
1964         isl_set *slice;
1965         isl_set *origin;
1966         isl_schedule *sched;
1967         int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
1968
1969         D = isl_union_set_read_from_str(ctx, d);
1970         W = isl_union_map_read_from_str(ctx, w);
1971         R = isl_union_map_read_from_str(ctx, r);
1972         S = isl_union_map_read_from_str(ctx, s);
1973
1974         W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
1975         R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
1976
1977         empty = isl_union_map_empty(isl_union_map_get_space(S));
1978         isl_union_map_compute_flow(isl_union_map_copy(R),
1979                                    isl_union_map_copy(W), empty,
1980                                    isl_union_map_copy(S),
1981                                    &dep_raw, NULL, NULL, NULL);
1982         isl_union_map_compute_flow(isl_union_map_copy(W),
1983                                    isl_union_map_copy(W),
1984                                    isl_union_map_copy(R),
1985                                    isl_union_map_copy(S),
1986                                    &dep_waw, &dep_war, NULL, NULL);
1987
1988         dep = isl_union_map_union(dep_waw, dep_war);
1989         dep = isl_union_map_union(dep, dep_raw);
1990         validity = isl_union_map_copy(dep);
1991         proximity = isl_union_map_copy(dep);
1992
1993         sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
1994                                                validity, proximity);
1995         schedule = isl_schedule_get_map(sched);
1996         isl_schedule_free(sched);
1997         isl_union_map_free(W);
1998         isl_union_map_free(R);
1999         isl_union_map_free(S);
2000
2001         is_injection = 1;
2002         isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2003
2004         domain = isl_union_map_domain(isl_union_map_copy(schedule));
2005         is_complete = isl_union_set_is_subset(D, domain);
2006         isl_union_set_free(D);
2007         isl_union_set_free(domain);
2008
2009         test = isl_union_map_reverse(isl_union_map_copy(schedule));
2010         test = isl_union_map_apply_range(test, dep);
2011         test = isl_union_map_apply_range(test, schedule);
2012
2013         delta = isl_union_map_deltas(test);
2014         if (isl_union_set_n_set(delta) == 0) {
2015                 is_tilable = 1;
2016                 is_parallel = 1;
2017                 is_nonneg = 1;
2018                 isl_union_set_free(delta);
2019         } else {
2020                 delta_set = isl_set_from_union_set(delta);
2021
2022                 slice = isl_set_universe(isl_set_get_space(delta_set));
2023                 for (i = 0; i < tilable; ++i)
2024                         slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2025                 is_tilable = isl_set_is_subset(delta_set, slice);
2026                 isl_set_free(slice);
2027
2028                 slice = isl_set_universe(isl_set_get_space(delta_set));
2029                 for (i = 0; i < parallel; ++i)
2030                         slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2031                 is_parallel = isl_set_is_subset(delta_set, slice);
2032                 isl_set_free(slice);
2033
2034                 origin = isl_set_universe(isl_set_get_space(delta_set));
2035                 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2036                         origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2037
2038                 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2039                 delta_set = isl_set_lexmin(delta_set);
2040
2041                 is_nonneg = isl_set_is_equal(delta_set, origin);
2042
2043                 isl_set_free(origin);
2044                 isl_set_free(delta_set);
2045         }
2046
2047         if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2048             is_injection < 0 || is_complete < 0)
2049                 return -1;
2050         if (!is_complete)
2051                 isl_die(ctx, isl_error_unknown,
2052                         "generated schedule incomplete", return -1);
2053         if (!is_injection)
2054                 isl_die(ctx, isl_error_unknown,
2055                         "generated schedule not injective on each statement",
2056                         return -1);
2057         if (!is_nonneg)
2058                 isl_die(ctx, isl_error_unknown,
2059                         "negative dependences in generated schedule",
2060                         return -1);
2061         if (!is_tilable)
2062                 isl_die(ctx, isl_error_unknown,
2063                         "generated schedule not as tilable as expected",
2064                         return -1);
2065         if (!is_parallel)
2066                 isl_die(ctx, isl_error_unknown,
2067                         "generated schedule not as parallel as expected",
2068                         return -1);
2069
2070         return 0;
2071 }
2072
2073 int test_special_schedule(isl_ctx *ctx, const char *domain,
2074         const char *validity, const char *proximity, const char *expected_sched)
2075 {
2076         isl_union_set *dom;
2077         isl_union_map *dep;
2078         isl_union_map *prox;
2079         isl_union_map *sched1, *sched2;
2080         isl_schedule *schedule;
2081         int equal;
2082
2083         dom = isl_union_set_read_from_str(ctx, domain);
2084         dep = isl_union_map_read_from_str(ctx, validity);
2085         prox = isl_union_map_read_from_str(ctx, proximity);
2086         schedule = isl_union_set_compute_schedule(dom, dep, prox);
2087         sched1 = isl_schedule_get_map(schedule);
2088         isl_schedule_free(schedule);
2089
2090         sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2091
2092         equal = isl_union_map_is_equal(sched1, sched2);
2093         isl_union_map_free(sched1);
2094         isl_union_map_free(sched2);
2095
2096         if (equal < 0)
2097                 return -1;
2098         if (!equal)
2099                 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2100                         return -1);
2101
2102         return 0;
2103 }
2104
2105 int test_schedule(isl_ctx *ctx)
2106 {
2107         const char *D, *W, *R, *V, *P, *S;
2108
2109         /* Jacobi */
2110         D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2111         W = "{ S1[t,i] -> a[t,i] }";
2112         R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2113                 "S1[t,i] -> a[t-1,i+1] }";
2114         S = "{ S1[t,i] -> [t,i] }";
2115         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2116                 return -1;
2117
2118         /* Fig. 5 of CC2008 */
2119         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2120                                 "j <= -1 + N }";
2121         W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2122                                 "j >= 2 and j <= -1 + N }";
2123         R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2124                                 "j >= 2 and j <= -1 + N; "
2125                     "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2126                                 "j >= 2 and j <= -1 + N }";
2127         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2128         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2129                 return -1;
2130
2131         D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2132         W = "{ S1[i] -> a[i] }";
2133         R = "{ S2[i] -> a[i+1] }";
2134         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2135         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2136                 return -1;
2137
2138         D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2139         W = "{ S1[i] -> a[i] }";
2140         R = "{ S2[i] -> a[9-i] }";
2141         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2142         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2143                 return -1;
2144
2145         D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2146         W = "{ S1[i] -> a[i] }";
2147         R = "[N] -> { S2[i] -> a[N-1-i] }";
2148         S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2149         if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2150                 return -1;
2151         
2152         D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2153         W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2154         R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2155         S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2156         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2157                 return -1;
2158
2159         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2160         W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2161         R = "{ S2[i,j] -> a[i-1,j] }";
2162         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2163         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2164                 return -1;
2165
2166         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2167         W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2168         R = "{ S2[i,j] -> a[i,j-1] }";
2169         S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2170         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2171                 return -1;
2172
2173         D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2174         W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2175                     "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2176         R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2177         S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2178                     "S_0[] -> [0, 0, 0] }";
2179         if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2180                 return -1;
2181         ctx->opt->schedule_parametric = 0;
2182         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2183                 return -1;
2184         ctx->opt->schedule_parametric = 1;
2185
2186         D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2187                     "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2188         W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2189         R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2190                     "S4[i] -> a[i,N] }";
2191         S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2192                 "S4[i] -> [4,i,0] }";
2193         if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2194                 return -1;
2195
2196         D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2197         W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2198                                         "j <= N }";
2199         R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2200                                         "j <= N; "
2201                     "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2202                                         "j <= N }";
2203         S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2204         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2205                 return -1;
2206
2207         D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2208                     " S_2[t] : t >= 0 and t <= -1 + N; "
2209                     " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2210                                 "i <= -1 + N }";
2211         W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2212                     " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2213                     " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2214                                                 "i >= 0 and i <= -1 + N }";
2215         R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2216                                             "i >= 0 and i <= -1 + N; "
2217                     " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2218         S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2219                     " S_0[t] -> [0, t, 0] }";
2220
2221         if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2222                 return -1;
2223         ctx->opt->schedule_parametric = 0;
2224         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2225                 return -1;
2226         ctx->opt->schedule_parametric = 1;
2227
2228         D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2229         S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2230         if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2231                 return -1;
2232
2233         D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2234             "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2235         W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2236                                             "j >= 0 and j <= -1 + N; "
2237                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2238         R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2239                                             "j >= 0 and j <= -1 + N; "
2240                         "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2241         S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2242         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2243                 return -1;
2244
2245         D = "{ S_0[i] : i >= 0 }";
2246         W = "{ S_0[i] -> a[i] : i >= 0 }";
2247         R = "{ S_0[i] -> a[0] : i >= 0 }";
2248         S = "{ S_0[i] -> [0, i, 0] }";
2249         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2250                 return -1;
2251
2252         D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2253         W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2254         R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2255         S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2256         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2257                 return -1;
2258
2259         D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2260                                 "k <= -1 + n and k >= 0 }";
2261         W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "                                                  "k <= -1 + n and k >= 0 }";
2262         R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2263                                         "k <= -1 + n and k >= 0; "
2264                     "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2265                                         "k <= -1 + n and k >= 0; "
2266                     "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2267                                         "k <= -1 + n and k >= 0 }";
2268         S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2269         ctx->opt->schedule_outer_zero_distance = 1;
2270         if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2271                 return -1;
2272         ctx->opt->schedule_outer_zero_distance = 0;
2273
2274         D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2275         V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2276                                            "j >= 1 and j <= 7;"
2277                 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2278                                              "j >= 1 and j <= 8 }";
2279         P = "{ }";
2280         S = "{ S_0[i, j] -> [i + j, j] }";
2281         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2282         if (test_special_schedule(ctx, D, V, P, S) < 0)
2283                 return -1;
2284         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2285
2286         /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2287         D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2288                                  "j >= 0 and j <= -1 + i }";
2289         V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2290                                         "i <= -1 + N and j >= 0;"
2291                      "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2292                                         "i <= -2 + N }";
2293         P = "{ }";
2294         S = "{ S_0[i, j] -> [i, j] }";
2295         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2296         if (test_special_schedule(ctx, D, V, P, S) < 0)
2297                 return -1;
2298         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2299
2300         /* Test both algorithms on a case with only proximity dependences. */
2301         D = "{ S[i,j] : 0 <= i <= 10 }";
2302         V = "{ }";
2303         P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2304         S = "{ S[i, j] -> [j, i] }";
2305         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2306         if (test_special_schedule(ctx, D, V, P, S) < 0)
2307                 return -1;
2308         ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2309         return test_special_schedule(ctx, D, V, P, S);
2310 }
2311
2312 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2313 {
2314         isl_union_map *umap;
2315         int test;
2316
2317         umap = isl_union_map_read_from_str(ctx, str);
2318         test = isl_union_map_plain_is_injective(umap);
2319         isl_union_map_free(umap);
2320         if (test < 0)
2321                 return -1;
2322         if (test == injective)
2323                 return 0;
2324         if (injective)
2325                 isl_die(ctx, isl_error_unknown,
2326                         "map not detected as injective", return -1);
2327         else
2328                 isl_die(ctx, isl_error_unknown,
2329                         "map detected as injective", return -1);
2330 }
2331
2332 int test_injective(isl_ctx *ctx)
2333 {
2334         const char *str;
2335
2336         if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2337                 return -1;
2338         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2339                 return -1;
2340         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2341                 return -1;
2342         if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2343                 return -1;
2344         if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2345                 return -1;
2346         if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2347                 return -1;
2348         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2349                 return -1;
2350         if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2351                 return -1;
2352
2353         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2354         if (test_plain_injective(ctx, str, 1))
2355                 return -1;
2356         str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2357         if (test_plain_injective(ctx, str, 0))
2358                 return -1;
2359
2360         return 0;
2361 }
2362
2363 int test_aff(isl_ctx *ctx)
2364 {
2365         const char *str;
2366         isl_set *set;
2367         isl_space *dim;
2368         isl_local_space *ls;
2369         isl_aff *aff;
2370         int zero;
2371
2372         dim = isl_space_set_alloc(ctx, 0, 1);
2373         ls = isl_local_space_from_space(dim);
2374         aff = isl_aff_zero_on_domain(ls);
2375
2376         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2377         aff = isl_aff_scale_down_ui(aff, 3);
2378         aff = isl_aff_floor(aff);
2379         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2380         aff = isl_aff_scale_down_ui(aff, 2);
2381         aff = isl_aff_floor(aff);
2382         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2383
2384         str = "{ [10] }";
2385         set = isl_set_read_from_str(ctx, str);
2386         aff = isl_aff_gist(aff, set);
2387
2388         aff = isl_aff_add_constant_si(aff, -16);
2389         zero = isl_aff_plain_is_zero(aff);
2390         isl_aff_free(aff);
2391
2392         if (zero < 0)
2393                 return -1;
2394         if (!zero)
2395                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2396
2397         return 0;
2398 }
2399
2400 int test_dim_max(isl_ctx *ctx)
2401 {
2402         int equal;
2403         const char *str;
2404         isl_set *set1, *set2;
2405         isl_set *set;
2406         isl_map *map;
2407         isl_pw_aff *pwaff;
2408
2409         str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2410         set = isl_set_read_from_str(ctx, str);
2411         pwaff = isl_set_dim_max(set, 0);
2412         set1 = isl_set_from_pw_aff(pwaff);
2413         str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2414         set2 = isl_set_read_from_str(ctx, str);
2415         equal = isl_set_is_equal(set1, set2);
2416         isl_set_free(set1);
2417         isl_set_free(set2);
2418         if (equal < 0)
2419                 return -1;
2420         if (!equal)
2421                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2422
2423         str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2424         set = isl_set_read_from_str(ctx, str);
2425         pwaff = isl_set_dim_max(set, 0);
2426         set1 = isl_set_from_pw_aff(pwaff);
2427         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2428         set2 = isl_set_read_from_str(ctx, str);
2429         equal = isl_set_is_equal(set1, set2);
2430         isl_set_free(set1);
2431         isl_set_free(set2);
2432         if (equal < 0)
2433                 return -1;
2434         if (!equal)
2435                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2436
2437         str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2438         set = isl_set_read_from_str(ctx, str);
2439         pwaff = isl_set_dim_max(set, 0);
2440         set1 = isl_set_from_pw_aff(pwaff);
2441         str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2442         set2 = isl_set_read_from_str(ctx, str);
2443         equal = isl_set_is_equal(set1, set2);
2444         isl_set_free(set1);
2445         isl_set_free(set2);
2446         if (equal < 0)
2447                 return -1;
2448         if (!equal)
2449                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2450
2451         str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2452                         "0 <= i < N and 0 <= j < M }";
2453         map = isl_map_read_from_str(ctx, str);
2454         set = isl_map_range(map);
2455
2456         pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2457         set1 = isl_set_from_pw_aff(pwaff);
2458         str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2459         set2 = isl_set_read_from_str(ctx, str);
2460         equal = isl_set_is_equal(set1, set2);
2461         isl_set_free(set1);
2462         isl_set_free(set2);
2463
2464         pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2465         set1 = isl_set_from_pw_aff(pwaff);
2466         str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2467         set2 = isl_set_read_from_str(ctx, str);
2468         if (equal >= 0 && equal)
2469                 equal = isl_set_is_equal(set1, set2);
2470         isl_set_free(set1);
2471         isl_set_free(set2);
2472
2473         isl_set_free(set);
2474
2475         if (equal < 0)
2476                 return -1;
2477         if (!equal)
2478                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2479
2480         return 0;
2481 }
2482
2483 int test_product(isl_ctx *ctx)
2484 {
2485         const char *str;
2486         isl_set *set;
2487         isl_union_set *uset1, *uset2;
2488         int ok;
2489
2490         str = "{ A[i] }";
2491         set = isl_set_read_from_str(ctx, str);
2492         set = isl_set_product(set, isl_set_copy(set));
2493         ok = isl_set_is_wrapping(set);
2494         isl_set_free(set);
2495         if (ok < 0)
2496                 return -1;
2497         if (!ok)
2498                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2499
2500         str = "{ [] }";
2501         uset1 = isl_union_set_read_from_str(ctx, str);
2502         uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
2503         str = "{ [[] -> []] }";
2504         uset2 = isl_union_set_read_from_str(ctx, str);
2505         ok = isl_union_set_is_equal(uset1, uset2);
2506         isl_union_set_free(uset1);
2507         isl_union_set_free(uset2);
2508         if (ok < 0)
2509                 return -1;
2510         if (!ok)
2511                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2512
2513         return 0;
2514 }
2515
2516 int test_equal(isl_ctx *ctx)
2517 {
2518         const char *str;
2519         isl_set *set, *set2;
2520         int equal;
2521
2522         str = "{ S_6[i] }";
2523         set = isl_set_read_from_str(ctx, str);
2524         str = "{ S_7[i] }";
2525         set2 = isl_set_read_from_str(ctx, str);
2526         equal = isl_set_is_equal(set, set2);
2527         isl_set_free(set);
2528         isl_set_free(set2);
2529         if (equal < 0)
2530                 return -1;
2531         if (equal)
2532                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2533
2534         return 0;
2535 }
2536
2537 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2538         enum isl_dim_type type, unsigned pos, int fixed)
2539 {
2540         int test;
2541
2542         test = isl_map_plain_is_fixed(map, type, pos, NULL);
2543         isl_map_free(map);
2544         if (test < 0)
2545                 return -1;
2546         if (test == fixed)
2547                 return 0;
2548         if (fixed)
2549                 isl_die(ctx, isl_error_unknown,
2550                         "map not detected as fixed", return -1);
2551         else
2552                 isl_die(ctx, isl_error_unknown,
2553                         "map detected as fixed", return -1);
2554 }
2555
2556 int test_fixed(isl_ctx *ctx)
2557 {
2558         const char *str;
2559         isl_map *map;
2560
2561         str = "{ [i] -> [i] }";
2562         map = isl_map_read_from_str(ctx, str);
2563         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2564                 return -1;
2565         str = "{ [i] -> [1] }";
2566         map = isl_map_read_from_str(ctx, str);
2567         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2568                 return -1;
2569         str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2570         map = isl_map_read_from_str(ctx, str);
2571         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2572                 return -1;
2573         map = isl_map_read_from_str(ctx, str);
2574         map = isl_map_neg(map);
2575         if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2576                 return -1;
2577
2578         return 0;
2579 }
2580
2581 int test_vertices(isl_ctx *ctx)
2582 {
2583         const char *str;
2584         isl_basic_set *bset;
2585         isl_vertices *vertices;
2586
2587         str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
2588         bset = isl_basic_set_read_from_str(ctx, str);
2589         vertices = isl_basic_set_compute_vertices(bset);
2590         isl_basic_set_free(bset);
2591         isl_vertices_free(vertices);
2592         if (!vertices)
2593                 return -1;
2594
2595         str = "{ A[t, i] : t = 14 and i = 1 }";
2596         bset = isl_basic_set_read_from_str(ctx, str);
2597         vertices = isl_basic_set_compute_vertices(bset);
2598         isl_basic_set_free(bset);
2599         isl_vertices_free(vertices);
2600         if (!vertices)
2601                 return -1;
2602
2603         return 0;
2604 }
2605
2606 int test_union_pw(isl_ctx *ctx)
2607 {
2608         int equal;
2609         const char *str;
2610         isl_union_set *uset;
2611         isl_union_pw_qpolynomial *upwqp1, *upwqp2;
2612
2613         str = "{ [x] -> x^2 }";
2614         upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
2615         upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
2616         uset = isl_union_pw_qpolynomial_domain(upwqp1);
2617         upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
2618         upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
2619         equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
2620         isl_union_pw_qpolynomial_free(upwqp1);
2621         isl_union_pw_qpolynomial_free(upwqp2);
2622         if (equal < 0)
2623                 return -1;
2624         if (!equal)
2625                 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2626
2627         return 0;
2628 }
2629
2630 int main()
2631 {
2632         struct isl_ctx *ctx;
2633
2634         srcdir = getenv("srcdir");
2635         assert(srcdir);
2636
2637         ctx = isl_ctx_alloc();
2638         if (test_vertices(ctx) < 0)
2639                 goto error;
2640         if (test_fixed(ctx) < 0)
2641                 goto error;
2642         if (test_equal(ctx) < 0)
2643                 goto error;
2644         if (test_product(ctx) < 0)
2645                 goto error;
2646         if (test_dim_max(ctx) < 0)
2647                 goto error;
2648         if (test_aff(ctx) < 0)
2649                 goto error;
2650         if (test_injective(ctx) < 0)
2651                 goto error;
2652         if (test_schedule(ctx) < 0)
2653                 goto error;
2654         if (test_union_pw(ctx) < 0)
2655                 goto error;
2656         test_factorize(ctx);
2657         test_subset(ctx);
2658         test_lift(ctx);
2659         test_bound(ctx);
2660         test_union(ctx);
2661         test_split_periods(ctx);
2662         test_parse(ctx);
2663         test_pwqp(ctx);
2664         test_lex(ctx);
2665         if (test_sv(ctx) < 0)
2666                 goto error;
2667         test_bijective(ctx);
2668         test_dep(ctx);
2669         test_read(ctx);
2670         test_bounded(ctx);
2671         test_construction(ctx);
2672         test_dim(ctx);
2673         test_div(ctx);
2674         test_application(ctx);
2675         test_affine_hull(ctx);
2676         test_convex_hull(ctx);
2677         test_gist(ctx);
2678         if (test_coalesce(ctx) < 0)
2679                 goto error;
2680         test_closure(ctx);
2681         test_lexmin(ctx);
2682         isl_ctx_free(ctx);
2683         return 0;
2684 error:
2685         isl_ctx_free(ctx);
2686         return -1;
2687 }