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
17 #include <isl/options.h>
18 #include <isl/typed_cpp.h>
20 static void die_impl(const char *file
, int line
, const char *message
)
22 fprintf(stderr
, "Assertion failed in %s:%d %s\n", file
, line
, message
);
26 static void assert_impl(bool condition
, const char *file
, int line
,
32 return die_impl(file
, line
, message
);
35 #define die(msg) die_impl(__FILE__, __LINE__, msg)
37 #define assert(exp) assert_impl(exp, __FILE__, __LINE__, #exp)
39 #include "isl_test_cpp-generic.cc"
41 /* Test that isl_bool values are returned correctly.
43 * In particular, check the conversion to bool in case of true and false, and
44 * exception throwing in case of error.
46 static void test_return_bool(isl::ctx ctx
)
48 isl::set
empty(ctx
, "{ : false }");
49 isl::set
univ(ctx
, "{ : }");
52 bool b_true
= empty
.is_empty();
53 bool b_false
= univ
.is_empty();
57 die("no exception raised");
58 } catch (const isl::exception_invalid
&e
) {
67 /* Test that return values are handled correctly.
69 * Test that isl C++ objects, integers, boolean values, and strings are
72 static void test_return(isl::ctx ctx
)
76 test_return_bool(ctx
);
77 test_return_string(ctx
);
80 /* Test that foreach functions are modeled correctly.
82 * Verify that lambdas are correctly called as callback of a 'foreach'
83 * function and that variables captured by the lambda work correctly. Also
84 * check that the foreach function handles exceptions thrown from
85 * the lambda and that it propagates the exception.
87 static void test_foreach(isl::ctx ctx
)
89 isl::set
s(ctx
, "{ [0]; [1]; [2] }");
91 std::vector
<isl::basic_set
> basic_sets
;
93 auto add_to_vector
= [&] (isl::basic_set bs
) {
94 basic_sets
.push_back(bs
);
97 s
.foreach_basic_set(add_to_vector
);
99 assert(basic_sets
.size() == 3);
100 assert(isl::set(basic_sets
[0]).is_subset(s
));
101 assert(isl::set(basic_sets
[1]).is_subset(s
));
102 assert(isl::set(basic_sets
[2]).is_subset(s
));
103 assert(!basic_sets
[0].is_equal(basic_sets
[1]));
105 auto fail
= [&] (isl::basic_set bs
) {
111 s
.foreach_basic_set(fail
);
112 die("no exception raised");
113 } catch (char const *s
) {
119 /* Test the functionality of "foreach_scc" functions.
121 * In particular, test it on a list of elements that can be completely sorted
122 * but where two of the elements ("a" and "b") are incomparable.
124 static void test_foreach_scc(isl::ctx ctx
)
126 isl::multi_pw_aff id
;
127 isl::id_list
list(ctx
, 3);
128 isl::id_list
sorted(ctx
, 3);
129 std::map
<std::string
, isl::map
> data
= {
130 { "a", isl::map(ctx
, "{ [0] -> [1] }") },
131 { "b", isl::map(ctx
, "{ [1] -> [0] }") },
132 { "c", isl::map(ctx
, "{ [i = 0:1] -> [i] }") },
135 for (const auto &kvp
: data
)
136 list
= list
.add(kvp
.first
);
137 id
= data
.at("a").space().domain().identity_multi_pw_aff_on_domain();
138 list
.foreach_scc([&data
, &id
] (isl::id a
, isl::id b
) {
139 auto map
= data
.at(b
.name()).apply_domain(data
.at(a
.name()));
140 return !map
.lex_ge_at(id
).is_empty();
141 }, [&sorted
] (isl::id_list scc
) {
142 assert(scc
.size() == 1);
143 sorted
= sorted
.concat(scc
);
145 assert(sorted
.size() == 3);
146 assert(sorted
.at(0).name() == "b");
147 assert(sorted
.at(1).name() == "c");
148 assert(sorted
.at(2).name() == "a");
151 /* Test the functionality of "every" functions.
153 * In particular, test the generic functionality and
154 * test that exceptions are properly propagated.
156 static void test_every(isl::ctx ctx
)
158 isl::union_set
us(ctx
, "{ A[i]; B[j] }");
160 test_every_generic(ctx
);
162 auto fail
= [] (isl::set s
) -> bool {
168 die("no exception raised");
169 } catch (char const *s
) {
175 /* Test that an exception is generated for an isl error and
176 * that the error message is captured by the exception.
177 * Also check that the exception can be copied and that copying
178 * does not throw any exceptions.
180 static void test_exception(isl::ctx ctx
)
182 isl::multi_union_pw_aff
mupa(ctx
, "[]");
185 static_assert(std::is_nothrow_copy_constructible
<isl::exception
>::value
,
186 "exceptions must be nothrow-copy-constructible");
187 static_assert(std::is_nothrow_assignable
<isl::exception
,
188 isl::exception
>::value
,
189 "exceptions must be nothrow-assignable");
192 auto umap
= isl::union_map::from(mupa
);
193 } catch (const isl::exception_unsupported
&error
) {
194 die("caught wrong exception");
195 } catch (const isl::exception
&error
) {
196 assert(strstr(error
.what(), "without explicit domain"));
199 assert(strstr(copy
.what(), "without explicit domain"));
202 /* Test basic schedule tree functionality.
204 * In particular, create a simple schedule tree and
205 * - perform some generic tests
206 * - test map_descendant_bottom_up in the failing case
207 * - test foreach_descendant_top_down
208 * - test every_descendant
210 static void test_schedule_tree(isl::ctx ctx
)
212 auto root
= test_schedule_tree_generic(ctx
);
214 auto fail_map
= [](isl::schedule_node node
) {
220 root
.map_descendant_bottom_up(fail_map
);
221 die("no exception raised");
222 } catch (char const *s
) {
228 auto inc_count
= [&count
](isl::schedule_node node
) {
232 root
.foreach_descendant_top_down(inc_count
);
236 auto inc_count_once
= [&count
](isl::schedule_node node
) {
240 root
.foreach_descendant_top_down(inc_count_once
);
243 auto is_not_domain
= [](isl::schedule_node node
) {
244 return !node
.isa
<isl::schedule_node_domain
>();
246 assert(root
.child(0).every_descendant(is_not_domain
));
247 assert(!root
.every_descendant(is_not_domain
));
249 auto fail
= [](isl::schedule_node node
) {
255 root
.every_descendant(fail
);
256 die("no exception raised");
257 } catch (char const *s
) {
262 auto domain
= root
.as
<isl::schedule_node_domain
>().domain();
263 auto filters
= isl::union_set(ctx
, "{}");
264 auto collect_filters
= [&filters
](isl::schedule_node node
) {
265 if (node
.isa
<isl::schedule_node_filter
>()) {
266 auto filter
= node
.as
<isl::schedule_node_filter
>();
267 filters
= filters
.unite(filter
.filter());
271 root
.every_descendant(collect_filters
);
272 assert(domain
.is_equal(filters
));
275 /* Test basic AST generation from a schedule tree.
277 * In particular, create a simple schedule tree and
278 * - perform some generic tests
279 * - test at_each_domain in the failing case
281 static void test_ast_build(isl::ctx ctx
)
283 auto schedule
= test_ast_build_generic(ctx
);
286 int count_ast_fail
= 0;
287 auto fail_inc_count_ast
=
288 [&count_ast_fail
, &do_fail
](isl::ast_node node
,
289 isl::ast_build build
) {
295 auto build
= isl::ast_build(ctx
);
296 build
= build
.set_at_each_domain(fail_inc_count_ast
);
299 auto ast
= build
.node_from(schedule
);
300 } catch (char const *s
) {
304 assert(count_ast_fail
> 0);
305 auto build_copy
= build
;
308 [&count_ast
](isl::ast_node node
, isl::ast_build build
) {
312 build_copy
= build_copy
.set_at_each_domain(inc_count_ast
);
313 auto ast
= build_copy
.node_from(schedule
);
314 assert(count_ast
== 2);
317 ast
= build
.node_from(schedule
);
318 assert(count_ast_fail
== 2);
321 /* Basic test of the templated interface.
323 * Intersecting the domain of an access relation
324 * with statement instances should be allowed,
325 * while intersecting the range with statement instances
326 * should result in a compile-time error.
328 static void test_typed(isl::ctx ctx
)
332 isl::typed::map
<ST
, AR
> access(ctx
, "{ S[i, j] -> A[i] }");
333 isl::typed::set
<ST
> instances(ctx
, "{ S[i, j] : 0 <= i, j < 10 }");
335 #ifndef COMPILE_ERROR
336 access
.intersect_domain(instances
);
338 access
.intersect_range(instances
);
342 /* Test the (unchecked) isl C++ interface
345 * - The isl C <-> C++ pointer interface
346 * - Object construction
347 * - Different parameter types
348 * - Different return types
349 * - Foreach functions
350 * - Foreach SCC function
355 * - AST expression generation
356 * - Templated interface
360 isl_ctx
*ctx
= isl_ctx_alloc();
362 isl_options_set_on_error(ctx
, ISL_ON_ERROR_ABORT
);
365 test_constructors(ctx
);
366 test_parameters(ctx
);
369 test_foreach_scc(ctx
);
373 test_schedule_tree(ctx
);
375 test_ast_build_expr(ctx
);