Skip to content

Instantly share code, notes, and snippets.

@flavius
Created March 10, 2012 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save flavius/2012324 to your computer and use it in GitHub Desktop.
Save flavius/2012324 to your computer and use it in GitHub Desktop.
[submodule "cxxtest"]
path = cxxtest
url = git://github.com/CxxTest/cxxtest.git

This serves as a good starting point for testing and semi-automatization purposes for LV 706.007.

Getting started

  1. fork your own copy from https://gist.github.com/2012324
  2. git clone <that copy's private clone URL> framework side-by-side with your normal repository so you will end up with two directories uni/sep/project/ and uni/sep/framework/
  3. cd framework; git submodule init; git submodule update
  4. create a symlink from uni/sep/framework/src/ to uni/sep/project/<src> - <src> is where your .cpp and .h files lie, in your private repository (subversion or git) that noone will see.

Make Targets

  • make
    • just compile the program
  • make tests
    • compile the program if necessary, then run the tests on it
  • make deliverable
    • create archive (ready for abgabe, DO NOT FORGET to update the DELIVERABLES variable in Makefile)
    • run the tests beforehand => IMPOSSIBLE to hand in something that doesn't pass the tests
  • make format
    • run astyle over the source code (the DELIVERABLES variable) - not guaranteed to be perfect, but Weber-proof in the scope of astyle at least

Testing unit

Add your tests in uni/sep/framework/tests/. The documentation is at http://cxxtest.com/cxxtest/doc/guide.html.

Commit and push your tests, so anyone can benefit from them.

???

  • fun
  • profit (ECTS)
sep> current player: Bjarne(Black)
sep> current player: Bjarne(Black)
sep> current player: Bjarne(Black)
sep> Bye!
status
StaTus
STATUS
quit
sep> |Z|Y|X|W|V|
-+-+-+-+-+-+
9| | | | | |
-+-+-+-+-+-+
8| | | | | |
-+-+-+-+-+-+
7| | | | | |
-+-+-+-+-+-+
6| | | | | |
-+-+-+-+-+-+
5| | | | | |
-+-+-+-+-+-+
sep> Bye!
sep> |Z|Y|X|W|V|
-+-+-+-+-+-+
9| | | | | |
-+-+-+-+-+-+
8| | | | | |
-+-+-+-+-+-+
7| | | | | |
-+-+-+-+-+-+
6| | | | | |
-+-+-+-+-+-+
5| | | | | |
-+-+-+-+-+-+
0x60c780sep> Error: Wrong usage of command.
sep> sep> sep> Error: Unknown command.
sep> Error: Unknown command.
sep> sep> Error: Wrong usage of command.
sep> sep> |Z|Y|X|W|V|
-+-+-+-+-+-+
9| | | | | |
-+-+-+-+-+-+
8| | | | | |
-+-+-+-+-+-+
7| | | | | |
-+-+-+-+-+-+
6| | | | | |
-+-+-+-+-+-+
5| | | | | |
-+-+-+-+-+-+
sep> Bye!
view xxxxxxx
status abc
hello
bla
quit xxxxxx
view
Quit
# files linked to the test runner (usually every .o file having a corresponding .cpp file, except main.cpp)
TESTABLE=Action.o Command.o Exception.o Game.o Player.o Quit.o Status.o View.o helpers.o \
SetName.o Save.o Move.o Draw.o DrawAction.o MoveAction.o CastlingAction.o ChessPiece.o \
Knight.o Queen.o Bishop.o King.o Pawn.o Rook.o
# files formatted by astyle
DELIVERABLES=Action.h Action.cpp Command.h Command.cpp Exception.h Exception.cpp \
Game.h Game.cpp Piece.h Player.h Player.cpp Point.h Quit.h Quit.cpp Status.h Status.cpp View.h View.cpp \
helpers.h helpers.cpp SetName.h SetName.cpp Save.h Save.cpp Move.h Move.cpp Draw.h Draw.cpp \
DrawAction.h DrawAction.cpp MoveAction.h MoveAction.cpp CastlingAction.h CastlingAction.cpp \
ChessPiece.h ChessPiece.cpp Knight.cpp Queen.cpp Bishop.cpp King.cpp Pawn.cpp Rook.cpp \
Knight.h Queen.h Bishop.h King.h Pawn.h Rook.h
# deliverable name
ASS=ass2
# compile flags for both the executable and the test suite
CXXFLAGS=-Wall -g3
# you shouldn't need to change anything below
CXX=g++
RUNNER_SRC=runner.cpp
VALGRIND_OPTS=--error-exitcode=1 --leak-check=summary
RUNNER=runner
TESTTOOL=./cxxtest/bin/cxxtestgen
OBJS=main.o $(TESTABLE)
TESTS=$(wildcard tests/*.h)
$(ASS): $(OBJS)
$(CXX) $(CXXFLAGS) $(OBJS) -o $(ASS)
%.o: src/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
.PHONY: tests clean
tests: $(RUNNER)
valgrind $(VALGRIND_OPTS) ./$(RUNNER)
$(RUNNER): $(TESTABLE)
$(TESTTOOL) --runner=XUnitPrinter -o $(RUNNER_SRC) tests/*.h
$(CXX) $(CXXFLAGS) -o $(RUNNER) $(TESTABLE) $(RUNNER_SRC) -I./cxxtest -I.
format:
cd src; astyle -A1 -j -c -s2 -p -k3 -Y $(DELIVERABLES)
deliverable: format tests
head -n `grep -n -B 1 ".PHONY" Makefile | sed -n 's/^\([0-9]*\).*/\1/p;q;'` Makefile > src/Makefile
sed -i 's/src\///' src/Makefile
tar -czf $(ASS).tar.gz -C src $(DELIVERABLES) Makefile
rm -f src/Makefile
clean:
rm -rf *.o *.gch $(ASS) $(RUNNER_SRC) $(RUNNER) $(ASS).tar.gz
#include <cxxtest/TestSuite.h>
#include <src/Game.h>
#include <src/Player.h>
#include <src/Point.h>
#include <string>
#include <sstream>
#include <fstream>
#include <streambuf>
using namespace Base;
using namespace std;
class OfficialTests : public CxxTest::TestSuite
{
Game* game;
stringstream *input;
stringstream *output;
streambuf* cout_backup, *cin_backup;
string run_output;
public:
void setUp() {
run_output = "";
cout_backup = cout.rdbuf();
cin_backup = cin.rdbuf();
input = new stringstream;
output = new stringstream;
cin.rdbuf(input->rdbuf());
cout.rdbuf(output->rdbuf());
}
void tearDown() {
cin.rdbuf(cin_backup);
cout.rdbuf(cout_backup);
delete input;
delete output;
/*
cout << endl;
cout << "--------------------" << endl;
cout << run_output;
cout << "--------------------" << endl;
*/
}
void test_Quit() {
run_standard_diff_file("ass2_01");
}
void test_CaseInsensitive_Command_Quit() {
run_standard_diff_file("ass2_02");
}
void test_CaseInsensitive_Command_Status() {
run_standard_diff_file("ass2_03");
}
void test_Command_Echo() {
run_standard_diff_file("ass2_04");
}
void test_Command_View() {
run_standard_diff_file("ass2_05");
}
void test_Garbage_Commands() {
run_standard_diff_file("ass2_06");
}
void test_Space_After_Echo() {
run_standard_diff_file("ass2_07");
}
void run_standard_diff_file(string name) {
game = getFixture();
*input << getCommands(name);
game->run();
string buffer;
getline(*output, buffer, '\0');
string expected = getExpected(name);
TS_ASSERT_EQUALS(expected, buffer);
delete game;
}
Game *getFixture() {
Point bounds = {5,5};
Game *game = new Game(bounds, 0, 0, '9', 'Z', false, false);
game->addPlayer(new Player(BLACK, "Bjarne"));
return game;
}
string readFile(string filename) {
ifstream t(filename.c_str());
string str;
t.seekg(0, ios::end);
str.reserve(t.tellg());
t.seekg(0, ios::beg);
str.assign((istreambuf_iterator<char>(t)),
istreambuf_iterator<char>());
return str;
}
string getCommands(string cmdfile) {
string filename = "tests/plainfiles/" + cmdfile + ".txt";
return readFile(filename);
}
string getExpected(string cmdfile) {
string filename = "tests/plainfiles/" + cmdfile + ".ref";
return readFile(filename);
}
};
// PlayerCopyCtor.h
#include <cxxtest/TestSuite.h>
#include <src/Player.h>
#include <string>
class PlayerCopyCtor : public CxxTest::TestSuite
{
public:
void setUp() {}
void tearDown() {}
void test_CopyCtor() {
Player foo(YELLOW, "Foo");
Player bar=foo;
bar.setColor(BLACK);
bar.setName("Bar");
TS_ASSERT_EQUALS("Foo(Yellow)", foo.getFullName());
TS_ASSERT_EQUALS("Bar(Black)", bar.getFullName());
}
};
// PlayerFullNames.h
#include <cxxtest/TestSuite.h>
#include <cxxtest/GlobalFixture.h>
#include <src/Player.h>
#include <string>
using Base::Player;
class Fixture1 : public CxxTest::GlobalFixture
{
public:
unsigned setUpCount;
unsigned tearDownCount;
Fixture1() { setUpCount = tearDownCount = 0; }
bool setUp() { return true; }
bool tearDown() { return true; }
bool setUpWorld() { setUpCount++; return true;}
bool tearDownWorld() { tearDownCount++; return true;}
};
static Fixture1 fixture1;
/** CRAPPY SPECIFICATION DOES NOT ALLOW US TO DO IT PROPERLY
enum _Test_Color
{
BLACK, WHITE, RED, GREEN, BLUE, YELLOW, ORANGE, BROWN
};
*/
static const std::string _test_color_names[] = { "Black", "White", "Red", "Green",
"Blue", "Yellow", "Orange", "Brown"};
static const std::string _test_player_names[] = { "Foo Black", "Bar White",
"Red Moon", "Green World", "Blue Screen of Death", "Yellow Submarine",
"Orange X", "Brown ...haha, da sag ich nichts"};
class PlayerFullNames : public CxxTest::TestSuite
{
Player *player;
std::string expected;
Color color;
public:
void setUp() {
unsigned cnt = fixture1.setUpCount-1;
color = static_cast<Color>(cnt);
player = new Player(color, _test_player_names[cnt]);
expected = _test_player_names[cnt] + "(" + _test_color_names[cnt] + ")";
}
void tearDown() {
delete player;
}
void test_BlackPlayer(void)
{
TS_ASSERT_EQUALS(expected, player->getFullName());
}
void test_WhitePlayer(void)
{
TS_ASSERT_EQUALS(expected, player->getFullName());
}
void test_RedPlayer(void)
{
TS_ASSERT_EQUALS(expected, player->getFullName());
}
void test_GreenPlayer(void)
{
TS_ASSERT_EQUALS(expected, player->getFullName());
}
void test_BluePlayer(void)
{
TS_ASSERT_EQUALS(expected, player->getFullName());
}
void test_YellowPlayer(void)
{
TS_ASSERT_EQUALS(expected, player->getFullName());
}
void test_OrangePlayer(void)
{
TS_ASSERT_EQUALS(expected, player->getFullName());
}
void test_BrownPlayer(void)
{
TS_ASSERT_EQUALS(expected, player->getFullName());
}
};
// PlayerName.h
#include <cxxtest/TestSuite.h>
#include <src/Player.h>
#include <string>
class PlayerName : public CxxTest::TestSuite
{
Player* player;
public:
void setUp() {
player = new Player(BLACK, "Foo");
}
void tearDown() {
delete player;
}
void test_NormalName() {
TS_ASSERT_EQUALS("Foo", player->getName());
}
void test_SetName() {
player->setName("Bar");
TS_ASSERT_EQUALS("Bar", player->getName());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment