From 7e2fd0a8d1b3cfb985450d56d569eab96b2fb56c Mon Sep 17 00:00:00 2001
From: Marc Di Luzio <mdiluzio@feralinteractive.com>
Date: Tue, 16 Dec 2014 13:13:02 +0000
Subject: [PATCH] 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.
---
 game/game.cpp |  2 +-
 game/game.h   |  2 +-
 game/unit.cpp | 12 +++++++++
 game/unit.h   |  4 +--
 test/test.cpp | 68 +++++++++++++++++++++++++++++++++++++++++----------
 5 files changed, 71 insertions(+), 17 deletions(-)

diff --git a/game/game.cpp b/game/game.cpp
index 8cf47d3..aff5fb8 100644
--- a/game/game.cpp
+++ b/game/game.cpp
@@ -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;
                     }
diff --git a/game/game.h b/game/game.h
index 2ea176a..e774867 100644
--- a/game/game.h
+++ b/game/game.h
@@ -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;
diff --git a/game/unit.cpp b/game/unit.cpp
index d70d0a7..3d8af12 100644
--- a/game/unit.cpp
+++ b/game/unit.cpp
@@ -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;
diff --git a/game/unit.h b/game/unit.h
index ebd5852..41d157a 100644
--- a/game/unit.h
+++ b/game/unit.h
@@ -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;
diff --git a/test/test.cpp b/test/test.cpp
index beb65bd..96ac285 100644
--- a/test/test.cpp
+++ b/test/test.cpp
@@ -70,7 +70,31 @@ const char* tests()
 
         if( game.GetNumUnits() )
             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;
 }