-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathtest_frozenlist.py
More file actions
393 lines (314 loc) · 11.2 KB
/
test_frozenlist.py
File metadata and controls
393 lines (314 loc) · 11.2 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# FIXME:
# mypy: disable-error-code="misc"
import pickle
from collections.abc import MutableSequence
from copy import deepcopy
import pytest
from frozenlist import FrozenList, PyFrozenList
class FrozenListMixin:
FrozenList = NotImplemented
SKIP_METHODS = {
"__abstractmethods__",
"__slots__",
"__static_attributes__",
"__firstlineno__",
}
def test___class_getitem__(self) -> None:
assert self.FrozenList[str] is not None
def test_subclass(self) -> None:
assert issubclass(self.FrozenList, MutableSequence)
def test_iface(self) -> None:
for name in set(dir(MutableSequence)) - self.SKIP_METHODS:
if name.startswith("_") and not name.endswith("_"):
continue
assert hasattr(self.FrozenList, name)
def test_ctor_default(self) -> None:
_list = self.FrozenList([])
assert not _list.frozen
def test_ctor(self) -> None:
_list = self.FrozenList([1])
assert not _list.frozen
def test_ctor_copy_list(self) -> None:
orig = [1]
_list = self.FrozenList(orig)
del _list[0]
assert _list != orig
def test_freeze(self) -> None:
_list = self.FrozenList()
_list.freeze()
assert _list.frozen
def test_repr(self) -> None:
_list = self.FrozenList([1])
assert repr(_list) == "<FrozenList(frozen=False, [1])>"
_list.freeze()
assert repr(_list) == "<FrozenList(frozen=True, [1])>"
def test_getitem(self) -> None:
_list = self.FrozenList([1, 2])
assert _list[1] == 2
def test_setitem(self) -> None:
_list = self.FrozenList([1, 2])
_list[1] = 3
assert _list[1] == 3
def test_delitem(self) -> None:
_list = self.FrozenList([1, 2])
del _list[0]
assert len(_list) == 1
assert _list[0] == 2
def test_len(self) -> None:
_list = self.FrozenList([1])
assert len(_list) == 1
def test_iter(self) -> None:
_list = self.FrozenList([1, 2])
assert list(iter(_list)) == [1, 2]
def test_reversed(self) -> None:
_list = self.FrozenList([1, 2])
assert list(reversed(_list)) == [2, 1]
def test_eq(self) -> None:
_list = self.FrozenList([1])
assert _list == [1]
def test_ne(self) -> None:
_list = self.FrozenList([1])
assert _list != [2]
def test_le(self) -> None:
_list = self.FrozenList([1])
assert _list <= [1]
def test_lt(self) -> None:
_list = self.FrozenList([1])
assert _list < [3]
def test_ge(self) -> None:
_list = self.FrozenList([1])
assert _list >= [1]
def test_gt(self) -> None:
_list = self.FrozenList([2])
assert _list > [1]
def test_insert(self) -> None:
_list = self.FrozenList([2])
_list.insert(0, 1)
assert _list == [1, 2]
def test_frozen_setitem(self) -> None:
_list = self.FrozenList([1])
_list.freeze()
with pytest.raises(RuntimeError):
_list[0] = 2
def test_frozen_delitem(self) -> None:
_list = self.FrozenList([1])
_list.freeze()
with pytest.raises(RuntimeError):
del _list[0]
def test_frozen_insert(self) -> None:
_list = self.FrozenList([1])
_list.freeze()
with pytest.raises(RuntimeError):
_list.insert(0, 2)
def test_contains(self) -> None:
_list = self.FrozenList([2])
assert 2 in _list
def test_iadd(self) -> None:
_list = self.FrozenList([1])
_list += [2]
assert _list == [1, 2]
def test_iadd_frozen(self) -> None:
_list = self.FrozenList([1])
_list.freeze()
with pytest.raises(RuntimeError):
_list += [2]
assert _list == [1]
def test_index(self) -> None:
_list = self.FrozenList([1])
assert _list.index(1) == 0
def test_remove(self) -> None:
_list = self.FrozenList([1])
_list.remove(1)
assert len(_list) == 0
def test_remove_frozen(self) -> None:
_list = self.FrozenList([1])
_list.freeze()
with pytest.raises(RuntimeError):
_list.remove(1)
assert _list == [1]
def test_clear(self) -> None:
_list = self.FrozenList([1])
_list.clear()
assert len(_list) == 0
def test_clear_frozen(self) -> None:
_list = self.FrozenList([1])
_list.freeze()
with pytest.raises(RuntimeError):
_list.clear()
assert _list == [1]
def test_extend(self) -> None:
_list = self.FrozenList([1])
_list.extend([2])
assert _list == [1, 2]
def test_extend_frozen(self) -> None:
_list = self.FrozenList([1])
_list.freeze()
with pytest.raises(RuntimeError):
_list.extend([2])
assert _list == [1]
def test_reverse(self) -> None:
_list = self.FrozenList([1, 2])
_list.reverse()
assert _list == [2, 1]
def test_reverse_frozen(self) -> None:
_list = self.FrozenList([1, 2])
_list.freeze()
with pytest.raises(RuntimeError):
_list.reverse()
assert _list == [1, 2]
def test_pop(self) -> None:
_list = self.FrozenList([1, 2])
assert _list.pop(0) == 1
assert _list == [2]
def test_pop_default(self) -> None:
_list = self.FrozenList([1, 2])
assert _list.pop() == 2
assert _list == [1]
def test_pop_frozen(self) -> None:
_list = self.FrozenList([1, 2])
_list.freeze()
with pytest.raises(RuntimeError):
_list.pop()
assert _list == [1, 2]
def test_append(self) -> None:
_list = self.FrozenList([1, 2])
_list.append(3)
assert _list == [1, 2, 3]
def test_append_frozen(self) -> None:
_list = self.FrozenList([1, 2])
_list.freeze()
with pytest.raises(RuntimeError):
_list.append(3)
assert _list == [1, 2]
def test_hash(self) -> None:
_list = self.FrozenList([1, 2])
with pytest.raises(RuntimeError):
hash(_list)
def test_hash_frozen(self) -> None:
_list = self.FrozenList([1, 2])
_list.freeze()
h = hash(_list)
assert h == hash((1, 2))
def test_dict_key(self) -> None:
_list = self.FrozenList([1, 2])
with pytest.raises(RuntimeError):
{_list: "hello"}
_list.freeze()
{_list: "hello"}
def test_count(self) -> None:
_list = self.FrozenList([1, 2])
assert _list.count(1) == 1
def test_deepcopy_unfrozen(self) -> None:
orig = self.FrozenList([1, 2, 3])
copied = deepcopy(orig)
assert copied == orig
assert copied is not orig
assert list(copied) == list(orig)
assert not copied.frozen
# Verify the copy is mutable
copied.append(4)
assert len(copied) == 4
assert len(orig) == 3
def test_deepcopy_frozen(self) -> None:
orig = self.FrozenList([1, 2, 3])
orig.freeze()
copied = deepcopy(orig)
assert copied == orig
assert copied is not orig
assert list(copied) == list(orig)
assert copied.frozen
# Verify the copy is also frozen
with pytest.raises(RuntimeError):
copied.append(4)
def test_deepcopy_nested(self) -> None:
inner = self.FrozenList([1, 2])
orig = self.FrozenList([inner, 3])
copied = deepcopy(orig)
assert copied == orig
assert copied[0] is not orig[0]
assert isinstance(copied[0], self.FrozenList)
# Modify the inner list in the copy
copied[0].append(3)
assert len(copied[0]) == 3
assert len(orig[0]) == 2
def test_deepcopy_circular(self) -> None:
orig = self.FrozenList([1, 2])
orig.append(orig) # Create circular reference
copied = deepcopy(orig)
# Check structure is preserved
assert len(copied) == 3
assert copied[0] == 1
assert copied[1] == 2
assert copied[2] is copied # Circular reference preserved
# Verify they are different objects
assert copied is not orig
assert copied[2] is not orig
# Modify the copy
copied.append(3)
assert len(copied) == 4
assert len(orig) == 3
def test_deepcopy_circular_frozen(self) -> None:
orig = self.FrozenList([1, 2])
orig.append(orig) # Create circular reference
orig.freeze()
copied = deepcopy(orig)
# Check structure is preserved
assert len(copied) == 3
assert copied[0] == 1
assert copied[1] == 2
assert copied[2] is copied # Circular reference preserved
assert copied.frozen
# Verify frozen state
with pytest.raises(RuntimeError):
copied.append(3)
def test_deepcopy_nested_circular(self) -> None:
# Create a complex nested structure with circular references
inner1 = self.FrozenList([1, 2])
inner2 = self.FrozenList([3, 4])
orig = self.FrozenList([inner1, inner2])
# Add circular references
inner1.append(inner2) # inner1 -> inner2
inner2.append(orig) # inner2 -> orig (outer list)
orig.append(orig) # orig -> orig (self reference)
copied = deepcopy(orig)
# Verify structure
assert len(copied) == 3
assert isinstance(copied[0], self.FrozenList)
assert isinstance(copied[1], self.FrozenList)
assert copied[2] is copied # Self reference preserved
# Verify nested circular references
assert len(copied[0]) == 3
assert copied[0][2] is copied[1] # inner1 -> inner2 preserved
assert len(copied[1]) == 3
assert copied[1][2] is copied # inner2 -> orig preserved
# All objects should be new instances
assert copied is not orig
assert copied[0] is not orig[0]
assert copied[1] is not orig[1]
def test_deepcopy_multiple_references(self) -> None:
# Test that multiple references to the same object are preserved
shared = self.FrozenList([1, 2])
orig = self.FrozenList([shared, shared, 3])
copied = deepcopy(orig)
# Both references should point to the same copied object
assert copied[0] is copied[1]
assert copied[0] is not shared
assert isinstance(copied[0], self.FrozenList)
# Modify through one reference
copied[0].append(3)
assert len(copied[0]) == 3
assert len(copied[1]) == 3 # Should see the change
assert len(shared) == 2 # Original unchanged
def test_pickling(self):
f = self.FrozenList([1, 2])
result = pickle.loads(pickle.dumps(f))
assert result == f
def test_pickling_frozen(self):
f = self.FrozenList([1, 2])
f.freeze()
result = pickle.loads(pickle.dumps(f))
assert result.frozen == f.frozen
class TestFrozenList(FrozenListMixin):
FrozenList = FrozenList # type: ignore[assignment] # FIXME
class TestFrozenListPy(FrozenListMixin):
FrozenList = PyFrozenList # type: ignore[assignment] # FIXME