fix callers of functions returning additional boolean through pointer
[isl.git] / isl_test_cpp-generic.cc
blob7356de366ae39568ca352f1e1429521754c6f22e
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
11 #ifndef SIZE_VAL
12 #define SIZE_VAL(s) (s)
13 #endif
15 /* Test the pointer interface for interaction between isl C and C++ types.
17 * This tests:
18 * - construction from an isl C object
19 * - check that constructed objects are non-null
20 * - get a non-owned C pointer from an isl C++ object usable in __isl_keep
21 * methods
22 * - use copy to get an owned C pointer from an isl C++ object which is usable
23 * in __isl_take methods. Verify that the original C++ object retains a valid
24 * pointer.
25 * - use release to get an owned C pointer from an isl C++ object which is
26 * usable in __isl_take methods. Verify that the original C++ object gave up
27 * its pointer and now is null.
29 void test_pointer(isl::ctx ctx)
31 isl_set *c_empty = isl_set_read_from_str(ctx.get(), "{ : false }");
32 isl::set empty = isl::manage(c_empty);
33 assert(IS_TRUE(empty.is_empty()));
34 assert(isl_set_is_empty(empty.get()));
36 assert(!empty.is_null());
37 isl_set_free(empty.copy());
38 assert(!empty.is_null());
39 isl_set_free(empty.release());
40 assert(empty.is_null());
43 /* Test that isl objects can be constructed.
45 * This tests:
46 * - construction of a null object
47 * - construction from a string
48 * - construction from an integer
49 * - static constructor without a parameter
50 * - conversion construction (implicit)
51 * - conversion construction (explicit)
53 * The tests to construct from integers and strings cover functionality that
54 * is also tested in the parameter type tests, but here we verify that
55 * multiple overloaded constructors are available and that overload resolution
56 * works as expected.
58 * Construction from an isl C pointer is tested in test_pointer.
60 void test_constructors(isl::ctx ctx)
62 isl::val null;
63 assert(null.is_null());
65 isl::val zero_from_str = isl::val(ctx, "0");
66 assert(IS_TRUE(zero_from_str.is_zero()));
68 isl::val zero_int_con = isl::val(ctx, 0);
69 assert(IS_TRUE(zero_int_con.is_zero()));
71 isl::val zero_static_con = isl::val::zero(ctx);
72 assert(IS_TRUE(zero_static_con.is_zero()));
74 isl::basic_set bs(ctx, "{ [1] }");
75 isl::set result(ctx, "{ [1] }");
76 isl::set s = bs;
77 assert(IS_TRUE(s.is_equal(result)));
78 isl::set s2(bs);
79 assert(IS_TRUE(s.unite(s2).is_equal(result)));
82 /* Test integer function parameters.
84 * Verify that extreme values and zero work.
86 void test_parameters_int(isl::ctx ctx)
88 isl::val long_max_str(ctx, std::to_string(LONG_MAX));
89 isl::val long_max_int(ctx, LONG_MAX);
90 assert(IS_TRUE(long_max_str.eq(long_max_int)));
92 isl::val long_min_str(ctx, std::to_string(LONG_MIN));
93 isl::val long_min_int(ctx, LONG_MIN);
94 assert(IS_TRUE(long_min_str.eq(long_min_int)));
96 isl::val long_zero_str = isl::val(ctx, std::to_string(0));
97 isl::val long_zero_int = isl::val(ctx, 0);
98 assert(IS_TRUE(long_zero_str.eq(long_zero_int)));
101 /* Test isl objects parameters.
103 * Verify that isl objects can be passed as lvalue and rvalue parameters.
104 * Also verify that isl object parameters are automatically type converted if
105 * there is an inheritance relation. Finally, test function calls without
106 * any additional parameters, apart from the isl object on which
107 * the method is called.
109 void test_parameters_obj(isl::ctx ctx)
111 isl::set a(ctx, "{ [0] }");
112 isl::set b(ctx, "{ [1] }");
113 isl::set c(ctx, "{ [2] }");
114 isl::set expected(ctx, "{ [i] : 0 <= i <= 2 }");
116 isl::set tmp = a.unite(b);
117 isl::set res_lvalue_param = tmp.unite(c);
118 assert(IS_TRUE(res_lvalue_param.is_equal(expected)));
120 isl::set res_rvalue_param = a.unite(b).unite(c);
121 assert(IS_TRUE(res_rvalue_param.is_equal(expected)));
123 isl::basic_set a2(ctx, "{ [0] }");
124 assert(IS_TRUE(a.is_equal(a2)));
126 isl::val two(ctx, 2);
127 isl::val half(ctx, "1/2");
128 isl::val res_only_this_param = two.inv();
129 assert(IS_TRUE(res_only_this_param.eq(half)));
132 /* Test different kinds of parameters to be passed to functions.
134 * This includes integer and isl C++ object parameters.
136 void test_parameters(isl::ctx ctx)
138 test_parameters_int(ctx);
139 test_parameters_obj(ctx);
142 /* Test that isl objects are returned correctly.
144 * This only tests that after combining two objects, the result is successfully
145 * returned.
147 void test_return_obj(isl::ctx ctx)
149 isl::val one(ctx, "1");
150 isl::val two(ctx, "2");
151 isl::val three(ctx, "3");
153 isl::val res = one.add(two);
155 assert(IS_TRUE(res.eq(three)));
158 /* Test that integer values are returned correctly.
160 void test_return_int(isl::ctx ctx)
162 isl::val one(ctx, "1");
163 isl::val neg_one(ctx, "-1");
164 isl::val zero(ctx, "0");
166 assert(one.sgn() > 0);
167 assert(neg_one.sgn() < 0);
168 assert(zero.sgn() == 0);
171 /* Test that strings are returned correctly.
172 * Do so by calling overloaded isl::ast_build::from_expr methods.
174 void test_return_string(isl::ctx ctx)
176 isl::set context(ctx, "[n] -> { : }");
177 isl::ast_build build = isl::ast_build::from_context(context);
178 isl::pw_aff pw_aff(ctx, "[n] -> { [n] }");
179 isl::set set(ctx, "[n] -> { : n >= 0 }");
181 isl::ast_expr expr = build.expr_from(pw_aff);
182 const char *expected_string = "n";
183 assert(expected_string == expr.to_C_str());
185 expr = build.expr_from(set);
186 expected_string = "n >= 0";
187 assert(expected_string == expr.to_C_str());
190 /* Test the functionality of "every" functions
191 * that does not depend on the type of C++ bindings.
193 static void test_every_generic(isl::ctx ctx)
195 isl::union_set us(ctx, "{ A[i]; B[j] }");
197 auto is_empty = [] (isl::set s) {
198 return s.is_empty();
200 assert(!IS_TRUE(us.every_set(is_empty)));
202 auto is_non_empty = [] (isl::set s) {
203 return !s.is_empty();
205 assert(IS_TRUE(us.every_set(is_non_empty)));
207 auto in_A = [] (isl::set s) {
208 return s.is_subset(isl::set(s.ctx(), "{ A[x] }"));
210 assert(!IS_TRUE(us.every_set(in_A)));
212 auto not_in_A = [] (isl::set s) {
213 return !s.is_subset(isl::set(s.ctx(), "{ A[x] }"));
215 assert(!IS_TRUE(us.every_set(not_in_A)));
218 /* Construct a simple schedule tree with an outer sequence node and
219 * a single-dimensional band node in each branch, with one of them
220 * marked coincident.
222 static isl::schedule construct_schedule_tree(isl::ctx ctx)
224 isl::union_set A(ctx, "{ A[i] : 0 <= i < 10 }");
225 isl::union_set B(ctx, "{ B[i] : 0 <= i < 20 }");
227 auto node = isl::schedule_node::from_domain(A.unite(B));
228 node = node.child(0);
230 isl::union_set_list filters(ctx, 0);
231 filters = filters.add(A).add(B);
232 node = node.insert_sequence(filters);
234 isl::multi_union_pw_aff f_A(ctx, "[ { A[i] -> [i] } ]");
235 node = node.child(0);
236 node = node.child(0);
237 node = node.insert_partial_schedule(f_A);
238 auto band = node.as<isl::schedule_node_band>();
239 band = band.member_set_coincident(0, true);
240 node = band.ancestor(2);
242 isl::multi_union_pw_aff f_B(ctx, "[ { B[i] -> [i] } ]");
243 node = node.child(1);
244 node = node.child(0);
245 node = node.insert_partial_schedule(f_B);
246 node = node.ancestor(2);
248 return node.schedule();
251 /* Test basic schedule tree functionality that is independent
252 * of the type of bindings.
254 * In particular, create a simple schedule tree and
255 * - check that the root node is a domain node
256 * - check that an object of a subclass can be used as one of the superclass
257 * - test map_descendant_bottom_up in the successful case
259 static isl::schedule_node test_schedule_tree_generic(isl::ctx ctx)
261 auto schedule = construct_schedule_tree(ctx);
262 auto root = schedule.root();
264 assert(IS_TRUE(root.isa<isl::schedule_node_domain>()));
265 root = root.as<isl::schedule_node_domain>().child(0).parent();
267 int count = 0;
268 auto inc_count = [&count](isl::schedule_node node) {
269 count++;
270 return node;
272 root = root.map_descendant_bottom_up(inc_count);
273 assert(count == 8);
275 return root;
278 /* Test marking band members for unrolling.
279 * "schedule" is the schedule created by construct_schedule_tree.
280 * It schedules two statements, with 10 and 20 instances, respectively.
281 * Unrolling all band members therefore results in 30 at-domain calls
282 * by the AST generator.
284 static void test_ast_build_unroll(isl::schedule schedule)
286 auto root = schedule.root();
287 auto mark_unroll = [](isl::schedule_node node) {
288 if (IS_TRUE(node.isa<isl::schedule_node_band>())) {
289 auto band = node.as<isl::schedule_node_band>();
290 node = band.member_set_ast_loop_unroll(0);
292 return node;
294 root = root.map_descendant_bottom_up(mark_unroll);
295 schedule = root.schedule();
297 int count_ast = 0;
298 auto inc_count_ast =
299 [&count_ast](isl::ast_node node, isl::ast_build build) {
300 count_ast++;
301 return node;
303 auto build = isl::ast_build(schedule.ctx());
304 build = build.set_at_each_domain(inc_count_ast);
305 auto ast = build.node_from(schedule);
306 assert(count_ast == 30);
309 /* Test basic AST generation from a schedule tree that is independent
310 * of the type of bindings.
312 * In particular, create a simple schedule tree and
313 * - generate an AST from the schedule tree
314 * - test at_each_domain in the successful case
315 * - test unrolling
317 static isl::schedule test_ast_build_generic(isl::ctx ctx)
319 auto schedule = construct_schedule_tree(ctx);
321 int count_ast = 0;
322 auto inc_count_ast =
323 [&count_ast](isl::ast_node node, isl::ast_build build) {
324 count_ast++;
325 return node;
327 auto build = isl::ast_build(ctx);
328 auto build_copy = build.set_at_each_domain(inc_count_ast);
329 auto ast = build.node_from(schedule);
330 assert(count_ast == 0);
331 count_ast = 0;
332 ast = build_copy.node_from(schedule);
333 assert(count_ast == 2);
334 build = build_copy;
335 count_ast = 0;
336 ast = build.node_from(schedule);
337 assert(count_ast == 2);
339 test_ast_build_unroll(schedule);
341 return schedule;
344 /* Test basic AST expression generation from an affine expression.
346 static void test_ast_build_expr(isl::ctx ctx)
348 isl::pw_aff pa(ctx, "[n] -> { [n + 1] }");
349 isl::ast_build build = isl::ast_build::from_context(pa.domain());
351 auto expr = build.expr_from(pa);
352 auto op = expr.as<isl::ast_expr_op>();
353 assert(IS_TRUE(op.isa<isl::ast_expr_op_add>()));
354 assert(SIZE_VAL(op.n_arg()) == 2);