comparison ext/UnitTest++/src/tests/TestUnitTest++.cpp @ 37:0d325e9d5953

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