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