Actually barrel-shift
[naja.git] / naja / tests / test_player.py
index 9939f58b31c4118953f0c0b6e5f5b6cc07dd4b66..07583b3bf11a312bdefed1c267f623b069b42c18 100644 (file)
@@ -1,6 +1,6 @@
 from unittest import TestCase
 
-from naja.constants import BITS
+from naja.constants import BITS, MOVES
 from naja.player import PlayerBits, Player
 
 
@@ -54,37 +54,53 @@ class TestPlayerBits(TestCase):
         bits = PlayerBits(0x03)
         self.assertEqual(bits.check_bits([BITS.NORTH]), True)
         self.assertEqual(bits.check_bits([BITS.NORTH, BITS.SOUTH]), True)
-        self.assertEqual(bits.check_bits([BITS.CYAN]), False)
-        self.assertEqual(bits.check_bits([BITS.CYAN, BITS.MSB]), False)
+        self.assertEqual(bits.check_bits([BITS.BLUE]), False)
+        self.assertEqual(bits.check_bits([BITS.BLUE, BITS.MSB]), False)
         self.assertEqual(
-            bits.check_bits([BITS.NORTH, BITS.SOUTH, BITS.CYAN]), False)
+            bits.check_bits([BITS.NORTH, BITS.SOUTH, BITS.BLUE]), False)
 
     def test_set_bits(self):
         bits = PlayerBits(0x03)
         self.assertEqual(bits._bits, 0x03)
-        bits.set_bits([BITS.NORTH, BITS.CYAN])
+        bits.set_bits([BITS.NORTH, BITS.BLUE])
         self.assertEqual(bits._bits, 0x13)
-        bits.set_bits([BITS.NORTH, BITS.CYAN, BITS.MSB])
+        bits.set_bits([BITS.NORTH, BITS.BLUE, BITS.MSB])
         self.assertEqual(bits._bits, 0x93)
 
     def test_clear_bits(self):
         bits = PlayerBits(0x03)
         self.assertEqual(bits._bits, 0x03)
-        bits.clear_bits([BITS.NORTH, BITS.CYAN])
+        bits.clear_bits([BITS.NORTH, BITS.BLUE])
         self.assertEqual(bits._bits, 0x02)
-        bits.clear_bits([BITS.NORTH, BITS.CYAN, BITS.MSB])
+        bits.clear_bits([BITS.NORTH, BITS.BLUE, BITS.MSB])
         self.assertEqual(bits._bits, 0x02)
 
     def test_toggle_bits(self):
         bits = PlayerBits(0x03)
         self.assertEqual(bits._bits, 0x03)
-        bits.toggle_bits([BITS.NORTH, BITS.CYAN])
+        bits.toggle_bits([BITS.NORTH, BITS.BLUE])
         self.assertEqual(bits._bits, 0x12)
-        bits.toggle_bits([BITS.NORTH, BITS.CYAN])
+        bits.toggle_bits([BITS.NORTH, BITS.BLUE])
         self.assertEqual(bits._bits, 0x03)
-        bits.toggle_bits([BITS.NORTH, BITS.CYAN, BITS.MSB])
+        bits.toggle_bits([BITS.NORTH, BITS.BLUE, BITS.MSB])
         self.assertEqual(bits._bits, 0x92)
 
+    def test_shift_bits_left(self):
+        bits = PlayerBits(0x03)
+        self.assertEqual(bits._bits, 0x03)
+        bits.shift_bits_left(1)
+        self.assertEqual(bits._bits, 0x06)
+        bits.shift_bits_left(6)
+        self.assertEqual(bits._bits, 0x81)
+
+    def test_shift_bits_right(self):
+        bits = PlayerBits(0x06)
+        self.assertEqual(bits._bits, 0x06)
+        bits.shift_bits_right(1)
+        self.assertEqual(bits._bits, 0x03)
+        bits.shift_bits_right(1)
+        self.assertEqual(bits._bits, 0x81)
+
 
 class TestPlayer(TestCase):
     def test_new_player(self):
@@ -96,64 +112,55 @@ class TestPlayer(TestCase):
         player = Player.import_player({
             'bits': 0xaa,
             'position': [1, 2],
+            'movement_mode': MOVES.CASTLE,
         })
         self.assertEqual(player.bits.bits, 0xaa)
         self.assertEqual(player.position, (1, 2))
+        self.assertEqual(player.movement_mode, MOVES.CASTLE)
 
     def test_export_player(self):
-        player = Player(0xaa, (1, 2))
+        player = Player(0xaa, (1, 2), MOVES.CASTLE)
         self.assertEqual(player.export(), {
             'bits': 0xaa,
             'position': [1, 2],
+            'movement_mode': MOVES.CASTLE,
         })
 
-    def test_move(self):
-        player = Player(0x0f, (2, 2))
-        self.assertEqual(player.move(BITS.NORTH), True)
-        self.assertEqual(player.position, (2, 1))
-
-        player = Player(0x0f, (2, 2))
-        self.assertEqual(player.move(BITS.SOUTH), True)
-        self.assertEqual(player.position, (2, 3))
-
-        player = Player(0x0f, (2, 2))
-        self.assertEqual(player.move(BITS.WEST), True)
-        self.assertEqual(player.position, (1, 2))
+    def test_export_player_default_movement_mode(self):
+        player = Player(0xaa, (1, 2))
+        self.assertEqual(player.export(), {
+            'bits': 0xaa,
+            'position': [1, 2],
+            'movement_mode': MOVES.ADJACENT,
+        })
 
+    def test_legal_moves_all_available(self):
         player = Player(0x0f, (2, 2))
-        self.assertEqual(player.move(BITS.EAST), True)
-        self.assertEqual(player.position, (3, 2))
-
-    def test_move_flags_clear(self):
-        player = Player(0x00, (2, 2))
-        self.assertEqual(player.move(BITS.NORTH), False)
-        self.assertEqual(player.position, (2, 2))
-
-        player = Player(0x00, (2, 2))
-        self.assertEqual(player.move(BITS.SOUTH), False)
-        self.assertEqual(player.position, (2, 2))
-
-        player = Player(0x00, (2, 2))
-        self.assertEqual(player.move(BITS.WEST), False)
-        self.assertEqual(player.position, (2, 2))
-
-        player = Player(0x00, (2, 2))
-        self.assertEqual(player.move(BITS.EAST), False)
-        self.assertEqual(player.position, (2, 2))
-
-    def test_move_flags_edges(self):
-        player = Player(0x0f, (2, 0))
-        self.assertEqual(player.move(BITS.NORTH), False)
-        self.assertEqual(player.position, (2, 0))
-
-        player = Player(0x0f, (2, 4))
-        self.assertEqual(player.move(BITS.SOUTH), False)
-        self.assertEqual(player.position, (2, 4))
+        self.assertEqual(
+            player.legal_moves(), [(2, 2), (2, 1), (2, 3), (3, 2), (1, 2)])
 
+    def test_legal_moves_some_unavailable(self):
         player = Player(0x0f, (0, 2))
-        self.assertEqual(player.move(BITS.WEST), False)
-        self.assertEqual(player.position, (0, 2))
-
-        player = Player(0x0f, (4, 2))
-        self.assertEqual(player.move(BITS.EAST), False)
-        self.assertEqual(player.position, (4, 2))
+        player.bits.clear_bit(BITS.NORTH)
+        self.assertEqual(player.legal_moves(), [(0, 2), (0, 3), (1, 2)])
+
+    def test_legal_moves_castle(self):
+        player = Player(0x0f, (1, 3), MOVES.CASTLE)
+        self.assertEqual(player.legal_moves(), [
+            (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 3), (3, 3),
+            (4, 3)])
+
+    def test_legal_moves_bishop(self):
+        player = Player(0x0f, (1, 3), MOVES.BISHOP)
+        self.assertEqual(player.legal_moves(), [
+            (0, 2), (0, 4), (1, 3), (2, 2), (2, 4), (3, 1), (4, 0)])
+
+    def test_legal_moves_knight(self):
+        player = Player(0x0f, (1, 3), MOVES.KNIGHT)
+        self.assertEqual(player.legal_moves(), [
+            (0, 1), (1, 3), (2, 1), (3, 2), (3, 4)])
+
+    def test_set_position(self):
+        player = Player(0x0f, (3, 3), MOVES.BISHOP)
+        player.set_position((4, 4))
+        self.assertEqual(player.movement_mode, MOVES.ADJACENT)