isl_ast_build_set_iterators: reuse isl_ast_build_dim
[isl.git] / isl_test_cpp.cc
blob929d69c4dd6aa5c5bab7ab9b47cac9ca20552c6f
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 that an exception is generated for an isl error and
117 * that the error message is captured by the exception.
118 * Also check that the exception can be copied and that copying
119 * does not throw any exceptions.
121 static void test_exception(isl::ctx ctx)
123 isl::multi_union_pw_aff mupa(ctx, "[]");
124 isl::exception copy;
126 static_assert(std::is_nothrow_copy_constructible<isl::exception>::value,
127 "exceptions must be nothrow-copy-constructible");
128 static_assert(std::is_nothrow_assignable<isl::exception,
129 isl::exception>::value,
130 "exceptions must be nothrow-assignable");
132 try {
133 auto umap = isl::union_map::from(mupa);
134 } catch (const isl::exception_unsupported &error) {
135 die("caught wrong exception");
136 } catch (const isl::exception &error) {
137 assert(strstr(error.what(), "without explicit domain"));
138 copy = error;
140 assert(strstr(copy.what(), "without explicit domain"));
143 /* Test the (unchecked) isl C++ interface
145 * This includes:
146 * - The isl C <-> C++ pointer interface
147 * - Object construction
148 * - Different parameter types
149 * - Different return types
150 * - Foreach functions
151 * - Exceptions
153 int main()
155 isl_ctx *ctx = isl_ctx_alloc();
157 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
159 test_pointer(ctx);
160 test_constructors(ctx);
161 test_parameters(ctx);
162 test_return(ctx);
163 test_foreach(ctx);
164 test_exception(ctx);
166 isl_ctx_free(ctx);
168 return EXIT_SUCCESS;