-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_arraydeque.py
334 lines (281 loc) · 10.4 KB
/
test_arraydeque.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
#!/usr/bin/env python
"""
test_arraydeque.py
This file contains a comprehensive test suite for the ArrayDeque type
implemented in arraydeque.c. Its functionality is intended to be compatible
with CPython's collections.deque. The tests below cover basic operations,
bounded (maxlen) behavior, rotations, indexing, iteration, comparisons,
pickling, copying, weak references, and subclassing.
To run the tests:
python test_arraydeque.py
"""
import unittest
import pickle
import copy
from arraydeque import ArrayDeque
from collections import deque # for reference comparisons
# A "big" number used in some lengthy tests.
BIG = 100000
# ---------------------------
# Basic Functionality Testing
# ---------------------------
class TestArrayDequeBasic(unittest.TestCase):
def setUp(self):
self.d = ArrayDeque()
def test_append_and_pop(self):
# Append right and pop from the right.
self.d.append(1)
self.d.append(2)
self.d.append(3)
self.assertEqual(len(self.d), 3)
self.assertEqual(self.d.pop(), 3)
self.assertEqual(self.d.pop(), 2)
self.assertEqual(self.d.pop(), 1)
self.assertEqual(len(self.d), 0)
with self.assertRaises(IndexError):
self.d.pop()
def test_appendleft_and_popleft(self):
# Append left and pop from the left.
self.d.appendleft(1)
self.d.appendleft(2)
self.d.appendleft(3)
self.assertEqual(len(self.d), 3)
self.assertEqual(self.d.popleft(), 3)
self.assertEqual(self.d.popleft(), 2)
self.assertEqual(self.d.popleft(), 1)
with self.assertRaises(IndexError):
self.d.popleft()
def test_extend(self):
# Extend on the right.
self.d.extend([10, 20, 30])
self.assertEqual(list(self.d), [10, 20, 30])
def test_extendleft(self):
# Extend on the left (order reversed relative to the iterable)
self.d.extendleft([10, 20, 30])
self.assertEqual(list(self.d), [30, 20, 10])
def test_clear(self):
# Test clear method.
self.d.extend([1, 2, 3, 4])
self.assertEqual(len(self.d), 4)
self.d.clear()
self.assertEqual(len(self.d), 0)
self.assertEqual(list(self.d), [])
# Reuse after clear.
self.d.append(99)
self.assertEqual(list(self.d), [99])
def test_iteration(self):
# Test that the ArrayDeque is iterable.
items = [1, 2, 3, 4, 5]
self.d.extend(items)
self.assertEqual(list(iter(self.d)), items)
def test_repr_and_str(self):
# Test that __repr__ and __str__ return strings.
self.d.extend([1, 2, 3])
rep = repr(self.d)
st = str(self.d)
self.assertIsInstance(rep, str)
self.assertIsInstance(st, str)
def test_indexing_and_assignment(self):
# Test __getitem__ and __setitem__.
self.d.extend([100, 200, 300, 400])
self.assertEqual(self.d[0], 100)
self.assertEqual(self.d[1], 200)
self.assertEqual(self.d[2], 300)
self.assertEqual(self.d[3], 400)
# Test negative indices.
self.assertEqual(self.d[-1], 400)
self.assertEqual(self.d[-2], 300)
# Assignment tests.
self.d[0] = 111
self.d[-1] = 444
self.assertEqual(self.d[0], 111)
self.assertEqual(self.d[3], 444)
# Out-of-range access.
with self.assertRaises(IndexError):
_ = self.d[4]
with self.assertRaises(IndexError):
self.d[-5] = 999
def test_invalid_index_type(self):
# Non-integer indices should raise a TypeError.
self.d.extend([1, 2, 3])
with self.assertRaises(TypeError):
_ = self.d['0']
with self.assertRaises(TypeError):
self.d['1'] = 100
def test_slice_unsuported(self):
# Slicing should raise a TypeError (in line with CPython's deque).
self.d.extend([1, 2, 3, 4, 5])
with self.assertRaises(TypeError):
_ = self.d[1:3]
def test_contains(self):
# Test __contains__ behavior.
self.d.extend([10, 20, 30])
for item in [10, 20, 30]:
self.assertTrue(item in self.d)
self.assertFalse(999 in self.d)
def test_initializer_with_iterable(self):
# Initializer should accept an iterable (and optional maxlen).
d1 = ArrayDeque([1, 2, 3, 4])
self.assertEqual(list(d1), [1, 2, 3, 4])
d2 = ArrayDeque([])
self.assertEqual(list(d2), [])
# ---------------------------
# Rotation Testing
# ---------------------------
class TestArrayDequeRotation(unittest.TestCase):
def setUp(self):
self.d = ArrayDeque('abcde')
def test_rotate_right(self):
self.d.rotate(1)
self.assertEqual(list(self.d), list('eabcd'))
def test_rotate_left(self):
self.d.rotate(-1)
self.assertEqual(list(self.d), list('bcdea'))
def test_rotate_default(self):
# Default rotate() rotates by 1.
self.d.rotate()
self.assertEqual(list(self.d), list('eabcd'))
def test_rotate_multiple(self):
original = list(self.d)
for i in range(10):
self.d.rotate(i)
self.d.rotate(-i)
self.assertEqual(list(self.d), original)
def test_rotate_empty_deque(self):
d_empty = ArrayDeque()
d_empty.rotate(5)
self.assertEqual(list(d_empty), [])
# ---------------------------
# Maxlen (Bounded) Behavior Testing
# ---------------------------
class TestArrayDequeMaxlen(unittest.TestCase):
def test_default_maxlen(self):
d = ArrayDeque()
# When maxlen is not specified, it behaves as unbounded.
self.assertEqual(d.maxlen, None)
def test_set_maxlen(self):
d = ArrayDeque([1, 2, 3], maxlen=5)
self.assertEqual(d.maxlen, 5)
def test_bounded_append(self):
# Bounded deque: appending to a full deque discards the leftmost element.
d = ArrayDeque(maxlen=3)
d.append(1)
d.append(2)
d.append(3)
self.assertEqual(list(d), [1, 2, 3])
d.append(4)
self.assertEqual(list(d), [2, 3, 4])
def test_bounded_appendleft(self):
# Bounded deque: appending left to a full deque discards the rightmost element.
d = ArrayDeque(maxlen=3)
d.appendleft(1)
d.appendleft(2)
d.appendleft(3)
self.assertEqual(list(d), [3, 2, 1])
d.appendleft(4)
self.assertEqual(list(d), [4, 3, 2])
def test_initialization_truncation(self):
# Initializing with an iterable longer than maxlen should retain only the rightmost items.
d = ArrayDeque(range(10), maxlen=5)
self.assertEqual(list(d), list(range(5, 10)))
def test_maxlen_zero(self):
# When maxlen==0, the deque should remain empty.
d = ArrayDeque(maxlen=0)
d.append(1)
d.appendleft(2)
self.assertEqual(list(d), [])
with self.assertRaises(IndexError):
d.pop()
with self.assertRaises(IndexError):
d.popleft()
def test_maxlen_readonly(self):
# The maxlen attribute is read-only.
d = ArrayDeque('abc', maxlen=3)
with self.assertRaises(AttributeError):
d.maxlen = 10
# ---------------------------
# Comparison and Other Methods Testing
# ---------------------------
class TestArrayDequeComparisons(unittest.TestCase):
def setUp(self):
self.ad = ArrayDeque('abc')
self.cd = deque('abc')
def test_equality(self):
# ArrayDeque equality compares by iterating, so it should match list(deque) equality.
self.assertEqual(list(self.ad), list(self.cd))
d2 = ArrayDeque('abc')
self.assertEqual(self.ad, d2)
d3 = ArrayDeque('abcd')
self.assertNotEqual(self.ad, d3)
def test_iteration_order(self):
items = list('abcdef')
d = ArrayDeque(items)
self.assertEqual(list(d), items)
def test_multiplication_unsupported(self):
# Multiplication is not implemented (unlike list), so it should raise TypeError.
with self.assertRaises(TypeError):
_ = self.ad * 2
def test_delitem_unsupported(self):
# __delitem__ is not implemented.
with self.assertRaises(TypeError):
del self.ad[1]
def test_insert_unsupported(self):
# Insert is not implemented by ArrayDeque.
with self.assertRaises(AttributeError):
self.ad.insert(1, 'X')
def test_remove(self):
# Remove the first appearance of a value.
d = ArrayDeque('abcbc')
d.remove('b')
# "abcbc" becomes ['a','c','b','c'] after removal of the first "b"
self.assertEqual(list(d), ['a', 'c', 'b', 'c'])
with self.assertRaises(ValueError):
d.remove('z')
def test_count(self):
d = ArrayDeque('abbccc')
self.assertEqual(d.count('a'), 1)
self.assertEqual(d.count('b'), 2)
self.assertEqual(d.count('c'), 3)
self.assertEqual(d.count('z'), 0)
# ---------------------------
# Pickling and Copying Testing
# ---------------------------
class TestArrayDequePickleCopy(unittest.TestCase):
def test_pickle(self):
d = ArrayDeque(range(10))
s = pickle.dumps(d, pickle.HIGHEST_PROTOCOL)
d2 = pickle.loads(s)
self.assertEqual(list(d), list(d2))
self.assertEqual(d.maxlen, d2.maxlen)
def test_deepcopy(self):
d = ArrayDeque([['a'], ['b']])
d2 = copy.deepcopy(d)
self.assertEqual(list(d), list(d2))
d[0].append('c')
self.assertNotEqual(d[0], d2[0])
def test_copy(self):
d = ArrayDeque(['x', 'y'])
d2 = copy.copy(d)
self.assertEqual(list(d), list(d2))
d.append('z')
self.assertNotEqual(list(d), list(d2))
# ---------------------------
# Subclassing Testing
# ---------------------------
class CustomDeque(ArrayDeque):
pass
class TestArrayDequeSubclass(unittest.TestCase):
def test_subclass_basic(self):
d = CustomDeque('abc')
self.assertEqual(list(d), list('abc'))
d.append('d')
self.assertEqual(list(d), list('abcd'))
# Ensure that conversion back to iterable produces an instance of the subclass.
d2 = CustomDeque(d)
self.assertIsInstance(d2, CustomDeque)
self.assertEqual(list(d2), list(d))
# ---------------------------
# Main: Run all tests
# ---------------------------
if __name__ == '__main__':
unittest.main()