TestUnitTest++.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "../UnitTest++.h"
  2. #include "../ReportAssert.h"
  3. #include "ScopedCurrentTest.h"
  4. #include <vector>
  5. // These are sample tests that show the different features of the framework
  6. namespace {
  7. TEST(ValidCheckSucceeds)
  8. {
  9. bool const b = true;
  10. CHECK(b);
  11. }
  12. TEST(CheckWorksWithPointers)
  13. {
  14. void* p = (void *)0x100;
  15. CHECK(p);
  16. CHECK(p != 0);
  17. }
  18. TEST(ValidCheckEqualSucceeds)
  19. {
  20. int const x = 3;
  21. int const y = 3;
  22. CHECK_EQUAL(x, y);
  23. }
  24. TEST(CheckEqualWorksWithPointers)
  25. {
  26. void* p = (void *)0;
  27. CHECK_EQUAL((void*)0, p);
  28. }
  29. TEST(ValidCheckCloseSucceeds)
  30. {
  31. CHECK_CLOSE(2.0f, 2.001f, 0.01f);
  32. CHECK_CLOSE(2.001f, 2.0f, 0.01f);
  33. }
  34. TEST(ArrayCloseSucceeds)
  35. {
  36. float const a1[] = {1, 2, 3};
  37. float const a2[] = {1, 2.01f, 3};
  38. CHECK_ARRAY_CLOSE(a1, a2, 3, 0.1f);
  39. }
  40. TEST (CheckArrayCloseWorksWithVectors)
  41. {
  42. std::vector< float > a(4);
  43. for (int i = 0; i < 4; ++i)
  44. a[i] = (float)i;
  45. CHECK_ARRAY_CLOSE(a, a, (int)a.size(), 0.0001f);
  46. }
  47. TEST(CheckThrowMacroSucceedsOnCorrectException)
  48. {
  49. struct TestException {};
  50. CHECK_THROW(throw TestException(), TestException);
  51. }
  52. TEST(CheckAssertSucceeds)
  53. {
  54. CHECK_ASSERT(UnitTest::ReportAssert("desc", "file", 0));
  55. }
  56. TEST(CheckThrowMacroFailsOnMissingException)
  57. {
  58. class NoThrowTest : public UnitTest::Test
  59. {
  60. public:
  61. NoThrowTest() : Test("nothrow") {}
  62. void DontThrow() const
  63. {
  64. }
  65. virtual void RunImpl() const
  66. {
  67. CHECK_THROW(DontThrow(), int);
  68. }
  69. };
  70. UnitTest::TestResults results;
  71. {
  72. ScopedCurrentTest scopedResults(results);
  73. NoThrowTest test;
  74. test.Run();
  75. }
  76. CHECK_EQUAL(1, results.GetFailureCount());
  77. }
  78. TEST(CheckThrowMacroFailsOnWrongException)
  79. {
  80. class WrongThrowTest : public UnitTest::Test
  81. {
  82. public:
  83. WrongThrowTest() : Test("wrongthrow") {}
  84. virtual void RunImpl() const
  85. {
  86. CHECK_THROW(throw "oops", int);
  87. }
  88. };
  89. UnitTest::TestResults results;
  90. {
  91. ScopedCurrentTest scopedResults(results);
  92. WrongThrowTest test;
  93. test.Run();
  94. }
  95. CHECK_EQUAL(1, results.GetFailureCount());
  96. }
  97. struct SimpleFixture
  98. {
  99. SimpleFixture()
  100. {
  101. ++instanceCount;
  102. }
  103. ~SimpleFixture()
  104. {
  105. --instanceCount;
  106. }
  107. static int instanceCount;
  108. };
  109. int SimpleFixture::instanceCount = 0;
  110. TEST_FIXTURE(SimpleFixture, DefaultFixtureCtorIsCalled)
  111. {
  112. CHECK(SimpleFixture::instanceCount > 0);
  113. }
  114. TEST_FIXTURE(SimpleFixture, OnlyOneFixtureAliveAtATime)
  115. {
  116. CHECK_EQUAL(1, SimpleFixture::instanceCount);
  117. }
  118. void CheckBool(const bool b)
  119. {
  120. CHECK(b);
  121. }
  122. TEST(CanCallCHECKOutsideOfTestFunction)
  123. {
  124. CheckBool(true);
  125. }
  126. }