test isl_space_add_unnamed_tuple_ui export
[isl.git] / isl_test_cpp-generic.cc
blobb774f078d060fd22a5d9efc6178caeb68105f32b
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 /* Check basic construction of spaces.
220 static void test_space(isl::ctx ctx)
222 isl::space unit = isl::space::unit(ctx);
223 isl::space set_space = unit.add_named_tuple("A", 3);
224 isl::space map_space = set_space.add_named_tuple("B", 2);
226 isl::set set = isl::set::universe(set_space);
227 isl::map map = isl::map::universe(map_space);
228 assert(IS_TRUE(set.is_equal(isl::set(ctx, "{ A[*,*,*] }"))));
229 assert(IS_TRUE(map.is_equal(isl::map(ctx, "{ A[*,*,*] -> B[*,*] }"))));
232 /* Construct a simple schedule tree with an outer sequence node and
233 * a single-dimensional band node in each branch, with one of them
234 * marked coincident.
236 static isl::schedule construct_schedule_tree(isl::ctx ctx)
238 isl::union_set A(ctx, "{ A[i] : 0 <= i < 10 }");
239 isl::union_set B(ctx, "{ B[i] : 0 <= i < 20 }");
241 auto node = isl::schedule_node::from_domain(A.unite(B));
242 node = node.child(0);
244 isl::union_set_list filters(ctx, 0);
245 filters = filters.add(A).add(B);
246 node = node.insert_sequence(filters);
248 isl::multi_union_pw_aff f_A(ctx, "[ { A[i] -> [i] } ]");
249 node = node.child(0);
250 node = node.child(0);
251 node = node.insert_partial_schedule(f_A);
252 auto band = node.as<isl::schedule_node_band>();
253 band = band.member_set_coincident(0, true);
254 node = band.ancestor(2);
256 isl::multi_union_pw_aff f_B(ctx, "[ { B[i] -> [i] } ]");
257 node = node.child(1);
258 node = node.child(0);
259 node = node.insert_partial_schedule(f_B);
260 node = node.ancestor(2);
262 return node.schedule();
265 /* Test basic schedule tree functionality that is independent
266 * of the type of bindings.
268 * In particular, create a simple schedule tree and
269 * - check that the root node is a domain node
270 * - check that an object of a subclass can be used as one of the superclass
271 * - test map_descendant_bottom_up in the successful case
273 static isl::schedule_node test_schedule_tree_generic(isl::ctx ctx)
275 auto schedule = construct_schedule_tree(ctx);
276 auto root = schedule.root();
278 assert(IS_TRUE(root.isa<isl::schedule_node_domain>()));
279 root = root.as<isl::schedule_node_domain>().child(0).parent();
281 int count = 0;
282 auto inc_count = [&count](isl::schedule_node node) {
283 count++;
284 return node;
286 root = root.map_descendant_bottom_up(inc_count);
287 assert(count == 8);
289 return root;
292 /* Test marking band members for unrolling.
293 * "schedule" is the schedule created by construct_schedule_tree.
294 * It schedules two statements, with 10 and 20 instances, respectively.
295 * Unrolling all band members therefore results in 30 at-domain calls
296 * by the AST generator.
298 static void test_ast_build_unroll(isl::schedule schedule)
300 auto root = schedule.root();
301 auto mark_unroll = [](isl::schedule_node node) {
302 if (IS_TRUE(node.isa<isl::schedule_node_band>())) {
303 auto band = node.as<isl::schedule_node_band>();
304 node = band.member_set_ast_loop_unroll(0);
306 return node;
308 root = root.map_descendant_bottom_up(mark_unroll);
309 schedule = root.schedule();
311 int count_ast = 0;
312 auto inc_count_ast =
313 [&count_ast](isl::ast_node node, isl::ast_build build) {
314 count_ast++;
315 return node;
317 auto build = isl::ast_build(schedule.ctx());
318 build = build.set_at_each_domain(inc_count_ast);
319 auto ast = build.node_from(schedule);
320 assert(count_ast == 30);
323 /* Test basic AST generation from a schedule tree that is independent
324 * of the type of bindings.
326 * In particular, create a simple schedule tree and
327 * - generate an AST from the schedule tree
328 * - test at_each_domain in the successful case
329 * - test unrolling
331 static isl::schedule test_ast_build_generic(isl::ctx ctx)
333 auto schedule = construct_schedule_tree(ctx);
335 int count_ast = 0;
336 auto inc_count_ast =
337 [&count_ast](isl::ast_node node, isl::ast_build build) {
338 count_ast++;
339 return node;
341 auto build = isl::ast_build(ctx);
342 auto build_copy = build.set_at_each_domain(inc_count_ast);
343 auto ast = build.node_from(schedule);
344 assert(count_ast == 0);
345 count_ast = 0;
346 ast = build_copy.node_from(schedule);
347 assert(count_ast == 2);
348 build = build_copy;
349 count_ast = 0;
350 ast = build.node_from(schedule);
351 assert(count_ast == 2);
353 test_ast_build_unroll(schedule);
355 return schedule;
358 /* Test basic AST expression generation from an affine expression.
360 static void test_ast_build_expr(isl::ctx ctx)
362 isl::pw_aff pa(ctx, "[n] -> { [n + 1] }");
363 isl::ast_build build = isl::ast_build::from_context(pa.domain());
365 auto expr = build.expr_from(pa);
366 auto op = expr.as<isl::ast_expr_op>();
367 assert(IS_TRUE(op.isa<isl::ast_expr_op_add>()));
368 assert(SIZE_VAL(op.n_arg()) == 2);