forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissing.jl
248 lines (220 loc) · 9.26 KB
/
missing.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
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
# This file is a part of Julia. License is MIT: https://julialang.org/license
@testset "MissingException" begin
@test sprint(showerror, MissingException("test")) == "MissingException: test"
end
@testset "convert" begin
@test convert(Union{Int, Missing}, 1) === 1
@test convert(Union{Int, Missing}, 1.0) === 1
@test_throws MethodError convert(Missing, 1)
@test_throws MethodError convert(Union{Int, Missing}, "a")
end
@testset "promote rules" begin
@test promote_type(Missing, Missing) == Missing
@test promote_type(Missing, Int) == Union{Missing, Int}
@test promote_type(Int, Missing) == Union{Missing, Int}
@test promote_type(Int, Any) == Any
@test promote_type(Any, Any) == Any
@test promote_type(Missing, Any) == Any
@test promote_type(Any, Missing) == Any
@test promote_type(Union{Int, Missing}, Missing) == Union{Int, Missing}
@test promote_type(Missing, Union{Int, Missing}) == Union{Int, Missing}
@test promote_type(Union{Int, Missing}, Int) == Union{Int, Missing}
@test promote_type(Int, Union{Int, Missing}) == Union{Int, Missing}
@test promote_type(Any, Union{Int, Missing}) == Any
@test promote_type(Union{Int, Missing}, Union{Int, Missing}) == Union{Int, Missing}
@test promote_type(Union{Float64, Missing}, Union{String, Missing}) == Any
@test promote_type(Union{Float64, Missing}, Union{Int, Missing}) == Union{Float64, Missing}
@test_broken promote_type(Union{Void, Missing, Int}, Float64) == Any
end
@testset "comparison operators" begin
@test (missing == missing) === missing
@test (1 == missing) === missing
@test (missing == 1) === missing
@test (missing != missing) === missing
@test (1 != missing) === missing
@test (missing != 1) === missing
@test isequal(missing, missing)
@test !isequal(1, missing)
@test !isequal(missing, 1)
@test (missing < missing) === missing
@test (missing < 1) === missing
@test (1 < missing) === missing
@test (missing <= missing) === missing
@test (missing <= 1) === missing
@test (1 <= missing) === missing
@test !isless(missing, missing)
@test !isless(missing, 1)
@test isless(1, missing)
end
@testset "arithmetic operators" begin
arithmetic_operators = [+, -, *, /, ^, Base.div, Base.mod, Base.fld, Base.rem]
# All unary operators return missing when evaluating missing
for f in [!, +, -]
@test ismissing(f(missing))
end
# All arithmetic operators return missing when operating on two missing's
# All arithmetic operators return missing when operating on a scalar and an missing
# All arithmetic operators return missing when operating on an missing and a scalar
for f in arithmetic_operators
@test ismissing(f(missing, missing))
@test ismissing(f(1, missing))
@test ismissing(f(missing, 1))
end
end
@testset "bit operators" begin
bit_operators = [&, |, ⊻]
# All bit operators return missing when operating on two missing's
for f in bit_operators
@test ismissing(f(missing, missing))
end
end
@testset "boolean operators" begin
@test ismissing(missing & true)
@test ismissing(true & missing)
@test !(missing & false)
@test !(false & missing)
@test ismissing(missing | false)
@test ismissing(false | missing)
@test missing | true
@test true | missing
@test ismissing(xor(missing, true))
@test ismissing(xor(true, missing))
@test ismissing(xor(missing, false))
@test ismissing(xor(false, missing))
@test ismissing(missing & 1)
@test ismissing(1 & missing)
@test ismissing(missing | 1)
@test ismissing(1 | missing)
@test ismissing(xor(missing, 1))
@test ismissing(xor(1, missing))
end
@testset "* string concatenation" begin
@test ismissing("a" * missing)
@test ismissing(missing * "a")
end
# Emulate a unitful type such as Dates.Minute
struct Unit
value::Int
end
Base.zero(::Type{Unit}) = Unit(0)
Base.one(::Type{Unit}) = 1
@testset "elementary functions" begin
elementary_functions = [abs, abs2, sign,
acos, acosh, asin, asinh, atan, atanh, sin, sinh,
conj, cos, cosh, tan, tanh,
exp, exp2, expm1, log, log10, log1p, log2,
exponent, sqrt, gamma, lgamma,
identity, zero, one, oneunit,
iseven, isodd, ispow2,
isfinite, isinf, isnan, iszero,
isinteger, isreal, isempty, transpose, float]
# All elementary functions return missing when evaluating missing
for f in elementary_functions
@test ismissing(f(missing))
end
for T in (Int, Float64)
@test zero(Union{T, Missing}) === T(0)
@test one(Union{T, Missing}) === T(1)
@test oneunit(Union{T, Missing}) === T(1)
end
@test_throws MethodError zero(Union{Symbol, Missing})
@test_throws MethodError one(Union{Symbol, Missing})
@test_throws MethodError oneunit(Union{Symbol, Missing})
for T in (Unit,)
@test zero(Union{T, Missing}) === T(0)
@test one(Union{T, Missing}) === 1
@test oneunit(Union{T, Missing}) === T(1)
end
@test_throws MethodError zero(Any)
@test_throws MethodError one(Any)
@test_throws MethodError oneunit(Any)
@test_throws MethodError zero(String)
@test_throws MethodError zero(Union{String, Missing})
end
@testset "rounding functions" begin
rounding_functions = [ceil, floor, round, trunc]
# All rounding functions return missing when evaluating missing as first argument
for f in rounding_functions
@test ismissing(f(missing))
@test ismissing(f(missing, 1))
@test ismissing(f(missing, 1, 1))
@test ismissing(f(Union{Int, Missing}, missing))
@test_throws MissingException f(Int, missing)
end
end
@testset "printing" begin
@test sprint(show, missing) == "missing"
@test sprint(showcompact, missing) == "missing"
@test sprint(show, [missing]) == "$Missing[missing]"
@test sprint(show, [1 missing]) == "$(Union{Int, Missing})[1 missing]"
b = IOBuffer()
display(TextDisplay(b), [missing])
@test String(take!(b)) == "1-element Array{$Missing,1}:\n missing"
b = IOBuffer()
display(TextDisplay(b), [1 missing])
@test String(take!(b)) == "1×2 Array{$(Union{Int, Missing}),2}:\n 1 missing"
end
@testset "arrays with missing values" begin
x = convert(Vector{Union{Int, Missing}}, [1.0, missing])
@test isa(x, Vector{Union{Int, Missing}})
@test isequal(x, [1, missing])
x = convert(Vector{Union{Int, Missing}}, [1.0])
@test isa(x, Vector{Union{Int, Missing}})
@test x == [1]
x = convert(Vector{Union{Int, Missing}}, [missing])
@test isa(x, Vector{Union{Int, Missing}})
@test isequal(x, [missing])
end
@testset "== and != on arrays" begin
@test ismissing([1, missing] == [1, missing])
@test ismissing(["a", missing] == ["a", missing])
@test ismissing(Any[1, missing] == Any[1, missing])
@test ismissing(Any[missing] == Any[missing])
@test ismissing([missing] == [missing])
@test ismissing(Any[missing, 2] == Any[1, missing])
@test ismissing([missing, false] == BitArray([true, false]))
@test ismissing(Any[missing, false] == BitArray([true, false]))
@test Union{Int, Missing}[1] == Union{Float64, Missing}[1.0]
@test Union{Int, Missing}[1] == [1.0]
@test Union{Bool, Missing}[true] == BitArray([true])
@test !(Union{Int, Missing}[1] == [2])
@test !([1] == Union{Int, Missing}[2])
@test !(Union{Int, Missing}[1] == Union{Int, Missing}[2])
@test ismissing([1, missing] != [1, missing])
@test ismissing(["a", missing] != ["a", missing])
@test ismissing(Any[1, missing] != Any[1, missing])
@test ismissing(Any[missing] != Any[missing])
@test ismissing([missing] != [missing])
@test ismissing(Any[missing, 2] != Any[1, missing])
@test ismissing([missing, false] != BitArray([true, false]))
@test ismissing(Any[missing, false] != BitArray([true, false]))
@test !(Union{Int, Missing}[1] != Union{Float64, Missing}[1.0])
@test !(Union{Int, Missing}[1] != [1.0])
@test !(Union{Bool, Missing}[true] != BitArray([true]))
@test Union{Int, Missing}[1] != [2]
@test [1] != Union{Int, Missing}[2]
@test Union{Int, Missing}[1] != Union{Int, Missing}[2]
end
@testset "any & all" begin
@test any([true, missing])
@test any(x -> x == 1, [1, missing])
@test ismissing(any([false, missing]))
@test ismissing(any(x -> x == 1, [2, missing]))
@test ismissing(all([true, missing]))
@test ismissing(all(x -> x == 1, [1, missing]))
@test !all([false, missing])
@test !all(x -> x == 1, [2, missing])
@test 1 in [1, missing]
@test ismissing(2 in [1, missing])
@test ismissing(missing in [1, missing])
end
@testset "float" begin
@test isequal(float([1, missing]), [1, missing])
@test float([1, missing]) isa Vector{Union{Float64, Missing}}
@test isequal(float(Union{Int, Missing}[missing]), [missing])
@test float(Union{Int, Missing}[missing]) isa Vector{Union{Float64, Missing}}
@test float(Union{Int, Missing}[1]) == [1]
@test float(Union{Int, Missing}[1]) isa Vector{Union{Float64, Missing}}
@test isequal(float([missing]), [missing])
@test float([missing]) isa Vector{Missing}
end