add ISL_CTX_{GET,SET}_STR_DEF macros
[platform/upstream/isl.git] / isl_test.c
index 05c8364..a70c3d9 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
@@ -196,6 +196,14 @@ 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;
+
+       if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
+                                     "[n] -> { [i] : i <= n }") < 0)
+               return -1;
+
        return 0;
 }
 
@@ -312,7 +320,9 @@ void test_dim(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;
@@ -617,6 +627,28 @@ static int test_div(isl_ctx *ctx)
        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;
 }
 
@@ -1982,20 +2014,89 @@ 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 },
+       { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
+       { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
+       { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
+       { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
+};
+
+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;
 }
 
 int test_factorize(isl_ctx *ctx)
@@ -2980,6 +3081,34 @@ int test_eliminate(isl_ctx *ctx)
        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;
@@ -3011,12 +3140,238 @@ int test_align_parameters(isl_ctx *ctx)
        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;
+}
+
+const char *set_conversion_tests[] = {
+       "[N] -> { [i] : N - 1 <= 2 i <= N }",
+       "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
+       "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
+       "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
+};
+
+/* Check that converting from isl_set to isl_pw_multi_aff and back
+ * to isl_set produces the original isl_set.
+ */
+static int test_set_conversion(isl_ctx *ctx)
+{
+       int i;
+       const char *str;
+       isl_set *set1, *set2;
+       isl_pw_multi_aff *pma;
+       int equal;
+
+       for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
+               str = set_conversion_tests[i];
+               set1 = isl_set_read_from_str(ctx, str);
+               pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
+               set2 = isl_set_from_pw_multi_aff(pma);
+               equal = isl_set_is_equal(set1, set2);
+               isl_set_free(set1);
+               isl_set_free(set2);
+
+               if (equal < 0)
+                       return -1;
+               if (!equal)
+                       isl_die(ctx, isl_error_unknown, "bad conversion",
+                               return -1);
+       }
+
+       return 0;
+}
+
+/* Check that converting from isl_map to isl_pw_multi_aff and back
+ * to isl_map produces the original isl_map.
+ */
+static int test_map_conversion(isl_ctx *ctx)
+{
+       const char *str;
+       isl_map *map1, *map2;
+       isl_pw_multi_aff *pma;
+       int equal;
+
+       str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
+               "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
+               "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
+               "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
+               "9e <= -2 - 2a) }";
+       map1 = isl_map_read_from_str(ctx, str);
+       pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
+       map2 = isl_map_from_pw_multi_aff(pma);
+       equal = isl_map_is_equal(map1, map2);
+       isl_map_free(map1);
+       isl_map_free(map2);
+
+       if (equal < 0)
+               return -1;
+       if (!equal)
+               isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
+
+       return 0;
+}
+
+static int test_conversion(isl_ctx *ctx)
+{
+       if (test_set_conversion(ctx) < 0)
+               return -1;
+       if (test_map_conversion(ctx) < 0)
+               return -1;
+       return 0;
+}
+
+struct {
+       const char *set;
+       const char *ma;
+       const char *res;
+} preimage_tests[] = {
+       { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
+         "{ A[j,i] -> B[i,j] }",
+         "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
+       { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
+         "{ A[a,b] -> B[a/2,b/6] }",
+         "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
+       { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
+         "{ A[a,b] -> B[a/2,b/6] }",
+         "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
+                   "exists i,j : a = 2 i and b = 6 j }" },
+       { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
+         "[n] -> { : 0 <= n <= 100 }" },
+       { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
+         "{ A[a] -> B[2a] }",
+         "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
+       { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
+         "{ A[a] -> B[([a/2])] }",
+         "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
+       { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
+         "{ A[a] -> B[a,a,a/3] }",
+         "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
+       { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
+         "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
+};
+
+int test_preimage(isl_ctx *ctx)
+{
+       int i;
+       isl_basic_set *bset1, *bset2;
+       isl_multi_aff *ma;
+       int equal;
+
+       for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
+               bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
+               ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
+               bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
+               bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
+               equal = isl_basic_set_is_equal(bset1, bset2);
+               isl_basic_set_free(bset1);
+               isl_basic_set_free(bset2);
+               if (equal < 0)
+                       return -1;
+               if (!equal)
+                       isl_die(ctx, isl_error_unknown, "bad preimage",
+                               return -1);
+       }
+
+       return 0;
+}
+
+struct {
+       const char *ma1;
+       const char *ma;
+       const char *res;
+} pullback_tests[] = {
+       { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
+         "{ A[a,b] -> C[b + 2a] }" },
+       { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
+       { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
+       { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
+         "{ A[a] -> C[(a)/6] }" },
+       { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
+       { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
+         "{ A[a] -> C[(2a)/3] }" },
+       { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
+       { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
+         "{ A[i,j] -> C[i + j, i + j] }"},
+       { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
+       { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
+         "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
+};
+
+static int test_pullback(isl_ctx *ctx)
+{
+       int i;
+       isl_multi_aff *ma1, *ma2;
+       isl_multi_aff *ma;
+       int equal;
+
+       for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
+               ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
+               ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
+               ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
+               ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
+               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, "bad pullback",
+                               return -1);
+       }
+
+       return 0;
+}
+
 struct {
        const char *name;
        int (*fn)(isl_ctx *ctx);
 } tests [] = {
+       { "conversion", &test_conversion },
+       { "list", &test_list },
        { "align parameters", &test_align_parameters },
+       { "preimage", &test_preimage },
+       { "pullback", &test_pullback },
        { "eliminate", &test_eliminate },
+       { "reisdue class", &test_residue_class },
        { "div", &test_div },
        { "slice", &test_slice },
        { "fixed power", &test_fixed_power },
@@ -3036,6 +3391,8 @@ struct {
        { "affine hull", &test_affine_hull },
        { "coalesce", &test_coalesce },
        { "factorize", &test_factorize },
+       { "subset", &test_subset },
+       { "subtract", &test_subtract },
 };
 
 int main()
@@ -3052,7 +3409,6 @@ int main()
                if (tests[i].fn(ctx) < 0)
                        goto error;
        }
-       test_subset(ctx);
        test_lift(ctx);
        test_bound(ctx);
        test_union(ctx);