PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/ext/oiio/src/include/unittest.h

http://github.com/imageworks/OpenColorIO
C++ Header | 151 lines | 95 code | 23 blank | 33 comment | 4 complexity | 972f0350e8e428aa1ddfd09cd7f02f64 MD5 | raw file
  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
 38extern int unit_test_failures;
 39
 40void unittest_fail();
 41
 42typedef void (*OIIOTestFunc)();
 43
 44struct OIIOTest
 45{
 46    OIIOTest(std::string testgroup, std::string testname, OIIOTestFunc test) :
 47        group(testgroup), name(testname), function(test) { };
 48    std::string group, name;
 49    OIIOTestFunc function;
 50};
 51
 52typedef std::vector<OIIOTest*> UnitTests;
 53
 54UnitTests& GetUnitTests();
 55
 56struct AddTest { AddTest(OIIOTest* test); };
 57
 58/// OIIO_CHECK_* macros checks if the conditions is met, and if not,
 59/// prints an error message indicating the module and line where the
 60/// error occurred, but does NOT abort.  This is helpful for unit tests
 61/// where we do not want one failure.
 62#define OIIO_CHECK_ASSERT(x)                                            \
 63    ((x) ? ((void)0)                                                    \
 64         : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n"          \
 65                       << "FAILED: " << #x << "\n"),                    \
 66            (void)++unit_test_failures))
 67
 68#define OIIO_CHECK_EQUAL(x,y)                                           \
 69    (((x) == (y)) ? ((void)0)                                           \
 70         : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n"          \
 71             << "FAILED: " << #x << " == " << #y << "\n"                \
 72             << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
 73            (void)++unit_test_failures))
 74
 75#define OIIO_CHECK_NE(x,y)                                              \
 76    (((x) != (y)) ? ((void)0)                                           \
 77         : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n"          \
 78             << "FAILED: " << #x << " != " << #y << "\n"                \
 79             << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
 80            (void)++unit_test_failures))
 81
 82#define OIIO_CHECK_LT(x,y)                                              \
 83    (((x) < (y)) ? ((void)0)                                            \
 84         : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n"          \
 85             << "FAILED: " << #x << " < " << #y << "\n"                 \
 86             << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
 87            (void)++unit_test_failures))
 88
 89#define OIIO_CHECK_GT(x,y)                                              \
 90    (((x) > (y)) ? ((void)0)                                            \
 91         : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n"          \
 92             << "FAILED: " << #x << " > " << #y << "\n"                 \
 93             << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
 94            (void)++unit_test_failures))
 95
 96#define OIIO_CHECK_LE(x,y)                                              \
 97    (((x) <= (y)) ? ((void)0)                                           \
 98         : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n"          \
 99             << "FAILED: " << #x << " <= " << #y << "\n"                \
100             << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
101            (void)++unit_test_failures))
102
103#define OIIO_CHECK_GE(x,y)                                              \
104    (((x) >= (y)) ? ((void)0)                                           \
105         : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n"          \
106             << "FAILED: " << #x << " >= " << #y << "\n"                \
107             << "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
108            (void)++unit_test_failures))
109
110#define OIIO_CHECK_CLOSE(x,y,tol)                                       \
111    ((std::abs((x) - (y)) < tol) ? ((void)0)                            \
112         : ((std::cout << __FILE__ << ":" << __LINE__ << ":\n"          \
113             << "FAILED: abs(" << #x << " - " << #y << ") < " << #tol << "\n" \
114             << "\tvalues were '" << (x) << "', '" << (y) << "' and '" << (tol) << "'\n"), \
115            (void)++unit_test_failures))
116
117#define OIIO_CHECK_THOW(S, E)                                           \
118    try { S; throw "throwanything"; } catch( E const& ex ) { } catch (...) { \
119        std::cout << __FILE__ << ":" << __LINE__ << ":\n"               \
120        << "FAILED: " << #E << " is expected to be thrown\n";           \
121        ++unit_test_failures; }
122
123#define OIIO_CHECK_NO_THOW(S)                                           \
124    try { S; } catch (...) {                                            \
125        std::cout << __FILE__ << ":" << __LINE__ << ":\n"               \
126        << "FAILED: exception thrown from " << #S <<"\n";               \
127        ++unit_test_failures; }
128
129#define OIIO_ADD_TEST(group, name)                                      \
130    static void oiiotest_##group##_##name();                            \
131    AddTest oiioaddtest_##group##_##name(new OIIOTest(#group, #name, oiiotest_##group##_##name)); \
132    static void oiiotest_##group##_##name()
133
134#define OIIO_TEST_SETUP() \
135    int unit_test_failures = 0
136
137#define OIIO_TEST_APP(app)                                              \
138    std::vector<OIIOTest*>& GetUnitTests() {                            \
139        static std::vector<OIIOTest*> oiio_unit_tests;                  \
140        return oiio_unit_tests; }                                       \
141    AddTest::AddTest(OIIOTest* test){GetUnitTests().push_back(test);};  \
142    OIIO_TEST_SETUP(); \
143    int main(int, char **) { std::cerr << "\n" << #app <<"\n\n";        \
144        for(size_t i = 0; i < GetUnitTests().size(); ++i) {             \
145            int _tmp = unit_test_failures; GetUnitTests()[i]->function(); \
146            std::cerr << "Test [" << GetUnitTests()[i]->group << "] [" << GetUnitTests()[i]->name << "] - "; \
147            std::cerr << (_tmp == unit_test_failures ? "PASSED" : "FAILED") << "\n"; } \
148        std::cerr << "\n" << unit_test_failures << " tests failed\n\n";   \
149        return unit_test_failures; }
150
151#endif /* OPENCOLORIO_UNITTEST_H */