File size: 4,323 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Modified from the pyrsistent test suite.

Pre-modification, these were MIT licensed, and are copyright:

    Copyright (c) 2022 Tobias Gustafsson

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.
"""

import pickle

import pytest

from rpds import List


def test_literalish_works():
    assert List(1, 2, 3) == List([1, 2, 3])


def test_first_and_rest():
    pl = List([1, 2])
    assert pl.first == 1
    assert pl.rest.first == 2
    assert pl.rest.rest == List()


def test_instantiate_large_list():
    assert List(range(1000)).first == 0


def test_iteration():
    assert list(List()) == []
    assert list(List([1, 2, 3])) == [1, 2, 3]


def test_push_front():
    assert List([1, 2, 3]).push_front(0) == List([0, 1, 2, 3])


def test_push_front_empty_list():
    assert List().push_front(0) == List([0])


def test_truthiness():
    assert List([1])
    assert not List()


def test_len():
    assert len(List([1, 2, 3])) == 3
    assert len(List()) == 0


def test_first_illegal_on_empty_list():
    with pytest.raises(IndexError):
        List().first


def test_rest_return_self_on_empty_list():
    assert List().rest == List()


def test_reverse():
    assert reversed(List([1, 2, 3])) == List([3, 2, 1])

    assert reversed(List()) == List()


def test_inequality():
    assert List([1, 2]) != List([1, 3])
    assert List([1, 2]) != List([1, 2, 3])
    assert List() != List([1, 2, 3])


def test_repr():
    assert str(List()) == "List([])"
    assert str(List([1, 2, 3])) in "List([1, 2, 3])"


def test_hashing():
    o = object()

    assert hash(List([o, o])) == hash(List([o, o]))
    assert hash(List([o])) == hash(List([o]))
    assert hash(List()) == hash(List([]))
    assert not (hash(List([1, 2])) == hash(List([1, 3])))
    assert not (hash(List([1, 2])) == hash(List([2, 1])))
    assert not (hash(List([o])) == hash(List([o, o])))
    assert not (hash(List([])) == hash(List([o])))

    assert hash(List([1, 2])) != hash(List([1, 3]))
    assert hash(List([1, 2])) != hash(List([2, 1]))
    assert hash(List([o])) != hash(List([o, o]))
    assert hash(List([])) != hash(List([o]))
    assert not (hash(List([o, o])) != hash(List([o, o])))
    assert not (hash(List([o])) != hash(List([o])))
    assert not (hash(List([])) != hash(List([])))


def test_sequence():
    m = List("asdf")
    assert m == List(["a", "s", "d", "f"])


# Non-pyrsistent-test-suite tests


def test_drop_first():
    assert List([1, 2, 3]).drop_first() == List([2, 3])


def test_drop_first_empty():
    """
    rpds itself returns an Option<List> here but we try IndexError instead.
    """
    with pytest.raises(IndexError):
        List([]).drop_first()


def test_more_eq():
    o = object()

    assert List([o, o]) == List([o, o])
    assert List([o]) == List([o])
    assert List() == List([])
    assert not (List([1, 2]) == List([1, 3]))
    assert not (List([o]) == List([o, o]))
    assert not (List([]) == List([o]))

    assert List([1, 2]) != List([1, 3])
    assert List([o]) != List([o, o])
    assert List([]) != List([o])
    assert not (List([o, o]) != List([o, o]))
    assert not (List([o]) != List([o]))
    assert not (List() != List([]))


def test_pickle():
    assert pickle.loads(pickle.dumps(List([1, 2, 3, 4]))) == List([1, 2, 3, 4])