|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright (c) 2008-2010, Sony Pictures Imageworks Inc |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are |
| 7 | +// met: |
| 8 | +// |
| 9 | +// Redistributions of source code must retain the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer. |
| 11 | +// Redistributions in binary form must reproduce the above copyright |
| 12 | +// notice, this list of conditions and the following disclaimer in the |
| 13 | +// documentation and/or other materials provided with the distribution. |
| 14 | +// Neither the name of the organization Sony Pictures Imageworks nor the |
| 15 | +// names of its contributors |
| 16 | +// may be used to endorse or promote products derived from this software |
| 17 | +// without specific prior written permission. |
| 18 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | +// "AS |
| 20 | +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 21 | +// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 22 | +// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | +// OWNER |
| 24 | +// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | +// SPECIAL, |
| 26 | +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +/////////////////////////////////////////////////////////////////////////////// |
| 33 | + |
| 34 | +#ifndef INCLUDED_PYSTRING_UNITTEST_H |
| 35 | +#define INCLUDED_PYSTRING_UNITTEST_H |
| 36 | + |
| 37 | +#include <iostream> |
| 38 | +#include <cmath> |
| 39 | +#include <vector> |
| 40 | + |
| 41 | +extern int unit_test_failures; |
| 42 | + |
| 43 | +void unittest_fail(); |
| 44 | + |
| 45 | +typedef void (*PYSTRINGTestFunc)(); |
| 46 | + |
| 47 | +struct PYSTRINGTest |
| 48 | +{ |
| 49 | + PYSTRINGTest(std::string testgroup, std::string testname, PYSTRINGTestFunc test) : |
| 50 | + group(testgroup), name(testname), function(test) { }; |
| 51 | + std::string group, name; |
| 52 | + PYSTRINGTestFunc function; |
| 53 | +}; |
| 54 | + |
| 55 | +typedef std::vector<PYSTRINGTest*> UnitTests; |
| 56 | + |
| 57 | +UnitTests& GetUnitTests(); |
| 58 | + |
| 59 | +struct AddTest { AddTest(PYSTRINGTest* test); }; |
| 60 | + |
| 61 | +/// PYSTRING_CHECK_* macros checks if the conditions is met, and if not, |
| 62 | +/// prints an error message indicating the module and line where the |
| 63 | +/// error occurred, but does NOT abort. This is helpful for unit tests |
| 64 | +/// where we do not want one failure. |
| 65 | +#define PYSTRING_CHECK_ASSERT(x) \ |
| 66 | + ((x) ? ((void)0) \ |
| 67 | + : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \ |
| 68 | + << "FAILED: " << #x << "\n"), \ |
| 69 | + (void)++unit_test_failures)) |
| 70 | + |
| 71 | +#define PYSTRING_CHECK_EQUAL(x,y) \ |
| 72 | + (((x) == (y)) ? ((void)0) \ |
| 73 | + : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \ |
| 74 | + << "FAILED: " << #x << " == " << #y << "\n" \ |
| 75 | + << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \ |
| 76 | + (void)++unit_test_failures)) |
| 77 | + |
| 78 | +#define PYSTRING_CHECK_NE(x,y) \ |
| 79 | + (((x) != (y)) ? ((void)0) \ |
| 80 | + : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \ |
| 81 | + << "FAILED: " << #x << " != " << #y << "\n" \ |
| 82 | + << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \ |
| 83 | + (void)++unit_test_failures)) |
| 84 | + |
| 85 | +#define PYSTRING_CHECK_LT(x,y) \ |
| 86 | + (((x) < (y)) ? ((void)0) \ |
| 87 | + : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \ |
| 88 | + << "FAILED: " << #x << " < " << #y << "\n" \ |
| 89 | + << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \ |
| 90 | + (void)++unit_test_failures)) |
| 91 | + |
| 92 | +#define PYSTRING_CHECK_GT(x,y) \ |
| 93 | + (((x) > (y)) ? ((void)0) \ |
| 94 | + : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \ |
| 95 | + << "FAILED: " << #x << " > " << #y << "\n" \ |
| 96 | + << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \ |
| 97 | + (void)++unit_test_failures)) |
| 98 | + |
| 99 | +#define PYSTRING_CHECK_LE(x,y) \ |
| 100 | + (((x) <= (y)) ? ((void)0) \ |
| 101 | + : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \ |
| 102 | + << "FAILED: " << #x << " <= " << #y << "\n" \ |
| 103 | + << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \ |
| 104 | + (void)++unit_test_failures)) |
| 105 | + |
| 106 | +#define PYSTRING_CHECK_GE(x,y) \ |
| 107 | + (((x) >= (y)) ? ((void)0) \ |
| 108 | + : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \ |
| 109 | + << "FAILED: " << #x << " >= " << #y << "\n" \ |
| 110 | + << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \ |
| 111 | + (void)++unit_test_failures)) |
| 112 | + |
| 113 | +#define PYSTRING_CHECK_CLOSE(x,y,tol) \ |
| 114 | + ((std::abs((x) - (y)) < tol) ? ((void)0) \ |
| 115 | + : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \ |
| 116 | + << "FAILED: abs(" << #x << " - " << #y << ") < " << #tol << "\n" \ |
| 117 | + << "\tvalues were '" << (x) << "', '" << (y) << "' and '" << (tol) << "'\n"), \ |
| 118 | + (void)++unit_test_failures)) |
| 119 | + |
| 120 | +#define PYSTRING_CHECK_THOW(S, E) \ |
| 121 | + try { S; throw "throwanything"; } catch( E const& ex ) { } catch (...) { \ |
| 122 | + std::cout << __FILE__ << ":" << __LINE__ << ":\n" \ |
| 123 | + << "FAILED: " << #E << " is expected to be thrown\n"; \ |
| 124 | + ++unit_test_failures; } |
| 125 | + |
| 126 | +#define PYSTRING_CHECK_NO_THOW(S) \ |
| 127 | + try { S; } catch (...) { \ |
| 128 | + std::cout << __FILE__ << ":" << __LINE__ << ":\n" \ |
| 129 | + << "FAILED: exception thrown from " << #S <<"\n"; \ |
| 130 | + ++unit_test_failures; } |
| 131 | + |
| 132 | +#define PYSTRING_ADD_TEST(group, name) \ |
| 133 | + static void pystringtest_##group##_##name(); \ |
| 134 | + AddTest pystringaddtest_##group##_##name(new PYSTRINGTest(#group, #name, pystringtest_##group##_##name)); \ |
| 135 | + static void pystringtest_##group##_##name() |
| 136 | + |
| 137 | +#define PYSTRING_TEST_SETUP() \ |
| 138 | + int unit_test_failures = 0 |
| 139 | + |
| 140 | +#define PYSTRING_TEST_APP(app) \ |
| 141 | + std::vector<PYSTRINGTest*>& GetUnitTests() { \ |
| 142 | + static std::vector<PYSTRINGTest*> pystring_unit_tests; \ |
| 143 | + return pystring_unit_tests; } \ |
| 144 | + AddTest::AddTest(PYSTRINGTest* test){GetUnitTests().push_back(test);}; \ |
| 145 | + PYSTRING_TEST_SETUP(); \ |
| 146 | + int main(int, char **) { std::cerr << "\n" << #app <<"\n\n"; \ |
| 147 | + for(size_t i = 0; i < GetUnitTests().size(); ++i) { \ |
| 148 | + int _tmp = unit_test_failures; GetUnitTests()[i]->function(); \ |
| 149 | + std::cerr << "Test [" << GetUnitTests()[i]->group << "] [" << GetUnitTests()[i]->name << "] - "; \ |
| 150 | + std::cerr << (_tmp == unit_test_failures ? "PASSED" : "FAILED") << "\n"; } \ |
| 151 | + std::cerr << "\n" << unit_test_failures << " tests failed\n\n"; \ |
| 152 | + return unit_test_failures; } |
| 153 | + |
| 154 | +#endif |
0 commit comments