Browse Source

Added test coverage on GameOfLifeLibrary

Ivan Arkhipov 6 years ago
parent
commit
d87d115013
2 changed files with 142 additions and 5 deletions
  1. 13 5
      GameOfLifeLibrary.py
  2. 129 0
      GameOfLifeLibrary_test.py

+ 13 - 5
GameOfLifeLibrary.py

@@ -1,5 +1,8 @@
 class GameOfLife:
     def __init__(self, sizex, sizey):
+        assert sizex > 0
+        assert sizey > 0
+
         self.sizex = sizex
         self.sizey = sizey
         self.cells = [[NoUnit(i, j) for j in range(sizey)] for i in range(sizex)]
@@ -77,9 +80,6 @@ class Unit:
     def should_be_killed(self, neighbours):
         return False
 
-    def is_empty(self):
-        return self.type == 'NoUnit' or self.type == 'Unit'
-
     def can_spawn_shrimp(self, neighbours):
         return False
 
@@ -92,6 +92,9 @@ class Unit:
     def __repr__(self):
         return self.type + '(' + str(self.posx) + ',' + str(self.posy) + ')'
 
+    def __eq__(self, other):
+        return self.type == other.type and self.posx == other.posx and self.posy == other.posy
+
 
 class NoUnit(Unit):
     def __init__(self, posx, posy):
@@ -107,7 +110,6 @@ class NoUnit(Unit):
         return ns_num == 3
 
 
-
 class FishUnit(Unit):
     def __init__(self, posx, posy):
         super().__init__(posx, posy)
@@ -117,6 +119,12 @@ class FishUnit(Unit):
         ns_num = sum(1 if neighbour.type == 'FishUnit' else 0 for neighbour in neighbours)
         return ns_num < 2 or ns_num >= 4
 
+    def __cmp__(self, other):
+        if self.type == other.type and self.posx == other.posx and self.posy == other.posy:
+            return 0
+        else:
+            return 1
+
 
 class ShrimpUnit(Unit):
     def __init__(self, posx, posy):
@@ -134,4 +142,4 @@ class RockUnit(Unit):
         self.type = 'RockUnit'
 
     def should_be_killed(self, neighbours):
-        return False
+        return False

+ 129 - 0
GameOfLifeLibrary_test.py

@@ -0,0 +1,129 @@
+import unittest
+from GameOfLifeLibrary import *
+
+
+class FishTestCase(unittest.TestCase):
+    def test_fish_removal_1(self):
+        nbs = [FishUnit(0, 0)] * 1 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
+        fish = FishUnit(0, 0)
+        self.assertTrue(fish.should_be_killed(nbs))
+
+    def test_fish_removal_2(self):
+        nbs = [FishUnit(0, 0)] * 2 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
+        fish = FishUnit(0, 0)
+        self.assertFalse(fish.should_be_killed(nbs))
+
+    def test_fish_removal_3(self):
+        nbs = [FishUnit(0, 0)] * 3 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
+        fish = FishUnit(0, 0)
+        self.assertFalse(fish.should_be_killed(nbs))
+
+    def test_fish_removal_4(self):
+        nbs = [FishUnit(0, 0)] * 4 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
+        fish = FishUnit(0, 0)
+        self.assertTrue(fish.should_be_killed(nbs))
+
+    def test_fish_creation_1(self):
+        nbs = [FishUnit(0, 0)] * 2 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
+        unit = NoUnit(0, 0)
+        self.assertFalse(unit.can_spawn_fish(nbs))
+
+    def test_fish_creation_2(self):
+        nbs = [FishUnit(0, 0)] * 3 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
+        unit = NoUnit(0, 0)
+        self.assertTrue(unit.can_spawn_fish(nbs))
+
+    def test_fish_creation_3(self):
+        nbs = [FishUnit(0, 0)] * 4 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
+        unit = NoUnit(0, 0)
+        self.assertFalse(unit.can_spawn_fish(nbs))
+
+
+class ShrimpTestCase(unittest.TestCase):
+    def test_shrimp_removal_1(self):
+        nbs = [ShrimpUnit(0, 0)] * 1 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
+        shrimp = ShrimpUnit(0, 0)
+        self.assertTrue(shrimp.should_be_killed(nbs))
+
+    def test_shrimp_removal_2(self):
+        nbs = [ShrimpUnit(0, 0)] * 2 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
+        shrimp = ShrimpUnit(0, 0)
+        self.assertFalse(shrimp.should_be_killed(nbs))
+
+    def test_shrimp_removal_3(self):
+        nbs = [ShrimpUnit(0, 0)] * 3 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
+        shrimp = ShrimpUnit(0, 0)
+        self.assertFalse(shrimp.should_be_killed(nbs))
+
+
+    def test_shrimp_removal_4(self):
+        nbs = [ShrimpUnit(0, 0)] * 4 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
+        shrimp = ShrimpUnit(0, 0)
+        self.assertTrue(shrimp.should_be_killed(nbs))
+
+    def test_shrimp_creation_1(self):
+        nbs = [ShrimpUnit(0, 0)] * 2 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
+        unit = NoUnit(0, 0)
+        self.assertFalse(unit.can_spawn_shrimp(nbs))
+
+    def test_shrimp_creation_2(self):
+        nbs = [ShrimpUnit(0, 0)] * 3 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
+        unit = NoUnit(0, 0)
+        self.assertTrue(unit.can_spawn_shrimp(nbs))
+
+    def test_shrimp_creation_3(self):
+        nbs = [ShrimpUnit(0, 0)] * 4 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
+        unit = NoUnit(0, 0)
+        self.assertFalse(unit.can_spawn_shrimp(nbs))
+
+
+class GameOfLifeClassTestCase(unittest.TestCase):
+    def test_init_1(self):
+        game = GameOfLife(3, 3)
+        game.parse_field(['nfr', 'fff', 'sfs'])
+        result = [[NoUnit(0, 0), FishUnit(0, 1), RockUnit(0, 2)],
+                  [FishUnit(1, 0), FishUnit(1, 1), FishUnit(1, 2)],
+                  [ShrimpUnit(2, 0), FishUnit(2, 1), ShrimpUnit(2, 2)]]
+        self.assertListEqual(game.cells, result)
+
+    def test_init_2(self):
+        game = GameOfLife(3, 4)
+        game.parse_field(['nfrr', 'fffr', 'sfsr'])
+        result = [[NoUnit(0, 0), FishUnit(0, 1), RockUnit(0, 2), RockUnit(0, 3)],
+                  [FishUnit(1, 0), FishUnit(1, 1), FishUnit(1, 2), RockUnit(1, 3)],
+                  [ShrimpUnit(2, 0), FishUnit(2, 1), ShrimpUnit(2, 2), RockUnit(2, 3)]]
+        self.assertListEqual(game.cells, result)
+
+    def test_init_3(self):
+        game = GameOfLife(4, 3)
+        game.parse_field(['nfr', 'fff', 'sfs', 'ssf'])
+        result = [[NoUnit(0, 0), FishUnit(0, 1), RockUnit(0, 2)],
+                  [FishUnit(1, 0), FishUnit(1, 1), FishUnit(1, 2)],
+                  [ShrimpUnit(2, 0), FishUnit(2, 1), ShrimpUnit(2, 2)],
+                  [ShrimpUnit(3, 0), ShrimpUnit(3, 1), FishUnit(3, 2)]]
+        self.assertListEqual(game.cells, result)
+
+    def test_iterate_1(self):
+        game = GameOfLife(3, 3)
+        game.parse_field(['nff', 'fff', 'fff'])
+        game.iterate()
+        result = [[FishUnit(0, 0), NoUnit(0, 1), FishUnit(0, 2)],
+                  [NoUnit(1, 0), NoUnit(1, 1), NoUnit(1, 2)],
+                  [FishUnit(2, 0), NoUnit(2, 1), FishUnit(2, 2)]]
+        self.assertListEqual(game.cells, result)
+
+    def test_iterate_2(self):
+        game = GameOfLife(5, 5)
+        game.parse_field(['nffss', 'ffrrs', 'fffss', 'ssnss', 'frnnf'])
+        game.iterate()
+        game.iterate()
+        result = [[NoUnit(0, 0), FishUnit(0, 1), NoUnit(0, 2), NoUnit(0, 3), NoUnit(0, 4)],
+                  [FishUnit(1, 0), NoUnit(1, 1), RockUnit(1, 2), RockUnit(1, 3), NoUnit(1, 4)],
+                  [NoUnit(2, 0), NoUnit(2, 1), NoUnit(2, 2), ShrimpUnit(2, 3), NoUnit(2, 4)],
+                  [NoUnit(3, 0), NoUnit(3, 1), NoUnit(3, 2), ShrimpUnit(3, 3), NoUnit(3, 4)],
+                  [NoUnit(4, 0), RockUnit(4, 1), NoUnit(4, 2), ShrimpUnit(4, 3), NoUnit(4, 4)]]
+        self.assertListEqual(game.cells, result)
+
+
+if __name__ == '__main__':
+    unittest.main()