|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Main application class |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <wchar.h> |
|
19 |
|
20 #include <cstring> |
|
21 #include <exception> |
|
22 |
|
23 #include <iostream> |
|
24 #include <string> |
|
25 |
|
26 #include "itk.h" |
|
27 |
|
28 // This is a GCCE toolchain workaround needed when compiling with GCCE |
|
29 // and using main() entry point |
|
30 #ifdef __GCCE__ |
|
31 #include <staticlibinit_gcce.h> |
|
32 #endif |
|
33 |
|
34 |
|
35 /** |
|
36 * These demonstrates (and tests) ITK, the integration test kit. To be |
|
37 * able to test / demo both successful and failed runs, the option |
|
38 * "buggy" is used, in which case "implementation" functions behave |
|
39 * "buggily". |
|
40 * |
|
41 * ITK features: |
|
42 * |
|
43 * UNIT TEST FETAURES |
|
44 * ================== |
|
45 * |
|
46 * (a) ITK_EXPECT - a macro that tells what should be true (see |
|
47 * examples), but on failure, execution is continued |
|
48 * |
|
49 * (b) ITK_ASSERT - the difference between expect and assert is that |
|
50 * if an assert fails, then the rest of test cases in the same |
|
51 * test context are not executed. (There is always a root context |
|
52 * encompassing all the test cases). |
|
53 * |
|
54 * (c) ITK_MSG - just producing a message, accepts printf like |
|
55 * formatted output. |
|
56 * |
|
57 * (d) One can define a context with setup and teardown. See example. |
|
58 * |
|
59 * |
|
60 * INTEGRATION TEST FEATURES |
|
61 * ========================= |
|
62 * |
|
63 * (a) Tests cases that are tested through what they produce in the |
|
64 * standard output and error can also be run. |
|
65 * |
|
66 * (b) See itktestcase.h for details, also the sample code here. |
|
67 * |
|
68 * |
|
69 */ |
|
70 |
|
71 |
|
72 int Negate(int n) |
|
73 { |
|
74 return -n; |
|
75 } |
|
76 |
|
77 |
|
78 class SimpleMath |
|
79 { |
|
80 private: |
|
81 bool buggy_; |
|
82 |
|
83 public: |
|
84 SimpleMath(bool buggy) |
|
85 : buggy_(buggy) |
|
86 { |
|
87 ; |
|
88 } |
|
89 |
|
90 |
|
91 int add(int left, |
|
92 int right) |
|
93 { |
|
94 return buggy_ ? left - right : left + right; |
|
95 } |
|
96 |
|
97 int sub(int left, |
|
98 int right) |
|
99 { |
|
100 return buggy_ ? left + right : left - right; |
|
101 } |
|
102 |
|
103 void doSomeIO() |
|
104 { |
|
105 using namespace std; |
|
106 |
|
107 if (buggy_) |
|
108 { |
|
109 cout << "Producing InCoRrEcT output" << endl; |
|
110 cout << endl; |
|
111 cout << "Garble garble farble" << endl; |
|
112 cout << endl; |
|
113 |
|
114 cerr << "Producing InCoRrEct output on stderr too" << endl; |
|
115 } |
|
116 else |
|
117 { |
|
118 cout << "Producing CORRECT output" << endl; |
|
119 cout << endl; |
|
120 cout << "Sensible sensible sense" << endl; |
|
121 cout << endl; |
|
122 } |
|
123 } |
|
124 |
|
125 |
|
126 void doSomeMoreIO() |
|
127 { |
|
128 using namespace std; |
|
129 |
|
130 int |
|
131 a,b; |
|
132 |
|
133 cin >> a; |
|
134 cin >> b; |
|
135 |
|
136 cout << "Producing " << a << "+" << b << "=" << add(a,b) << endl; |
|
137 } |
|
138 |
|
139 }; |
|
140 |
|
141 |
|
142 /** |
|
143 * Simple test function unit testing some functionality: making |
|
144 * computations and then making statements about the results. |
|
145 */ |
|
146 void TestNegate(Itk::TestMgr * testMgr) |
|
147 { |
|
148 ITK_EXPECT(testMgr, |
|
149 5 == Negate(-5), |
|
150 "Negate(-5) does not work"); |
|
151 ITK_EXPECT(testMgr, |
|
152 -5 == Negate(5), |
|
153 "Negate(5) does not work"); |
|
154 ITK_EXPECT(testMgr, |
|
155 0 == Negate(0), |
|
156 "Negate(0) does not work"); |
|
157 } |
|
158 |
|
159 |
|
160 class SimpleMathTest : public Itk::ITestContext |
|
161 { |
|
162 private: |
|
163 bool buggy_; |
|
164 SimpleMath * simpleMath_; |
|
165 public: |
|
166 SimpleMathTest(bool buggy) |
|
167 : buggy_(buggy), |
|
168 simpleMath_(NULL) |
|
169 { |
|
170 ; |
|
171 } |
|
172 |
|
173 |
|
174 ~SimpleMathTest() |
|
175 { |
|
176 ; |
|
177 } |
|
178 |
|
179 |
|
180 virtual void setup() throw (Itk::PanicExc) |
|
181 { |
|
182 try |
|
183 { |
|
184 simpleMath_ = new SimpleMath(buggy_); |
|
185 } |
|
186 catch (std::exception & exc) |
|
187 { |
|
188 ITK_PANIC("Could not construct SimpleMath: %s", |
|
189 exc.what()); |
|
190 } |
|
191 catch (...) |
|
192 { |
|
193 ITK_PANIC("Could not construct SimpleMath: unknown reason."); |
|
194 } |
|
195 } |
|
196 |
|
197 virtual void tearDown() throw() |
|
198 { |
|
199 delete simpleMath_; |
|
200 simpleMath_ = NULL; |
|
201 } |
|
202 |
|
203 |
|
204 void testAdd(Itk::TestMgr * testMgr) |
|
205 { |
|
206 ITK_EXPECT(testMgr, |
|
207 simpleMath_->add(4,5) == 9, |
|
208 "Addition does not work"); |
|
209 ITK_EXPECT(testMgr, |
|
210 simpleMath_->add(-11,11) == 0, |
|
211 "Addition does not work"); |
|
212 ITK_EXPECT(testMgr, |
|
213 simpleMath_->add(-4,-5) == -9, |
|
214 "Addition does not work"); |
|
215 } |
|
216 |
|
217 |
|
218 void testSub(Itk::TestMgr * testMgr) |
|
219 { |
|
220 ITK_EXPECT(testMgr, |
|
221 simpleMath_->sub(4,5) == -1, |
|
222 "Subtraction does not work"); |
|
223 ITK_ASSERT(testMgr, |
|
224 simpleMath_->sub(11,11) == 0, |
|
225 "Subtraction does not work"); |
|
226 ITK_EXPECT(testMgr, |
|
227 simpleMath_->sub(-4,-5) == 1, |
|
228 "Subtraction does not work"); |
|
229 } |
|
230 |
|
231 |
|
232 void testDoSomeIO(Itk::TestMgr * testMgr) |
|
233 { |
|
234 ITK_MSG(testMgr, |
|
235 "Testing Some IO"); |
|
236 |
|
237 simpleMath_->doSomeIO(); |
|
238 } |
|
239 |
|
240 |
|
241 void testDoSomeMoreIO(Itk::TestMgr * testMgr) |
|
242 { |
|
243 ITK_MSG(testMgr, |
|
244 "Testing Some MORE IO (with input too)"); |
|
245 |
|
246 using namespace Itk; |
|
247 |
|
248 Timestamp |
|
249 before; |
|
250 getTimestamp(&before); |
|
251 |
|
252 simpleMath_->doSomeMoreIO(); |
|
253 |
|
254 Timestamp |
|
255 after; |
|
256 getTimestamp(&after); |
|
257 |
|
258 long |
|
259 elapsedMs = getElapsedMs(&after, |
|
260 &before); |
|
261 |
|
262 ITK_MSG(testMgr, |
|
263 "Testing more IO took %d ms", |
|
264 elapsedMs); |
|
265 |
|
266 ITK_REPORT(testMgr, |
|
267 "Elapsed time", |
|
268 "%d (ms)", |
|
269 elapsedMs); |
|
270 ITK_DBGMSG(testMgr, |
|
271 "A message for the debug console."); |
|
272 } |
|
273 |
|
274 }; |
|
275 |
|
276 |
|
277 bool runTests(bool buggy) |
|
278 { |
|
279 bool |
|
280 rv = true; |
|
281 |
|
282 using namespace std; |
|
283 using namespace Itk; |
|
284 |
|
285 SuiteTester |
|
286 suiteTester("Arithmetic tests"); |
|
287 |
|
288 // free testing function |
|
289 suiteTester.add("Negating", |
|
290 TestNegate); |
|
291 |
|
292 // an IContext derivant (with setup and teardown |
|
293 SimpleMathTest |
|
294 * simpleMathTest = new SimpleMathTest(buggy); |
|
295 |
|
296 // test context |
|
297 ContextTester |
|
298 * contextTester = new ContextTester("simplemath", |
|
299 simpleMathTest); |
|
300 |
|
301 // member testing function with IO captured test definition |
|
302 contextTester->add("Doing some IO", |
|
303 simpleMathTest, |
|
304 &SimpleMathTest::testDoSomeIO, |
|
305 "doSomeIO"); |
|
306 contextTester->add("Doing some more IO", |
|
307 simpleMathTest, |
|
308 &SimpleMathTest::testDoSomeMoreIO, |
|
309 "doSomeMoreIO", |
|
310 "Testing more IO took"); // lenience |
|
311 |
|
312 // member testing function |
|
313 contextTester->add("Adding", |
|
314 simpleMathTest, |
|
315 &SimpleMathTest::testAdd); |
|
316 contextTester->add("Subtracting", |
|
317 simpleMathTest, |
|
318 &SimpleMathTest::testSub); |
|
319 |
|
320 |
|
321 suiteTester.add(contextTester); |
|
322 |
|
323 TestRunConsole |
|
324 ui(std::cout); |
|
325 |
|
326 TestMgr |
|
327 testMgr(&ui, |
|
328 "c:\\data\\itkdemo\\"); |
|
329 |
|
330 rv = testMgr.run(&suiteTester); |
|
331 |
|
332 testMgr.generateSummary(std::cout); |
|
333 |
|
334 return rv; |
|
335 } |
|
336 |
|
337 |
|
338 |
|
339 void Usage(const char * exeName) |
|
340 { |
|
341 using namespace std; |
|
342 |
|
343 cout << "Usage:" << endl; |
|
344 cout << exeName << " [b]" << endl; |
|
345 cout << " b: 'BuGgY run - fail tests on purpose" << endl; |
|
346 } |
|
347 |
|
348 |
|
349 |
|
350 int main(int argc, |
|
351 const char * argv[]) |
|
352 { |
|
353 bool |
|
354 buggy = false; |
|
355 const char |
|
356 * exeName = argv[0]; |
|
357 |
|
358 if (argc == 2) |
|
359 { |
|
360 if (strcmp("b", argv[1]) == 0) |
|
361 { |
|
362 buggy = true; |
|
363 } |
|
364 else |
|
365 { |
|
366 Usage(exeName); |
|
367 exit(1); |
|
368 } |
|
369 } |
|
370 else if (argc == 1) |
|
371 { |
|
372 ; |
|
373 } |
|
374 else |
|
375 { |
|
376 Usage(exeName); |
|
377 exit(1); |
|
378 } |
|
379 |
|
380 int |
|
381 rv = runTests(buggy); |
|
382 |
|
383 int |
|
384 c = getchar(); |
|
385 c = getchar(); |
|
386 c = getchar(); |
|
387 |
|
388 return rv; |
|
389 } |