add isl_pw_*_intersect_domain_wrapped_{domain,range}
[isl.git] / isl_test_cpp.cc
blob2183ad4328f1f20938a67f5a2d58d981b2b2aeeb
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>
13 #include <string.h>
15 #include <isl/options.h>
16 #include <isl/cpp.h>
18 static void die_impl(const char *file, int line, const char *message)
20 fprintf(stderr, "Assertion failed in %s:%d %s\n", file, line, message);
21 exit(EXIT_FAILURE);
24 static void assert_impl(bool condition, const char *file, int line,
25 const char *message)
27 if (condition)
28 return;
30 return die_impl(file, line, message);
33 #define die(msg) die_impl(__FILE__, __LINE__, msg)
34 #define assert(exp) assert_impl(exp, __FILE__, __LINE__, #exp)
36 #include "isl_test_cpp-generic.cc"
38 /* Test that isl_bool values are returned correctly.
40 * In particular, check the conversion to bool in case of true and false, and
41 * exception throwing in case of error.
43 static void test_return_bool(isl::ctx ctx)
45 isl::set empty(ctx, "{ : false }");
46 isl::set univ(ctx, "{ : }");
47 isl::set null;
49 bool b_true = empty.is_empty();
50 bool b_false = univ.is_empty();
51 bool caught = false;
52 try {
53 null.is_empty();
54 die("no exception raised");
55 } catch (const isl::exception_invalid &e) {
56 caught = true;
59 assert(b_true);
60 assert(!b_false);
61 assert(caught);
64 /* Test that return values are handled correctly.
66 * Test that isl C++ objects, integers, boolean values, and strings are
67 * returned correctly.
69 static void test_return(isl::ctx ctx)
71 test_return_obj(ctx);
72 test_return_int(ctx);
73 test_return_bool(ctx);
74 test_return_string(ctx);
77 /* Test that foreach functions are modeled correctly.
79 * Verify that lambdas are correctly called as callback of a 'foreach'
80 * function and that variables captured by the lambda work correctly. Also
81 * check that the foreach function handles exceptions thrown from
82 * the lambda and that it propagates the exception.
84 static void test_foreach(isl::ctx ctx)
86 isl::set s(ctx, "{ [0]; [1]; [2] }");
88 std::vector<isl::basic_set> basic_sets;
90 auto add_to_vector = [&] (isl::basic_set bs) {
91 basic_sets.push_back(bs);
94 s.foreach_basic_set(add_to_vector);
96 assert(basic_sets.size() == 3);
97 assert(isl::set(basic_sets[0]).is_subset(s));
98 assert(isl::set(basic_sets[1]).is_subset(s));
99 assert(isl::set(basic_sets[2]).is_subset(s));
100 assert(!basic_sets[0].is_equal(basic_sets[1]));
102 auto fail = [&] (isl::basic_set bs) {
103 throw "fail";
106 bool caught = false;
107 try {
108 s.foreach_basic_set(fail);
109 die("no exception raised");
110 } catch (char const *s) {
111 caught = true;
113 assert(caught);
116 /* Test the functionality of "every" functions.
118 * In particular, test the generic functionality and
119 * test that exceptions are properly propagated.
121 static void test_every(isl::ctx ctx)
123 isl::union_set us(ctx, "{ A[i]; B[j] }");
125 test_every_generic(ctx);
127 auto fail = [] (isl::set s) -> bool {
128 throw "fail";
130 bool caught = false;
131 try {
132 us.every_set(fail);
133 die("no exception raised");
134 } catch (char const *s) {
135 caught = true;
137 assert(caught);
140 /* Test that an exception is generated for an isl error and
141 * that the error message is captured by the exception.
142 * Also check that the exception can be copied and that copying
143 * does not throw any exceptions.
145 static void test_exception(isl::ctx ctx)
147 isl::multi_union_pw_aff mupa(ctx, "[]");
148 isl::exception copy;
150 static_assert(std::is_nothrow_copy_constructible<isl::exception>::value,
151 "exceptions must be nothrow-copy-constructible");
152 static_assert(std::is_nothrow_assignable<isl::exception,
153 isl::exception>::value,
154 "exceptions must be nothrow-assignable");
156 try {
157 auto umap = isl::union_map::from(mupa);
158 } catch (const isl::exception_unsupported &error) {
159 die("caught wrong exception");
160 } catch (const isl::exception &error) {
161 assert(strstr(error.what(), "without explicit domain"));
162 copy = error;
164 assert(strstr(copy.what(), "without explicit domain"));
167 /* Test basic schedule tree functionality.
169 * In particular, create a simple schedule tree and
170 * - perform some generic tests
171 * - test map_descendant_bottom_up in the failing case
172 * - test foreach_descendant_top_down
173 * - test every_descendant
175 static void test_schedule_tree(isl::ctx ctx)
177 auto root = test_schedule_tree_generic(ctx);
179 auto fail_map = [](isl::schedule_node node) {
180 throw "fail";
181 return node;
183 auto caught = false;
184 try {
185 root.map_descendant_bottom_up(fail_map);
186 die("no exception raised");
187 } catch (char const *s) {
188 caught = true;
190 assert(caught);
192 int count = 0;
193 auto inc_count = [&count](isl::schedule_node node) {
194 count++;
195 return true;
197 root.foreach_descendant_top_down(inc_count);
198 assert(count == 8);
200 count = 0;
201 auto inc_count_once = [&count](isl::schedule_node node) {
202 count++;
203 return false;
205 root.foreach_descendant_top_down(inc_count_once);
206 assert(count == 1);
208 auto is_not_domain = [](isl::schedule_node node) {
209 return !node.isa<isl::schedule_node_domain>();
211 assert(root.child(0).every_descendant(is_not_domain));
212 assert(!root.every_descendant(is_not_domain));
214 auto fail = [](isl::schedule_node node) {
215 throw "fail";
216 return true;
218 caught = false;
219 try {
220 root.every_descendant(fail);
221 die("no exception raised");
222 } catch (char const *s) {
223 caught = true;
225 assert(caught);
227 auto domain = root.as<isl::schedule_node_domain>().domain();
228 auto filters = isl::union_set(ctx, "{}");
229 auto collect_filters = [&filters](isl::schedule_node node) {
230 if (node.isa<isl::schedule_node_filter>()) {
231 auto filter = node.as<isl::schedule_node_filter>();
232 filters = filters.unite(filter.filter());
234 return true;
236 root.every_descendant(collect_filters);
237 assert(domain.is_equal(filters));
240 /* Test basic AST generation from a schedule tree.
242 * In particular, create a simple schedule tree and
243 * - perform some generic tests
244 * - test at_each_domain in the failing case
246 static void test_ast_build(isl::ctx ctx)
248 auto schedule = test_ast_build_generic(ctx);
250 bool do_fail = true;
251 int count_ast_fail = 0;
252 auto fail_inc_count_ast =
253 [&count_ast_fail, &do_fail](isl::ast_node node,
254 isl::ast_build build) {
255 count_ast_fail++;
256 if (do_fail)
257 throw "fail";
258 return node;
260 auto build = isl::ast_build(ctx);
261 build = build.set_at_each_domain(fail_inc_count_ast);
262 auto caught = false;
263 try {
264 auto ast = build.node_from(schedule);
265 } catch (char const *s) {
266 caught = true;
268 assert(caught);
269 assert(count_ast_fail > 0);
270 auto build_copy = build;
271 int count_ast = 0;
272 auto inc_count_ast =
273 [&count_ast](isl::ast_node node, isl::ast_build build) {
274 count_ast++;
275 return node;
277 build_copy = build_copy.set_at_each_domain(inc_count_ast);
278 auto ast = build_copy.node_from(schedule);
279 assert(count_ast == 2);
280 count_ast_fail = 0;
281 do_fail = false;
282 ast = build.node_from(schedule);
283 assert(count_ast_fail == 2);
286 /* Test the (unchecked) isl C++ interface
288 * This includes:
289 * - The isl C <-> C++ pointer interface
290 * - Object construction
291 * - Different parameter types
292 * - Different return types
293 * - Foreach functions
294 * - Exceptions
295 * - Spaces
296 * - Schedule trees
297 * - AST generation
298 * - AST expression generation
300 int main()
302 isl_ctx *ctx = isl_ctx_alloc();
304 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
306 test_pointer(ctx);
307 test_constructors(ctx);
308 test_parameters(ctx);
309 test_return(ctx);
310 test_foreach(ctx);
311 test_every(ctx);
312 test_exception(ctx);
313 test_space(ctx);
314 test_schedule_tree(ctx);
315 test_ast_build(ctx);
316 test_ast_build_expr(ctx);
318 isl_ctx_free(ctx);
320 return EXIT_SUCCESS;