isl_output.c: print_nested_tuple: rename "local_dim" argument to "local_space"
[isl.git] / isl_test_cpp-generic.cc
blobc3ae55b1950c5ec347d361bd68e6b41f6d4456a7
1 /* Copyright 2016-2017 Tobias Grosser
3 * Use of this software is governed by the MIT license
5 * Written by Tobias Grosser, Weststrasse 47, CH-8003, Zurich
6 */
8 #ifndef IS_TRUE
9 #define IS_TRUE(b) (b)
10 #endif
12 /* Test the pointer interface for interaction between isl C and C++ types.
14 * This tests:
15 * - construction from an isl C object
16 * - check that constructed objects are non-null
17 * - get a non-owned C pointer from an isl C++ object usable in __isl_keep
18 * methods
19 * - use copy to get an owned C pointer from an isl C++ object which is usable
20 * in __isl_take methods. Verify that the original C++ object retains a valid
21 * pointer.
22 * - use release to get an owned C pointer from an isl C++ object which is
23 * usable in __isl_take methods. Verify that the original C++ object gave up
24 * its pointer and now is null.
26 void test_pointer(isl::ctx ctx)
28 isl_set *c_empty = isl_set_read_from_str(ctx.get(), "{ : false }");
29 isl::set empty = isl::manage(c_empty);
30 assert(IS_TRUE(empty.is_empty()));
31 assert(isl_set_is_empty(empty.get()));
33 assert(!empty.is_null());
34 isl_set_free(empty.copy());
35 assert(!empty.is_null());
36 isl_set_free(empty.release());
37 assert(empty.is_null());
40 /* Test that isl objects can be constructed.
42 * This tests:
43 * - construction of a null object
44 * - construction from a string
45 * - construction from an integer
46 * - static constructor without a parameter
47 * - conversion construction (implicit)
48 * - conversion construction (explicit)
50 * The tests to construct from integers and strings cover functionality that
51 * is also tested in the parameter type tests, but here we verify that
52 * multiple overloaded constructors are available and that overload resolution
53 * works as expected.
55 * Construction from an isl C pointer is tested in test_pointer.
57 void test_constructors(isl::ctx ctx)
59 isl::val null;
60 assert(null.is_null());
62 isl::val zero_from_str = isl::val(ctx, "0");
63 assert(IS_TRUE(zero_from_str.is_zero()));
65 isl::val zero_int_con = isl::val(ctx, 0);
66 assert(IS_TRUE(zero_int_con.is_zero()));
68 isl::val zero_static_con = isl::val::zero(ctx);
69 assert(IS_TRUE(zero_static_con.is_zero()));
71 isl::basic_set bs(ctx, "{ [1] }");
72 isl::set result(ctx, "{ [1] }");
73 isl::set s = bs;
74 assert(IS_TRUE(s.is_equal(result)));
75 isl::set s2(bs);
76 assert(IS_TRUE(s.unite(s2).is_equal(result)));
79 /* Test integer function parameters.
81 * Verify that extreme values and zero work.
83 void test_parameters_int(isl::ctx ctx)
85 isl::val long_max_str(ctx, std::to_string(LONG_MAX));
86 isl::val long_max_int(ctx, LONG_MAX);
87 assert(IS_TRUE(long_max_str.eq(long_max_int)));
89 isl::val long_min_str(ctx, std::to_string(LONG_MIN));
90 isl::val long_min_int(ctx, LONG_MIN);
91 assert(IS_TRUE(long_min_str.eq(long_min_int)));
93 isl::val long_zero_str = isl::val(ctx, std::to_string(0));
94 isl::val long_zero_int = isl::val(ctx, 0);
95 assert(IS_TRUE(long_zero_str.eq(long_zero_int)));
98 /* Test isl objects parameters.
100 * Verify that isl objects can be passed as lvalue and rvalue parameters.
101 * Also verify that isl object parameters are automatically type converted if
102 * there is an inheritance relation. Finally, test function calls without
103 * any additional parameters, apart from the isl object on which
104 * the method is called.
106 void test_parameters_obj(isl::ctx ctx)
108 isl::set a(ctx, "{ [0] }");
109 isl::set b(ctx, "{ [1] }");
110 isl::set c(ctx, "{ [2] }");
111 isl::set expected(ctx, "{ [i] : 0 <= i <= 2 }");
113 isl::set tmp = a.unite(b);
114 isl::set res_lvalue_param = tmp.unite(c);
115 assert(IS_TRUE(res_lvalue_param.is_equal(expected)));
117 isl::set res_rvalue_param = a.unite(b).unite(c);
118 assert(IS_TRUE(res_rvalue_param.is_equal(expected)));
120 isl::basic_set a2(ctx, "{ [0] }");
121 assert(IS_TRUE(a.is_equal(a2)));
123 isl::val two(ctx, 2);
124 isl::val half(ctx, "1/2");
125 isl::val res_only_this_param = two.inv();
126 assert(IS_TRUE(res_only_this_param.eq(half)));
129 /* Test different kinds of parameters to be passed to functions.
131 * This includes integer and isl C++ object parameters.
133 void test_parameters(isl::ctx ctx)
135 test_parameters_int(ctx);
136 test_parameters_obj(ctx);
139 /* Test that isl objects are returned correctly.
141 * This only tests that after combining two objects, the result is successfully
142 * returned.
144 void test_return_obj(isl::ctx ctx)
146 isl::val one(ctx, "1");
147 isl::val two(ctx, "2");
148 isl::val three(ctx, "3");
150 isl::val res = one.add(two);
152 assert(IS_TRUE(res.eq(three)));
155 /* Test that integer values are returned correctly.
157 void test_return_int(isl::ctx ctx)
159 isl::val one(ctx, "1");
160 isl::val neg_one(ctx, "-1");
161 isl::val zero(ctx, "0");
163 assert(one.sgn() > 0);
164 assert(neg_one.sgn() < 0);
165 assert(zero.sgn() == 0);
168 /* Test that strings are returned correctly.
169 * Do so by calling overloaded isl::ast_build::from_expr methods.
171 void test_return_string(isl::ctx ctx)
173 isl::set context(ctx, "[n] -> { : }");
174 isl::ast_build build = isl::ast_build::from_context(context);
175 isl::pw_aff pw_aff(ctx, "[n] -> { [n] }");
176 isl::set set(ctx, "[n] -> { : n >= 0 }");
178 isl::ast_expr expr = build.expr_from(pw_aff);
179 const char *expected_string = "n";
180 assert(expected_string == expr.to_C_str());
182 expr = build.expr_from(set);
183 expected_string = "n >= 0";
184 assert(expected_string == expr.to_C_str());