GameOfLifeLibrary_test.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_str(self):
  63. unit = Unit(4, 4)
  64. self.assertEqual(unit.__str__(), 'Unit(4, 4)')
  65. self.assertEqual(unit.__repr__(), 'Unit(4, 4)')
  66. self.assertEqual(unit, Unit(4, 4))
  67. self.assertNotEqual(unit, ShrimpUnit(4, 4))
  68. self.assertNotEqual(unit, RockUnit(4, 4))
  69. self.assertNotEqual(unit, Unit(2, 4))
  70. self.assertNotEqual(unit, NoUnit(4, 4))
  71. def test_init_1(self):
  72. game = GameOfLife(3, 3)
  73. game.parse_field(['nfr', 'fff', 'sfs'])
  74. result = [[NoUnit(0, 0), FishUnit(0, 1), RockUnit(0, 2)],
  75. [FishUnit(1, 0), FishUnit(1, 1), FishUnit(1, 2)],
  76. [ShrimpUnit(2, 0), FishUnit(2, 1), ShrimpUnit(2, 2)]]
  77. self.assertEqual(game.sizex, 3)
  78. self.assertEqual(game.sizey, 3)
  79. self.assertListEqual(game.cells, result)
  80. def test_init_2(self):
  81. game = GameOfLife(3, 4)
  82. game.parse_field(['nfrr', 'fffr', 'sfsr'])
  83. result = [[NoUnit(0, 0), FishUnit(0, 1), RockUnit(0, 2), RockUnit(0, 3)],
  84. [FishUnit(1, 0), FishUnit(1, 1), FishUnit(1, 2), RockUnit(1, 3)],
  85. [ShrimpUnit(2, 0), FishUnit(2, 1), ShrimpUnit(2, 2), RockUnit(2, 3)]]
  86. self.assertEqual(game.sizex, 3)
  87. self.assertEqual(game.sizey, 4)
  88. self.assertListEqual(game.cells, result)
  89. def test_init_3(self):
  90. game = GameOfLife(4, 3)
  91. game.parse_field(['nfr', 'fff', 'sfs', 'ssf'])
  92. result = [[NoUnit(0, 0), FishUnit(0, 1), RockUnit(0, 2)],
  93. [FishUnit(1, 0), FishUnit(1, 1), FishUnit(1, 2)],
  94. [ShrimpUnit(2, 0), FishUnit(2, 1), ShrimpUnit(2, 2)],
  95. [ShrimpUnit(3, 0), ShrimpUnit(3, 1), FishUnit(3, 2)]]
  96. self.assertEqual(game.sizex, 4)
  97. self.assertEqual(game.sizey, 3)
  98. self.assertListEqual(game.cells, result)
  99. def test_iterate_1(self):
  100. game = GameOfLife(3, 3)
  101. game.parse_field(['nff', 'fff', 'fff'])
  102. game.iterate()
  103. result = [[FishUnit(0, 0), NoUnit(0, 1), FishUnit(0, 2)],
  104. [NoUnit(1, 0), NoUnit(1, 1), NoUnit(1, 2)],
  105. [FishUnit(2, 0), NoUnit(2, 1), FishUnit(2, 2)]]
  106. self.assertListEqual(game.cells, result)
  107. def test_iterate_2(self):
  108. game = GameOfLife(5, 5)
  109. game.parse_field(['nffss', 'ffrrs', 'fffss', 'ssnss', 'frnnf'])
  110. game.iterate()
  111. game.iterate()
  112. result = [[NoUnit(0, 0), FishUnit(0, 1), NoUnit(0, 2), NoUnit(0, 3), NoUnit(0, 4)],
  113. [FishUnit(1, 0), NoUnit(1, 1), RockUnit(1, 2), RockUnit(1, 3), NoUnit(1, 4)],
  114. [NoUnit(2, 0), NoUnit(2, 1), NoUnit(2, 2), ShrimpUnit(2, 3), NoUnit(2, 4)],
  115. [NoUnit(3, 0), NoUnit(3, 1), NoUnit(3, 2), ShrimpUnit(3, 3), NoUnit(3, 4)],
  116. [NoUnit(4, 0), RockUnit(4, 1), NoUnit(4, 2), ShrimpUnit(4, 3), NoUnit(4, 4)]]
  117. self.assertListEqual(game.cells, result)
  118. if __name__ == '__main__':
  119. unittest.main()