add exported isl_multi_aff_involves_locals
[isl.git] / isl_test_cpp-checked.cc
blob1f74e7f9cb67a8a416b1c9635aaaa0722acd2963
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 #include <vector>
9 #include <string>
10 #include <limits.h>
11 #include <stdio.h>
12 #include <stdlib.h>
14 #include <isl/options.h>
15 #include <isl/cpp-checked.h>
17 namespace isl { using namespace checked; }
19 static void assert_impl(bool condition, const char *file, int line,
20 const char *message)
22 if (condition)
23 return;
25 fprintf(stderr, "Assertion failed in %s:%d %s\n", file, line, message);
26 exit(EXIT_FAILURE);
29 static void assert_impl(isl::boolean condition, const char *file, int line,
30 const char *message)
32 assert_impl(bool(condition), file, line, message);
35 /* Return the value encapsulated by "s".
37 static int size_val(isl::size s)
39 return s.is_error() ? -1 : unsigned(s);
42 #define assert(exp) assert_impl(exp, __FILE__, __LINE__, #exp)
43 #define IS_TRUE(b) (b).is_true()
44 #define SIZE_VAL(s) size_val(s)
46 #include "isl_test_cpp-generic.cc"
48 /* Test that isl_bool values are returned correctly.
50 * We check in detail the following parts of the isl::boolean class:
51 * - The is_true, is_false, and is_error functions return true in case they
52 * are called on a true, false, or error instance of isl::boolean,
53 * respectively
54 * - Explicit conversion to 'bool'
55 * - Implicit conversion to 'bool'
56 * - The complement operator
57 * - Explicit construction from 'true' and 'false'
58 * - Explicit construction form isl_bool
60 void test_return_bool(isl::ctx ctx)
62 isl::set empty(ctx, "{ : false }");
63 isl::set univ(ctx, "{ : }");
64 isl::set null;
66 isl::boolean b_true = empty.is_empty();
67 isl::boolean b_false = univ.is_empty();
68 isl::boolean b_error = null.is_empty();
70 assert(b_true.is_true());
71 assert(!b_true.is_false());
72 assert(!b_true.is_error());
74 assert(!b_false.is_true());
75 assert(b_false.is_false());
76 assert(!b_false.is_error());
78 assert(!b_error.is_true());
79 assert(!b_error.is_false());
80 assert(b_error.is_error());
82 assert(bool(b_true) == true);
83 assert(bool(b_false) == false);
85 assert(b_true);
87 assert((!b_false).is_true());
88 assert((!b_true).is_false());
89 assert((!b_error).is_error());
91 assert(isl::boolean(true).is_true());
92 assert(!isl::boolean(true).is_false());
93 assert(!isl::boolean(true).is_error());
95 assert(isl::boolean(false).is_false());
96 assert(!isl::boolean(false).is_true());
97 assert(!isl::boolean(false).is_error());
99 assert(isl::manage(isl_bool_true).is_true());
100 assert(!isl::manage(isl_bool_true).is_false());
101 assert(!isl::manage(isl_bool_true).is_error());
103 assert(isl::manage(isl_bool_false).is_false());
104 assert(!isl::manage(isl_bool_false).is_true());
105 assert(!isl::manage(isl_bool_false).is_error());
107 assert(isl::manage(isl_bool_error).is_error());
108 assert(!isl::manage(isl_bool_error).is_true());
109 assert(!isl::manage(isl_bool_error).is_false());
112 /* Test that return values are handled correctly.
114 * Test that isl C++ objects, integers, boolean values, and strings are
115 * returned correctly.
117 void test_return(isl::ctx ctx)
119 test_return_obj(ctx);
120 test_return_int(ctx);
121 test_return_bool(ctx);
122 test_return_string(ctx);
125 /* Test that foreach functions are modeled correctly.
127 * Verify that lambdas are correctly called as callback of a 'foreach'
128 * function and that variables captured by the lambda work correctly. Also
129 * check that the foreach function takes account of the return value of the
130 * lambda and aborts in case isl::stat::error is returned and then returns
131 * isl::stat::error itself.
133 void test_foreach(isl::ctx ctx)
135 isl::set s(ctx, "{ [0]; [1]; [2] }");
137 std::vector<isl::basic_set> basic_sets;
139 auto add_to_vector = [&] (isl::basic_set bs) {
140 basic_sets.push_back(bs);
141 return isl::stat::ok();
144 isl::stat ret1 = s.foreach_basic_set(add_to_vector);
146 assert(ret1.is_ok());
147 assert(basic_sets.size() == 3);
148 assert(isl::set(basic_sets[0]).is_subset(s).is_true());
149 assert(isl::set(basic_sets[1]).is_subset(s).is_true());
150 assert(isl::set(basic_sets[2]).is_subset(s).is_true());
151 assert(!basic_sets[0].is_equal(basic_sets[1]).is_true());
153 auto fail = [&] (isl::basic_set bs) {
154 return isl::stat::error();
157 isl::stat ret2 = s.foreach_basic_set(fail);
159 assert(ret2.is_error());
162 /* Test the functionality of "every" functions.
164 * In particular, test the generic functionality and
165 * test that error conditions are properly propagated.
167 static void test_every(isl::ctx ctx)
169 isl::union_set us(ctx, "{ A[i]; B[j] }");
171 test_every_generic(ctx);
173 auto fail = [] (isl::set s){
174 return isl::boolean::error();
176 assert(us.every_set(fail).is_error());
179 /* Test basic schedule tree functionality.
181 * In particular, create a simple schedule tree and
182 * - perform some generic tests
183 * - test map_descendant_bottom_up in the failing case
184 * - test foreach_descendant_top_down
185 * - test every_descendant
187 static void test_schedule_tree(isl::ctx ctx)
189 auto root = test_schedule_tree_generic(ctx);
191 auto fail_map = [](isl::schedule_node node) {
192 return isl::schedule_node();
194 assert(root.map_descendant_bottom_up(fail_map).is_null());
196 int count = 0;
197 auto inc_count = [&count](isl::schedule_node node) {
198 count++;
199 return isl::boolean(true);
201 assert(root.foreach_descendant_top_down(inc_count).is_ok());
202 assert(count == 8);
204 count = 0;
205 auto inc_count_once = [&count](isl::schedule_node node) {
206 count++;
207 return isl::boolean(false);
209 assert(root.foreach_descendant_top_down(inc_count_once).is_ok());
210 assert(count == 1);
212 auto is_not_domain = [](isl::schedule_node node) {
213 return !node.isa<isl::schedule_node_domain>();
215 assert(root.child(0).every_descendant(is_not_domain).is_true());
216 assert(root.every_descendant(is_not_domain).is_false());
218 auto fail = [](isl::schedule_node node) {
219 return isl::boolean();
221 assert(root.every_descendant(fail).is_error());
223 auto domain = root.as<isl::schedule_node_domain>().domain();
224 auto filters = isl::union_set(ctx, "{}");
225 auto collect_filters = [&filters](isl::schedule_node node) {
226 if (node.isa<isl::schedule_node_filter>().is_true()) {
227 auto filter = node.as<isl::schedule_node_filter>();
228 filters = filters.unite(filter.filter());
230 return isl::boolean(true);
232 assert(!root.every_descendant(collect_filters).is_error());
233 assert(domain.is_equal(filters).is_true());
236 /* Test basic AST generation from a schedule tree.
238 * In particular, create a simple schedule tree and
239 * - perform some generic tests
240 * - test at_each_domain in the failing case
242 static void test_ast_build(isl::ctx ctx)
244 auto schedule = test_ast_build_generic(ctx);
246 bool do_fail = true;
247 int count_ast_fail = 0;
248 auto fail_inc_count_ast =
249 [&count_ast_fail, &do_fail](isl::ast_node node,
250 isl::ast_build build) {
251 count_ast_fail++;
252 return do_fail ? isl::ast_node() : node;
254 auto build = isl::ast_build(ctx);
255 build = build.set_at_each_domain(fail_inc_count_ast);
256 auto ast = build.node_from(schedule);
257 assert(ast.is_null());
258 assert(count_ast_fail > 0);
259 auto build_copy = build;
260 int count_ast = 0;
261 auto inc_count_ast =
262 [&count_ast](isl::ast_node node, isl::ast_build build) {
263 count_ast++;
264 return node;
266 build_copy = build_copy.set_at_each_domain(inc_count_ast);
267 ast = build_copy.node_from(schedule);
268 assert(!ast.is_null());
269 assert(count_ast == 2);
270 count_ast_fail = 0;
271 do_fail = false;
272 ast = build.node_from(schedule);
273 assert(!ast.is_null());
274 assert(count_ast_fail == 2);
277 /* Test the isl checked C++ interface
279 * This includes:
280 * - The isl C <-> C++ pointer interface
281 * - Object construction
282 * - Different parameter types
283 * - Different return types
284 * - Foreach functions
285 * - Every functions
286 * - Spaces
287 * - Schedule trees
288 * - AST generation
289 * - AST expression generation
291 int main()
293 isl_ctx *ctx = isl_ctx_alloc();
295 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
297 test_pointer(ctx);
298 test_constructors(ctx);
299 test_parameters(ctx);
300 test_return(ctx);
301 test_foreach(ctx);
302 test_every(ctx);
303 test_space(ctx);
304 test_schedule_tree(ctx);
305 test_ast_build(ctx);
306 test_ast_build_expr(ctx);
308 isl_ctx_free(ctx);
310 return EXIT_SUCCESS;