-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathruntests.jl
189 lines (164 loc) · 5.75 KB
/
runtests.jl
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
using UniqueVectors
using Test
using SparseArrays
using InteractiveUtils: @which
@test length(UniqueVector([1,5,6,3])) == 4
@test_throws ArgumentError UniqueVector([1,3,5,6,3])
uv = UniqueVector{String}()
@test isempty(uv)
@test allunique(uv)
@test_throws ArgumentError pop!(uv)
@test findfirst(isequal("cat"), uv) == nothing
@test findfirst(isequal("cat"), uv) == nothing
@test findfirst!(isequal("cat"), uv) == 1
@test !isempty(uv)
@test "cat" in uv
@test "dog" ∉ uv
@test count(isequal("cat"), uv) == 1
@test count(isequal("dog"), uv) == 0
@test findall(isequal("cat"), uv) == [1]
@test findall(isequal("dog"), uv) == Int[]
@test findfirst!(isequal("dog"), uv) == 2
@test findfirst!(isequal("cat"), uv) == 1
@test findfirst!(isequal("mouse"), uv) == 3
@test findfirst!(isequal("dog"), uv) == 2
@test findfirst(isequal("cat"), uv) == 1
@test findlast(isequal("cat"), uv) == 1
@test findfirst(isequal("dog"), uv) == 2
@test findfirst(isequal("mouse"), uv) == 3
@test uv[1] == "cat"
@test uv[2] == "dog"
@test uv[3] == "mouse"
@test uv[:] == ["cat", "dog", "mouse"]
@test size(uv) == (3,)
@test length(uv) == 3
@test lastindex(uv) == 3
@test allunique(uv)
@test unique!(uv) === uv
@test unique(uv) == uv
@test unique(uv) == uv.items
@test unique(uv) !== uv.items
empty!(unique(uv))
@test length(uv.items) == 3
uv2 = UniqueVector([1, 2, 3])
@test eltype(uv2) == Int
@test findfirst(isequal(0x02), uv2) == 2
@test findfirst!(isequal(0x02), uv2) == 2
@test uv2 == UniqueVector(collect(i for i in 1:3))
for elt in [3,2,1]
@test pop!(uv2) == elt
end
@test isempty(uv2)
uv2 = copy(uv)
@test uv2 == uv
@test empty!(uv) === uv
@test isempty(uv)
@test sizehint!(uv, 2) === uv
@test findfirst(isequal("cat"), uv) == nothing
@test findfirst(isequal("cat"), uv) == nothing
@test findfirst!(isequal("horse"), uv) == 1
@test_throws ArgumentError push!(uv, "horse")
@test length(uv) == 1
@test push!(uv, "human") === uv
@test findfirst(isequal("human"), uv) == 2
@test pop!(uv) == "human"
@test length(uv) == 1
@test uv[:] == ["horse"]
@test findfirst(isequal("human"), uv) == nothing
@test findlast(isequal("human"), uv) == nothing
@test uv2[:] == ["cat", "dog", "mouse"]
@test findfirst(isequal("cat"), uv2) == 1
# Test swap!
let uv = UniqueVector(["cat", "dog", "mouse", "human"]), original = copy(uv)
uv = swap!(uv, 2, 2)
@test uv == original
@test_throws BoundsError swap!(uv, 5, 5)
swap!(uv, 2, 3)
@test findfirst(isequal("mouse"), uv) == 2
@test findfirst(isequal("mouse"), original) == 3
@test findfirst(isequal("dog"), uv) == 3
@test findfirst(isequal("dog"), original) == 2
end
# Test permute! and invpermute!
let uv = UniqueVector(["cat", "dog", "mouse", "human"]), original = copy(uv)
perm = sortperm(rand(4))
permute!(uv, perm)
@test uv == original[perm]
@test uv[findfirst(isequal("dog"), uv)] == "dog"
invpermute!(uv, perm)
@test uv == original
invpermute!(uv, perm)
@test uv == original[invperm(perm)]
@test uv[findfirst(isequal("dog"), uv)] == "dog"
end
@test UniqueVector([1,2,3,4]) == UniqueVector(1:4)
# Test it works with `Any` datatype
let uv3 = UniqueVector([1,"cat",2,"dog"])
@test eltype(uv3) == Any
@test findfirst(isequal(1), uv3) == 1
@test findlast(isequal(1), uv3) == 1
@test findall(isequal(1), uv3) == [1]
@test count(isequal(1), uv3) == 1
@test findfirst!(isequal("dog"), uv3) == 4
@test findfirst!(isequal("horse"), uv3) == 5
end
# Test setindex!
uv4 = UniqueVector(["cat", "dog", "mouse"])
@test_throws BoundsError uv4[4] = "horse"
uv4[2] = "horse"
uv4[3] = "dog"
@test uv4[:] == ["cat", "horse", "dog"]
uv4[1] = "cat"
@test_throws ArgumentError uv4[2] = "dog"
push!(uv4, "mouse")
@test uv4[:] == ["cat", "horse", "dog", "mouse"]
@test uv4[1:2] == ["cat", "horse"]
# Test view
@test view(uv4, 2:3) isa SubArray
@test findfirst(isequal("dog"), view(uv4, 2:3)) == 2
@test @which(findfirst(isequal("dog"), view(uv4, 2:3))).module == UniqueVectors
uv5 = UniqueVector{Float64}()
push!(uv5, 3)
@test uv5[:] == [3.0]
uv5[1] = 4
@test uv5[:] == [4.0]
@test 4 ∈ uv5
@test findfirst(isequal(4), uv5) == 1
@test findlast(isequal(4), uv5) == 1
@test findall(isequal(4), uv5) == [1]
# Test indexin and findall(in)
@test indexin([1,2,34,0,5,56], UniqueVector([34,56,35,1,5,0])) == [4,nothing,1,6,5,2]
@test indexin([1,2,34,0,5,56], UniqueVector([34,56,35,1,5,0])) == [4,nothing,1,6,5,2]
@test findall(in(UniqueVector([34,56,35,1,5,0])), [1,2,34,0,5,56]) == [1,3,4,5,6]
@test findall(in(UniqueVector([34,56,35,1,5,0])), (1,2,34,0,5,56)) == [1,3,4,5,6]
@test findall(in(UniqueVector([5,7,9])), [5 6; 7 8]) == findall(in([5,7,9]), [5 6; 7 8])
# Test findnext and findprev
@test findnext(isequal(7), UniqueVector([3,5,7,9]), 1) == 3
@test findnext(isequal(7), UniqueVector([3,5,7,9]), 2) == 3
@test findnext(isequal(7), UniqueVector([3,5,7,9]), 3) == 3
@test findnext(isequal(7), UniqueVector([3,5,7,9]), 4) == nothing
@test findnext(isequal(7), UniqueVector([3,5,7,9]), 4) == nothing
@test findprev(isequal(7), UniqueVector([3,5,7,9]), 1) == nothing
@test findprev(isequal(7), UniqueVector([3,5,7,9]), 1) == nothing
@test findprev(isequal(7), UniqueVector([3,5,7,9]), 2) == nothing
@test findprev(isequal(7), UniqueVector([3,5,7,9]), 2) == nothing
@test findprev(isequal(7), UniqueVector([3,5,7,9]), 3) == 3
@test findprev(isequal(7), UniqueVector([3,5,7,9]), 4) == 3
# Test constructor behavior (#7)
let items = [1,2,3]
@test UniqueVector(items).items !== items
@test UniqueVector{Int}(items).items !== items
end
# Test compatibility with SparseArrays
let arr = [1, 3, 5]
uv = UniqueVector(arr)
I = [1, 4, 3, 5]
J = [4, 7, 18, 9]
V = [1, 2, -5, 3]
let S = sparse(I, J, V)
@test findall(in(uv), S) == findall(in(arr), S)
end
let R = sparsevec(I, V)
@test findall(in(uv), R) == findall(in(arr), R)
end
end