-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_matrix.py
164 lines (107 loc) · 3.07 KB
/
test_matrix.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from matrix import Matrix
def test_matrix_init():
m, n = 3, 3
matrix = Matrix(m, n)
for row in matrix:
for col in row:
assert col == Matrix.blank_char
assert len(matrix) == m
assert len(matrix[0]) == n
def test_matrix_colorize():
m, n = 3, 3
matrix = Matrix(m, n)
matrix.colorize(0, 0, 'X')
matrix.colorize(m - 1, n - 1, 'X')
assert matrix[0][0] == 'X'
assert matrix[m - 1][n - 1]
return matrix
def test_matrix_clear():
matrix = test_matrix_colorize()
matrix.clean()
for row in matrix:
for col in row:
assert col == Matrix.blank_char
def test_matrix_hcolorize():
m, n = 3, 3
matrix = Matrix(m, n)
matrix.hcolorize(1, 1, 2, 'X')
assert matrix[1][1] == 'X'
assert matrix[1][2] == 'X'
matrix.hcolorize(2, 0, 1, 'X')
assert matrix[2][0] == 'X'
assert matrix[2][1] == 'X'
matrix.hcolorize(0, 0, 2, 'X')
assert matrix[0][0] == 'X'
assert matrix[0][1] == 'X'
assert matrix[0][2] == 'X'
def test_matrix_vcolorize():
m, n = 3, 3
matrix = Matrix(m, n)
matrix.vcolorize(1, 1, 2, 'X')
assert matrix[1][1] == 'X'
assert matrix[2][1] == 'X'
matrix.vcolorize(2, 0, 1, 'X')
assert matrix[0][2] == 'X'
assert matrix[1][2] == 'X'
matrix.vcolorize(0, 0, 2, 'X')
assert matrix[0][0] == 'X'
assert matrix[1][0] == 'X'
assert matrix[2][0] == 'X'
def test_matrix_hfind():
m, n = 3, 3
matrix = Matrix(m, n)
matrix.colorize(0, 0, 'X')
matrix.colorize(0, 2, 'X')
matrix.colorize(1, 2, 'X')
assert 0 in matrix.hfind(0, 'X')
assert 0 in matrix.hfind(2, 'X')
assert 1 in matrix.hfind(2, 'X')
def test_matrix_vfind():
m, n = 3, 3
matrix = Matrix(m, n)
matrix.colorize(0, 0, 'X')
matrix.colorize(0, 2, 'X')
matrix.colorize(1, 2, 'X')
assert 0 in matrix.vfind(0, 'X')
assert 2 in matrix.vfind(0, 'X')
assert 2 in matrix.vfind(1, 'X')
def test_matrix_fill():
m, n = 5, 5
matrix = Matrix(m, n)
matrix.colorize(2, 0, 'X')
matrix.colorize(2, 4, 'X')
matrix.colorize(0, 2, 'X')
matrix.colorize(4, 2, 'X')
matrix.fill(2, 2, 'X')
assert ['X'] * 5 == matrix[2]
for i in range(0, 5):
matrix[i][2] == 'X'
def test_matrix_rect():
m, n = 5, 5
matrix = Matrix(m, n)
matrix.rect(0, 0, 1, 1, 'X')
assert matrix[0][0] == 'X'
assert matrix[0][1] == 'X'
assert matrix[1][0] == 'X'
assert matrix[1][1] == 'X'
def test_matrix_replace():
m, n = 2, 2
matrix = Matrix(m, n)
matrix.colorize(0, 1, 'X')
matrix.replace(0, 0, 'C')
assert matrix[0][0] == 'C'
assert matrix[0][1] == 'C'
assert matrix[1][0] == 'X'
assert matrix[1][1] == 'C'
def test_matrix_str():
m, n = 2, 2
matrix = Matrix(m, n)
matrix.colorize(0, 1, 'X')
assert str(matrix) == 'OO\nXO'
def test_matrix_save():
m, n = 2, 2
matrix = Matrix(m, n)
matrix.colorize(0, 1, 'X')
matrix.save('/tmp/matrix.bmp')
with open('/tmp/matrix.bmp') as f:
assert str(matrix) == f.read()