isl_stream_read_multi_aff: extract out read_universe_params
[isl.git] / isl_test_cpp.cc
blob01308d171859ce18f59c7f26c6581892c0f8cbd4
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 <map>
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);
23 exit(EXIT_FAILURE);
26 static void assert_impl(bool condition, const char *file, int line,
27 const char *message)
29 if (condition)
30 return;
32 return die_impl(file, line, message);
35 #define die(msg) die_impl(__FILE__, __LINE__, msg)
36 #undef assert
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, "{ : }");
50 isl::set null;
52 bool b_true = empty.is_empty();
53 bool b_false = univ.is_empty();
54 bool caught = false;
55 try {
56 null.is_empty();
57 die("no exception raised");
58 } catch (const isl::exception_invalid &e) {
59 caught = true;
62 assert(b_true);
63 assert(!b_false);
64 assert(caught);
67 /* Test that return values are handled correctly.
69 * Test that isl C++ objects, integers, boolean values, and strings are
70 * returned correctly.
72 static void test_return(isl::ctx ctx)
74 test_return_obj(ctx);
75 test_return_int(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) {
106 throw "fail";
109 bool caught = false;
110 try {
111 s.foreach_basic_set(fail);
112 die("no exception raised");
113 } catch (char const *s) {
114 caught = true;
116 assert(caught);
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 {
163 throw "fail";
165 bool caught = false;
166 try {
167 us.every_set(fail);
168 die("no exception raised");
169 } catch (char const *s) {
170 caught = true;
172 assert(caught);
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, "[]");
183 isl::exception copy;
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");
191 try {
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"));
197 copy = error;
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) {
215 throw "fail";
216 return node;
218 auto caught = false;
219 try {
220 root.map_descendant_bottom_up(fail_map);
221 die("no exception raised");
222 } catch (char const *s) {
223 caught = true;
225 assert(caught);
227 int count = 0;
228 auto inc_count = [&count](isl::schedule_node node) {
229 count++;
230 return true;
232 root.foreach_descendant_top_down(inc_count);
233 assert(count == 8);
235 count = 0;
236 auto inc_count_once = [&count](isl::schedule_node node) {
237 count++;
238 return false;
240 root.foreach_descendant_top_down(inc_count_once);
241 assert(count == 1);
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) {
250 throw "fail";
251 return true;
253 caught = false;
254 try {
255 root.every_descendant(fail);
256 die("no exception raised");
257 } catch (char const *s) {
258 caught = true;
260 assert(caught);
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());
269 return true;
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);
285 bool do_fail = true;
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) {
290 count_ast_fail++;
291 if (do_fail)
292 throw "fail";
293 return node;
295 auto build = isl::ast_build(ctx);
296 build = build.set_at_each_domain(fail_inc_count_ast);
297 auto caught = false;
298 try {
299 auto ast = build.node_from(schedule);
300 } catch (char const *s) {
301 caught = true;
303 assert(caught);
304 assert(count_ast_fail > 0);
305 auto build_copy = build;
306 int count_ast = 0;
307 auto inc_count_ast =
308 [&count_ast](isl::ast_node node, isl::ast_build build) {
309 count_ast++;
310 return node;
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);
315 count_ast_fail = 0;
316 do_fail = false;
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)
330 struct ST {};
331 struct AR {};
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);
337 #else
338 access.intersect_range(instances);
339 #endif
342 /* Test the (unchecked) isl C++ interface
344 * This includes:
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
351 * - Exceptions
352 * - Spaces
353 * - Schedule trees
354 * - AST generation
355 * - AST expression generation
356 * - Templated interface
358 int main()
360 isl_ctx *ctx = isl_ctx_alloc();
362 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
364 test_pointer(ctx);
365 test_constructors(ctx);
366 test_parameters(ctx);
367 test_return(ctx);
368 test_foreach(ctx);
369 test_foreach_scc(ctx);
370 test_every(ctx);
371 test_exception(ctx);
372 test_space(ctx);
373 test_schedule_tree(ctx);
374 test_ast_build(ctx);
375 test_ast_build_expr(ctx);
376 test_typed(ctx);
378 isl_ctx_free(ctx);
380 return EXIT_SUCCESS;