aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pyrsistent/py2/tests/deque_test.py
blob: 7798a755834350938c8827668724d98b406b390f (plain) (blame)
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
import pickle
import pytest
from pyrsistent import pdeque, dq


def test_basic_right_and_left():
    x = pdeque([1, 2])

    assert x.right == 2
    assert x.left == 1
    assert len(x) == 2


def test_construction_with_maxlen():
    assert pdeque([1, 2, 3, 4], maxlen=2) == pdeque([3, 4])
    assert pdeque([1, 2, 3, 4], maxlen=4) == pdeque([1, 2, 3, 4])
    assert pdeque([], maxlen=2) == pdeque()


def test_construction_with_invalid_maxlen():
    with pytest.raises(TypeError):
        pdeque([], maxlen='foo')

    with pytest.raises(ValueError):
        pdeque([], maxlen=-3)


def test_pop():
    x = pdeque([1, 2, 3, 4]).pop()
    assert x.right == 3
    assert x.left == 1

    x = x.pop()
    assert x.right == 2
    assert x.left == 1

    x = x.pop()
    assert x.right == 1
    assert x.left == 1

    x = x.pop()
    assert x == pdeque()

    x = pdeque([1, 2]).pop()
    assert x == pdeque([1])

    x = x.pop()
    assert x == pdeque()

    assert pdeque().append(1).pop() == pdeque()
    assert pdeque().appendleft(1).pop() == pdeque()


def test_pop_multiple():
    assert pdeque([1, 2, 3, 4]).pop(3) == pdeque([1])
    assert pdeque([1, 2]).pop(3) == pdeque()


def test_pop_with_negative_index():
    assert pdeque([1, 2, 3]).pop(-1) == pdeque([1, 2, 3]).popleft(1)
    assert pdeque([1, 2, 3]).popleft(-1) == pdeque([1, 2, 3]).pop(1)


def test_popleft():
    x = pdeque([1, 2, 3, 4]).popleft()
    assert x.left == 2
    assert x.right == 4

    x = x.popleft()
    assert x.left == 3
    assert x.right == 4

    x = x.popleft()
    assert x.right == 4
    assert x.left == 4

    x = x.popleft()
    assert x == pdeque()

    x = pdeque([1, 2]).popleft()
    assert x == pdeque([2])

    x = x.popleft()
    assert x == pdeque()

    assert pdeque().append(1).popleft() == pdeque()
    assert pdeque().appendleft(1).popleft() == pdeque()


def test_popleft_multiple():
    assert pdeque([1, 2, 3, 4]).popleft(3) == pdeque([4])


def test_left_on_empty_deque():
    with pytest.raises(IndexError):
        pdeque().left


def test_right_on_empty_deque():
    with pytest.raises(IndexError):
        pdeque().right


def test_pop_empty_deque_returns_empty_deque():
    # The other option is to throw an index error, this is what feels best for now though
    assert pdeque().pop() == pdeque()
    assert pdeque().popleft() == pdeque()


def test_str():
    assert str(pdeque([1, 2, 3])) == 'pdeque([1, 2, 3])'
    assert str(pdeque([])) == 'pdeque([])'
    assert str(pdeque([1, 2], maxlen=4)) == 'pdeque([1, 2], maxlen=4)'


def test_append():
    assert pdeque([1, 2]).append(3).append(4) == pdeque([1, 2, 3, 4])


def test_append_with_maxlen():
    assert pdeque([1, 2], maxlen=2).append(3).append(4) == pdeque([3, 4])
    assert pdeque([1, 2], maxlen=3).append(3).append(4) == pdeque([2, 3, 4])
    assert pdeque([], maxlen=0).append(1) == pdeque()


def test_appendleft():
    assert pdeque([2, 1]).appendleft(3).appendleft(4) == pdeque([4, 3, 2, 1])


def test_appendleft_with_maxlen():
    assert pdeque([2, 1], maxlen=2).appendleft(3).appendleft(4) == pdeque([4, 3])
    assert pdeque([2, 1], maxlen=3).appendleft(3).appendleft(4) == pdeque([4, 3, 2])
    assert pdeque([], maxlen=0).appendleft(1) == pdeque()


def test_extend():
    assert pdeque([1, 2]).extend([3, 4]) == pdeque([1, 2, 3, 4])


def test_extend_with_maxlen():
    assert pdeque([1, 2], maxlen=3).extend([3, 4]) == pdeque([2, 3, 4])
    assert pdeque([1, 2], maxlen=2).extend([3, 4]) == pdeque([3, 4])
    assert pdeque([], maxlen=2).extend([1, 2]) == pdeque([1, 2])
    assert pdeque([], maxlen=0).extend([1, 2]) == pdeque([])


def test_extendleft():
    assert pdeque([2, 1]).extendleft([3, 4]) == pdeque([4, 3, 2, 1])


def test_extendleft_with_maxlen():
    assert pdeque([1, 2], maxlen=3).extendleft([3, 4]) == pdeque([4, 3, 1])
    assert pdeque([1, 2], maxlen=2).extendleft([3, 4]) == pdeque([4, 3])
    assert pdeque([], maxlen=2).extendleft([1, 2]) == pdeque([2, 1])
    assert pdeque([], maxlen=0).extendleft([1, 2]) == pdeque([])


def test_count():
    x = pdeque([1, 2, 3, 2, 1])
    assert x.count(1) == 2
    assert x.count(2) == 2


def test_remove():
    assert pdeque([1, 2, 3, 4]).remove(2) == pdeque([1, 3, 4])
    assert pdeque([1, 2, 3, 4]).remove(4) == pdeque([1, 2, 3])

    # Right list must be reversed before removing element
    assert pdeque([1, 2, 3, 3, 4, 5, 4, 6]).remove(4) == pdeque([1, 2, 3, 3, 5, 4, 6])


def test_remove_element_missing():
    with pytest.raises(ValueError):
        pdeque().remove(2)

    with pytest.raises(ValueError):
        pdeque([1, 2, 3]).remove(4)


def test_reverse():
    assert pdeque([1, 2, 3, 4]).reverse() == pdeque([4, 3, 2, 1])
    assert pdeque().reverse() == pdeque()


def test_rotate_right():
    assert pdeque([1, 2, 3, 4, 5]).rotate(2) == pdeque([4, 5, 1, 2, 3])
    assert pdeque([1, 2]).rotate(0) == pdeque([1, 2])
    assert pdeque().rotate(2) == pdeque()


def test_rotate_left():
    assert pdeque([1, 2, 3, 4, 5]).rotate(-2) == pdeque([3, 4, 5, 1, 2])
    assert pdeque().rotate(-2) == pdeque()


def test_set_maxlen():
    x = pdeque([], maxlen=4)
    assert x.maxlen == 4

    with pytest.raises(AttributeError):
        x.maxlen = 5


def test_comparison():
    small = pdeque([1, 2])
    large = pdeque([1, 2, 3])

    assert small < large
    assert large > small
    assert not small > large
    assert not large < small
    assert large != small

    # Not equal to other types
    assert small != [1, 2]


def test_pickling():
    input = pdeque([1, 2, 3], maxlen=5)
    output = pickle.loads(pickle.dumps(input, -1))

    assert output == input
    assert output.maxlen == input.maxlen


def test_indexing():
    assert pdeque([1, 2, 3])[0] == 1
    assert pdeque([1, 2, 3])[1] == 2
    assert pdeque([1, 2, 3])[2] == 3
    assert pdeque([1, 2, 3])[-1] == 3
    assert pdeque([1, 2, 3])[-2] == 2
    assert pdeque([1, 2, 3])[-3] == 1


def test_one_element_indexing():
    assert pdeque([2])[0] == 2
    assert pdeque([2])[-1] == 2


def test_empty_indexing():
    with pytest.raises(IndexError):
        assert pdeque([])[0] == 1


def test_indexing_out_of_range():
    with pytest.raises(IndexError):
        pdeque([1, 2, 3])[-4]

    with pytest.raises(IndexError):
        pdeque([1, 2, 3])[3]

    with pytest.raises(IndexError):
        pdeque([2])[-2]


def test_indexing_invalid_type():
    with pytest.raises(TypeError) as e:
        pdeque([1, 2, 3])['foo']

    assert 'cannot be interpreted' in str(e.value)


def test_slicing():
    assert pdeque([1, 2, 3])[1:2] == pdeque([2])
    assert pdeque([1, 2, 3])[2:1] == pdeque([])
    assert pdeque([1, 2, 3])[-2:-1] == pdeque([2])
    assert pdeque([1, 2, 3])[::2] == pdeque([1, 3])


def test_hashing():
    assert hash(pdeque([1, 2, 3])) == hash(pdeque().append(1).append(2).append(3))


def test_index():
    assert pdeque([1, 2, 3]).index(3) == 2


def test_literalish():
    assert dq(1, 2, 3) == pdeque([1, 2, 3])


def test_supports_weakref():
    import weakref
    weakref.ref(dq(1, 2))


def test_iterable():
    """
    PDeques can be created from iterables even though they can't be len()
    hinted.
    """

    assert pdeque(iter("a")) == pdeque(iter("a"))