interface/cpp.cc: store C++ callback function in a struct
[isl.git] / isl_test_cpp-checked.cc
blob848dd17f61e6e9ecd7a6a46ac2a4ba0abd074c37
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>
14 #include <isl/options.h>
15 #include <isl/cpp-checked.h>
17 static void assert_impl(bool condition, const char *file, int line,
18 const char *message)
20 if (condition)
21 return;
23 fprintf(stderr, "Assertion failed in %s:%d %s\n", file, line, message);
24 exit(EXIT_FAILURE);
27 static void assert_impl(isl::boolean condition, const char *file, int line,
28 const char *message)
30 assert_impl(bool(condition), file, line, message);
33 #define assert(exp) assert_impl(exp, __FILE__, __LINE__, #exp)
35 #include "isl_test_cpp-generic.cc"
37 /* Test that isl_bool values are returned correctly.
39 * We check in detail the following parts of the isl::boolean class:
40 * - The is_true, is_false, and is_error functions return true in case they
41 * are called on a true, false, or error instance of isl::boolean,
42 * respectively
43 * - Explicit conversion to 'bool'
44 * - Implicit conversion to 'bool'
45 * - The complement operator
46 * - Explicit construction from 'true' and 'false'
47 * - Explicit construction form isl_bool
49 void test_return_bool(isl::ctx ctx)
51 isl::set empty(ctx, "{ : false }");
52 isl::set univ(ctx, "{ : }");
53 isl::set null;
55 isl::boolean b_true = empty.is_empty();
56 isl::boolean b_false = univ.is_empty();
57 isl::boolean b_error = null.is_empty();
59 assert(b_true.is_true());
60 assert(!b_true.is_false());
61 assert(!b_true.is_error());
63 assert(!b_false.is_true());
64 assert(b_false.is_false());
65 assert(!b_false.is_error());
67 assert(!b_error.is_true());
68 assert(!b_error.is_false());
69 assert(b_error.is_error());
71 assert(bool(b_true) == true);
72 assert(bool(b_false) == false);
74 assert(b_true);
76 assert((!b_false).is_true());
77 assert((!b_true).is_false());
78 assert((!b_error).is_error());
80 assert(isl::boolean(true).is_true());
81 assert(!isl::boolean(true).is_false());
82 assert(!isl::boolean(true).is_error());
84 assert(isl::boolean(false).is_false());
85 assert(!isl::boolean(false).is_true());
86 assert(!isl::boolean(false).is_error());
88 assert(isl::manage(isl_bool_true).is_true());
89 assert(!isl::manage(isl_bool_true).is_false());
90 assert(!isl::manage(isl_bool_true).is_error());
92 assert(isl::manage(isl_bool_false).is_false());
93 assert(!isl::manage(isl_bool_false).is_true());
94 assert(!isl::manage(isl_bool_false).is_error());
96 assert(isl::manage(isl_bool_error).is_error());
97 assert(!isl::manage(isl_bool_error).is_true());
98 assert(!isl::manage(isl_bool_error).is_false());
101 /* Test that return values are handled correctly.
103 * Test that isl C++ objects, integers, boolean values, and strings are
104 * returned correctly.
106 void test_return(isl::ctx ctx)
108 test_return_obj(ctx);
109 test_return_int(ctx);
110 test_return_bool(ctx);
111 test_return_string(ctx);
114 /* Test that foreach functions are modeled correctly.
116 * Verify that lambdas are correctly called as callback of a 'foreach'
117 * function and that variables captured by the lambda work correctly. Also
118 * check that the foreach function takes account of the return value of the
119 * lambda and aborts in case isl::stat::error is returned and then returns
120 * isl::stat::error itself.
122 void test_foreach(isl::ctx ctx)
124 isl::set s(ctx, "{ [0]; [1]; [2] }");
126 std::vector<isl::basic_set> basic_sets;
128 auto add_to_vector = [&] (isl::basic_set bs) {
129 basic_sets.push_back(bs);
130 return isl::stat::ok;
133 isl::stat ret1 = s.foreach_basic_set(add_to_vector);
135 assert(ret1 == isl::stat::ok);
136 assert(basic_sets.size() == 3);
137 assert(isl::set(basic_sets[0]).is_subset(s));
138 assert(isl::set(basic_sets[1]).is_subset(s));
139 assert(isl::set(basic_sets[2]).is_subset(s));
140 assert(!basic_sets[0].is_equal(basic_sets[1]));
142 auto fail = [&] (isl::basic_set bs) {
143 return isl::stat::error;
146 isl::stat ret2 = s.foreach_basic_set(fail);
148 assert(ret2 == isl::stat::error);
151 /* Test the isl checked C++ interface
153 * This includes:
154 * - The isl C <-> C++ pointer interface
155 * - Object construction
156 * - Different parameter types
157 * - Different return types
158 * - Foreach functions
160 int main()
162 isl_ctx *ctx = isl_ctx_alloc();
164 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
166 test_pointer(ctx);
167 test_constructors(ctx);
168 test_parameters(ctx);
169 test_return(ctx);
170 test_foreach(ctx);
172 isl_ctx_free(ctx);