isl_poly_is_cst: use isl_bool_ok
[isl.git] / isl_test_cpp.cc
blob6fccb8adc80cc2de6a1fffeff018416b295d6aca
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 the (unchecked) isl C++ interface
169 * This includes:
170 * - The isl C <-> C++ pointer interface
171 * - Object construction
172 * - Different parameter types
173 * - Different return types
174 * - Foreach functions
175 * - Exceptions
177 int main()
179 isl_ctx *ctx = isl_ctx_alloc();
181 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
183 test_pointer(ctx);
184 test_constructors(ctx);
185 test_parameters(ctx);
186 test_return(ctx);
187 test_foreach(ctx);
188 test_every(ctx);
189 test_exception(ctx);
191 isl_ctx_free(ctx);
193 return EXIT_SUCCESS;