Home | History | Annotate | Line # | Download | only in atf-c++
      1 //
      2 // Automated Testing Framework (atf)
      3 //
      4 // Copyright (c) 2007 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.
     15 //
     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.
     28 //
     29 
     30 #if !defined(_ATF_CXX_MACROS_HPP_)
     31 #define _ATF_CXX_MACROS_HPP_
     32 
     33 #include <sstream>
     34 #include <stdexcept>
     35 #include <vector>
     36 
     37 #include <atf-c++/tests.hpp>
     38 
     39 // Do not define inline methods for the test case classes.  Doing so
     40 // significantly increases the memory requirements of GNU G++ during
     41 // compilation.
     42 
     43 #define ATF_TEST_CASE_WITHOUT_HEAD(name) \
     44     namespace { \
     45     class atfu_tc_ ## name : public atf::tests::tc { \
     46         void body(void) const; \
     47     public: \
     48         atfu_tc_ ## name(void); \
     49     }; \
     50     static atfu_tc_ ## name* atfu_tcptr_ ## name; \
     51     atfu_tc_ ## name::atfu_tc_ ## name(void) : atf::tests::tc(#name, false) {} \
     52     }
     53 
     54 #define ATF_TEST_CASE(name) \
     55     namespace { \
     56     class atfu_tc_ ## name : public atf::tests::tc { \
     57         void head(void); \
     58         void body(void) const; \
     59     public: \
     60         atfu_tc_ ## name(void); \
     61     }; \
     62     static atfu_tc_ ## name* atfu_tcptr_ ## name; \
     63     atfu_tc_ ## name::atfu_tc_ ## name(void) : atf::tests::tc(#name, false) {} \
     64     }
     65 
     66 #define ATF_TEST_CASE_WITH_CLEANUP(name) \
     67     namespace { \
     68     class atfu_tc_ ## name : public atf::tests::tc { \
     69         void head(void); \
     70         void body(void) const; \
     71         void cleanup(void) const; \
     72     public: \
     73         atfu_tc_ ## name(void); \
     74     }; \
     75     static atfu_tc_ ## name* atfu_tcptr_ ## name; \
     76     atfu_tc_ ## name::atfu_tc_ ## name(void) : atf::tests::tc(#name, true) {} \
     77     }
     78 
     79 #define ATF_TEST_CASE_NAME(name) atfu_tc_ ## name
     80 #define ATF_TEST_CASE_USE(name) (atfu_tcptr_ ## name) = NULL
     81 
     82 #define ATF_TEST_CASE_HEAD(name) \
     83     void \
     84     atfu_tc_ ## name::head(void)
     85 
     86 #define ATF_TEST_CASE_BODY(name) \
     87     void \
     88     atfu_tc_ ## name::body(void) \
     89         const
     90 
     91 #define ATF_TEST_CASE_CLEANUP(name) \
     92     void \
     93     atfu_tc_ ## name::cleanup(void) \
     94         const
     95 
     96 #define ATF_FAIL(reason) atf::tests::tc::fail(reason)
     97 
     98 #define ATF_SKIP(reason) atf::tests::tc::skip(reason)
     99 
    100 #define ATF_PASS() atf::tests::tc::pass()
    101 
    102 #define ATF_REQUIRE(x) \
    103     do { \
    104         if (!(x)) { \
    105             std::ostringstream atfu_ss; \
    106             atfu_ss << "Line " << __LINE__ << ": " << #x << " not met"; \
    107             atf::tests::tc::fail(atfu_ss.str()); \
    108         } \
    109     } while (false)
    110 
    111 #define ATF_REQUIRE_EQ(x, y) \
    112     do { \
    113         if ((x) != (y)) { \
    114             std::ostringstream atfu_ss; \
    115             atfu_ss << "Line " << __LINE__ << ": " << #x << " != " << #y \
    116                      << " (" << (x) << " != " << (y) << ")"; \
    117             atf::tests::tc::fail(atfu_ss.str()); \
    118         } \
    119     } while (false)
    120 
    121 #define ATF_REQUIRE_IN(element, collection) \
    122     ATF_REQUIRE((collection).find(element) != (collection).end())
    123 
    124 #define ATF_REQUIRE_NOT_IN(element, collection) \
    125     ATF_REQUIRE((collection).find(element) == (collection).end())
    126 
    127 #define ATF_REQUIRE_MATCH(regexp, string) \
    128     do { \
    129         if (!atf::tests::detail::match(regexp, string)) { \
    130             std::ostringstream atfu_ss; \
    131             atfu_ss << "Line " << __LINE__ << ": '" << string << "' does not " \
    132                     << "match regexp '" << regexp << "'"; \
    133             atf::tests::tc::fail(atfu_ss.str()); \
    134         } \
    135     } while (false)
    136 
    137 #define ATF_REQUIRE_THROW(e, x) \
    138     do { \
    139         try { \
    140             x; \
    141             std::ostringstream atfu_ss; \
    142             atfu_ss << "Line " << __LINE__ << ": " #x " did not throw " \
    143                         #e " as expected"; \
    144             atf::tests::tc::fail(atfu_ss.str()); \
    145         } catch (const e&) { \
    146         } catch (const std::exception& atfu_e) { \
    147             std::ostringstream atfu_ss; \
    148             atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
    149                         "unexpected error (not " #e "): " << atfu_e.what(); \
    150             atf::tests::tc::fail(atfu_ss.str()); \
    151         } catch (...) { \
    152             std::ostringstream atfu_ss; \
    153             atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
    154                         "unexpected error (not " #e ")"; \
    155             atf::tests::tc::fail(atfu_ss.str()); \
    156         } \
    157     } while (false)
    158 
    159 #define ATF_REQUIRE_THROW_RE(type, regexp, x) \
    160     do { \
    161         try { \
    162             x; \
    163             std::ostringstream atfu_ss; \
    164             atfu_ss << "Line " << __LINE__ << ": " #x " did not throw " \
    165                         #type " as expected"; \
    166             atf::tests::tc::fail(atfu_ss.str()); \
    167         } catch (const type& e) { \
    168             if (!atf::tests::detail::match(regexp, e.what())) { \
    169                 std::ostringstream atfu_ss; \
    170                 atfu_ss << "Line " << __LINE__ << ": " #x " threw " #type "(" \
    171                         << e.what() << "), but does not match '" << regexp \
    172                         << "'"; \
    173                 atf::tests::tc::fail(atfu_ss.str()); \
    174             } \
    175         } catch (const std::exception& atfu_e) { \
    176             std::ostringstream atfu_ss; \
    177             atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
    178                         "unexpected error (not " #type "): " << atfu_e.what(); \
    179             atf::tests::tc::fail(atfu_ss.str()); \
    180         } catch (...) { \
    181             std::ostringstream atfu_ss; \
    182             atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
    183                         "unexpected error (not " #type ")"; \
    184             atf::tests::tc::fail(atfu_ss.str()); \
    185         } \
    186     } while (false)
    187 
    188 #define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
    189     atf::tests::tc::check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, \
    190                                 bool_expr)
    191 
    192 #define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
    193     atf::tests::tc::require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, \
    194                                   bool_expr)
    195 
    196 #define ATF_INIT_TEST_CASES(tcs) \
    197     namespace atf { \
    198         namespace tests { \
    199             int run_tp(int, char* const*, \
    200                        void (*)(std::vector< atf::tests::tc * >&)); \
    201         } \
    202     } \
    203     \
    204     static void atfu_init_tcs(std::vector< atf::tests::tc * >&); \
    205     \
    206     int \
    207     main(int argc, char* const* argv) \
    208     { \
    209         return atf::tests::run_tp(argc, argv, atfu_init_tcs); \
    210     } \
    211     \
    212     static \
    213     void \
    214     atfu_init_tcs(std::vector< atf::tests::tc * >& tcs)
    215 
    216 #define ATF_ADD_TEST_CASE(tcs, tcname) \
    217     do { \
    218         atfu_tcptr_ ## tcname = new atfu_tc_ ## tcname(); \
    219         (tcs).push_back(atfu_tcptr_ ## tcname); \
    220     } while (0);
    221 
    222 #endif // !defined(_ATF_CXX_MACROS_HPP_)
    223