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