GameOfLifeLibrary_test.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import unittest
  2. from GameOfLifeLibrary import *
  3. class FishTestCase(unittest.TestCase):
  4. def test_fish_removal_1(self):
  5. nbs = [FishUnit(0, 0)] * 1 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
  6. fish = FishUnit(0, 0)
  7. self.assertTrue(fish.should_be_killed(nbs))
  8. def test_fish_removal_2(self):
  9. nbs = [FishUnit(0, 0)] * 2 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
  10. fish = FishUnit(0, 0)
  11. self.assertFalse(fish.should_be_killed(nbs))
  12. def test_fish_removal_3(self):
  13. nbs = [FishUnit(0, 0)] * 3 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
  14. fish = FishUnit(0, 0)
  15. self.assertFalse(fish.should_be_killed(nbs))
  16. def test_fish_removal_4(self):
  17. nbs = [FishUnit(0, 0)] * 4 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
  18. fish = FishUnit(0, 0)
  19. self.assertTrue(fish.should_be_killed(nbs))
  20. def test_fish_creation_1(self):
  21. nbs = [FishUnit(0, 0)] * 2 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
  22. unit = NoUnit(0, 0)
  23. self.assertFalse(unit.can_spawn_fish(nbs))
  24. def test_fish_creation_2(self):
  25. nbs = [FishUnit(0, 0)] * 3 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
  26. unit = NoUnit(0, 0)
  27. self.assertTrue(unit.can_spawn_fish(nbs))
  28. def test_fish_creation_3(self):
  29. nbs = [FishUnit(0, 0)] * 4 + [NoUnit(0, 0)] * 3 + [ShrimpUnit(0, 0)] * 5
  30. unit = NoUnit(0, 0)
  31. self.assertFalse(unit.can_spawn_fish(nbs))
  32. class ShrimpTestCase(unittest.TestCase):
  33. def test_shrimp_removal_1(self):
  34. nbs = [ShrimpUnit(0, 0)] * 1 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
  35. shrimp = ShrimpUnit(0, 0)
  36. self.assertTrue(shrimp.should_be_killed(nbs))
  37. def test_shrimp_removal_2(self):
  38. nbs = [ShrimpUnit(0, 0)] * 2 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
  39. shrimp = ShrimpUnit(0, 0)
  40. self.assertFalse(shrimp.should_be_killed(nbs))
  41. def test_shrimp_removal_3(self):
  42. nbs = [ShrimpUnit(0, 0)] * 3 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
  43. shrimp = ShrimpUnit(0, 0)
  44. self.assertFalse(shrimp.should_be_killed(nbs))
  45. def test_shrimp_removal_4(self):
  46. nbs = [ShrimpUnit(0, 0)] * 4 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
  47. shrimp = ShrimpUnit(0, 0)
  48. self.assertTrue(shrimp.should_be_killed(nbs))
  49. def test_shrimp_creation_1(self):
  50. nbs = [ShrimpUnit(0, 0)] * 2 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
  51. unit = NoUnit(0, 0)
  52. self.assertFalse(unit.can_spawn_shrimp(nbs))
  53. def test_shrimp_creation_2(self):
  54. nbs = [ShrimpUnit(0, 0)] * 3 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
  55. unit = NoUnit(0, 0)
  56. self.assertTrue(unit.can_spawn_shrimp(nbs))
  57. def test_shrimp_creation_3(self):
  58. nbs = [ShrimpUnit(0, 0)] * 4 + [NoUnit(0, 0)] * 3 + [FishUnit(0, 0)] * 5
  59. unit = NoUnit(0, 0)
  60. self.assertFalse(unit.can_spawn_shrimp(nbs))
  61. class GameOfLifeClassTestCase(unittest.TestCase):
  62. def test_init_1(self):
  63. game = GameOfLife(3, 3)
  64. game.parse_field(['nfr', 'fff', 'sfs'])
  65. result = [[NoUnit(0, 0), FishUnit(0, 1), RockUnit(0, 2)],
  66. [FishUnit(1, 0), FishUnit(1, 1), FishUnit(1, 2)],
  67. [ShrimpUnit(2, 0), FishUnit(2, 1), ShrimpUnit(2, 2)]]
  68. self.assertListEqual(game.cells, result)
  69. def test_init_2(self):
  70. game = GameOfLife(3, 4)
  71. game.parse_field(['nfrr', 'fffr', 'sfsr'])
  72. result = [[NoUnit(0, 0), FishUnit(0, 1), RockUnit(0, 2), RockUnit(0, 3)],
  73. [FishUnit(1, 0), FishUnit(1, 1), FishUnit(1, 2), RockUnit(1, 3)],
  74. [ShrimpUnit(2, 0), FishUnit(2, 1), ShrimpUnit(2, 2), RockUnit(2, 3)]]
  75. self.assertListEqual(game.cells, result)
  76. def test_init_3(self):
  77. game = GameOfLife(4, 3)
  78. game.parse_field(['nfr', 'fff', 'sfs', 'ssf'])
  79. result = [[NoUnit(0, 0), FishUnit(0, 1), RockUnit(0, 2)],
  80. [FishUnit(1, 0), FishUnit(1, 1), FishUnit(1, 2)],
  81. [ShrimpUnit(2, 0), FishUnit(2, 1), ShrimpUnit(2, 2)],
  82. [ShrimpUnit(3, 0), ShrimpUnit(3, 1), FishUnit(3, 2)]]
  83. self.assertListEqual(game.cells, result)
  84. def test_iterate_1(self):
  85. game = GameOfLife(3, 3)
  86. game.parse_field(['nff', 'fff', 'fff'])
  87. game.iterate()
  88. result = [[FishUnit(0, 0), NoUnit(0, 1), FishUnit(0, 2)],
  89. [NoUnit(1, 0), NoUnit(1, 1), NoUnit(1, 2)],
  90. [FishUnit(2, 0), NoUnit(2, 1), FishUnit(2, 2)]]
  91. self.assertListEqual(game.cells, result)
  92. def test_iterate_2(self):
  93. game = GameOfLife(5, 5)
  94. game.parse_field(['nffss', 'ffrrs', 'fffss', 'ssnss', 'frnnf'])
  95. game.iterate()
  96. game.iterate()
  97. result = [[NoUnit(0, 0), FishUnit(0, 1), NoUnit(0, 2), NoUnit(0, 3), NoUnit(0, 4)],
  98. [FishUnit(1, 0), NoUnit(1, 1), RockUnit(1, 2), RockUnit(1, 3), NoUnit(1, 4)],
  99. [NoUnit(2, 0), NoUnit(2, 1), NoUnit(2, 2), ShrimpUnit(2, 3), NoUnit(2, 4)],
  100. [NoUnit(3, 0), NoUnit(3, 1), NoUnit(3, 2), ShrimpUnit(3, 3), NoUnit(3, 4)],
  101. [NoUnit(4, 0), RockUnit(4, 1), NoUnit(4, 2), ShrimpUnit(4, 3), NoUnit(4, 4)]]
  102. self.assertListEqual(game.cells, result)
  103. if __name__ == '__main__':
  104. unittest.main()