forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnullable.jl
281 lines (239 loc) · 7.12 KB
/
nullable.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# This file is a part of Julia. License is MIT: http://julialang.org/license
types = [
Bool,
Float16,
Float32,
Float64,
Int128,
Int16,
Int32,
Int64,
Int8,
UInt16,
UInt32,
UInt64,
UInt8,
]
# Nullable{T}() = new(true)
for T in types
x = Nullable{T}()
@test x.isnull === true
@test isa(x.value, T)
@test eltype(Nullable{T}) === T
@test eltype(x) === T
end
# Nullable{T}(value::T) = new(false, value)
for T in types
x = Nullable{T}(zero(T))
@test x.isnull === false
@test isa(x.value, T)
@test x.value === zero(T)
@test eltype(x) === T
x = Nullable{T}(one(T))
@test x.isnull === false
@test isa(x.value, T)
@test x.value === one(T)
@test eltype(x) === T
end
# Nullable{T}(value::T, isnull::Bool) = new(isnull, value)
for T in types
x = Nullable{T}(zero(T),false)
@test x.isnull === false
@test isa(x.value, T)
@test x.value === zero(T)
@test eltype(x) === T
x = Nullable{T}(zero(T),true)
@test x.isnull === true
@test isa(x.value, T)
@test eltype(Nullable{T}) === T
@test eltype(x) === T
end
# immutable NullException <: Exception
@test isa(NullException(), NullException)
@test_throws NullException throw(NullException())
# Nullable{T}(value::T) = Nullable{T}(value)
for T in types
v = zero(T)
x = Nullable(v)
@test x.isnull === false
@test isa(x.value, T)
@test x.value === v
v = one(T)
x = Nullable(v)
@test x.isnull === false
@test isa(x.value, T)
@test x.value === v
end
# show{T}(io::IO, x::Nullable{T})
io1 = IOBuffer()
io2 = IOBuffer()
for (i, T) in enumerate(types)
x1 = Nullable{T}()
x2 = Nullable(zero(T))
x3 = Nullable(one(T))
show(io1, x1)
@test takebuf_string(io1) == @sprintf("Nullable{%s}()", T)
show(io1, x2)
show(io2, get(x2))
@test takebuf_string(io1) == @sprintf("Nullable{%s}(%s)", T, takebuf_string(io2))
show(io1, x3)
show(io2, get(x3))
@test takebuf_string(io1) == @sprintf("Nullable{%s}(%s)", T, takebuf_string(io2))
a1 = [x2]
show(IOContext(io1, compact=false), a1)
show(IOContext(io2, compact=false), x2)
@test takebuf_string(io1) ==
@sprintf("Nullable{%s}[%s]", string(T), takebuf_string(io2))
show(io1, a1)
show(IOContext(io2, compact=true), x2)
@test takebuf_string(io1) ==
@sprintf("Nullable{%s}[%s]", string(T), takebuf_string(io2))
end
# showcompact(io::IO, x::Nullable)
io1 = IOBuffer()
io2 = IOBuffer()
for (i, T) in enumerate(types)
x1 = Nullable{T}()
x2 = Nullable(zero(T))
x3 = Nullable(one(T))
showcompact(io1, x1)
@test takebuf_string(io1) == "#NULL"
showcompact(io1, x2)
showcompact(io2, get(x2))
@test takebuf_string(io1) == takebuf_string(io2)
showcompact(io1, x3)
showcompact(io2, get(x3))
@test takebuf_string(io1) == takebuf_string(io2)
a1 = [x2]
showcompact(io1, a1)
showcompact(io2, x2)
@test takebuf_string(io1) ==
@sprintf("Nullable{%s}[%s]", string(T), takebuf_string(io2))
end
# get(x::Nullable)
for T in types
x1 = Nullable{T}()
x2 = Nullable(zero(T))
x3 = Nullable(one(T))
@test_throws NullException get(x1)
@test get(x2) === zero(T)
@test get(x3) === one(T)
end
# get{S, T}(x::Nullable{S}, y::T)
for T in types
x1 = Nullable{T}()
x2 = Nullable(zero(T))
x3 = Nullable(one(T))
@test get(x1, zero(T)) === zero(T)
@test get(x1, one(T)) === one(T)
@test get(x2, one(T)) === zero(T)
@test get(x3, zero(T)) === one(T)
end
# isnull(x::Nullable)
for T in types
x1 = Nullable{T}()
x2 = Nullable(zero(T))
x3 = Nullable(one(T))
@test isnull(x1) === true
@test isnull(x2) === false
@test isnull(x3) === false
end
# function isequal{S, T}(x::Nullable{S}, y::Nullable{T})
for T in types
x1 = Nullable{T}()
x2 = Nullable{T}()
x3 = Nullable(zero(T))
x4 = Nullable(one(T))
@test isequal(x1, x1) === true
@test isequal(x1, x2) === true
@test isequal(x1, x3) === false
@test isequal(x1, x4) === false
@test isequal(x2, x1) === true
@test isequal(x2, x2) === true
@test isequal(x2, x3) === false
@test isequal(x2, x4) === false
@test isequal(x3, x1) === false
@test isequal(x3, x2) === false
@test isequal(x3, x3) === true
@test isequal(x3, x4) === false
@test isequal(x4, x1) === false
@test isequal(x4, x2) === false
@test isequal(x4, x3) === false
@test isequal(x4, x4) === true
end
# function =={S, T}(x::Nullable{S}, y::Nullable{T})
for T in types
x1 = Nullable{T}()
x2 = Nullable{T}()
x3 = Nullable(zero(T))
x4 = Nullable(one(T))
@test_throws NullException (x1 == x1)
@test_throws NullException (x1 == x2)
@test_throws NullException (x1 == x3)
@test_throws NullException (x1 == x4)
@test_throws NullException (x2 == x1)
@test_throws NullException (x2 == x2)
@test_throws NullException (x2 == x3)
@test_throws NullException (x2 == x4)
@test_throws NullException (x3 == x1)
@test_throws NullException (x3 == x2)
@test_throws NullException (x3 == x3)
@test_throws NullException (x3 == x4)
@test_throws NullException (x4 == x1)
@test_throws NullException (x4 == x2)
@test_throws NullException (x4 == x3)
@test_throws NullException (x4 == x4)
end
# function hash(x::Nullable, h::UInt)
for T in types
x1 = Nullable{T}()
x2 = Nullable{T}()
x3 = Nullable(zero(T))
x4 = Nullable(one(T))
@test isa(hash(x1), UInt)
@test isa(hash(x2), UInt)
@test isa(hash(x3), UInt)
@test isa(hash(x4), UInt)
@test hash(x1) == hash(x2)
@test hash(x1) != hash(x3)
@test hash(x1) != hash(x4)
@test hash(x2) != hash(x3)
@test hash(x2) != hash(x4)
@test hash(x3) != hash(x4)
end
type TestNType{T}
v::Nullable{T}
end
for T in types
x1 = TestNType{T}(Nullable{T}())
@test isnull(x1.v)
x1.v = one(T)
@test !isnull(x1.v)
@test get(x1.v, one(T)) === one(T)
end
# issue #9462
for T in types
@test isa(convert(Nullable{Number}, Nullable(one(T))), Nullable{Number})
@test isa(convert(Nullable{Number}, one(T)), Nullable{Number})
@test isa(convert(Nullable{T}, one(T)), Nullable{T})
@test isa(convert(Nullable{Any}, Nullable(one(T))), Nullable{Any})
@test isa(convert(Nullable{Any}, one(T)), Nullable{Any})
# one(T) is convertible to every type in types
# let's test that with Nullables
for S in types
@test isa(convert(Nullable{T}, one(S)), Nullable{T})
end
end
@test isnull(convert(Nullable, nothing))
@test isnull(convert(Nullable{Int}, nothing))
@test isa(convert(Nullable{Int}, nothing), Nullable{Int})
@test convert(Nullable, 1) === Nullable(1)
@test convert(Nullable, Nullable(1)) === Nullable(1)
@test isequal(convert(Nullable, "a"), Nullable("a"))
@test isequal(convert(Nullable, Nullable("a")), Nullable("a"))
@test promote_type(Nullable{Int}, Int) === Nullable{Int}
@test promote_type(Nullable{Union{}}, Int) === Nullable{Int}
@test promote_type(Nullable{Float64}, Nullable{Int}) === Nullable{Float64}
@test promote_type(Nullable{Union{}}, Nullable{Int}) === Nullable{Int}
# issue #11675
@test repr(Nullable()) == "Nullable{Union{}}()"