1 #include <cxxtest/TestSuite.h>
2 #include "Dice.h"
3 #include "MockStdlib.h"
5 class TestDice : public CxxTest::TestSuite
6 {
7 public:
8 MockStdlib *stdlib;
10 void setUp()
11 {
12 TS_ASSERT( stdlib = new MockStdlib );
13 }
15 void tearDown()
16 {
17 delete stdlib;
18 }
20 void test_Randomize_uses_time()
21 {
22 stdlib->nextTime = 12345;
23 Dice dice;
24 TS_ASSERT_EQUALS( stdlib->lastSeed, 12345 );
25 }
27 void test_Roll()
28 {
29 Dice dice;
31 stdlib->nextRand = 0;
32 TS_ASSERT_EQUALS( dice.roll(), 1 );
34 stdlib->nextRand = 2;
35 TS_ASSERT_EQUALS( dice.roll(), 3 );
37 stdlib->nextRand = 5;
38 TS_ASSERT_EQUALS( dice.roll(), 6 );
40 stdlib->nextRand = 7;
41 TS_ASSERT_EQUALS( dice.roll(), 2 );
42 }
44 void test_Temporary_override_of_one_mock_function()
45 {
46 Dice dice;
48 stdlib->nextRand = 2;
49 TS_ASSERT_EQUALS( dice.roll(), 3 );
51 class Five : public T::Base_rand { int rand() { return 5; } };
53 Five *five = new Five;
54 TS_ASSERT_EQUALS( dice.roll(), 6 );
55 TS_ASSERT_EQUALS( dice.roll(), 6 );
56 TS_ASSERT_EQUALS( dice.roll(), 6 );
57 delete five;
59 stdlib->nextRand = 1;
60 TS_ASSERT_EQUALS( dice.roll(), 2 );
61 }
62 };