-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
123 lines (96 loc) · 3.67 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import unittest
from gameoflife import Life
import numpy as numpy
class Testlife(unittest.TestCase):
def setUp(self):
self.game = Life()
def test_valid(self):
self.assertTrue(self.game.select('blinker'))
def test_invalid(self):
self.assertFalse(self.game.select(''))
def test_case(self):
self.assertTrue(self.game.select('Beacon'))
self.assertTrue(self.game.select('toad'))
self.assertTrue(self.game.select('GLIDER'))
def test_no_selection(self):
self.assertIsNone(self.game.get_board())
class TestConfigs(unittest.TestCase):
def setUp(self):
self.game = Life()
def two_steps(self, pattern, board1, board2):
self.game.select(pattern)
init_board = self.game.get_board()
np.testing.assert_array_equal(init_board, board1)
self.game.update()
np.testing.assert_array_equal(self.game.get_board(), board2)
self.game.update()
np.testing.assert_array_equal(self.game.get_board(), init_board)
def test_blinker(self):
self.two_steps('blinker', np.array(
[[0,0,0,0,0],
[0,1,1,1,0],
[0,0,0,0,0]], dtype=np.uint8), np.array(
[[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0]], dtype=np.uint8))
def test_toad(self):
self.two_steps('toad', np.array(
[[0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0]], dtype=np.uint8), np.array(
[[0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 0],
[0, 1, 0, 0, 1, 0],
[0, 0, 1, 0, 0, 0]], dtype=np.uint8))
def test_beacon(self):
self.two_steps('beacon', np.array(
[[1, 1, 0, 0],
[1, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 1, 1]], dtype=np.uint8), np.array(
[[1, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 1]], dtype=np.uint8))
class TestConfig(unittest.TestCase):
def setUp(self):
self.game = Life()
def multiple_steps(self, pattern, *boards):
self.game.select(pattern)
init_board = self.game.get_board()
np.testing.assert_array_equal(init_board, boards[0])
for board in boards[1:]:
self.game.update()
np.testing.assert_array_equal(self.game.get_board(), board)
def test_glider(self):
board1 = np.array(
[[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)
board2 = np.array(
[[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)
board3 = np.array(
[[0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)
self.multiple_steps('glider', board1, board2, board3)
if __name__ == 'main':
unittest.main()