diff options
author | Max Horn | 2006-03-29 10:25:48 +0000 |
---|---|---|
committer | Max Horn | 2006-03-29 10:25:48 +0000 |
commit | 3820593bb8dfdca4e2e17c70b278065b7f7894d9 (patch) | |
tree | df3e8bebda3abcd99d0882dc9e8c6410f86850fb /test/cxxtest/sample/mock | |
parent | 9c94670e22cb68e32fd38c0d2b009539e35d4a71 (diff) | |
download | scummvm-rg350-3820593bb8dfdca4e2e17c70b278065b7f7894d9.tar.gz scummvm-rg350-3820593bb8dfdca4e2e17c70b278065b7f7894d9.tar.bz2 scummvm-rg350-3820593bb8dfdca4e2e17c70b278065b7f7894d9.zip |
bringing cxxtest-3.10.1 to ScummVM's main branch
svn-id: r21488
Diffstat (limited to 'test/cxxtest/sample/mock')
-rw-r--r-- | test/cxxtest/sample/mock/Dice.cpp | 14 | ||||
-rw-r--r-- | test/cxxtest/sample/mock/Dice.h | 13 | ||||
-rw-r--r-- | test/cxxtest/sample/mock/Makefile | 22 | ||||
-rw-r--r-- | test/cxxtest/sample/mock/MockStdlib.h | 31 | ||||
-rw-r--r-- | test/cxxtest/sample/mock/T/stdlib.h | 13 | ||||
-rw-r--r-- | test/cxxtest/sample/mock/TestDice.h | 62 | ||||
-rw-r--r-- | test/cxxtest/sample/mock/mock_stdlib.cpp | 2 | ||||
-rw-r--r-- | test/cxxtest/sample/mock/real_stdlib.cpp | 2 | ||||
-rw-r--r-- | test/cxxtest/sample/mock/roll.cpp | 11 |
9 files changed, 170 insertions, 0 deletions
diff --git a/test/cxxtest/sample/mock/Dice.cpp b/test/cxxtest/sample/mock/Dice.cpp new file mode 100644 index 0000000000..161b80fa2b --- /dev/null +++ b/test/cxxtest/sample/mock/Dice.cpp @@ -0,0 +1,14 @@ +#include <T/stdlib.h> +#include "Dice.h" + +Dice::Dice() +{ + T::srand( T::time( 0 ) ); +} + +unsigned Dice::roll() +{ + return (T::rand() % 6) + 1; +} + + diff --git a/test/cxxtest/sample/mock/Dice.h b/test/cxxtest/sample/mock/Dice.h new file mode 100644 index 0000000000..94271417e1 --- /dev/null +++ b/test/cxxtest/sample/mock/Dice.h @@ -0,0 +1,13 @@ +#ifndef __DICE_H +#define __DICE_H + +class Dice +{ +public: + Dice(); + + unsigned roll(); +}; + +#endif // __DICE_H + diff --git a/test/cxxtest/sample/mock/Makefile b/test/cxxtest/sample/mock/Makefile new file mode 100644 index 0000000000..709b7cbe95 --- /dev/null +++ b/test/cxxtest/sample/mock/Makefile @@ -0,0 +1,22 @@ +all: roll run + +clean: + rm -f *~ *.o roll test test.cpp + +CXXTEST = ../.. +CCFLAGS = -I. -I$(CXXTEST) + +roll: roll.o Dice.o real_stdlib.o + g++ -o $@ $^ + +run: test + ./test + +test: test.o Dice.o mock_stdlib.o + g++ -o $@ $^ + +.cpp.o: + g++ -c -o $@ $(CCFLAGS) $< + +test.cpp: TestDice.h + $(CXXTEST)/cxxtestgen.pl -o $@ --error-printer $< diff --git a/test/cxxtest/sample/mock/MockStdlib.h b/test/cxxtest/sample/mock/MockStdlib.h new file mode 100644 index 0000000000..aee62bafe7 --- /dev/null +++ b/test/cxxtest/sample/mock/MockStdlib.h @@ -0,0 +1,31 @@ +#include <T/stdlib.h> + +class MockStdlib : + public T::Base_srand, + public T::Base_rand, + public T::Base_time +{ +public: + unsigned lastSeed; + + void srand( unsigned seed ) + { + lastSeed = seed; + } + + int nextRand; + + int rand() + { + return nextRand; + } + + time_t nextTime; + + time_t time( time_t *t ) + { + if ( t ) + *t = nextTime; + return nextTime; + } +}; diff --git a/test/cxxtest/sample/mock/T/stdlib.h b/test/cxxtest/sample/mock/T/stdlib.h new file mode 100644 index 0000000000..30306ba22b --- /dev/null +++ b/test/cxxtest/sample/mock/T/stdlib.h @@ -0,0 +1,13 @@ +#ifndef __T__STDLIB_H +#define __T__STDLIB_H + +#include <stdlib.h> +#include <time.h> + +#include <cxxtest/Mock.h> + +CXXTEST_MOCK_VOID_GLOBAL( srand, ( unsigned seed ), ( seed ) ); +CXXTEST_MOCK_GLOBAL( int, rand, ( void ), () ); +CXXTEST_MOCK_GLOBAL( time_t, time, ( time_t *t ), ( t ) ); + +#endif // __T__STDLIB_H diff --git a/test/cxxtest/sample/mock/TestDice.h b/test/cxxtest/sample/mock/TestDice.h new file mode 100644 index 0000000000..35b3b7eec9 --- /dev/null +++ b/test/cxxtest/sample/mock/TestDice.h @@ -0,0 +1,62 @@ +#include <cxxtest/TestSuite.h> +#include "Dice.h" +#include "MockStdlib.h" + +class TestDice : public CxxTest::TestSuite +{ +public: + MockStdlib *stdlib; + + void setUp() + { + TS_ASSERT( stdlib = new MockStdlib ); + } + + void tearDown() + { + delete stdlib; + } + + void test_Randomize_uses_time() + { + stdlib->nextTime = 12345; + Dice dice; + TS_ASSERT_EQUALS( stdlib->lastSeed, 12345 ); + } + + void test_Roll() + { + Dice dice; + + stdlib->nextRand = 0; + TS_ASSERT_EQUALS( dice.roll(), 1 ); + + stdlib->nextRand = 2; + TS_ASSERT_EQUALS( dice.roll(), 3 ); + + stdlib->nextRand = 5; + TS_ASSERT_EQUALS( dice.roll(), 6 ); + + stdlib->nextRand = 7; + TS_ASSERT_EQUALS( dice.roll(), 2 ); + } + + void test_Temporary_override_of_one_mock_function() + { + Dice dice; + + stdlib->nextRand = 2; + TS_ASSERT_EQUALS( dice.roll(), 3 ); + + class Five : public T::Base_rand { int rand() { return 5; } }; + + Five *five = new Five; + TS_ASSERT_EQUALS( dice.roll(), 6 ); + TS_ASSERT_EQUALS( dice.roll(), 6 ); + TS_ASSERT_EQUALS( dice.roll(), 6 ); + delete five; + + stdlib->nextRand = 1; + TS_ASSERT_EQUALS( dice.roll(), 2 ); + } +}; diff --git a/test/cxxtest/sample/mock/mock_stdlib.cpp b/test/cxxtest/sample/mock/mock_stdlib.cpp new file mode 100644 index 0000000000..148a044d26 --- /dev/null +++ b/test/cxxtest/sample/mock/mock_stdlib.cpp @@ -0,0 +1,2 @@ +#define CXXTEST_MOCK_TEST_SOURCE_FILE +#include <T/stdlib.h> diff --git a/test/cxxtest/sample/mock/real_stdlib.cpp b/test/cxxtest/sample/mock/real_stdlib.cpp new file mode 100644 index 0000000000..db02f3a154 --- /dev/null +++ b/test/cxxtest/sample/mock/real_stdlib.cpp @@ -0,0 +1,2 @@ +#define CXXTEST_MOCK_REAL_SOURCE_FILE +#include <T/stdlib.h> diff --git a/test/cxxtest/sample/mock/roll.cpp b/test/cxxtest/sample/mock/roll.cpp new file mode 100644 index 0000000000..20ea967af3 --- /dev/null +++ b/test/cxxtest/sample/mock/roll.cpp @@ -0,0 +1,11 @@ +#include <stdio.h> +#include "Dice.h" + +int main() +{ + Dice dice; + printf( "First roll: %u\n", dice.roll() ); + printf( "Second roll: %u\n", dice.roll() ); + + return 0; +} |