Мой тест / папка содержит эти файлы
test/SimulationTest.cpp
test/main.cpp
test/HouseTest.cpp
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 );
}
}
Я что-то пропустил?
Задача ещё не решена.
Других решений пока нет …