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