deque_test.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import pickle
  2. import pytest
  3. from pyrsistent import pdeque, dq
  4. def test_basic_right_and_left():
  5. x = pdeque([1, 2])
  6. assert x.right == 2
  7. assert x.left == 1
  8. assert len(x) == 2
  9. def test_construction_with_maxlen():
  10. assert pdeque([1, 2, 3, 4], maxlen=2) == pdeque([3, 4])
  11. assert pdeque([1, 2, 3, 4], maxlen=4) == pdeque([1, 2, 3, 4])
  12. assert pdeque([], maxlen=2) == pdeque()
  13. def test_construction_with_invalid_maxlen():
  14. with pytest.raises(TypeError):
  15. pdeque([], maxlen='foo')
  16. with pytest.raises(ValueError):
  17. pdeque([], maxlen=-3)
  18. def test_pop():
  19. x = pdeque([1, 2, 3, 4]).pop()
  20. assert x.right == 3
  21. assert x.left == 1
  22. x = x.pop()
  23. assert x.right == 2
  24. assert x.left == 1
  25. x = x.pop()
  26. assert x.right == 1
  27. assert x.left == 1
  28. x = x.pop()
  29. assert x == pdeque()
  30. x = pdeque([1, 2]).pop()
  31. assert x == pdeque([1])
  32. x = x.pop()
  33. assert x == pdeque()
  34. assert pdeque().append(1).pop() == pdeque()
  35. assert pdeque().appendleft(1).pop() == pdeque()
  36. def test_pop_multiple():
  37. assert pdeque([1, 2, 3, 4]).pop(3) == pdeque([1])
  38. assert pdeque([1, 2]).pop(3) == pdeque()
  39. def test_pop_with_negative_index():
  40. assert pdeque([1, 2, 3]).pop(-1) == pdeque([1, 2, 3]).popleft(1)
  41. assert pdeque([1, 2, 3]).popleft(-1) == pdeque([1, 2, 3]).pop(1)
  42. def test_popleft():
  43. x = pdeque([1, 2, 3, 4]).popleft()
  44. assert x.left == 2
  45. assert x.right == 4
  46. x = x.popleft()
  47. assert x.left == 3
  48. assert x.right == 4
  49. x = x.popleft()
  50. assert x.right == 4
  51. assert x.left == 4
  52. x = x.popleft()
  53. assert x == pdeque()
  54. x = pdeque([1, 2]).popleft()
  55. assert x == pdeque([2])
  56. x = x.popleft()
  57. assert x == pdeque()
  58. assert pdeque().append(1).popleft() == pdeque()
  59. assert pdeque().appendleft(1).popleft() == pdeque()
  60. def test_popleft_multiple():
  61. assert pdeque([1, 2, 3, 4]).popleft(3) == pdeque([4])
  62. def test_left_on_empty_deque():
  63. with pytest.raises(IndexError):
  64. pdeque().left
  65. def test_right_on_empty_deque():
  66. with pytest.raises(IndexError):
  67. pdeque().right
  68. def test_pop_empty_deque_returns_empty_deque():
  69. # The other option is to throw an index error, this is what feels best for now though
  70. assert pdeque().pop() == pdeque()
  71. assert pdeque().popleft() == pdeque()
  72. def test_str():
  73. assert str(pdeque([1, 2, 3])) == 'pdeque([1, 2, 3])'
  74. assert str(pdeque([])) == 'pdeque([])'
  75. assert str(pdeque([1, 2], maxlen=4)) == 'pdeque([1, 2], maxlen=4)'
  76. def test_append():
  77. assert pdeque([1, 2]).append(3).append(4) == pdeque([1, 2, 3, 4])
  78. def test_append_with_maxlen():
  79. assert pdeque([1, 2], maxlen=2).append(3).append(4) == pdeque([3, 4])
  80. assert pdeque([1, 2], maxlen=3).append(3).append(4) == pdeque([2, 3, 4])
  81. assert pdeque([], maxlen=0).append(1) == pdeque()
  82. def test_appendleft():
  83. assert pdeque([2, 1]).appendleft(3).appendleft(4) == pdeque([4, 3, 2, 1])
  84. def test_appendleft_with_maxlen():
  85. assert pdeque([2, 1], maxlen=2).appendleft(3).appendleft(4) == pdeque([4, 3])
  86. assert pdeque([2, 1], maxlen=3).appendleft(3).appendleft(4) == pdeque([4, 3, 2])
  87. assert pdeque([], maxlen=0).appendleft(1) == pdeque()
  88. def test_extend():
  89. assert pdeque([1, 2]).extend([3, 4]) == pdeque([1, 2, 3, 4])
  90. def test_extend_with_maxlen():
  91. assert pdeque([1, 2], maxlen=3).extend([3, 4]) == pdeque([2, 3, 4])
  92. assert pdeque([1, 2], maxlen=2).extend([3, 4]) == pdeque([3, 4])
  93. assert pdeque([], maxlen=2).extend([1, 2]) == pdeque([1, 2])
  94. assert pdeque([], maxlen=0).extend([1, 2]) == pdeque([])
  95. def test_extendleft():
  96. assert pdeque([2, 1]).extendleft([3, 4]) == pdeque([4, 3, 2, 1])
  97. def test_extendleft_with_maxlen():
  98. assert pdeque([1, 2], maxlen=3).extendleft([3, 4]) == pdeque([4, 3, 1])
  99. assert pdeque([1, 2], maxlen=2).extendleft([3, 4]) == pdeque([4, 3])
  100. assert pdeque([], maxlen=2).extendleft([1, 2]) == pdeque([2, 1])
  101. assert pdeque([], maxlen=0).extendleft([1, 2]) == pdeque([])
  102. def test_count():
  103. x = pdeque([1, 2, 3, 2, 1])
  104. assert x.count(1) == 2
  105. assert x.count(2) == 2
  106. def test_remove():
  107. assert pdeque([1, 2, 3, 4]).remove(2) == pdeque([1, 3, 4])
  108. assert pdeque([1, 2, 3, 4]).remove(4) == pdeque([1, 2, 3])
  109. # Right list must be reversed before removing element
  110. assert pdeque([1, 2, 3, 3, 4, 5, 4, 6]).remove(4) == pdeque([1, 2, 3, 3, 5, 4, 6])
  111. def test_remove_element_missing():
  112. with pytest.raises(ValueError):
  113. pdeque().remove(2)
  114. with pytest.raises(ValueError):
  115. pdeque([1, 2, 3]).remove(4)
  116. def test_reverse():
  117. assert pdeque([1, 2, 3, 4]).reverse() == pdeque([4, 3, 2, 1])
  118. assert pdeque().reverse() == pdeque()
  119. def test_rotate_right():
  120. assert pdeque([1, 2, 3, 4, 5]).rotate(2) == pdeque([4, 5, 1, 2, 3])
  121. assert pdeque([1, 2]).rotate(0) == pdeque([1, 2])
  122. assert pdeque().rotate(2) == pdeque()
  123. def test_rotate_left():
  124. assert pdeque([1, 2, 3, 4, 5]).rotate(-2) == pdeque([3, 4, 5, 1, 2])
  125. assert pdeque().rotate(-2) == pdeque()
  126. def test_set_maxlen():
  127. x = pdeque([], maxlen=4)
  128. assert x.maxlen == 4
  129. with pytest.raises(AttributeError):
  130. x.maxlen = 5
  131. def test_comparison():
  132. small = pdeque([1, 2])
  133. large = pdeque([1, 2, 3])
  134. assert small < large
  135. assert large > small
  136. assert not small > large
  137. assert not large < small
  138. assert large != small
  139. # Not equal to other types
  140. assert small != [1, 2]
  141. def test_pickling():
  142. input = pdeque([1, 2, 3], maxlen=5)
  143. output = pickle.loads(pickle.dumps(input, -1))
  144. assert output == input
  145. assert output.maxlen == input.maxlen
  146. def test_indexing():
  147. assert pdeque([1, 2, 3])[0] == 1
  148. assert pdeque([1, 2, 3])[1] == 2
  149. assert pdeque([1, 2, 3])[2] == 3
  150. assert pdeque([1, 2, 3])[-1] == 3
  151. assert pdeque([1, 2, 3])[-2] == 2
  152. assert pdeque([1, 2, 3])[-3] == 1
  153. def test_one_element_indexing():
  154. assert pdeque([2])[0] == 2
  155. assert pdeque([2])[-1] == 2
  156. def test_empty_indexing():
  157. with pytest.raises(IndexError):
  158. assert pdeque([])[0] == 1
  159. def test_indexing_out_of_range():
  160. with pytest.raises(IndexError):
  161. pdeque([1, 2, 3])[-4]
  162. with pytest.raises(IndexError):
  163. pdeque([1, 2, 3])[3]
  164. with pytest.raises(IndexError):
  165. pdeque([2])[-2]
  166. def test_indexing_invalid_type():
  167. with pytest.raises(TypeError) as e:
  168. pdeque([1, 2, 3])['foo']
  169. assert 'cannot be interpreted' in str(e.value)
  170. def test_slicing():
  171. assert pdeque([1, 2, 3])[1:2] == pdeque([2])
  172. assert pdeque([1, 2, 3])[2:1] == pdeque([])
  173. assert pdeque([1, 2, 3])[-2:-1] == pdeque([2])
  174. assert pdeque([1, 2, 3])[::2] == pdeque([1, 3])
  175. def test_hashing():
  176. assert hash(pdeque([1, 2, 3])) == hash(pdeque().append(1).append(2).append(3))
  177. def test_index():
  178. assert pdeque([1, 2, 3]).index(3) == 2
  179. def test_literalish():
  180. assert dq(1, 2, 3) == pdeque([1, 2, 3])
  181. def test_supports_weakref():
  182. import weakref
  183. weakref.ref(dq(1, 2))
  184. def test_iterable():
  185. """
  186. PDeques can be created from iterables even though they can't be len()
  187. hinted.
  188. """
  189. assert pdeque(iter("a")) == pdeque(iter("a"))