Fix a few more bugs and implement more tests

Bug in infinite recursion on move assignment operator.
Bug in adding the wron units ID to the kill list.
This commit is contained in:
Marc Di Luzio 2014-12-16 13:13:02 +00:00
parent 9b00030039
commit 7e2fd0a8d1
5 changed files with 71 additions and 17 deletions

View file

@ -141,7 +141,7 @@ int CTTRTSGame::SimulateToNextTurn()
// if the unit is infront of our unit, then add it to the kill list
if( pair2.unit.getPos() == infront )
{
toKill.push_back(pair.unit.getID());
toKill.push_back(pair2.unit.getID());
pair.order = COrder();
break;
}

View file

@ -29,7 +29,7 @@ struct OrderUnitPair
}
// Move asignment operator
inline OrderUnitPair& operator=( OrderUnitPair&& rhs ) { *this = std::move(rhs); return *this; }
inline OrderUnitPair& operator=( OrderUnitPair&& rhs ) { this->unit = std::move(rhs.unit);this->order = rhs.order;rhs.order = COrder(); return *this; }
CUnit unit;
COrder order;

View file

@ -53,6 +53,18 @@ CUnit::CUnit(CUnit&& unit)
updateMyVisual();
}
CUnit& CUnit::operator=(CUnit&& unit)
{
unit_id = std::move(unit.unit_id) ;
team_id = std::move(unit.team_id) ;
player_id = std::move(unit.player_id) ;
unit_vis = std::move(unit.unit_vis) ;
dir = std::move(unit.dir) ;
pos = std::move(unit.pos) ;
return *this;
}
CUnit CUnit::getUnitFromVis( unitVis_c vis )
{
CUnit unit;

View file

@ -14,7 +14,7 @@ public:
CUnit();
CUnit(CUnit&& unit);
CUnit& operator=(CUnit&& unit) { *this = std::move(unit); return *this; }
CUnit& operator=(CUnit&& unit);
~CUnit() = default;
inline const unit_id_t& getID() const { return unit_id; }
@ -58,7 +58,7 @@ private:
unitVis_c updateMyVisual();
// Unit ID
const unit_id_t unit_id;
unit_id_t unit_id;
// Visual
unitVis_c unit_vis;

View file

@ -72,6 +72,30 @@ const char* tests()
return "Game started with non-zero unit number";
}
{
CTTRTSGame game( 5, 5 );
{
CUnit unit = CUnit::getUnitFromVis('^');
unit.setPos( {2,2} );
unit.setPlayer(0);
game.AddUnit(std::move(unit));
}
{
CUnit unit = CUnit::getUnitFromVis('^');
unit.setPos( {2,2} );
unit.setPlayer(0);
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";
}
}
{
CTTRTSGame game( 5, 5 );
@ -100,31 +124,49 @@ const char* tests()
}
{
CTTRTSGame game( 5, 5 );
CTTRTSGame game( 2, 1 );
unit_id_t id;
{
CUnit unit = CUnit::getUnitFromVis('^');
unit.setPos( {2,2} );
CUnit unit = CUnit::getUnitFromVis('>');
id = unit.getID();
COrder order;
unit.setPos( {0,0} );
unit.setPlayer(0);
game.AddUnit(std::move(unit));
}
if ( game.AddUnit(std::move(unit)) )
return "Game failed to add valid unit";
order.unit = id;
order.order = order_c::A;
if( game.IssueOrder(0,order) )
return "Game failed to issue valid order";
}
{
CUnit unit = CUnit::getUnitFromVis('^');
unit.setPos( {2,2} );
unit.setPlayer(0);
CUnit unit = CUnit::getUnitFromVis('<');
if( !game.AddUnit(std::move(unit)) )
return "Game should have rejected unit placed on the same spot";
unit.setPos( {1,0} );
unit.setPlayer(1);
if( game.GetNumUnits() != 1 )
return "Game ended up with too many units";
if ( game.AddUnit(std::move(unit)) )
return "Game failed to add valid unit";
}
game.SimulateToNextTurn();
if ( game.GetNumUnits() != 1 )
return "Game failed to kill a unit when it logically should have";
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";
}
return nullptr;
}