8
8
from spiral_matrix .spiral_matrix import SpiralMatrix
9
9
10
10
################################################################################
11
- class SpiralMatrixInstantiationTestCase (unittest .TestCase ):
11
+ class SpiralMatrixTestCase (unittest .TestCase ):
12
12
13
- def test_simple_instantiation (self ):
13
+ def test_01_simple_instantiation (self ):
14
14
15
15
pass_configs = [3 , 5 , 23 , 1.21e2 , '3' ]
16
16
for config in pass_configs :
@@ -55,94 +55,81 @@ def test_build(self):
55
55
################################################################################
56
56
class SpiralMatrixMethodsTestCase (unittest .TestCase ):
57
57
58
- def setUp (self ):
59
-
60
- self .matrix_dimension = 5
61
-
62
- def test_series (self ):
63
-
64
- m = SpiralMatrix (self .matrix_dimension )
58
+ def test_01_series_from_integers (self ):
65
59
66
60
pass_configs = [
67
- { 'start' : 1 , 'step' : 1 ,
61
+ { 'dimension' : 5 , ' start' : 1 , 'step' : 1 ,
68
62
'want_0_0_value' : 17 },
69
- { 'start' : 1000 , 'step' : 2 ,
63
+ { 'dimension' : 5 , ' start' : 1000 , 'step' : 2 ,
70
64
'want_0_0_value' : 1032 },
71
- { 'start' : - 100 , 'step' : 3 ,
65
+ { 'dimension' : 5 , ' start' : - 100 , 'step' : 3 ,
72
66
'want_0_0_value' : - 52 },
73
67
]
74
68
for config in pass_configs :
75
69
with self .subTest (config = config ):
76
- m .start , m .step , want_0_0_value = config .values ()
77
- m .series = m ._series ()
70
+ dimension , start , step , want_0_0_value = config .values ()
71
+ m = SpiralMatrix (dimension )
72
+ m .series = m ._series_from_integers (start , step )
78
73
self .assertIsInstance (m .series , range )
79
74
self .assertEqual (len (m .series ), m .max )
80
75
m ._build ()
81
76
self .assertEqual (m .matrix [0 ][0 ], want_0_0_value )
82
77
83
- def test_series_from_string (self ):
84
-
85
- m = SpiralMatrix (self .matrix_dimension )
78
+ def test_02_series_from_string (self ):
86
79
87
80
pass_configs = [
88
- { 'words' : 'vulture duck crocodile elephant tiger' ,
81
+ { 'dimension' : 5 , ' words' : 'vulture duck crocodile elephant tiger' ,
89
82
'want_0_0_value' : 'duck' },
90
- { 'words' : '10 4 2 45 31 8.88 22 -7 0 94' ,
83
+ { 'dimension' : 5 , ' words' : '10 4 2 45 31 8.88 22 -7 0 94' ,
91
84
'want_0_0_value' : '22' },
92
- { 'words' : '~ @#$ ) ( !*% +!' ,
85
+ { 'dimension' : 5 , ' words' : '~ @#$ ) ( !*% +!' ,
93
86
'want_0_0_value' : '!*%' },
94
87
]
95
88
for config in pass_configs :
96
89
with self .subTest (config = config ):
97
- m .words , want_0_0_value = config .values ()
98
- m .series = m ._series_from_string ()
90
+ dimension , words , want_0_0_value = config .values ()
91
+ m = SpiralMatrix (dimension )
92
+ m .series = m ._series_from_string (words )
99
93
self .assertIsInstance (m .series , list )
100
94
self .assertEqual (len (m .series ), m .max )
101
95
m ._build ()
102
96
self .assertEqual (m .matrix [0 ][0 ], want_0_0_value )
103
97
104
- def test_series_from_file (self ):
98
+ def test_03_series_from_file (self ):
105
99
106
- m = SpiralMatrix (self .matrix_dimension )
107
-
108
- # import os
109
- # file_path = '%s/input' % (os.path.dirname(os.path.abspath(__file__)))
110
- # pass_configs = ['tests/input/%s' % (f) for f in os.listdir(file_path)]
100
+ from os import path
101
+ cwd = path .dirname (__file__ )
111
102
112
103
pass_configs = [
113
- { 'filename' : 'test /test-input/5 -letter-words.txt' ,
114
- 'want_0_0_value ' : 'alike ' },
115
- { 'filename' : 'test /test-input/3 -letter-words.txt' ,
116
- 'want_0_0_value ' : 'alt ' },
117
- { 'filename' : 'test /test-input/lorem-ipsum.txt' ,
118
- 'want_0_0_value ' : 'at. ' },
104
+ { 'filename' : f' { cwd } /test-input/3 -letter-words.txt' ,
105
+ 'want_77th_element ' : 'cam ' },
106
+ { 'filename' : f' { cwd } /test-input/5 -letter-words.txt' ,
107
+ 'want_77th_element ' : 'cable ' },
108
+ { 'filename' : f' { cwd } /test-input/lorem-ipsum.txt' ,
109
+ 'want_77th_element ' : 'Integer ' },
119
110
]
111
+
120
112
for config in pass_configs :
121
113
with self .subTest (config = config ):
122
- filename , want_0_0_value = config .values ()
123
- with open (filename ) as file :
124
- m .file = file
125
- m .series = m ._series_from_file ()
126
- self .assertIsInstance (m .series , list )
127
- self .assertEqual (len (m .series ), m .max )
128
- m ._build ()
129
- self .assertEqual (m .matrix [0 ][0 ], want_0_0_value )
130
-
131
- # fail_configs = [
132
- # { 'filename': 'tests/input/binary.dat' },
133
- # { 'filename': 'tests/input/empty.txt' },
134
- # ]
135
- # for config in fail_configs:
136
- # with self.subTest(config=config):
137
- # filename, = config.values()
138
- # with open(filename) as file:
139
- # m.file = file
140
- # with self.assertRaises(argparse.ArgumentError):
141
- # m.series = m._series_from_file()
142
-
143
- def test_width (self ):
144
-
145
- m = SpiralMatrix (self .matrix_dimension )
114
+ filename , want_77th_element = config .values ()
115
+ m = SpiralMatrix (9 )
116
+ series = m ._series_from_file (filename )
117
+ self .assertIsInstance (series , list )
118
+ self .assertEqual (len (series ), m .max )
119
+ self .assertEqual (series [76 ], want_77th_element )
120
+
121
+ fail_configs = [
122
+ { 'filename' : f'{ cwd } /test-input/binary.dat' },
123
+ { 'filename' : f'{ cwd } /test-input/empty.txt' },
124
+ ]
125
+ for config in fail_configs :
126
+ with self .subTest (config = config ):
127
+ filename , = config .values ()
128
+ m = SpiralMatrix (3 )
129
+ with self .assertRaises (AttributeError ):
130
+ series = m ._series_from_file (filename )
131
+
132
+ def test_04_width (self ):
146
133
147
134
pass_configs = [
148
135
{ 'series' : ['vulture' , 'duck' , 'crocodile' , 'elephant' , 'tiger' ],
@@ -152,79 +139,82 @@ def test_width(self):
152
139
]
153
140
for config in pass_configs :
154
141
with self .subTest (config = config ):
155
- m .series , want_width = config .values ()
156
- m .width = m ._width ()
157
- self .assertIsInstance (m .width , int )
158
- self .assertEqual (m .width , want_width )
159
-
160
- def test_fill (self ):
142
+ series , want_width = config .values ()
143
+ m = SpiralMatrix (5 )
144
+ width = m ._width (series )
145
+ self .assertIsInstance (width , int )
146
+ self .assertEqual (width , want_width )
161
147
162
- m = SpiralMatrix (self . matrix_dimension )
148
+ def test_05_fill (self ):
163
149
164
150
pass_configs = [
165
- { 'series' : ['vulture' , 'duck' , 'crocodile' , 'elephant' , 'tiger' ],
166
- 'index' : 2 , 'coords' : (0 , 2 ) },
151
+ { 'series' : ['emu' , 'duck' , 'crocodile' , 'shark' , 'tiger' , 'worm' ],
152
+ 'index' : 12 ,
153
+ 'want_coords' : (1 , 4 ) },
167
154
{ 'series' : range (30 , 50 , 2 ),
168
- 'index' : 7 , 'coords' : (1 , 0 ) },
155
+ 'index' : 21 ,
156
+ 'want_coords' : (4 , 0 ) },
169
157
]
170
158
for config in pass_configs :
171
159
with self .subTest (config = config ):
172
- m .series , index , coords = config .values ()
173
- new_index = m ._fill (coords , index )
160
+ series , index , want_coords = config .values ()
161
+ m = SpiralMatrix (5 )
162
+ m .series = (list (series ) * 5 )[:m .max ]
163
+ new_index = m ._fill (want_coords , index )
174
164
self .assertIsInstance (new_index , int )
175
165
self .assertEqual (new_index , index + 1 )
176
- self .assertEqual (m .matrix [coords [0 ]][coords [1 ]],
177
- m .series [index ])
178
-
179
- def test_look (self ):
166
+ self .assertEqual (m .series [index ],
167
+ m .matrix [want_coords [0 ]][want_coords [1 ]])
180
168
181
- m = SpiralMatrix (self . matrix_dimension )
169
+ def test_06_look (self ):
182
170
183
171
pass_configs = [
184
- { 'cell' : (1 , 2 ), 'bearing' : m . N , 'look' : 'left' ,
172
+ { 'cell' : (1 , 2 ), 'bearing' : 'N' , 'look' : 'left' ,
185
173
'want_cell' : (1 , 1 ) },
186
- { 'cell' : (0 , 0 ), 'bearing' : m . E , 'look' : 'right' ,
174
+ { 'cell' : (0 , 0 ), 'bearing' : 'E' , 'look' : 'right' ,
187
175
'want_cell' : (1 , 0 ) },
188
- ### TODO: better handle a look outside the matrix. expect error?
189
- { 'cell' : (0 , 1 ), 'bearing' : m .E , 'look' : 'left' ,
176
+ { 'cell' : (0 , 1 ), 'bearing' : 'E' , 'look' : 'left' ,
190
177
'want_cell' : (- 1 , 1 ) },
191
178
]
192
179
for config in pass_configs :
193
180
with self .subTest (config = config ):
194
181
cell , bearing , look , want_cell = config .values ()
182
+ m = SpiralMatrix (5 )
183
+ bearing = m .compass [bearing ]
195
184
y , x = m ._look (cell , look , bearing )
196
185
self .assertEqual (y , want_cell [0 ])
197
186
self .assertEqual (x , want_cell [1 ])
198
187
199
- def test_turn (self ):
200
-
201
- m = SpiralMatrix (self .matrix_dimension )
188
+ def test_07_turn (self ):
202
189
203
190
pass_configs = [
204
- { 'cell' : (1 , 1 ), 'turn' : 'left' , 'bearing' : m . N ,
205
- 'want_bearing' : m . W },
206
- { 'cell' : (1 , 0 ), 'turn' : 'right' , 'bearing' : m . S ,
207
- 'want_bearing' : m . W },
191
+ { 'cell' : (1 , 1 ), 'turn' : 'left' , 'bearing' : 'N' ,
192
+ 'want_bearing' : 'W' },
193
+ { 'cell' : (1 , 0 ), 'turn' : 'right' , 'bearing' : 'S' ,
194
+ 'want_bearing' : 'W' },
208
195
]
209
196
for config in pass_configs :
210
197
with self .subTest (config = config ):
211
198
cell , turn , bearing , want_bearing = config .values ()
199
+ m = SpiralMatrix (5 )
200
+ bearing = m .compass [bearing ]
201
+ want_bearing = m .compass [want_bearing ]
212
202
new_bearing = m ._turn (turn , bearing )
213
203
self .assertEqual (new_bearing , want_bearing )
214
204
215
- def test_move (self ):
216
-
217
- m = SpiralMatrix (self .matrix_dimension )
205
+ def test_08_move (self ):
218
206
219
207
pass_configs = [
220
- { 'cell' : (1 , 1 ), 'bearing' : m . N ,
208
+ { 'cell' : (1 , 1 ), 'bearing' : 'N' ,
221
209
'want_coords' : (0 , 1 ) },
222
- { 'cell' : (0 , 2 ), 'bearing' : m . S ,
210
+ { 'cell' : (0 , 2 ), 'bearing' : 'S' ,
223
211
'want_coords' : (1 , 2 ) },
224
212
]
225
213
for config in pass_configs :
226
214
with self .subTest (config = config ):
227
215
cell , bearing , want_coords = config .values ()
216
+ m = SpiralMatrix (5 )
217
+ bearing = m .compass [bearing ]
228
218
y , x = m ._move (cell , bearing )
229
219
self .assertEqual ((y , x ), want_coords )
230
220
0 commit comments