我的测试/文件夹包含这些文件
test/SimulationTest.cpp
test/main.cpp
test/HouseTest.cpp
根据Catch说明,main.cpp
文件只包含此内容。
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
当我在其中一个测试中注释掉所有代码时,我可以将所有代码编译成一个大的可执行文件并且它可以工作。 但是如果我按原样保留代码 - 生成的可执行文件会消耗我机器上的所有内存并且它会挂起。
我像这样编译测试:
g++ -c -Wfatal-errors -Isource -ICatch-1.3.0/single_include -std=c++14 -Wall -O2 -pedantic -pthread -o test/HouseTest.o test/HouseTest.cpp
g++ -c -Wfatal-errors -Isource -ICatch-1.3.0/single_include -std=c++14 -Wall -O2 -pedantic -pthread -o test/SimulationTest.o test/SimulationTest.cpp
g++ -c -Wfatal-errors -Isource -ICatch-1.3.0/single_include -std=c++14 -Wall -O2 -pedantic -pthread -o test/main.o test/main.cpp
以下是测试(更别关注实现它们的代码):
测试一个:
#include <queue>
#include <catch.hpp>
#include "Direction.h"
#include "AbstractAlgorithm.h"
#include "House.h"
#include "Simulation.h"
class FakeAlgorithm : public AbstractAlgorithm {
public:
FakeAlgorithm( std::queue< Direction > & moves )
: _moves( moves )
{
}
virtual void setSensor( const AbstractSensor & sensor )
{
_sensor = & sensor;
}
virtual void setConfiguration( map<string, int> config )
{}
virtual Direction step()
{
sensed.push( _sensor->sense() );
Direction move = _moves.front();
_moves.pop();
return move;
}
virtual void aboutToFinish( int stepsTillFinishing )
{}
std::queue< Direction > & _moves;
AbstractSensor const * _sensor;
std::queue< SensorInformation > sensed;
};
SensorInformation pop( std::queue<SensorInformation> & queue )
{
SensorInformation result = queue.front();
queue.pop();
return result;
}
void REQUIRE_SENSE( SensorInformation info, int dirtLevel, bool east, bool west, bool south, bool north )
{
REQUIRE( info.dirtLevel == dirtLevel );
REQUIRE( info.isWall[ int(Direction::East) ] == east );
REQUIRE( info.isWall[ int(Direction::West) ] == west );
REQUIRE( info.isWall[ int(Direction::South) ] == south );
REQUIRE( info.isWall[ int(Direction::North) ] == north );
}
TEST_CASE( "Simulation Runs Algorithm", "[simulations]" ) {
House house( "fixtures/house_fixture_2.house" );
std::queue< Direction > moves;
moves.push( Direction::West );
moves.push( Direction::West );
moves.push( Direction::Stay );
moves.push( Direction::West );
moves.push( Direction::South );
moves.push( Direction::East );
moves.push( Direction::Stay );
moves.push( Direction::Stay );
moves.push( Direction::East );
moves.push( Direction::East );
moves.push( Direction::North );
FakeAlgorithm algorithm( moves );
Simulation tested( house, algorithm );
tested.go();
REQUIRE( house.clean() );
REQUIRE( algorithm.sensed.size() == 11 );
REQUIRE_SENSE( pop( algorithm.sensed ), 0, false, false, false, true ); //W
REQUIRE_SENSE( pop( algorithm.sensed ), 0, false, false, false, true ); //W
REQUIRE_SENSE( pop( algorithm.sensed ), 1, false, false, false, true ); //St
REQUIRE_SENSE( pop( algorithm.sensed ), 0, false, false, false, true ); //W
REQUIRE_SENSE( pop( algorithm.sensed ), 0, false, false, false, true ); //Sou
REQUIRE_SENSE( pop( algorithm.sensed ), 0, false, false, false, false ); //East
REQUIRE_SENSE( pop( algorithm.sensed ), 2, false, false, false, false ); //St
REQUIRE_SENSE( pop( algorithm.sensed ), 1, false, false, false, false ); //St
REQUIRE_SENSE( pop( algorithm.sensed ), 0, false, false, false, false ); //E
REQUIRE_SENSE( pop( algorithm.sensed ), 0, false, false, false, false ); //E
REQUIRE_SENSE( pop( algorithm.sensed ), 0, false, false, false, false ); //N
}
测试二:
#include <catch.hpp>
#include "House.h"
TEST_CASE( "House is read from file", "[houses]" ) {
House tested( "fixtures/house_fixture_1.house" );
REQUIRE( tested.wall( Position( 5, 4 ) ) );
REQUIRE( tested.wall( Position( 6, 6 ) ) );
REQUIRE( tested.docking( Position( 1, 8 ) ) );
REQUIRE( tested.dust( Position( 1, 3 ) ) == 9 );
REQUIRE( tested.dust( Position( 1, 4 ) ) == 9 );
REQUIRE( tested.dust( Position( 2, 3 ) ) == 9 );
REQUIRE( tested.dust( Position( 2, 4 ) ) == 9 );
REQUIRE( tested.dust( Position( 2, 15 )) == 1 );
REQUIRE( tested.dust( Position( 7, 15 )) == 6 );
REQUIRE( tested.dust( Position( 1, 15 )) == 0 );
}
TEST_CASE( "House is clean from file", "[houses]" ) {
House tested( "fixtures/house_fixture_2.house" );
tested.decreaseDust( Position( 1, 3 ) );
tested.decreaseDust( Position( 2, 3 ) );
REQUIRE( tested.dust( Position( 1, 3 ) ) == 0 );
REQUIRE( tested.dust( Position( 2, 3 ) ) == 0 );
REQUIRE( ! tested.clean() );
tested.decreaseDust( Position( 1, 4 ) );
REQUIRE( tested.dust( Position( 1, 4 ) ) == 1 );
tested.decreaseDust( Position( 1, 4 ) );
REQUIRE( tested.dust( Position( 1, 4 ) ) == 0 );
REQUIRE( ! tested.clean() );
tested.decreaseDust( Position( 2, 4 ) );
REQUIRE( tested.dust( Position( 2, 4 ) ) == 2 );
tested.decreaseDust( Position( 2, 4 ) );
REQUIRE( tested.dust( Position( 2, 4 ) ) == 1 );
tested.decreaseDust( Position( 2, 4 ) );
REQUIRE( tested.dust( Position( 2, 4 ) ) == 0 );
REQUIRE( tested.clean() );
for ( int i = 0; i < 100; ++i ) {
tested.decreaseDust( Position( 2, 4 ) );
REQUIRE( tested.dust( Position( 2, 4 ) ) == 0 );
}
}
我错过了什么吗?