1 | |
2 | #ifndef TEST_FRMWK_HPP___ |
3 | #define TEST_FRMWK_HPP___ |
4 | |
5 | /* Copyright (c) 2002,2003 CrystalClear Software, Inc. |
6 | * Use, modification and distribution is subject to the |
7 | * Boost Software License, Version 1.0. (See accompanying |
8 | * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) |
9 | * $Date$ |
10 | */ |
11 | |
12 | |
13 | #include <iostream> |
14 | #include <string> |
15 | #include <boost/config.hpp> |
16 | |
17 | //! Really simple test framework for counting and printing |
18 | class TestStats |
19 | { |
20 | public: |
21 | static TestStats& instance() {static TestStats ts; return ts;} |
22 | void addPassingTest() {testcount_++; passcount_++;} |
23 | void addFailingTest() {testcount_++;} |
24 | unsigned int testcount() const {return testcount_;} |
25 | unsigned int passcount() const {return passcount_;} |
26 | void print(std::ostream& out = std::cout) const |
27 | { |
28 | out << testcount_ << " Tests Executed: " ; |
29 | if (passcount() != testcount()) { |
30 | out << (testcount() - passcount()) << " FAILURES" ; |
31 | } |
32 | else { |
33 | out << "All Succeeded" << std::endl; |
34 | } |
35 | out << std::endl; |
36 | } |
37 | private: |
38 | TestStats() : testcount_(0), passcount_(0) {} |
39 | unsigned int testcount_; |
40 | unsigned int passcount_; |
41 | }; |
42 | |
43 | |
44 | inline bool check(const std::string& testname, bool testcond) |
45 | { |
46 | TestStats& stat = TestStats::instance(); |
47 | if (testcond) { |
48 | std::cout << "Pass :: " << testname << " " << std::endl; |
49 | stat.addPassingTest(); |
50 | return true; |
51 | } |
52 | else { |
53 | stat.addFailingTest(); |
54 | std::cout << "FAIL :: " << testname << " " << std::endl; |
55 | return false; |
56 | } |
57 | } |
58 | |
59 | // In the comparisons below, it is possible that T and U are signed and unsigned integer types, which generates warnings in some compilers. |
60 | #if defined(BOOST_MSVC) |
61 | # pragma warning(push) |
62 | # pragma warning(disable: 4389) |
63 | #elif defined(BOOST_CLANG) && defined(__has_warning) |
64 | # if __has_warning("-Wsign-compare") |
65 | # pragma clang diagnostic push |
66 | # pragma clang diagnostic ignored "-Wsign-compare" |
67 | # endif |
68 | #elif defined(BOOST_GCC) && (BOOST_GCC+0) >= 40600 |
69 | # pragma GCC diagnostic push |
70 | # pragma GCC diagnostic ignored "-Wsign-compare" |
71 | #endif |
72 | |
73 | template< typename T, typename U > |
74 | inline bool check_equal(const std::string& testname, T const& left, U const& right) |
75 | { |
76 | bool res = check(testname, left == right); |
77 | if (!res) |
78 | { |
79 | std::cout << " left = " << left << ", right = " << right << std::endl; |
80 | } |
81 | return res; |
82 | } |
83 | |
84 | #if defined(BOOST_MSVC) |
85 | # pragma warning(pop) |
86 | #elif defined(BOOST_CLANG) && defined(__has_warning) |
87 | # if __has_warning("-Wsign-compare") |
88 | # pragma clang diagnostic pop |
89 | # endif |
90 | #elif defined(BOOST_GCC) && (BOOST_GCC+0) >= 40600 |
91 | # pragma GCC diagnostic pop |
92 | #endif |
93 | |
94 | #ifndef BOOST_NO_STD_WSTRING |
95 | inline bool check_equal(const std::string& testname, std::wstring const& left, std::wstring const& right) |
96 | { |
97 | bool res = check(testname, testcond: left == right); |
98 | if (!res) |
99 | { |
100 | std::wcout << L" left = " << left << L", right = " << right << std::endl; |
101 | } |
102 | return res; |
103 | } |
104 | #endif |
105 | |
106 | inline int printTestStats() |
107 | { |
108 | TestStats& stat = TestStats::instance(); |
109 | stat.print(); |
110 | return stat.testcount() - stat.passcount(); |
111 | } |
112 | |
113 | #endif |
114 | |