comparison ext/UnitTest++/src/Checks.h @ 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 #ifndef UNITTEST_CHECKS_H
2 #define UNITTEST_CHECKS_H
3
4 #include "Config.h"
5 #include "TestResults.h"
6 #include "MemoryOutStream.h"
7
8 namespace UnitTest {
9
10
11 template< typename Value >
12 bool Check(Value const value)
13 {
14 return !!value; // doing double negative to avoid silly VS warnings
15 }
16
17
18 template< typename Expected, typename Actual >
19 void CheckEqual(TestResults& results, Expected const expected, Actual const actual, TestDetails const& details)
20 {
21 if (!(expected == actual))
22 {
23 UnitTest::MemoryOutStream stream;
24 stream << "Expected " << expected << " but was " << actual;
25
26 results.OnTestFailure(details, stream.GetText());
27 }
28 }
29
30 void CheckEqual(TestResults& results, char const* expected, char const* actual, TestDetails const& details);
31
32 void CheckEqual(TestResults& results, char* expected, char* actual, TestDetails const& details);
33
34 void CheckEqual(TestResults& results, char* expected, char const* actual, TestDetails const& details);
35
36 void CheckEqual(TestResults& results, char const* expected, char* actual, TestDetails const& details);
37
38 template< typename Expected, typename Actual, typename Tolerance >
39 bool AreClose(Expected const expected, Actual const actual, Tolerance const tolerance)
40 {
41 return (actual >= (expected - tolerance)) && (actual <= (expected + tolerance));
42 }
43
44 template< typename Expected, typename Actual, typename Tolerance >
45 void CheckClose(TestResults& results, Expected const expected, Actual const actual, Tolerance const tolerance,
46 TestDetails const& details)
47 {
48 if (!AreClose(expected, actual, tolerance))
49 {
50 UnitTest::MemoryOutStream stream;
51 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;
52
53 results.OnTestFailure(details, stream.GetText());
54 }
55 }
56
57
58 template< typename Expected, typename Actual >
59 void CheckArrayEqual(TestResults& results, Expected const expected, Actual const actual,
60 int const count, TestDetails const& details)
61 {
62 bool equal = true;
63 for (int i = 0; i < count; ++i)
64 equal &= (expected[i] == actual[i]);
65
66 if (!equal)
67 {
68 UnitTest::MemoryOutStream stream;
69 stream << "Expected [ ";
70 for (int i = 0; i < count; ++i)
71 stream << expected[i] << " ";
72 stream << "] but was [ ";
73 for (int i = 0; i < count; ++i)
74 stream << actual[i] << " ";
75 stream << "]";
76
77 results.OnTestFailure(details, stream.GetText());
78 }
79 }
80
81 template< typename Expected, typename Actual, typename Tolerance >
82 bool ArrayAreClose(Expected const expected, Actual const actual, int const count, Tolerance const tolerance)
83 {
84 bool equal = true;
85 for (int i = 0; i < count; ++i)
86 equal &= AreClose(expected[i], actual[i], tolerance);
87 return equal;
88 }
89
90 template< typename Expected, typename Actual, typename Tolerance >
91 void CheckArrayClose(TestResults& results, Expected const expected, Actual const actual,
92 int const count, Tolerance const tolerance, TestDetails const& details)
93 {
94 bool equal = ArrayAreClose(expected, actual, count, tolerance);
95
96 if (!equal)
97 {
98 UnitTest::MemoryOutStream stream;
99 stream << "Expected [ ";
100 for (int i = 0; i < count; ++i)
101 stream << expected[i] << " ";
102 stream << "] +/- " << tolerance << " but was [ ";
103 for (int i = 0; i < count; ++i)
104 stream << actual[i] << " ";
105 stream << "]";
106
107 results.OnTestFailure(details, stream.GetText());
108 }
109 }
110
111 template< typename Expected, typename Actual, typename Tolerance >
112 void CheckArray2DClose(TestResults& results, Expected const expected, Actual const actual,
113 int const rows, int const columns, Tolerance const tolerance, TestDetails const& details)
114 {
115 bool equal = true;
116 for (int i = 0; i < rows; ++i)
117 equal &= ArrayAreClose(expected[i], actual[i], columns, tolerance);
118
119 if (!equal)
120 {
121 UnitTest::MemoryOutStream stream;
122 stream << "Expected [ ";
123 for (int i = 0; i < rows; ++i)
124 {
125 stream << "[ ";
126 for (int j = 0; j < columns; ++j)
127 stream << expected[i][j] << " ";
128 stream << "] ";
129 }
130 stream << "] +/- " << tolerance << " but was [ ";
131 for (int i = 0; i < rows; ++i)
132 {
133 stream << "[ ";
134 for (int j = 0; j < columns; ++j)
135 stream << actual[i][j] << " ";
136 stream << "] ";
137 }
138 stream << "]";
139
140 results.OnTestFailure(details, stream.GetText());
141 }
142 }
143
144 }
145
146 #endif