ttrts/test/test.cpp

204 lines
5 KiB
C++
Raw Normal View History

#include <iostream> // std::cout
2014-12-16 13:12:57 +00:00
#include "board.h"
#include "orders.h"
2014-12-16 13:13:00 +00:00
#include "game.h"
2014-12-16 13:12:57 +00:00
const char* tests()
{
2014-12-16 13:13:02 +00:00
// Test if we can properly set a unit's visual
{
CUnit unit;
unit.setFromVisual('v');
2014-12-16 13:13:02 +00:00
if( unit.getVisual() != 'v' )
return "failed to properly create V unit";
}
2014-12-16 13:13:02 +00:00
// Test unique unit IDs
2014-12-16 13:13:00 +00:00
{
CUnit unit;
CUnit unit2;
if( unit.getID() == unit2.getID() )
2014-12-16 13:13:00 +00:00
return "Unit IDs the same";
}
// Test basic invalid unit conversion
{
CUnit unit1;
std::string unit1Desc = GetStringFromUnit(unit1);
CUnit unit2 = GetUnitFromString(unit1Desc);
if ( unit1 != unit2 )
return "Failed to convert an empty unit to string and back";
}
// Test custom unit conversion
{
CUnit unit1;
unit1.setFromVisual('v');
unit1.setPlayer(0);
unit1.setTeam(Team::Green);
unit1.setPos( uvector2(5,10) );
std::string unit1Desc = GetStringFromUnit(unit1);
CUnit unit2 = GetUnitFromString(unit1Desc);
if ( unit1 != unit2 )
return "Failed to convert custom unit to string and back";
}
2014-12-16 13:13:02 +00:00
// Test if we can successfully create a unit from a visual
{
CUnit unit = GetUnitFromVis('v');
if( unit.getVisual() != 'v' )
2014-12-16 13:13:01 +00:00
return "failed to properly create V unit with factory";
2014-12-16 13:12:57 +00:00
}
2014-12-16 13:13:02 +00:00
// Test if we can successfully convert orders back and forth
2014-12-16 13:12:57 +00:00
{
COrder order;
order.command = command_c::F;
2014-12-16 13:12:57 +00:00
order.unit = 10;
std::string order_string = GetStringFromOrder(order);
COrder order2 = GetOrderFromString(order_string);
if ( order2 != order )
return "failed order string conversion test";
}
2014-12-16 13:13:02 +00:00
// Test of the game can logically handle a blank game
2014-12-16 13:13:00 +00:00
{
CTTRTSGame game( 15, 10 );
if( game.SimulateToNextTurn() )
return "Failed to simulate a blank game";
if( game.GetNumUnits() )
return "Game started with non-zero unit number";
}
2014-12-16 13:13:02 +00:00
// Test if the game correctly rejects units placed ontop of others
{
CTTRTSGame game( 5, 5 );
{
CUnit unit = GetUnitFromVis('^');
unit.setPos( {2,2} );
unit.setPlayer(0);
unit.setTeam(Team::Red);
game.AddUnit(std::move(unit));
}
{
CUnit unit = GetUnitFromVis('^');
unit.setPos( {2,2} );
unit.setPlayer(0);
unit.setTeam(Team::Red);
if( !game.AddUnit(std::move(unit)) )
return "Game should have rejected unit placed on the same spot";
if( game.GetNumUnits() != 1 )
return "Game ended up with too many units";
}
}
2014-12-16 13:13:00 +00:00
2014-12-16 13:13:02 +00:00
// Test on a small board if a movement command succeeds correctly
{
CTTRTSGame game( 5, 5 );
CUnit unit = GetUnitFromVis('>');
const unit_id_t id = unit.getID();
COrder order;
unit.setPos( {2,2} );
unit.setPlayer(0);
unit.setTeam(Team::Red);
2014-12-16 13:13:01 +00:00
if ( game.AddUnit(std::move(unit)) )
return "Game failed to add valid unit";
order.unit = id;
order.command = command_c::F;
2014-12-16 13:13:01 +00:00
if( game.IssueOrder(0,order) )
return "Game failed to issue valid order";
2014-12-16 13:13:01 +00:00
if (game.SimulateToNextTurn() )
return "Game failed to simulate valid turn";
if( game.GetUnitByIDConst(id).getPos() != uvector2{3,2} )
return "Simple movement order failed";
2014-12-16 13:13:01 +00:00
}
2014-12-16 13:13:02 +00:00
// Test on a tiny board, whether a unit can correctly attack another
2014-12-16 13:13:01 +00:00
{
CTTRTSGame game( 2, 1 );
2014-12-16 13:13:01 +00:00
unit_id_t id;
2014-12-16 13:13:01 +00:00
{
CUnit unit = GetUnitFromVis('>');
id = unit.getID();
COrder order;
unit.setPos( {0,0} );
unit.setPlayer(1);
unit.setTeam(Team::Blue);
2014-12-16 13:13:01 +00:00
if ( game.AddUnit(std::move(unit)) )
return "Game failed to add valid unit";
order.unit = id;
order.command = command_c::A;
2014-12-16 13:13:01 +00:00
if( game.IssueOrder(0,order) )
return "Game failed to issue valid order";
}
2014-12-16 13:13:01 +00:00
{
CUnit unit = GetUnitFromVis('<');
2014-12-16 13:13:01 +00:00
unit.setPos( {1,0} );
unit.setPlayer(2);
unit.setTeam(Team::Red);
2014-12-16 13:13:01 +00:00
if ( game.AddUnit(std::move(unit)) )
return "Game failed to add valid unit";
2014-12-16 13:13:01 +00:00
}
game.SimulateToNextTurn();
if ( game.GetNumUnits() != 1 )
return "Game failed to kill a unit when it logically should have";
2014-12-16 13:13:01 +00:00
if ( game.GetUnitByIndex(0).getDir() != dir_t::E )
return "Game killed the wrong unit";
if ( game.GetUnitByIndex(0).getID() != id )
return "Game killed the wrong unit";
if ( game.CheckForWin() != Team::Blue )
return "Game failed to recognise a win for the right Team";
}
2014-12-16 13:13:02 +00:00
return 0;
}
// Main program entry point
int main()
{
std::cout<<"Running tests"<<std::endl;
const char* res = tests();
if( res )
{
std::cout<<"Tests failed - "<<res<<std::endl;
return -1;
}
std::cout<<"Tests succeeded"<<std::endl;
return 0;
};