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
12 /* Test the pointer interface for interaction between isl C and C++ types.
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
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
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.
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
55 * Construction from an isl C pointer is tested in test_pointer.
57 void test_constructors(isl::ctx ctx
)
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] }");
74 assert(IS_TRUE(s
.is_equal(result
)));
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
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());
187 /* Test the functionality of "every" functions
188 * that does not depend on the type of C++ bindings.
190 static void test_every_generic(isl::ctx ctx
)
192 isl::union_set
us(ctx
, "{ A[i]; B[j] }");
194 auto is_empty
= [] (isl::set s
) {
197 assert(!IS_TRUE(us
.every_set(is_empty
)));
199 auto is_non_empty
= [] (isl::set s
) {
200 return !s
.is_empty();
202 assert(IS_TRUE(us
.every_set(is_non_empty
)));
204 auto in_A
= [] (isl::set s
) {
205 return s
.is_subset(isl::set(s
.get_ctx(), "{ A[x] }"));
207 assert(!IS_TRUE(us
.every_set(in_A
)));
209 auto not_in_A
= [] (isl::set s
) {
210 return !s
.is_subset(isl::set(s
.get_ctx(), "{ A[x] }"));
212 assert(!IS_TRUE(us
.every_set(not_in_A
)));
215 /* Construct a simple schedule tree with an outer sequence node and
216 * a single-dimensional band node in each branch, with one of them
219 static isl::schedule
construct_schedule_tree(isl::ctx ctx
)
221 isl::union_set
A(ctx
, "{ A[i] : 0 <= i < 10 }");
222 isl::union_set
B(ctx
, "{ B[i] : 0 <= i < 20 }");
224 auto node
= isl::schedule_node::from_domain(A
.unite(B
));
225 node
= node
.child(0);
227 isl::union_set_list
filters(ctx
, 0);
228 filters
= filters
.add(A
).add(B
);
229 node
= node
.insert_sequence(filters
);
231 isl::multi_union_pw_aff
f_A(ctx
, "[ { A[i] -> [i] } ]");
232 node
= node
.child(0);
233 node
= node
.child(0);
234 node
= node
.insert_partial_schedule(f_A
);
235 auto band
= node
.as
<isl::schedule_node_band
>();
236 band
= band
.member_set_coincident(0, true);
237 node
= band
.ancestor(2);
239 isl::multi_union_pw_aff
f_B(ctx
, "[ { B[i] -> [i] } ]");
240 node
= node
.child(1);
241 node
= node
.child(0);
242 node
= node
.insert_partial_schedule(f_B
);
243 node
= node
.ancestor(2);
245 return node
.get_schedule();
248 /* Test basic schedule tree functionality that is independent
249 * of the type of bindings.
251 * In particular, create a simple schedule tree and
252 * - check that the root node is a domain node
253 * - check that an object of a subclass can be used as one of the superclass
254 * - test map_descendant_bottom_up in the successful case
256 static isl::schedule_node
test_schedule_tree_generic(isl::ctx ctx
)
258 auto schedule
= construct_schedule_tree(ctx
);
259 auto root
= schedule
.get_root();
261 assert(IS_TRUE(root
.isa
<isl::schedule_node_domain
>()));
262 root
= root
.as
<isl::schedule_node_domain
>().child(0).parent();
265 auto inc_count
= [&count
](isl::schedule_node node
) {
269 root
= root
.map_descendant_bottom_up(inc_count
);
275 /* Test basic AST generation from a schedule tree that is independent
276 * of the type of bindings.
278 * In particular, create a simple schedule tree and
279 * - generate an AST from the schedule tree
280 * - test at_each_domain in the successful case
282 static isl::schedule
test_ast_build_generic(isl::ctx ctx
)
284 auto schedule
= construct_schedule_tree(ctx
);
288 [&count_ast
](isl::ast_node node
, isl::ast_build build
) {
292 auto build
= isl::ast_build(ctx
);
293 auto build_copy
= build
.set_at_each_domain(inc_count_ast
);
294 auto ast
= build
.node_from(schedule
);
295 assert(count_ast
== 0);
297 ast
= build_copy
.node_from(schedule
);
298 assert(count_ast
== 2);
301 ast
= build
.node_from(schedule
);
302 assert(count_ast
== 2);