|
| 1 | +""" |
| 2 | +Modified from the pyrsistent test suite. |
| 3 | +
|
| 4 | +Pre-modification, these were MIT licensed, and are copyright: |
| 5 | +
|
| 6 | + Copyright (c) 2022 Tobias Gustafsson |
| 7 | +
|
| 8 | + Permission is hereby granted, free of charge, to any person |
| 9 | + obtaining a copy of this software and associated documentation |
| 10 | + files (the "Software"), to deal in the Software without |
| 11 | + restriction, including without limitation the rights to use, |
| 12 | + copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + copies of the Software, and to permit persons to whom the |
| 14 | + Software is furnished to do so, subject to the following |
| 15 | + conditions: |
| 16 | +
|
| 17 | + The above copyright notice and this permission notice shall be |
| 18 | + included in all copies or substantial portions of the Software. |
| 19 | +
|
| 20 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | + OTHER DEALINGS IN THE SOFTWARE. |
| 28 | +""" |
| 29 | + |
| 30 | +import pytest |
| 31 | + |
| 32 | +from rpds import Stack |
| 33 | + |
| 34 | + |
| 35 | +def test_literalish_works(): |
| 36 | + assert Stack(1, 2, 3) == Stack([1, 2, 3]) |
| 37 | + |
| 38 | + |
| 39 | +def test_pop_and_peek(): |
| 40 | + ps = Stack([1, 2]) |
| 41 | + assert ps.peek() == 2 |
| 42 | + assert ps.pop().peek() == 1 |
| 43 | + assert ps.pop().pop() == Stack() |
| 44 | + |
| 45 | + |
| 46 | +def test_instantiate_large_stack(): |
| 47 | + assert Stack(range(1000)).peek() == 999 |
| 48 | + |
| 49 | + |
| 50 | +def test_iteration(): |
| 51 | + assert list(Stack()) == [] |
| 52 | + assert list(Stack([1, 2, 3]))[::-1] == [1, 2, 3] |
| 53 | + |
| 54 | + |
| 55 | +def test_push(): |
| 56 | + assert Stack([1, 2, 3]).push(4) == Stack([1, 2, 3, 4]) |
| 57 | + |
| 58 | + |
| 59 | +def test_push_empty_stack(): |
| 60 | + assert Stack().push(0) == Stack([0]) |
| 61 | + |
| 62 | + |
| 63 | +def test_truthiness(): |
| 64 | + assert Stack([1]) |
| 65 | + assert not Stack() |
| 66 | + |
| 67 | + |
| 68 | +def test_len(): |
| 69 | + assert len(Stack([1, 2, 3])) == 3 |
| 70 | + assert len(Stack()) == 0 |
| 71 | + |
| 72 | + |
| 73 | +def test_peek_illegal_on_empty_stack(): |
| 74 | + with pytest.raises(IndexError): |
| 75 | + Stack().peek() |
| 76 | + |
| 77 | + |
| 78 | +def test_pop_illegal_on_empty_stack(): |
| 79 | + with pytest.raises(IndexError): |
| 80 | + Stack().pop() |
| 81 | + |
| 82 | + |
| 83 | +def test_inequality(): |
| 84 | + assert Stack([1, 2]) != Stack([1, 3]) |
| 85 | + assert Stack([1, 2]) != Stack([1, 2, 3]) |
| 86 | + assert Stack() != Stack([1, 2, 3]) |
| 87 | + |
| 88 | + |
| 89 | +def test_repr(): |
| 90 | + assert str(Stack()) == "Stack([])" |
| 91 | + assert str(Stack([1, 2, 3])) in "Stack([1, 2, 3])" |
| 92 | + |
| 93 | + |
| 94 | +def test_hashing(): |
| 95 | + o = object() |
| 96 | + |
| 97 | + assert hash(Stack([o, o])) == hash(Stack([o, o])) |
| 98 | + assert hash(Stack([o])) == hash(Stack([o])) |
| 99 | + assert hash(Stack()) == hash(Stack([])) |
| 100 | + assert not (hash(Stack([1, 2])) == hash(Stack([1, 3]))) |
| 101 | + assert not (hash(Stack([1, 2])) == hash(Stack([2, 1]))) |
| 102 | + assert not (hash(Stack([o])) == hash(Stack([o, o]))) |
| 103 | + assert not (hash(Stack([])) == hash(Stack([o]))) |
| 104 | + |
| 105 | + assert hash(Stack([1, 2])) != hash(Stack([1, 3])) |
| 106 | + assert hash(Stack([1, 2])) != hash(Stack([2, 1])) |
| 107 | + assert hash(Stack([o])) != hash(Stack([o, o])) |
| 108 | + assert hash(Stack([])) != hash(Stack([o])) |
| 109 | + assert not (hash(Stack([o, o])) != hash(Stack([o, o]))) |
| 110 | + assert not (hash(Stack([o])) != hash(Stack([o]))) |
| 111 | + assert not (hash(Stack([])) != hash(Stack([]))) |
| 112 | + |
| 113 | + |
| 114 | +def test_sequence(): |
| 115 | + m = Stack("asdf") |
| 116 | + assert m == Stack(["a", "s", "d", "f"]) |
| 117 | + |
| 118 | + |
| 119 | +# Non-pyrsistent-test-suite tests |
| 120 | + |
| 121 | + |
| 122 | +def test_more_eq(): |
| 123 | + o = object() |
| 124 | + |
| 125 | + assert Stack([o, o]) == Stack([o, o]) |
| 126 | + assert Stack([o]) == Stack([o]) |
| 127 | + assert Stack() == Stack([]) |
| 128 | + assert not (Stack([1, 2]) == Stack([1, 3])) |
| 129 | + assert not (Stack([o]) == Stack([o, o])) |
| 130 | + assert not (Stack([]) == Stack([o])) |
| 131 | + |
| 132 | + assert Stack([1, 2]) != Stack([1, 3]) |
| 133 | + assert Stack([o]) != Stack([o, o]) |
| 134 | + assert Stack([]) != Stack([o]) |
| 135 | + assert not (Stack([o, o]) != Stack([o, o])) |
| 136 | + assert not (Stack([o]) != Stack([o])) |
| 137 | + assert not (Stack() != Stack([])) |
| 138 | + |
| 139 | + |
| 140 | +def test_rpds_doc(): |
| 141 | + """ |
| 142 | + From the rpds docs. |
| 143 | + """ |
| 144 | + stack = Stack().push("stack") |
| 145 | + assert stack.peek() == "stack" |
| 146 | + |
| 147 | + a_stack = stack.push("a") |
| 148 | + assert a_stack.peek() == "a" |
| 149 | + |
| 150 | + stack_popped = a_stack.pop() |
| 151 | + assert stack_popped == stack |
0 commit comments