2 // Automated Testing Framework (atf)
4 // Copyright (c) 2009 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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.
31 #include "../../atf-c/error.h"
37 #include "../macros.hpp"
39 #include "exceptions.hpp"
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
48 struct test_error_data {
51 typedef struct test_error_data test_error_data_t;
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);
67 test_error(const char* msg)
70 test_error_data_t data;
74 err = atf_error_new("test", &data, sizeof(data), test_format);
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 "
91 ATF_TEST_CASE_BODY(throw_atf_error_libc)
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") !=
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)
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 "
126 ATF_TEST_CASE_BODY(throw_atf_error_unknown)
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 // ------------------------------------------------------------------------
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);