-
Notifications
You must be signed in to change notification settings - Fork 19
/
test_dynamic_array.py
347 lines (297 loc) · 13.9 KB
/
test_dynamic_array.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""Тесты для модуля dynamic_array"""
import unittest
import array
import time
import dynamic_array # pylint: disable=E0401
TEST_LEN = [
('d', [], 0),
('d', [1.0], 1),
('d', [1.0, 1.0], 2),
('d', [1.0, 2.0], 2),
('d', [1.0, 9999.0, 3.0, 4.0, 1.0], 5),
('i', [], 0),
('i', [1], 1),
('i', [1, 1], 2),
('i', [1, 2], 2),
('i', [1, 9999, 3, 4, 1], 5),
]
TEST_GETITEM = [
('d', [2.0, 3.0, 4.0], 3),
('i', [5, 1, -1], 3),
('d', [], 0),
('i', [], 0),
]
TEST_SETITEM_OVERFLOW_ERROR = [
('i', [1, 2, 3], 0)
]
TEST_APPEND = [
('d', [], 1, array.array('d', [1.0])),
('d', [1.0], 1, array.array('d', [1.0, 1.0])),
('d', [2.0], 1.0, array.array('d', [2.0, 1.0])),
('d', [2.0, 5.0], 1, array.array('d', [2.0, 5.0, 1.0])),
('i', [], 1, array.array('i', [1])),
('i', [1], 1, array.array('i', [1, 1])),
('i', [2], 1, array.array('i', [2, 1])),
('i', [2, 5], 1, array.array('i', [2, 5, 1])),
]
TEST_APPEND_TYPE_ERROR = [
('d', [1.0, 2.0, 3.0], 'qwe'),
('i', [1, 2, 3], 1.5),
('i', [1, 2, 3], 'qwe'),
]
TEST_APPEND_OVERFLOW_ERROR = [
('i', [1, 2, 3], 999999999999999),
]
TEST_INSERT = [
('d', [], 0, 8, array.array('d', [8.0])),
('d', [1.0], 0, 8, array.array('d', [8.0, 1.0])),
('d', [2.0, 5.0], 1, 8, array.array('d', [2.0, 8.0, 5.0])),
('d', [2.0, 5.0], 2, 8, array.array('d', [2.0, 5.0, 8.0])),
('d', [2.0, 5.0], 10, 8, array.array('d', [2.0, 5.0, 8.0])),
('d', [2.0, 5.0], -1, 8, array.array('d', [2.0, 8.0, 5.0])),
('d', [2.0, 5.0], -2, 8, array.array('d', [8.0, 2.0, 5.0])),
('d', [2.0, 5.0], -10, 8, array.array('d', [8.0, 2.0, 5.0])),
('i', [], 0, 8, array.array('i', [8])),
('i', [1], 0, 8, array.array('i', [8, 1])),
('i', [2, 5], 1, 8, array.array('i', [2, 8, 5])),
('i', [2, 5], 2, 8, array.array('i', [2, 5, 8])),
('i', [4, 4], 10, 8, array.array('i', [4, 4, 8])),
('i', [4, 4, 1], -1, 8, array.array('i', [4, 4, 8, 1])),
('i', [4, 4, 1], -3, 8, array.array('i', [8, 4, 4, 1])),
('i', [4, 1], -10, 8, array.array('i', [8, 4, 1])),
]
TEST_REMOVE = [
('d', [1.0], 1.0, array.array('d', [])),
('d', [2.0, 2.0, 2.0], 2.0, array.array('d', [2.0, 2.0])),
('d', [2.0, 5.0], 5.0, array.array('d', [2.0])),
('i', [1], 1, array.array('i', [])),
('i', [2, 2, 2], 2, array.array('i', [2, 2])),
('i', [2, 5], 5, array.array('i', [2])),
]
TEST_REMOVE_VALUE_ERROR = [
('d', [1.0], 2.0),
('i', [1, 5], 6),
]
TEST_POP = [
('d', [1.0], 0, 1.0, array.array('d', [])),
('d', [2.0, 1.0, 6.0], 1, 1.0, array.array('d', [2.0, 6.0])),
('d', [2.0, 5.0], 1, 5.0, array.array('d', [2.0])),
('d', [2.0, 5.0], -1, 5.0, array.array('d', [2.0])),
('d', [2.0, 5.0], -2, 2.0, array.array('d', [5.0])),
('i', [1], 0, 1, array.array('i', [])),
('i', [2, 1, 4], 1, 1, array.array('i', [2, 4])),
('i', [2, 5], 1, 5, array.array('i', [2])),
('i', [2, 5], -1, 5, array.array('i', [2])),
('i', [2, 5], -2, 2, array.array('i', [5])),
]
TEST_POP_WITHOUT_INDEX = [
('d', [1.0], 1.0, array.array('d', [])),
('d', [2.0, 1.0, 6.0], 6.0, array.array('d', [2.0, 1.0])),
('d', [2.0, 5.0], 5.0, array.array('d', [2.0])),
('i', [1], 1, array.array('i', [])),
('i', [2, 1, 4], 4, array.array('i', [2, 1])),
('i', [2, 5], 5, array.array('i', [2])),
]
TEST_POP_INDEX_ERROR = [
('d', [], 1),
('d', [10.0], 5),
('d', [2.0], -2),
('i', [], 1),
('i', [3, 1], 5),
('i', [4], -2),
]
TEST_REVERSED = [
('d', [], array.array('d', [])),
('d', [1.0], array.array('d', [1.0])),
('d', [2.0, 2.0, 2.0], array.array('d', [2.0, 2.0, 2.0])),
('d', [2.0, 5.0], array.array('d', [5.0, 2.0])),
('i', [], array.array('i', [])),
('i', [1], array.array('i', [1])),
('i', [2, 2, 2], array.array('i', [2, 2, 2])),
('i', [2, 5], array.array('i', [5, 2])),
]
TEST_EQ = [
('d', [], array.array('d', [])),
('d', [1.0], array.array('d', [1.0])),
('d', [2.0, 2.0, 2.0], array.array('d', [2.0, 2.0, 2.0])),
('d', [2.0, 5.0], array.array('d', [2.0, 5.0])),
('i', [], array.array('i', [])),
('i', [1], array.array('i', [1])),
('i', [2, 2, 2], array.array('i', [2, 2, 2])),
('i', [2, 5], array.array('i', [2, 5])),
]
class TestArray(unittest.TestCase):
"""Тест-кейс модуля dynamic_array"""
def test_len(self):
"""Тест метода len"""
for typecode, data, expected in TEST_LEN:
with self.subTest(typecode=typecode, data=data, expected=expected):
test_array = dynamic_array.Array(typecode, data)
self.assertEqual(len(test_array), expected)
def test_getitem(self):
"""Тест индексации"""
for typecode, data, array_len in TEST_GETITEM:
test_array = dynamic_array.Array(typecode, data)
for index in range(array_len):
with self.subTest(typecode=typecode, data=data, index=index):
item = test_array.__getitem__(index)
self.assertEqual(item, data[index])
def test_getitem_failed(self):
"""Тест исключения IndexError при индексации"""
for typecode, data, array_len in TEST_GETITEM:
test_array = dynamic_array.Array(typecode, data)
for index in [array_len, -(array_len + 1)]:
with self.subTest(typecode=typecode, data=data, index=index):
with self.assertRaises(IndexError):
test_array.__getitem__(index)
def test_setitem(self):
"""Тест __setitem__"""
for typecode, data, array_len in TEST_GETITEM:
test_array = dynamic_array.Array(typecode, data)
for index in range(array_len):
with self.subTest(typecode=typecode, data=data, index=index):
test_array.__setitem__(index, -42)
self.assertEqual(test_array[index], -42)
def test_setitem_failed(self):
"""Тест исключений IndexError и OverflowError для __setitem__"""
for typecode, data, array_len in TEST_GETITEM:
test_array = dynamic_array.Array(typecode, data)
for index in [array_len, -(array_len + 1)]:
with self.subTest(typecode=typecode, data=data, index=index):
with self.assertRaises(IndexError):
test_array.__setitem__(index, 42)
for typecode, data, index in TEST_SETITEM_OVERFLOW_ERROR:
test_array = dynamic_array.Array(typecode, data)
with self.subTest(typecode=typecode, data=data, index=index):
with self.assertRaises(OverflowError):
test_array.__setitem__(index, 99999999999999999999999999)
def test_append(self):
"""Тест метода append"""
for typecode, data, item, expected in TEST_APPEND:
with self.subTest(typecode=typecode, data=data,
item=item, expected=expected):
test_array = dynamic_array.Array(typecode, data)
test_array.append(item)
self.assertEqual(len(test_array), len(expected))
for i, expected_item in enumerate(expected):
array_item = test_array[i]
if typecode == 'd':
self.assertTrue(isinstance(array_item, float))
elif typecode == 'i':
self.assertTrue(isinstance(array_item, int))
self.assertEqual(array_item, expected_item)
def test_append_failed(self):
"""Тест метода append с исключениями TypeError и OverflowError"""
for typecode, data, item in TEST_APPEND_TYPE_ERROR:
with self.subTest(typecode=typecode, data=data, item=item):
test_array = dynamic_array.Array(typecode, data)
with self.assertRaises(TypeError):
test_array.append(item)
for typecode, data, item in TEST_APPEND_OVERFLOW_ERROR:
with self.subTest(typecode=typecode, data=data, item=item):
test_array = dynamic_array.Array(typecode, data)
with self.assertRaises(OverflowError):
test_array.append(item)
def test_insert(self):
"""Тест метода insert"""
for typecode, data, index, item, expected in TEST_INSERT:
with self.subTest(typecode=typecode, data=data, index=index,
item=item, expected=expected):
test_array = dynamic_array.Array(typecode, data)
test_array.insert(index, item)
self.assertEqual(len(test_array), len(expected))
for i, expected_item in enumerate(expected):
array_item = test_array[i]
if typecode == 'd':
self.assertTrue(isinstance(array_item, float))
elif typecode == 'i':
self.assertTrue(isinstance(array_item, int))
self.assertEqual(array_item, expected_item)
def test_remove(self):
"""Тест метода remove"""
for typecode, data, item, expected in TEST_REMOVE:
with self.subTest(typecode=typecode, data=data,
item=item, expected=expected):
test_array = dynamic_array.Array(typecode, data)
test_array.remove(item)
self.assertEqual(len(test_array), len(expected))
for i, expected_item in enumerate(expected):
array_item = test_array[i]
if typecode == 'd':
self.assertTrue(isinstance(array_item, float))
elif typecode == 'i':
self.assertTrue(isinstance(array_item, int))
self.assertEqual(array_item, expected_item)
def test_remove_failed(self):
"""Тест метода remove с исключением remove"""
for typecode, data, item in TEST_REMOVE_VALUE_ERROR:
with self.subTest(typecode=typecode, data=data, item=item):
test_array = dynamic_array.Array(typecode, data)
with self.assertRaises(ValueError):
test_array.remove(item)
def test_pop(self):
"""Тест метода pop"""
for typecode, data, index, expected_item, expected_array in TEST_POP:
with self.subTest(typecode=typecode, data=data, index=index,
expected_item=expected_item,
expected_array=expected_array):
test_array = dynamic_array.Array(typecode, data)
item = test_array.pop(index)
self.assertEqual(item, expected_item)
self.assertEqual(len(test_array), len(expected_array))
for i, ex_item in enumerate(expected_array):
array_item = test_array[i]
if typecode == 'd':
self.assertTrue(isinstance(array_item, float))
elif typecode == 'i':
self.assertTrue(isinstance(array_item, int))
self.assertEqual(array_item, ex_item)
for typecode, data, expected_item, expected_array in TEST_POP_WITHOUT_INDEX:
with self.subTest(typecode=typecode, data=data,
expected_item=expected_item,
expected_array=expected_array):
test_array = dynamic_array.Array(typecode, data)
item = test_array.pop()
self.assertEqual(item, expected_item)
self.assertEqual(len(test_array), len(expected_array))
for i, ex_item in enumerate(expected_array):
array_item = test_array[i]
if typecode == 'd':
self.assertTrue(isinstance(array_item, float))
elif typecode == 'i':
self.assertTrue(isinstance(array_item, int))
self.assertEqual(array_item, ex_item)
def test_pop_failed(self):
"""Тест метода pop с исключением IndexError"""
for typecode, data, index in TEST_POP_INDEX_ERROR:
with self.subTest(typecode=typecode, data=data, index=index):
test_array = dynamic_array.Array(typecode, data)
with self.assertRaises(IndexError):
test_array.pop(index)
def test_reversed(self):
"""Тест метода reversed"""
for typecode, data, expected in TEST_REVERSED:
with self.subTest(typecode=typecode, data=data, expected=expected):
test_array = dynamic_array.Array(typecode, data)
reversed_array = list(reversed(test_array))
for i, expected_item in enumerate(expected):
array_item = reversed_array[i]
if typecode == 'd':
self.assertTrue(isinstance(array_item, float))
elif typecode == 'i':
self.assertTrue(isinstance(array_item, int))
self.assertEqual(array_item, expected_item)
def test_eq(self):
"""Тест сравнения с array.array"""
for typecode, data, expected in TEST_EQ:
with self.subTest(typecode=typecode, data=data, expected=expected):
my_array = dynamic_array.Array(typecode, data)
self.assertEqual(my_array, expected)
def test_timeout_append(self): # pylint: disable=R0201
"""Тест времени выполнения метода append"""
start = time.time()
for _ in range(10_000):
my_array = dynamic_array.Array('i', [])
for i in range(10_000):
my_array.append(i)
print(f'\n\033[33mВремя вашего append: {time.time() - start} сек.\033[0m')