Initial commit

This commit is contained in:
Bassem Girgis
2018-12-20 17:34:07 -06:00
parent 7a2d899662
commit 81b4b9e273
34743 changed files with 5940233 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -0,0 +1,13 @@
#ifndef __DICE_H
#define __DICE_H
class Dice
{
public:
Dice();
unsigned roll();
};
#endif // __DICE_H

View File

@@ -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)/bin/cxxtestgen -o $@ --error-printer $<

View File

@@ -0,0 +1,33 @@
#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;
}
};

View File

@@ -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

View File

@@ -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);
}
};

View File

@@ -0,0 +1,2 @@
#define CXXTEST_MOCK_TEST_SOURCE_FILE
#include <T/stdlib.h>

View File

@@ -0,0 +1,2 @@
#define CXXTEST_MOCK_REAL_SOURCE_FILE
#include <T/stdlib.h>

View File

@@ -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;
}