1 // Copyright (c) 2009 The NetBSD Foundation, Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
7 // 1. Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // 2. Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
13 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "atf-c++/detail/exceptions.hpp"
29 #include "atf-c/error.h"
35 #include <atf-c++.hpp>
37 #include "atf-c++/detail/sanity.hpp"
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
45 struct test_error_data {
48 typedef struct test_error_data test_error_data_t;
52 test_format(const atf_error_t err, char *buf, size_t buflen)
54 const test_error_data_t* data;
56 PRE(atf_error_is(err, "test"));
58 data = static_cast< const test_error_data_t * >(atf_error_data(err));
59 snprintf(buf, buflen, "Message: %s", data->m_msg);
64 test_error(const char* msg)
67 test_error_data_t data;
71 err = atf_error_new("test", &data, sizeof(data), test_format);
78 // ------------------------------------------------------------------------
79 // Tests cases for the free functions.
80 // ------------------------------------------------------------------------
82 ATF_TEST_CASE(throw_atf_error_libc);
83 ATF_TEST_CASE_HEAD(throw_atf_error_libc)
85 set_md_var("descr", "Tests the throw_atf_error function when raising "
88 ATF_TEST_CASE_BODY(throw_atf_error_libc)
91 atf::throw_atf_error(atf_libc_error(1, "System error 1"));
92 } catch (const atf::system_error& e) {
93 ATF_REQUIRE(e.code() == 1);
94 ATF_REQUIRE(std::string(e.what()).find("System error 1") !=
96 } catch (const std::exception& e) {
97 ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
101 ATF_TEST_CASE(throw_atf_error_no_memory);
102 ATF_TEST_CASE_HEAD(throw_atf_error_no_memory)
104 set_md_var("descr", "Tests the throw_atf_error function when raising "
105 "a no_memory error");
107 ATF_TEST_CASE_BODY(throw_atf_error_no_memory)
110 atf::throw_atf_error(atf_no_memory_error());
111 } catch (const std::bad_alloc&) {
112 } catch (const std::exception& e) {
113 ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
117 ATF_TEST_CASE(throw_atf_error_unknown);
118 ATF_TEST_CASE_HEAD(throw_atf_error_unknown)
120 set_md_var("descr", "Tests the throw_atf_error function when raising "
123 ATF_TEST_CASE_BODY(throw_atf_error_unknown)
126 atf::throw_atf_error(test_error("The message"));
127 } catch (const std::runtime_error& e) {
128 const std::string msg = e.what();
129 ATF_REQUIRE(msg.find("The message") != std::string::npos);
130 } catch (const std::exception& e) {
131 ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
135 // ------------------------------------------------------------------------
137 // ------------------------------------------------------------------------
139 ATF_INIT_TEST_CASES(tcs)
141 // Add the test cases for the free functions.
142 ATF_ADD_TEST_CASE(tcs, throw_atf_error_libc);
143 ATF_ADD_TEST_CASE(tcs, throw_atf_error_no_memory);
144 ATF_ADD_TEST_CASE(tcs, throw_atf_error_unknown);