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