isl_pw_qpolynomial_fold_bound: avoid access to freed memory
[platform/upstream/isl.git] / isl_test.c
index 3548c91..ab446a2 100644 (file)
 #include <assert.h>
 #include <stdio.h>
 #include <limits.h>
-#include <isl_ctx.h>
-#include <isl_set.h>
-#include <isl_flow.h>
-#include <isl_constraint.h>
-#include <isl_polynomial.h>
-#include <isl_union_map.h>
+#include <isl/ctx.h>
+#include <isl/set.h>
+#include <isl/flow.h>
+#include <isl/constraint.h>
+#include <isl/polynomial.h>
+#include <isl/union_map.h>
+#include <isl_map_private.h>
 
 static char *srcdir;
 
@@ -28,6 +29,15 @@ void test_parse_map(isl_ctx *ctx, const char *str)
        isl_map_free(map);
 }
 
+void test_parse_pwqp(isl_ctx *ctx, const char *str)
+{
+       isl_pw_qpolynomial *pwqp;
+
+       pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
+       assert(pwqp);
+       isl_pw_qpolynomial_free(pwqp);
+}
+
 void test_parse(struct isl_ctx *ctx)
 {
        isl_map *map, *map2;
@@ -55,6 +65,8 @@ void test_parse(struct isl_ctx *ctx)
        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 ] }");
 }
 
 void test_read(struct isl_ctx *ctx)
@@ -1409,6 +1421,7 @@ void test_bijective(struct isl_ctx *ctx)
 void test_pwqp(struct isl_ctx *ctx)
 {
        const char *str;
+       isl_set *set;
        isl_pw_qpolynomial *pwqp1, *pwqp2;
 
        str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
@@ -1425,6 +1438,70 @@ void test_pwqp(struct isl_ctx *ctx)
        assert(isl_pw_qpolynomial_is_zero(pwqp1));
 
        isl_pw_qpolynomial_free(pwqp1);
+
+       str = "{ [i] -> i }";
+       pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
+       str = "{ [k] : exists a : k = 2a }";
+       set = isl_set_read_from_str(ctx, str, 0);
+       pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
+       str = "{ [i] -> i }";
+       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 = "{ [i] -> i + [ (i + [i/3])/2 ] }";
+       pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
+       str = "{ [10] }";
+       set = isl_set_read_from_str(ctx, str, 0);
+       pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
+       str = "{ [i] -> 16 }";
+       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 = "{ [i] -> ([(i)/2]) }";
+       pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
+       str = "{ [k] : exists a : k = 2a+1 }";
+       set = isl_set_read_from_str(ctx, str, 0);
+       pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
+       str = "{ [i] -> -1/2 + 1/2 * i }";
+       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 = "{ [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)
@@ -1446,19 +1523,58 @@ void test_split_periods(isl_ctx *ctx)
 void test_union(isl_ctx *ctx)
 {
        const char *str;
-       isl_union_set *uset;
+       isl_union_set *uset1, *uset2;
        isl_union_map *umap1, *umap2;
 
        str = "{ [i] : 0 <= i <= 1 }";
-       uset = isl_union_set_from_set(isl_set_read_from_str(ctx, str, -1));
+       uset1 = isl_union_set_read_from_str(ctx, str);
        str = "{ [1] -> [0] }";
-       umap1 = isl_union_map_from_map(isl_map_read_from_str(ctx, str, -1));
+       umap1 = isl_union_map_read_from_str(ctx, str);
 
-       umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset), uset);
+       umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
        assert(isl_union_map_is_equal(umap1, umap2));
 
        isl_union_map_free(umap1);
        isl_union_map_free(umap2);
+
+       str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
+       umap1 = isl_union_map_read_from_str(ctx, str);
+       str = "{ A[i]; B[i] }";
+       uset1 = isl_union_set_read_from_str(ctx, str);
+
+       uset2 = isl_union_map_domain(umap1);
+
+       assert(isl_union_set_is_equal(uset1, uset2));
+
+       isl_union_set_free(uset1);
+       isl_union_set_free(uset2);
+}
+
+void test_bound(isl_ctx *ctx)
+{
+       const char *str;
+       isl_pw_qpolynomial *pwqp;
+       isl_pw_qpolynomial_fold *pwf;
+
+       str = "{ [[a, b, c, d] -> [e]] -> 0 }";
+       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) == 4);
+       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);
 }
 
 int main()
@@ -1469,6 +1585,8 @@ int main()
        assert(srcdir);
 
        ctx = isl_ctx_alloc();
+       test_lift(ctx);
+       test_bound(ctx);
        test_union(ctx);
        test_split_periods(ctx);
        test_parse(ctx);