add isl_pw_multi_aff_range_product
[platform/upstream/isl.git] / isl_test.c
index f523a8e..d4ff25d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
  *
  * Written by Sven Verdoolaege, K.U.Leuven, Departement
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
@@ -12,7 +12,7 @@
 #include <limits.h>
 #include <isl_ctx_private.h>
 #include <isl_map_private.h>
-#include <isl/aff.h>
+#include <isl_aff_private.h>
 #include <isl/set.h>
 #include <isl/flow.h>
 #include <isl/constraint.h>
@@ -23,6 +23,8 @@
 #include <isl_options_private.h>
 #include <isl/vertices.h>
 
+#define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
+
 static char *srcdir;
 
 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
@@ -194,6 +196,10 @@ int test_parse(struct isl_ctx *ctx)
                                      "{ [a] -> [b] : true }") < 0)
                return -1;
 
+       if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
+                                     "{ [i] : i <= 10 }") < 0)
+               return -1;
+
        return 0;
 }
 
@@ -308,10 +314,14 @@ void test_dim(struct isl_ctx *ctx)
        isl_map_free(map2);
 }
 
-void test_div(struct isl_ctx *ctx)
+static int test_div(isl_ctx *ctx)
 {
+       unsigned n;
+       const char *str;
+       int empty;
        isl_int v;
        isl_space *dim;
+       isl_set *set;
        isl_local_space *ls;
        struct isl_basic_set *bset;
        struct isl_constraint *c;
@@ -604,6 +614,38 @@ void test_div(struct isl_ctx *ctx)
        isl_basic_set_free(bset);
 
        isl_int_clear(v);
+
+       str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
+           "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
+       set = isl_set_read_from_str(ctx, str);
+       set = isl_set_compute_divs(set);
+       isl_set_free(set);
+       if (!set)
+               return -1;
+
+       str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
+       bset = isl_basic_set_read_from_str(ctx, str);
+       n = isl_basic_set_dim(bset, isl_dim_div);
+       isl_basic_set_free(bset);
+       if (!bset)
+               return -1;
+       if (n != 0)
+               isl_die(ctx, isl_error_unknown,
+                       "expecting no existentials", return -1);
+
+       str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
+       set = isl_set_read_from_str(ctx, str);
+       set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
+       set = isl_set_fix_si(set, isl_dim_set, 2, -3);
+       empty = isl_set_is_empty(set);
+       isl_set_free(set);
+       if (empty < 0)
+               return -1;
+       if (!empty)
+               isl_die(ctx, isl_error_unknown,
+                       "result not as accurate as expected", return -1);
+
+       return 0;
 }
 
 void test_application_case(struct isl_ctx *ctx, const char *name)
@@ -848,6 +890,38 @@ int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
        return 0;
 }
 
+int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
+{
+       int r = 0;
+       int bounded;
+
+       bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
+       isl_options_set_coalesce_bounded_wrapping(ctx, 0);
+
+       if (test_coalesce_set(ctx,
+               "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
+                       "-x - y + 1 >= 0 and -3 <= z <= 3;"
+               "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
+                       "x-z + 20 >= 0 and x+z + 20 >= 0 and "
+                       "-10 <= y <= 0}", 1) < 0)
+               goto error;
+       if (test_coalesce_set(ctx,
+               "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
+               goto error;
+       if (test_coalesce_set(ctx,
+               "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
+               goto error;
+
+       if (0) {
+error:
+               r = -1;
+       }
+
+       isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
+
+       return r;
+}
+
 int test_coalesce(struct isl_ctx *ctx)
 {
        const char *str;
@@ -1076,19 +1150,10 @@ int test_coalesce(struct isl_ctx *ctx)
        test_coalesce_set(ctx,
                "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
                "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
-       test_coalesce_set(ctx,
-               "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
-                       "-x - y + 1 >= 0 and -3 <= z <= 3;"
-               "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
-                       "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
-       if (test_coalesce_set(ctx,
-               "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
+       if (test_coalesce_unbounded_wrapping(ctx) < 0)
                return -1;
        if (test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0) < 0)
                return -1;
-       if (test_coalesce_set(ctx,
-               "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
-               return -1;
        if (test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1) < 0)
                return -1;
        if (test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1) < 0)
@@ -1112,6 +1177,9 @@ int test_coalesce(struct isl_ctx *ctx)
                        "j >= 1 and j' <= i + j - i' and i >= 1; "
                "[1, 1, 1, 1] }", 0) < 0)
                return -1;
+       if (test_coalesce_set(ctx, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
+                                    "[i,j] : exists a : j = 3a }", 1) < 0)
+               return -1;
        return 0;
 }
 
@@ -1942,23 +2010,88 @@ void test_lift(isl_ctx *ctx)
        isl_basic_set_free(bset);
 }
 
-void test_subset(isl_ctx *ctx)
+struct {
+       const char *set1;
+       const char *set2;
+       int subset;
+} subset_tests[] = {
+       { "{ [112, 0] }",
+         "{ [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) }", 1 },
+       { "{ [65] }",
+         "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
+               "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
+               "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
+                   "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
+                   "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
+                   "256e0 <= 255i and 256e0 >= -255 + 255i and "
+                   "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
+                   "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
+                   "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
+};
+
+static int test_subset(isl_ctx *ctx)
 {
-       const char *str;
+       int i;
        isl_set *set1, *set2;
+       int subset;
+
+       for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
+               set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
+               set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
+               subset = isl_set_is_subset(set1, set2);
+               isl_set_free(set1);
+               isl_set_free(set2);
+               if (subset < 0)
+                       return -1;
+               if (subset != subset_tests[i].subset)
+                       isl_die(ctx, isl_error_unknown,
+                               "incorrect subset result", return -1);
+       }
 
-       str = "{ [112, 0] }";
-       set1 = isl_set_read_from_str(ctx, str);
-       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);
-       assert(isl_set_is_subset(set1, set2));
-       isl_set_free(set1);
-       isl_set_free(set2);
+       return 0;
+}
+
+struct {
+       const char *minuend;
+       const char *subtrahend;
+       const char *difference;
+} subtract_domain_tests[] = {
+       { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
+       { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
+       { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
+};
+
+static int test_subtract(isl_ctx *ctx)
+{
+       int i;
+       isl_union_map *umap1, *umap2;
+       isl_union_set *uset;
+       int equal;
+
+       for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
+               umap1 = isl_union_map_read_from_str(ctx,
+                               subtract_domain_tests[i].minuend);
+               uset = isl_union_set_read_from_str(ctx,
+                               subtract_domain_tests[i].subtrahend);
+               umap2 = isl_union_map_read_from_str(ctx,
+                               subtract_domain_tests[i].difference);
+               umap1 = isl_union_map_subtract_domain(umap1, uset);
+               equal = isl_union_map_is_equal(umap1, umap2);
+               isl_union_map_free(umap1);
+               isl_union_map_free(umap2);
+               if (equal < 0)
+                       return -1;
+               if (!equal)
+                       isl_die(ctx, isl_error_unknown,
+                               "incorrect subtract domain result", return -1);
+       }
+
+       return 0;
 }
 
-void test_factorize(isl_ctx *ctx)
+int test_factorize(isl_ctx *ctx)
 {
        const char *str;
        isl_basic_set *bset;
@@ -1973,9 +2106,11 @@ void test_factorize(isl_ctx *ctx)
            "3i5 >= -2i0 - i2 + 3i4 }";
        bset = isl_basic_set_read_from_str(ctx, str);
        f = isl_basic_set_factorizer(bset);
-       assert(f);
        isl_basic_set_free(bset);
        isl_factorizer_free(f);
+       if (!f)
+               isl_die(ctx, isl_error_unknown,
+                       "failed to construct factorizer", return -1);
 
        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 "
@@ -1989,9 +2124,13 @@ void test_factorize(isl_ctx *ctx)
            "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
        bset = isl_basic_set_read_from_str(ctx, str);
        f = isl_basic_set_factorizer(bset);
-       assert(f);
        isl_basic_set_free(bset);
        isl_factorizer_free(f);
+       if (!f)
+               isl_die(ctx, isl_error_unknown,
+                       "failed to construct factorizer", return -1);
+
+       return 0;
 }
 
 static int check_injective(__isl_take isl_map *map, void *user)
@@ -2463,17 +2602,45 @@ int test_injective(isl_ctx *ctx)
        return 0;
 }
 
+static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
+{
+       isl_aff *aff2;
+       int equal;
+
+       if (!aff)
+               return -1;
+
+       aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
+       equal = isl_aff_plain_is_equal(aff, aff2);
+       isl_aff_free(aff2);
+
+       return equal;
+}
+
+static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
+{
+       int equal;
+
+       equal = aff_plain_is_equal(aff, str);
+       if (equal < 0)
+               return -1;
+       if (!equal)
+               isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
+                       "result not as expected", return -1);
+       return 0;
+}
+
 int test_aff(isl_ctx *ctx)
 {
        const char *str;
        isl_set *set;
-       isl_space *dim;
+       isl_space *space;
        isl_local_space *ls;
        isl_aff *aff;
-       int zero;
+       int zero, equal;
 
-       dim = isl_space_set_alloc(ctx, 0, 1);
-       ls = isl_local_space_from_space(dim);
+       space = isl_space_set_alloc(ctx, 0, 1);
+       ls = isl_local_space_from_space(space);
        aff = isl_aff_zero_on_domain(ls);
 
        aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
@@ -2497,6 +2664,14 @@ int test_aff(isl_ctx *ctx)
        if (!zero)
                isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
 
+       aff = isl_aff_read_from_str(ctx, "{ [-1] }");
+       aff = isl_aff_scale_down_ui(aff, 64);
+       aff = isl_aff_floor(aff);
+       equal = aff_check_plain_equal(aff, "{ [-1] }");
+       isl_aff_free(aff);
+       if (equal < 0)
+               return -1;
+
        return 0;
 }
 
@@ -2824,64 +2999,242 @@ int test_fixed_power(isl_ctx *ctx)
        return 0;
 }
 
+int test_slice(isl_ctx *ctx)
+{
+       const char *str;
+       isl_map *map;
+       int equal;
+
+       str = "{ [i] -> [j] }";
+       map = isl_map_read_from_str(ctx, str);
+       map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
+       equal = map_check_equal(map, "{ [i] -> [i] }");
+       isl_map_free(map);
+       if (equal < 0)
+               return -1;
+
+       str = "{ [i] -> [j] }";
+       map = isl_map_read_from_str(ctx, str);
+       map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
+       equal = map_check_equal(map, "{ [i] -> [j] }");
+       isl_map_free(map);
+       if (equal < 0)
+               return -1;
+
+       str = "{ [i] -> [j] }";
+       map = isl_map_read_from_str(ctx, str);
+       map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
+       equal = map_check_equal(map, "{ [i] -> [-i] }");
+       isl_map_free(map);
+       if (equal < 0)
+               return -1;
+
+       str = "{ [i] -> [j] }";
+       map = isl_map_read_from_str(ctx, str);
+       map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
+       equal = map_check_equal(map, "{ [0] -> [j] }");
+       isl_map_free(map);
+       if (equal < 0)
+               return -1;
+
+       str = "{ [i] -> [j] }";
+       map = isl_map_read_from_str(ctx, str);
+       map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
+       equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
+       isl_map_free(map);
+       if (equal < 0)
+               return -1;
+
+       str = "{ [i] -> [j] }";
+       map = isl_map_read_from_str(ctx, str);
+       map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
+       equal = map_check_equal(map, "{ [i] -> [j] : false }");
+       isl_map_free(map);
+       if (equal < 0)
+               return -1;
+
+       return 0;
+}
+
+int test_eliminate(isl_ctx *ctx)
+{
+       const char *str;
+       isl_map *map;
+       int equal;
+
+       str = "{ [i] -> [j] : i = 2j }";
+       map = isl_map_read_from_str(ctx, str);
+       map = isl_map_eliminate(map, isl_dim_out, 0, 1);
+       equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
+       isl_map_free(map);
+       if (equal < 0)
+               return -1;
+
+       return 0;
+}
+
+/* Check that isl_set_dim_residue_class detects that the values of j
+ * in the set below are all odd and that it does not detect any spurious
+ * strides.
+ */
+static int test_residue_class(isl_ctx *ctx)
+{
+       const char *str;
+       isl_set *set;
+       isl_int m, r;
+       int res;
+
+       str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
+               "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
+       set = isl_set_read_from_str(ctx, str);
+       isl_int_init(m);
+       isl_int_init(r);
+       res = isl_set_dim_residue_class(set, 1, &m, &r);
+       if (res >= 0 &&
+           (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
+               isl_die(ctx, isl_error_unknown, "incorrect residue class",
+                       res = -1);
+       isl_int_clear(r);
+       isl_int_clear(m);
+       isl_set_free(set);
+
+       return res;
+}
+
+int test_align_parameters(isl_ctx *ctx)
+{
+       const char *str;
+       isl_space *space;
+       isl_multi_aff *ma1, *ma2;
+       int equal;
+
+       str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
+       ma1 = isl_multi_aff_read_from_str(ctx, str);
+
+       space = isl_space_params_alloc(ctx, 1);
+       space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
+       ma1 = isl_multi_aff_align_params(ma1, space);
+
+       str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
+       ma2 = isl_multi_aff_read_from_str(ctx, str);
+
+       equal = isl_multi_aff_plain_is_equal(ma1, ma2);
+
+       isl_multi_aff_free(ma1);
+       isl_multi_aff_free(ma2);
+
+       if (equal < 0)
+               return -1;
+       if (!equal)
+               isl_die(ctx, isl_error_unknown,
+                       "result not as expected", return -1);
+
+       return 0;
+}
+
+static int test_list(isl_ctx *ctx)
+{
+       isl_id *a, *b, *c, *d, *id;
+       isl_id_list *list;
+       int ok;
+
+       a = isl_id_alloc(ctx, "a", NULL);
+       b = isl_id_alloc(ctx, "b", NULL);
+       c = isl_id_alloc(ctx, "c", NULL);
+       d = isl_id_alloc(ctx, "d", NULL);
+
+       list = isl_id_list_alloc(ctx, 4);
+       list = isl_id_list_add(list, a);
+       list = isl_id_list_add(list, b);
+       list = isl_id_list_add(list, c);
+       list = isl_id_list_add(list, d);
+       list = isl_id_list_drop(list, 1, 1);
+
+       if (isl_id_list_n_id(list) != 3) {
+               isl_id_list_free(list);
+               isl_die(ctx, isl_error_unknown,
+                       "unexpected number of elements in list", return -1);
+       }
+
+       id = isl_id_list_get_id(list, 0);
+       ok = id == a;
+       isl_id_free(id);
+       id = isl_id_list_get_id(list, 1);
+       ok = ok && id == c;
+       isl_id_free(id);
+       id = isl_id_list_get_id(list, 2);
+       ok = ok && id == d;
+       isl_id_free(id);
+
+       isl_id_list_free(list);
+
+       if (!ok)
+               isl_die(ctx, isl_error_unknown,
+                       "unexpected elements in list", return -1);
+
+       return 0;
+}
+
+struct {
+       const char *name;
+       int (*fn)(isl_ctx *ctx);
+} tests [] = {
+       { "list", &test_list },
+       { "align parameters", &test_align_parameters },
+       { "eliminate", &test_eliminate },
+       { "reisdue class", &test_residue_class },
+       { "div", &test_div },
+       { "slice", &test_slice },
+       { "fixed power", &test_fixed_power },
+       { "sample", &test_sample },
+       { "output", &test_output },
+       { "vertices", &test_vertices },
+       { "fixed", &test_fixed },
+       { "equal", &test_equal },
+       { "product", &test_product },
+       { "dim_max", &test_dim_max },
+       { "affine", &test_aff },
+       { "injective", &test_injective },
+       { "schedule", &test_schedule },
+       { "union_pw", &test_union_pw },
+       { "parse", &test_parse },
+       { "single-valued", &test_sv },
+       { "affine hull", &test_affine_hull },
+       { "coalesce", &test_coalesce },
+       { "factorize", &test_factorize },
+       { "subset", &test_subset },
+       { "subtract", &test_subtract },
+};
+
 int main()
 {
+       int i;
        struct isl_ctx *ctx;
 
        srcdir = getenv("srcdir");
        assert(srcdir);
 
        ctx = isl_ctx_alloc();
-       if (test_fixed_power(ctx) < 0)
-               goto error;
-       if (test_sample(ctx) < 0)
-               goto error;
-       if (test_output(ctx) < 0)
-               goto error;
-       if (test_vertices(ctx) < 0)
-               goto error;
-       if (test_fixed(ctx) < 0)
-               goto error;
-       if (test_equal(ctx) < 0)
-               goto error;
-       if (test_product(ctx) < 0)
-               goto error;
-       if (test_dim_max(ctx) < 0)
-               goto error;
-       if (test_aff(ctx) < 0)
-               goto error;
-       if (test_injective(ctx) < 0)
-               goto error;
-       if (test_schedule(ctx) < 0)
-               goto error;
-       if (test_union_pw(ctx) < 0)
-               goto error;
-       test_factorize(ctx);
-       test_subset(ctx);
+       for (i = 0; i < ARRAY_SIZE(tests); ++i) {
+               printf("%s\n", tests[i].name);
+               if (tests[i].fn(ctx) < 0)
+                       goto error;
+       }
        test_lift(ctx);
        test_bound(ctx);
        test_union(ctx);
        test_split_periods(ctx);
-       if (test_parse(ctx) < 0)
-               goto error;
        test_pwqp(ctx);
        test_lex(ctx);
-       if (test_sv(ctx) < 0)
-               goto error;
        test_bijective(ctx);
        test_dep(ctx);
        test_read(ctx);
        test_bounded(ctx);
        test_construction(ctx);
        test_dim(ctx);
-       test_div(ctx);
        test_application(ctx);
-       if (test_affine_hull(ctx) < 0)
-               goto error;
        test_convex_hull(ctx);
        test_gist(ctx);
-       if (test_coalesce(ctx) < 0)
-               goto error;
        test_closure(ctx);
        test_lexmin(ctx);
        isl_ctx_free(ctx);