aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorInigoGutierrez <inigogf.95@gmail.com>2022-12-14 15:41:49 +0100
committerInigoGutierrez <inigogf.95@gmail.com>2022-12-14 15:41:49 +0100
commit824d5e3b6954407a694f5739cbeb40f66324c8d7 (patch)
tree2edd317df4a83afbb305e017f3d5afd92b6b5422 /tests
parente8b9bf589e698b51e55ae59693b5bb0293f86a26 (diff)
downloadimago-824d5e3b6954407a694f5739cbeb40f66324c8d7.tar.gz
imago-824d5e3b6954407a694f5739cbeb40f66324c8d7.zip
Developing Unit Testing.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_gameMove.py6
-rw-r--r--tests/test_gameState.py89
-rw-r--r--tests/test_imagoIO.py40
-rw-r--r--tests/test_monteCarlo.py48
4 files changed, 180 insertions, 3 deletions
diff --git a/tests/test_gameMove.py b/tests/test_gameMove.py
index 6569c5b..a7edfab 100644
--- a/tests/test_gameMove.py
+++ b/tests/test_gameMove.py
@@ -18,13 +18,13 @@ class TestGameMove(unittest.TestCase):
self.assertIsNone(firstMove.coords)
- secondMove = firstMove.addMove(1, 2)
+ secondMove = firstMove.addMoveByCoords(1, 2)
self.assertIsNone(firstMove.coords)
self.assertEqual(secondMove.coords[0], 1)
self.assertEqual(secondMove.coords[1], 2)
- thirdMove = secondMove.addMove(5, 7)
+ thirdMove = secondMove.addMoveByCoords(5, 7)
self.assertIsNone(firstMove.coords)
self.assertIsNone(thirdMove.previousMove.previousMove.coords)
@@ -66,7 +66,7 @@ class TestGameMove(unittest.TestCase):
(2,0), (2,1), (2,2)))
)
- secondMove = firstMove.addMove(1, 2)
+ secondMove = firstMove.addMoveByCoords(1, 2)
self.assertSetEqual(
secondMove.getPlayableVertices(),
set(((0,0), (0,1), (0,2),
diff --git a/tests/test_gameState.py b/tests/test_gameState.py
new file mode 100644
index 0000000..638e269
--- /dev/null
+++ b/tests/test_gameState.py
@@ -0,0 +1,89 @@
+"""Tests for the input/output component."""
+
+import unittest
+
+from imago.data.enums import Player
+from imago.gameLogic.gameState import GameState
+
+class TestGameState(unittest.TestCase):
+ """Test GameState component."""
+
+ def testCurrentPlayer(self):
+ """Test simple commands."""
+ size = 9
+ state = GameState(size)
+
+ self.assertEqual(state.getCurrentPlayer(), Player.BLACK)
+ self.assertEqual(state.getPlayerCode(), 'B')
+
+ state.playMove(0, 0)
+ self.assertEqual(state.getCurrentPlayer(), Player.WHITE)
+ self.assertEqual(state.getPlayerCode(), 'W')
+
+ def testPlays(self):
+ """Test simple commands."""
+ size = 3
+ state = GameState(size)
+
+ self.assertEqual(state.getBoard().getBoard(),
+ [
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY],
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY],
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY]
+ ])
+
+ state.playMove(1, 1)
+ self.assertEqual(state.getBoard().getBoard(),
+ [
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY],
+ [Player.EMPTY, Player.BLACK, Player.EMPTY],
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY]
+ ])
+
+ state.playPass()
+ self.assertEqual(state.getBoard().getBoard(),
+ [
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY],
+ [Player.EMPTY, Player.BLACK, Player.EMPTY],
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY]
+ ])
+
+ state.undo()
+ self.assertEqual(state.getBoard().getBoard(),
+ [
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY],
+ [Player.EMPTY, Player.BLACK, Player.EMPTY],
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY]
+ ])
+
+ state.undo()
+ self.assertEqual(state.getBoard().getBoard(),
+ [
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY],
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY],
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY]
+ ])
+
+ state.passForPlayer(Player.WHITE)
+ self.assertEqual(state.getBoard().getBoard(),
+ [
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY],
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY],
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY]
+ ])
+
+
+ self.assertFalse(state.playMove(-1, -1))
+ self.assertEqual(state.getBoard().getBoard(),
+ [
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY],
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY],
+ [Player.EMPTY, Player.EMPTY, Player.EMPTY]
+ ])
+
+ state.lastMove = None
+ self.assertEqual(state.getCurrentPlayer(), Player.BLACK)
+ self.assertRaises(RuntimeError, state.playMove, 0, 0)
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/test_imagoIO.py b/tests/test_imagoIO.py
new file mode 100644
index 0000000..c3c6fda
--- /dev/null
+++ b/tests/test_imagoIO.py
@@ -0,0 +1,40 @@
+"""Tests for the input/output component."""
+
+import unittest
+
+import io
+import sys
+
+from imago.engine.imagoIO import ImagoIO
+
+class TestImagoIO(unittest.TestCase):
+ """Test ImagoIO component."""
+
+ @unittest.mock.patch('imago.engine.imagoIO.input', create=True)
+
+ def testSimpleCommands(self, mocked_input):
+ """Test simple commands."""
+
+ mocked_input.side_effect = [
+ 'name\n',
+ 'version\n',
+ 'protocol_version\n',
+ 'quit\n'
+ ]
+
+ testout = io.StringIO()
+ imagoIO = ImagoIO(outputStream=testout)
+
+ imagoIO.start()
+ value = testout.getvalue()
+ self.assertEqual(
+ '= Imago\n\n' +
+ '= 0.0.0\n\n' +
+ '= 2\n\n',
+ value
+ )
+
+ testout.close()
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/test_monteCarlo.py b/tests/test_monteCarlo.py
index b217cf9..496c073 100644
--- a/tests/test_monteCarlo.py
+++ b/tests/test_monteCarlo.py
@@ -2,6 +2,7 @@
import unittest
+from imago.engine.decisionAlgorithm import DecisionAlgorithm
from imago.gameLogic.gameState import GameState
from imago.engine.monteCarlo import MCTS
from imago.engine.monteCarlo import MCTSNode
@@ -17,6 +18,53 @@ class TestMonteCarlo(unittest.TestCase):
tree = MCTS(state.lastMove)
#print(tree.pickMove().toString())
+ def testForceNextMove(self):
+ """Test forcing next move."""
+
+ # Next move before expansion (no children nodes)
+ state = GameState(TEST_BOARD_SIZE)
+ tree = MCTS(state.lastMove)
+ self.assertEqual(set(), tree.root.children)
+ tree.forceNextMove((0, 1))
+ self.assertEqual(set(), tree.root.children)
+
+ # Next move after expansion (with children nodes)
+ tree.expansions = 2
+ tree.simulationsPerExpansion = 2
+ tree.pickMove()
+ self.assertEqual(tree.expansions, len(tree.root.children))
+ nextMoveCoords = list(tree.root.children)[0].move.coords
+ tree.forceNextMove(nextMoveCoords)
+
+ def testPass(self):
+ """Test passing as next move."""
+ state = GameState(TEST_BOARD_SIZE)
+ tree = MCTS(state.lastMove)
+ self.assertFalse(tree.root.move.isPass)
+ tree.forceNextMove("pass")
+ self.assertTrue(tree.root.move.isPass)
+
+ def testClearBoard(self):
+ """Test clearing board returns root to original and retains information."""
+ state = GameState(TEST_BOARD_SIZE)
+ tree = MCTS(state.lastMove)
+
+ firstMoveCoords = (0,0)
+ secondMoveCoords = (1,0)
+ thirdMoveCoords = (0,1)
+
+ tree.forceNextMove(firstMoveCoords)
+ tree.forceNextMove(secondMoveCoords)
+ tree.forceNextMove(thirdMoveCoords)
+ tree.clearBoard()
+
+ nextNode = list(tree.root.children)[0]
+ self.assertEqual(firstMoveCoords, nextNode.move.coords)
+ nextNode = list(nextNode.children)[0]
+ self.assertEqual(secondMoveCoords, nextNode.move.coords)
+ nextNode = list(nextNode.children)[0]
+ self.assertEqual(thirdMoveCoords, nextNode.move.coords)
+
#def testSimulation(self):
# """Test calculation of group liberties."""
# board = GameBoard(TEST_BOARD_SIZE, TEST_BOARD_SIZE)