testfrmwk.hpp 3.0 KB

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