1 | /*
|
---|
2 | * CacheableTest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Feb 2, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "CacheableTest.hpp"
|
---|
9 |
|
---|
10 | #include <cppunit/CompilerOutputter.h>
|
---|
11 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
12 | #include <cppunit/ui/text/TestRunner.h>
|
---|
13 |
|
---|
14 | #include <boost/bind.hpp>
|
---|
15 |
|
---|
16 | #include "Patterns/Cacheable.hpp"
|
---|
17 |
|
---|
18 | // Registers the fixture into the 'registry'
|
---|
19 | CPPUNIT_TEST_SUITE_REGISTRATION( CacheableTest );
|
---|
20 |
|
---|
21 | class threeNumbers : public Observable {
|
---|
22 | public:
|
---|
23 | bool hasRecalced;
|
---|
24 | int x;
|
---|
25 | int y;
|
---|
26 | int z;
|
---|
27 |
|
---|
28 | void setX(int _x){
|
---|
29 | START_OBSERVER;;
|
---|
30 | x = _x;
|
---|
31 | FINISH_OBSERVER;
|
---|
32 | }
|
---|
33 |
|
---|
34 | void setY
|
---|
35 | (int _y){
|
---|
36 | START_OBSERVER;;
|
---|
37 | y = _y;
|
---|
38 | FINISH_OBSERVER;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void setZ(int _z){
|
---|
42 | START_OBSERVER;;
|
---|
43 | z = _z;
|
---|
44 | FINISH_OBSERVER;
|
---|
45 | }
|
---|
46 |
|
---|
47 | int calcSum(){
|
---|
48 | hasRecalced = true;
|
---|
49 | return x+y+z;
|
---|
50 | }
|
---|
51 |
|
---|
52 | threeNumbers(int _x,int _y, int _z) :
|
---|
53 | sum(this,boost::bind(&threeNumbers::calcSum,this)),
|
---|
54 | x(_x),y(_y),z(_z),
|
---|
55 | hasRecalced(false)
|
---|
56 | {}
|
---|
57 |
|
---|
58 | Cacheable<int> sum;
|
---|
59 | };
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | void CacheableTest::setUp(){
|
---|
65 | numbers = new threeNumbers(1,2,3);
|
---|
66 | }
|
---|
67 |
|
---|
68 | void CacheableTest::tearDown(){
|
---|
69 | delete numbers;
|
---|
70 | }
|
---|
71 |
|
---|
72 | void CacheableTest::doesRecalcTest()
|
---|
73 | {
|
---|
74 | CPPUNIT_ASSERT_EQUAL( 6, *(numbers->sum));
|
---|
75 | CPPUNIT_ASSERT_EQUAL( true, numbers->hasRecalced);
|
---|
76 | numbers->hasRecalced=false;
|
---|
77 | numbers->setX(4);
|
---|
78 | CPPUNIT_ASSERT_EQUAL( false, numbers->hasRecalced);
|
---|
79 | CPPUNIT_ASSERT_EQUAL( 9, *(numbers->sum));
|
---|
80 | CPPUNIT_ASSERT_EQUAL( true, numbers->hasRecalced);
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 |
|
---|
85 | /********************************************** Main routine **************************************/
|
---|
86 |
|
---|
87 | int main(int argc, char **argv)
|
---|
88 | {
|
---|
89 | // Get the top level suite from the registry
|
---|
90 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
91 |
|
---|
92 | // Adds the test to the list of test to run
|
---|
93 | CppUnit::TextUi::TestRunner runner;
|
---|
94 | runner.addTest( suite );
|
---|
95 |
|
---|
96 | // Change the default outputter to a compiler error format outputter
|
---|
97 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
98 | std::cerr ) );
|
---|
99 | // Run the tests.
|
---|
100 | bool wasSucessful = runner.run();
|
---|
101 |
|
---|
102 | // Return error code 1 if the one of test failed.
|
---|
103 | return wasSucessful ? 0 : 1;
|
---|
104 | };
|
---|
105 |
|
---|