From 6c2cc134abf3f32d1d6ec172c6201f8d990c88ab Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sun, 24 Aug 2014 12:52:10 +0200 Subject: Initial commit --- qml/python/tests/__init__.py | 3 + qml/python/tests/test.sgf | 13 +++ qml/python/tests/test2.sgf | 6 ++ qml/python/tests/test_game.py | 101 +++++++++++++++++++++++ qml/python/tests/test_transformations.py | 134 +++++++++++++++++++++++++++++++ 5 files changed, 257 insertions(+) create mode 100644 qml/python/tests/__init__.py create mode 100644 qml/python/tests/test.sgf create mode 100644 qml/python/tests/test2.sgf create mode 100644 qml/python/tests/test_game.py create mode 100644 qml/python/tests/test_transformations.py (limited to 'qml/python/tests') diff --git a/qml/python/tests/__init__.py b/qml/python/tests/__init__.py new file mode 100644 index 0000000..7847780 --- /dev/null +++ b/qml/python/tests/__init__.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + diff --git a/qml/python/tests/test.sgf b/qml/python/tests/test.sgf new file mode 100644 index 0000000..bdbe898 --- /dev/null +++ b/qml/python/tests/test.sgf @@ -0,0 +1,13 @@ +(;GM[1]FF[3] +;AW[ro][nq][oq][pq][qq][rq][mr] +AB[or][pr][qr][rr][sr] + (;W[os] + (;B[ps];W[rs];B[ns];W[nr]) + (;B[ns];W[nr];B[rs];W[ps];B[qs];W[os]) + ) +(;W[rs] + (;B[os];W[qs]) + (;B[qs];W[os];B[nr];W[ns]) +) +) + diff --git a/qml/python/tests/test2.sgf b/qml/python/tests/test2.sgf new file mode 100644 index 0000000..a11356c --- /dev/null +++ b/qml/python/tests/test2.sgf @@ -0,0 +1,6 @@ +(;GM[1]FF[3] +;AW[qn][mp][qp][rp][kq][mq][oq][pq][nr][pr][rs] +AB[nn][mo][np][op][pp][nq][qq][rq][qr][sr][qs] +(;B[or];W[os];B[ps];W[or];B[mr]) +(;B[ps]WV[ps];W[or];B[mr];W[lr]) +) diff --git a/qml/python/tests/test_game.py b/qml/python/tests/test_game.py new file mode 100644 index 0000000..de43909 --- /dev/null +++ b/qml/python/tests/test_game.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import unittest +from python import sgfparser, game + +def create_board(fic): + with open("python/tests/%s" % fic) as f: + cursor = sgfparser.Cursor(f.read()) + + return game.Game(cursor) + +class TestBoard(unittest.TestCase): + """ Test for the board management. + """ + + def testConvCoord(self): + """ Test the conversion in the coordinates system. + """ + + self.assertEqual((1,2),game.Game.conv_coord("ab")) + self.assertEqual((1,1),game.Game.conv_coord("aa")) + + def test_createTree(self): + + def fun(pos): + return pos + + with open("python/tests/test.sgf") as f: + cursor = sgfparser.Cursor(f.read()) + + cursor.next() + + expected_tee = \ + [{'AB': ['or', 'pr', 'qr', 'rr', 'sr'], 'AW': ['ro', 'nq', 'oq', 'pq', 'qq', 'rq', 'mr']}, + [ + [{'W': ['os']}, + [ + [{'B': ['ps']}, {'W': ['rs']}, {'B': ['ns']}, {'W': ['nr']}], + [{'B': ['ns']}, {'W': ['nr']}, {'B': ['rs']}, {'W': ['ps']}, {'B': ['qs']}, {'W': ['os']}] + ] + ], + [{'W': ['rs']}, + [ + [{'B': ['os']}, {'W': ['qs']}], + [{'B': ['qs']}, {'W': ['os']}, {'B': ['nr']}, {'W': ['ns']}] + ] + ] + ] + ] + self.assertEqual(expected_tee, game.Game.create_tree(cursor, fun)) + + def test_init(self): + + def fun(pos): + return (0,0) + currentgame = create_board("test.sgf") + + self.assertEqual(11, currentgame.min_x) + self.assertEqual(13, currentgame.min_y) + self.assertEqual(19, currentgame.max_x) + self.assertEqual(19, currentgame.max_y) + + self.assertEqual((9, 7), currentgame.get_size()) + + # There is only 2 state : initial board, and 2 possibilities. + self.assertEqual(2, len(currentgame.tree)) + + self.assertTrue(currentgame.side['TOP']) + self.assertTrue(currentgame.side['LEFT']) + self.assertFalse(currentgame.side['BOTTOM']) + self.assertFalse(currentgame.side['RIGHT']) + + currentgame.normalize() + + def test_level2(self): + + def fun(pos): + return (0,0) + currentgame = create_board("test2.sgf") + + print(currentgame.tree) + + currentgame.normalize() + + self.assertEqual(11, currentgame.min_x) + self.assertEqual(13, currentgame.min_y) + self.assertEqual(19, currentgame.max_x) + self.assertEqual(19, currentgame.max_y) + + self.assertEqual((9, 7), currentgame.get_size()) + + # There is only 2 state : initial board, and 2 possibilities. + self.assertEqual(2, len(currentgame.tree)) + + self.assertTrue(currentgame.side['TOP']) + self.assertTrue(currentgame.side['LEFT']) + self.assertFalse(currentgame.side['BOTTOM']) + self.assertFalse(currentgame.side['RIGHT']) + + currentgame.normalize() diff --git a/qml/python/tests/test_transformations.py b/qml/python/tests/test_transformations.py new file mode 100644 index 0000000..b802b9f --- /dev/null +++ b/qml/python/tests/test_transformations.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest + +from python.transformations import Rotation, Translation, Symmetry + +class FakeBoard(): + + def __init__(self, min_x, min_y, max_x, max_y): + self.min_x = min_x + self.min_y = min_y + self.max_x = max_x + self.max_y = max_y + + def get_size(self): + x_size = self.max_x - self.min_x + y_size = self.max_y - self.min_y + return min(19, x_size), min(19, y_size) + +class TestRotation(unittest.TestCase): + """ Test the rotation transformation + """ + + def test_apply_points(self): + """ Test the points rotation. + """ + rotation = Rotation(FakeBoard(2, 1, 8, 5)) + self.assertEqual((4, 2), rotation.apply_points((2, 1))) + + rotation = Rotation(FakeBoard(0, 0, 6, 4)) + self.assertEqual((4, 0), rotation.apply_points((0, 0))) + self.assertEqual((4, 3), rotation.apply_points((3, 0))) + self.assertEqual((1, 0), rotation.apply_points((0, 3))) + self.assertEqual((1, 3), rotation.apply_points((3, 3))) + + rotation = Rotation(FakeBoard(1, 1, 6, 4)) + self.assertEqual((4, 1), rotation.apply_points((1, 0))) + + + + def test_get_new_size(self): + """ Test the goban size rotation. + """ + rotation = Rotation(FakeBoard(2, 1, 8, 5)) + self.assertEqual((0, 2, 4, 8), rotation.get_new_size()) + + rotation = Rotation(FakeBoard(0, 0, 6, 4)) + self.assertEqual((0, 0, 4, 6), rotation.get_new_size()) + + def test_is_valid(self): + """ Test the is_valid method. + """ + + # Do not rotate if height > width + rotation = Rotation(FakeBoard(0, 0, 6, 4)) + self.assertTrue(rotation.is_valid()) + + # Always rotate if width > height + rotation = Rotation(FakeBoard(0, 0, 4, 6)) + self.assertFalse(rotation.is_valid()) + + # May rotate if width = height (not tested here…) + +class TestTranslation(unittest.TestCase): + """ Test the translation transformation. + """ + + def test_apply_points(self): + """ Test the points translation. + """ + translation = Translation(FakeBoard(2, 1, 8, 5)) + self.assertEqual((0, 0), translation.apply_points((2, 1))) + self.assertEqual((6, 4), translation.apply_points((8, 5))) + + def test_get_new_size(self): + """ Test the goban size translation. + """ + translation = Translation(FakeBoard(2, 1, 8, 5)) + + self.assertEqual((0, 0, 6, 4), translation.get_new_size()) + + def test_is_valid(self): + """ Test the is_valid method. + """ + + translation = Translation(FakeBoard(1, 0, 6, 4)) + self.assertTrue(translation.is_valid()) + + translation = Translation(FakeBoard(0, 1, 6, 4)) + self.assertTrue(translation.is_valid()) + + translation = Translation(FakeBoard(0, 0, 4, 6)) + self.assertFalse(translation.is_valid()) + +class TestRotation(unittest.TestCase): + """ Test the simetry transformation. + """ + + def test_apply_points(self): + """ Test the points Symmetry. + """ + symmetry = Symmetry(FakeBoard(2, 1, 8, 5)) + symmetry.x_flip = True + symmetry.y_flip = False + self.assertEqual((8, 1), symmetry.apply_points((2, 1))) + self.assertEqual((2, 5), symmetry.apply_points((8, 5))) + + symmetry.x_flip = False + symmetry.y_flip = True + self.assertEqual((2, 5), symmetry.apply_points((2, 1))) + self.assertEqual((8, 1), symmetry.apply_points((8, 5))) + + def test_get_new_size(self): + """ Test the goban size Symmetry. + """ + symmetry = Symmetry(FakeBoard(2, 1, 8, 5)) + + self.assertEqual((2, 1, 8, 5), symmetry.get_new_size()) + + def test_is_valid(self): + """ Test the is_valid method. + """ + + symmetry = Symmetry(FakeBoard(1, 0, 6, 4)) + symmetry.x_flip = True + self.assertTrue(symmetry.is_valid()) + + + symmetry = Symmetry(FakeBoard(0, 0, 4, 6)) + symmetry.x_flip = False + symmetry.y_flip = False + self.assertFalse(symmetry.is_valid()) + -- cgit v1.2.3