[e3c8b4] | 1 | /*
|
---|
| 2 | * CacheableTest.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Feb 2, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[e3c8b4] | 13 | #include "CacheableTest.hpp"
|
---|
| 14 |
|
---|
| 15 | #include <cppunit/CompilerOutputter.h>
|
---|
| 16 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 17 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 18 |
|
---|
| 19 | #include <boost/bind.hpp>
|
---|
| 20 |
|
---|
| 21 | #include "Patterns/Cacheable.hpp"
|
---|
| 22 |
|
---|
[9b6b2f] | 23 | #ifdef HAVE_TESTRUNNER
|
---|
| 24 | #include "UnitTestMain.hpp"
|
---|
| 25 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 26 |
|
---|
| 27 | /********************************************** Test classes **************************************/
|
---|
| 28 |
|
---|
[e3c8b4] | 29 | // Registers the fixture into the 'registry'
|
---|
| 30 | CPPUNIT_TEST_SUITE_REGISTRATION( CacheableTest );
|
---|
| 31 |
|
---|
| 32 | class threeNumbers : public Observable {
|
---|
| 33 | public:
|
---|
| 34 | int x;
|
---|
| 35 | int y;
|
---|
| 36 | int z;
|
---|
[ead4e6] | 37 | Cacheable<int> sum;
|
---|
| 38 | bool hasRecalced;
|
---|
[e3c8b4] | 39 |
|
---|
| 40 | void setX(int _x){
|
---|
[2ba827] | 41 | OBSERVE;
|
---|
[e3c8b4] | 42 | x = _x;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | void setY
|
---|
| 46 | (int _y){
|
---|
[2ba827] | 47 | OBSERVE;
|
---|
[e3c8b4] | 48 | y = _y;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | void setZ(int _z){
|
---|
[2ba827] | 52 | OBSERVE;
|
---|
[e3c8b4] | 53 | z = _z;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | int calcSum(){
|
---|
| 57 | hasRecalced = true;
|
---|
| 58 | return x+y+z;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | threeNumbers(int _x,int _y, int _z) :
|
---|
[cd5047] | 62 | Observable("threeNumbers"),
|
---|
[e3c8b4] | 63 | x(_x),y(_y),z(_z),
|
---|
[cd5047] | 64 | sum(this,boost::bind(&threeNumbers::calcSum,this),"sum"),
|
---|
[e3c8b4] | 65 | hasRecalced(false)
|
---|
| 66 | {}
|
---|
| 67 | };
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | void CacheableTest::setUp(){
|
---|
| 73 | numbers = new threeNumbers(1,2,3);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void CacheableTest::tearDown(){
|
---|
| 77 | delete numbers;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | void CacheableTest::doesRecalcTest()
|
---|
| 81 | {
|
---|
| 82 | CPPUNIT_ASSERT_EQUAL( 6, *(numbers->sum));
|
---|
| 83 | CPPUNIT_ASSERT_EQUAL( true, numbers->hasRecalced);
|
---|
| 84 | numbers->hasRecalced=false;
|
---|
| 85 | numbers->setX(4);
|
---|
| 86 | CPPUNIT_ASSERT_EQUAL( false, numbers->hasRecalced);
|
---|
| 87 | CPPUNIT_ASSERT_EQUAL( 9, *(numbers->sum));
|
---|
| 88 | CPPUNIT_ASSERT_EQUAL( true, numbers->hasRecalced);
|
---|
[f8e486] | 89 | numbers->hasRecalced=false;
|
---|
| 90 | CPPUNIT_ASSERT_EQUAL( 9, *(numbers->sum));
|
---|
| 91 | #ifndef NO_CACHING
|
---|
| 92 | CPPUNIT_ASSERT_EQUAL( false, numbers->hasRecalced);
|
---|
| 93 | #else
|
---|
| 94 | CPPUNIT_ASSERT_EQUAL( true, numbers->hasRecalced);
|
---|
| 95 | #endif
|
---|
[e3c8b4] | 96 | }
|
---|