xref: /netbsd-src/external/mit/isl/dist/isl_test_cpp17.cc (revision 5971e316fdea024efff6be8f03536623db06833e)
1 #include <stdlib.h>
2 
3 #include <exception>
4 #include <sstream>
5 
6 #include <isl/options.h>
7 #include <isl/cpp.h>
8 
9 /* Throw a runtime exception.
10  */
die_impl(const char * file,int line,const char * message)11 static void die_impl(const char *file, int line, const char *message)
12 {
13 	std::ostringstream ss;
14 	ss << file << ":" << line << ": " << message;
15 	throw std::runtime_error(ss.str());
16 }
17 
18 #define die(msg) die_impl(__FILE__, __LINE__, msg)
19 
20 #include "isl_test_cpp17-generic.cc"
21 
22 /* Check that an isl::exception_invalid gets thrown by "fn".
23  */
check_invalid(const std::function<void (void)> & fn)24 static void check_invalid(const std::function<void(void)> &fn)
25 {
26 	bool caught = false;
27 	try {
28 		fn();
29 	} catch (const isl::exception_invalid &e) {
30 		caught = true;
31 	}
32 	if (!caught)
33 		die("no invalid exception was generated");
34 }
35 
36 /* Test id::user.
37  *
38  * In particular, check that the object attached to an identifier
39  * can be retrieved again and that retrieving an object of the wrong type
40  * or retrieving an object when no object was attached results in an exception.
41  */
test_user(isl::ctx ctx)42 static void test_user(isl::ctx ctx)
43 {
44 	isl::id id(ctx, "test", 5);
45 	isl::id id2(ctx, "test2");
46 	isl::id id3(ctx, "test3", std::string("s"));
47 
48 	auto int_user = id.user<int>();
49 	if (int_user != 5)
50 		die("wrong integer retrieved from isl::id");
51 	auto s_user = id3.user<std::string>();
52 	if (s_user != "s")
53 		die("wrong string retrieved from isl::id");
54 	check_invalid([&id] () { id.user<std::string>(); });
55 	check_invalid([&id2] () { id2.user<int>(); });
56 	check_invalid([&id2] () { id2.user<std::string>(); });
57 	check_invalid([&id3] () { id3.user<int>(); });
58 }
59 
60 /* Test the C++17 specific features of the (unchecked) isl C++ interface
61  *
62  * In particular, test
63  *  - id::try_user
64  *  - id::user
65  */
main()66 int main()
67 {
68 	isl_ctx *ctx = isl_ctx_alloc();
69 
70 	isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
71 
72 	test_try_user(ctx);
73 	test_user(ctx);
74 
75 	isl_ctx_free(ctx);
76 
77 	return EXIT_SUCCESS;
78 }
79