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