0

My test/ folder has these files

test/SimulationTest.cpp
test/main.cpp
test/HouseTest.cpp

the main.cpp file has only this, as per Catch instructions.

#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"

When I comment out all code in one of the tests, I can compile everything into one big executable and it works. But if I leave the code as it is - the resulting executables consumes all the memory on my machine and it hangs.

I compile the tests like so:

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

Here are the tests (never mind the code that implements them):

test one:

#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
}

test two:

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

Am I missing something?

kebs
  • 6,387
  • 4
  • 41
  • 70
  • So it compiles but the resulting program hangs? Have you used a debugger? – wonce Apr 09 '16 at 16:49
  • I'm not proficient with C++ debuggers - I know that it just takes up all the RAM on the machine, so I'm hoping for someone that has seen this exact thing to answer (if they solved it). I'm still at the start so I'm willing to switch to another framework if I don't find an easy fix. – Moran Netser Apr 09 '16 at 17:03

0 Answers0