Remove building with NOCRYPTO option
[minix3.git] / external / bsd / atf / dist / atf-c++ / detail / exceptions_test.cpp
blob821c192dd229fa40ba034ba42319b62dd663aeab
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2009 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 extern "C" {
31 #include "../../atf-c/error.h"
34 #include <cstdio>
35 #include <new>
37 #include "../macros.hpp"
39 #include "exceptions.hpp"
40 #include "sanity.hpp"
42 // ------------------------------------------------------------------------
43 // The "test" error.
44 // ------------------------------------------------------------------------
46 extern "C" {
48 struct test_error_data {
49 const char* m_msg;
51 typedef struct test_error_data test_error_data_t;
53 static
54 void
55 test_format(const atf_error_t err, char *buf, size_t buflen)
57 const test_error_data_t* data;
59 PRE(atf_error_is(err, "test"));
61 data = static_cast< const test_error_data_t * >(atf_error_data(err));
62 snprintf(buf, buflen, "Message: %s", data->m_msg);
65 static
66 atf_error_t
67 test_error(const char* msg)
69 atf_error_t err;
70 test_error_data_t data;
72 data.m_msg = msg;
74 err = atf_error_new("test", &data, sizeof(data), test_format);
76 return err;
79 } // extern
81 // ------------------------------------------------------------------------
82 // Tests cases for the free functions.
83 // ------------------------------------------------------------------------
85 ATF_TEST_CASE(throw_atf_error_libc);
86 ATF_TEST_CASE_HEAD(throw_atf_error_libc)
88 set_md_var("descr", "Tests the throw_atf_error function when raising "
89 "a libc error");
91 ATF_TEST_CASE_BODY(throw_atf_error_libc)
93 try {
94 atf::throw_atf_error(atf_libc_error(1, "System error 1"));
95 } catch (const atf::system_error& e) {
96 ATF_REQUIRE(e.code() == 1);
97 ATF_REQUIRE(std::string(e.what()).find("System error 1") !=
98 std::string::npos);
99 } catch (const std::exception& e) {
100 ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
104 ATF_TEST_CASE(throw_atf_error_no_memory);
105 ATF_TEST_CASE_HEAD(throw_atf_error_no_memory)
107 set_md_var("descr", "Tests the throw_atf_error function when raising "
108 "a no_memory error");
110 ATF_TEST_CASE_BODY(throw_atf_error_no_memory)
112 try {
113 atf::throw_atf_error(atf_no_memory_error());
114 } catch (const std::bad_alloc&) {
115 } catch (const std::exception& e) {
116 ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
120 ATF_TEST_CASE(throw_atf_error_unknown);
121 ATF_TEST_CASE_HEAD(throw_atf_error_unknown)
123 set_md_var("descr", "Tests the throw_atf_error function when raising "
124 "an unknown error");
126 ATF_TEST_CASE_BODY(throw_atf_error_unknown)
128 try {
129 atf::throw_atf_error(test_error("The message"));
130 } catch (const std::runtime_error& e) {
131 const std::string msg = e.what();
132 ATF_REQUIRE(msg.find("The message") != std::string::npos);
133 } catch (const std::exception& e) {
134 ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
138 // ------------------------------------------------------------------------
139 // Main.
140 // ------------------------------------------------------------------------
142 ATF_INIT_TEST_CASES(tcs)
144 // Add the test cases for the free functions.
145 ATF_ADD_TEST_CASE(tcs, throw_atf_error_libc);
146 ATF_ADD_TEST_CASE(tcs, throw_atf_error_no_memory);
147 ATF_ADD_TEST_CASE(tcs, throw_atf_error_unknown);