isl_stream_read_map: properly read nested divs
[platform/upstream/isl.git] / isl_test.c
index a5f9072..46177d6 100644 (file)
 #include <assert.h>
 #include <stdio.h>
 #include <limits.h>
-#include <isl/ctx.h>
+#include <isl_ctx_private.h>
+#include <isl_map_private.h>
 #include <isl/set.h>
 #include <isl/flow.h>
 #include <isl/constraint.h>
 #include <isl/polynomial.h>
 #include <isl/union_map.h>
+#include <isl_factorization.h>
 
 static char *srcdir;
 
@@ -28,6 +30,17 @@ void test_parse_map(isl_ctx *ctx, const char *str)
        isl_map_free(map);
 }
 
+void test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
+{
+       isl_map *map, *map2;
+
+       map = isl_map_read_from_str(ctx, str, -1);
+       map2 = isl_map_read_from_str(ctx, str2, -1);
+       assert(map && map2 && isl_map_is_equal(map, map2));
+       isl_map_free(map);
+       isl_map_free(map2);
+}
+
 void test_parse_pwqp(isl_ctx *ctx, const char *str)
 {
        isl_pw_qpolynomial *pwqp;
@@ -40,7 +53,7 @@ void test_parse_pwqp(isl_ctx *ctx, const char *str)
 void test_parse(struct isl_ctx *ctx)
 {
        isl_map *map, *map2;
-       const char *str;
+       const char *str, *str2;
 
        str = "{ [i] -> [-i] }";
        map = isl_map_read_from_str(ctx, str, -1);
@@ -53,6 +66,12 @@ void test_parse(struct isl_ctx *ctx)
        isl_map_free(map);
 
        test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
+       test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
+                               "p1 = 1 && (y1 <= y2 || y2 = 0) }");
+
+       str = "{ [x,y]  : [([x/2]+y)/3] >= 1 }";
+       str2 = "{ [x, y] : 2y >= 6 - x }";
+       test_parse_map_equal(ctx, str, str2);
 
        str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
        map = isl_map_read_from_str(ctx, str, -1);
@@ -65,6 +84,14 @@ void test_parse(struct isl_ctx *ctx)
        isl_map_free(map);
        isl_map_free(map2);
 
+       str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
+       map = isl_map_read_from_str(ctx, str, -1);
+       str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
+       map2 = isl_map_read_from_str(ctx, str, -1);
+       assert(isl_map_is_equal(map, map2));
+       isl_map_free(map);
+       isl_map_free(map2);
+
        test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
 }
 
@@ -564,10 +591,12 @@ void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
        fclose(input);
 }
 
-void test_convex_hull(struct isl_ctx *ctx)
+void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
 {
        const char *str1, *str2;
        isl_set *set1, *set2;
+       int orig_convex = ctx->opt->convex;
+       ctx->opt->convex = convex;
 
        test_convex_hull_case(ctx, "convex0");
        test_convex_hull_case(ctx, "convex1");
@@ -596,6 +625,14 @@ void test_convex_hull(struct isl_ctx *ctx)
        assert(isl_set_is_equal(set1, set2));
        isl_set_free(set1);
        isl_set_free(set2);
+
+       ctx->opt->convex = orig_convex;
+}
+
+void test_convex_hull(struct isl_ctx *ctx)
+{
+       test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
+       test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
 }
 
 void test_gist_case(struct isl_ctx *ctx, const char *name)
@@ -628,7 +665,33 @@ void test_gist_case(struct isl_ctx *ctx, const char *name)
 
 void test_gist(struct isl_ctx *ctx)
 {
+       const char *str;
+       isl_basic_set *bset1, *bset2;
+
        test_gist_case(ctx, "gist1");
+
+       str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
+           "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
+           "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
+           "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
+           "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
+           "16e0 >= 16 + 16p6 + 15p10 and  p2 <= 15 and p3 >= 0 and "
+           "p3 <= 31 and  p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
+           "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
+           "p10 <= 15 and p10 <= -1 + p0 - p6) }";
+       bset1 = isl_basic_set_read_from_str(ctx, str, -1);
+       str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
+           "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
+           "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
+           "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
+           "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
+           "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
+           "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
+           "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
+       bset2 = isl_basic_set_read_from_str(ctx, str, -1);
+       bset1 = isl_basic_set_gist(bset1, bset2);
+       assert(bset1 && bset1->n_div == 0);
+       isl_basic_set_free(bset1);
 }
 
 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
@@ -884,6 +947,12 @@ void test_coalesce(struct isl_ctx *ctx)
        test_coalesce_set(ctx,
                "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
        test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
+       test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1);
+       test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0);
+       test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1);
+       test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0);
+       test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0);
+       test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0);
 }
 
 void test_closure(struct isl_ctx *ctx)
@@ -896,11 +965,11 @@ void test_closure(struct isl_ctx *ctx)
 
        /* COCOA example 1 */
        map = isl_map_read_from_str(ctx,
-               "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
+               "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
                        "1 <= i and i < n and 1 <= j and j < n or "
                        "i2 = i + 1 and j2 = j - 1 and "
                        "1 <= i and i < n and 2 <= j and j <= n }", -1);
-       map = isl_map_power(map, 1, &exact);
+       map = isl_map_power(map, &exact);
        assert(exact);
        isl_map_free(map);
 
@@ -966,14 +1035,14 @@ void test_closure(struct isl_ctx *ctx)
 
        /* COCOA Fig.2 right */
        map = isl_map_read_from_str(ctx,
-               "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
+               "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
                        "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
                        "j <= n or "
                        "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
                        "j <= 2 i - 4 and j <= n - 3 or "
                        "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
                        "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
-       map = isl_map_power(map, 1, &exact);
+       map = isl_map_power(map, &exact);
        assert(exact);
        isl_map_free(map);
 
@@ -1098,6 +1167,31 @@ void test_closure(struct isl_ctx *ctx)
        assert(isl_map_is_equal(map, map2));
        isl_map_free(map);
        isl_map_free(map2);
+
+       str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
+           "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
+           "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
+           "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
+           "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
+           "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
+           "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
+           "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
+           "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
+           "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
+           "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
+           "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
+           "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
+           "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
+           "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
+           "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
+           "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
+           "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
+           "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
+           "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
+       map = isl_map_read_from_str(ctx, str, -1);
+       map = isl_map_transitive_closure(map, NULL);
+       assert(map);
+       isl_map_free(map);
 }
 
 void test_lex(struct isl_ctx *ctx)
@@ -1114,7 +1208,7 @@ void test_lex(struct isl_ctx *ctx)
 void test_lexmin(struct isl_ctx *ctx)
 {
        const char *str;
-       isl_map *map;
+       isl_map *map, *map2;
        isl_set *set;
        isl_set *set2;
 
@@ -1143,6 +1237,36 @@ void test_lexmin(struct isl_ctx *ctx)
        set = isl_set_intersect(set, set2);
        assert(!isl_set_is_empty(set));
        isl_set_free(set);
+
+       str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
+       map = isl_map_read_from_str(ctx, str, -1);
+       map = isl_map_lexmin(map);
+       str = "{ [x] -> [5] : 6 <= x <= 8; "
+               "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
+       map2 = isl_map_read_from_str(ctx, str, -1);
+       assert(isl_map_is_equal(map, map2));
+       isl_map_free(map);
+       isl_map_free(map2);
+
+       str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
+       map = isl_map_read_from_str(ctx, str, -1);
+       map2 = isl_map_copy(map);
+       map = isl_map_lexmin(map);
+       assert(isl_map_is_equal(map, map2));
+       isl_map_free(map);
+       isl_map_free(map2);
+
+       str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
+       map = isl_map_read_from_str(ctx, str, -1);
+       map = isl_map_lexmin(map);
+       str = "{ [x] -> [y] : (4y = x and x >= 0) or "
+               "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
+               "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
+               "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
+       map2 = isl_map_read_from_str(ctx, str, -1);
+       assert(isl_map_is_equal(map, map2));
+       isl_map_free(map);
+       isl_map_free(map2);
 }
 
 struct must_may {
@@ -1479,6 +1603,28 @@ void test_pwqp(struct isl_ctx *ctx)
        assert(isl_pw_qpolynomial_is_zero(pwqp1));
 
        isl_pw_qpolynomial_free(pwqp1);
+
+       str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
+       pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
+       str = "{ [i] -> ([(2 * [i/2])/5]) }";
+       pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
+
+       pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
+
+       assert(isl_pw_qpolynomial_is_zero(pwqp1));
+
+       isl_pw_qpolynomial_free(pwqp1);
+
+       str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
+       pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
+       str = "{ [x] -> x }";
+       pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
+
+       pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
+
+       assert(isl_pw_qpolynomial_is_zero(pwqp1));
+
+       isl_pw_qpolynomial_free(pwqp1);
 }
 
 void test_split_periods(isl_ctx *ctx)
@@ -1538,6 +1684,78 @@ void test_bound(isl_ctx *ctx)
        pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
        assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 4);
        isl_pw_qpolynomial_fold_free(pwf);
+
+       str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
+       pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
+       pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
+       assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 1);
+       isl_pw_qpolynomial_fold_free(pwf);
+}
+
+void test_lift(isl_ctx *ctx)
+{
+       const char *str;
+       isl_basic_map *bmap;
+       isl_basic_set *bset;
+
+       str = "{ [i0] : exists e0 : i0 = 4e0 }";
+       bset = isl_basic_set_read_from_str(ctx, str, 0);
+       bset = isl_basic_set_lift(bset);
+       bmap = isl_basic_map_from_range(bset);
+       bset = isl_basic_map_domain(bmap);
+       isl_basic_set_free(bset);
+}
+
+void test_subset(isl_ctx *ctx)
+{
+       const char *str;
+       isl_set *set1, *set2;
+
+       str = "{ [112, 0] }";
+       set1 = isl_set_read_from_str(ctx, str, 0);
+       str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
+               "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
+               "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
+       set2 = isl_set_read_from_str(ctx, str, 0);
+       assert(isl_set_is_subset(set1, set2));
+       isl_set_free(set1);
+       isl_set_free(set2);
+}
+
+void test_factorize(isl_ctx *ctx)
+{
+       const char *str;
+       isl_basic_set *bset;
+       isl_factorizer *f;
+
+       str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
+           "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
+           "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
+           "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
+           "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
+           "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
+           "3i5 >= -2i0 - i2 + 3i4 }";
+       bset = isl_basic_set_read_from_str(ctx, str, 0);
+       f = isl_basic_set_factorizer(bset);
+       assert(f);
+       isl_basic_set_free(bset);
+       isl_factorizer_free(f);
+
+       str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
+           "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
+           "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
+           "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
+           "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
+           "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
+           "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
+           "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
+           "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
+           "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
+       bset = isl_basic_set_read_from_str(ctx, str, 0);
+       f = isl_basic_set_factorizer(bset);
+       assert(f);
+       isl_basic_set_free(bset);
+       isl_factorizer_free(f);
 }
 
 int main()
@@ -1548,6 +1766,9 @@ int main()
        assert(srcdir);
 
        ctx = isl_ctx_alloc();
+       test_factorize(ctx);
+       test_subset(ctx);
+       test_lift(ctx);
        test_bound(ctx);
        test_union(ctx);
        test_split_periods(ctx);