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