Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change comparison operators to return null when one of the operands is null #33

Merged
merged 1 commit into from
Sep 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/Nulls.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__precompile__(true)
module Nulls

import Base: *, <, ==, <=, +, -, ^, /, &, |, xor
import Base: *, <, ==, !=, <=, +, -, ^, /, &, |, xor

export null, nulls, Null

Expand Down Expand Up @@ -37,16 +37,26 @@ Base.promote_rule(::Type{T}, ::Type{Null}) where {T} = Union{T, Null}
Base.convert(::Type{Union{T, Null}}, x) where {T} = convert(T, x)

# Comparison operators
<(::Null, ::Null) = false
<(::Null, b) = false
<(a, ::Null) = false
==(::Null, ::Null) = null
==(::Null, b) = null
==(a, ::Null) = null
# != must be defined explicitly since fallback expects a Bool
!=(::Null, ::Null) = null
!=(::Null, b) = null
!=(a, ::Null) = null
Base.isequal(::Null, ::Null) = true
Base.isequal(::Null, b) = false
Base.isequal(a, ::Null) = false
<(::Null, ::Null) = null
<(::Null, b) = null
<(a, ::Null) = null
Base.isless(::Null, ::Null) = false
Base.isless(::Null, b) = false
Base.isless(a, ::Null) = true
if VERSION < v"0.7.0-DEV.300"
<=(::Null, ::Null) = true
<=(::Null, b) = false
<=(a, ::Null) = false
<=(::Null, ::Null) = null
<=(::Null, b) = null
<=(a, ::Null) = null
end

# Unary operators/functions
Expand Down
43 changes: 24 additions & 19 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,21 @@ using Base.Test, Nulls
end

# Comparison operators
@test null == null
@test !(1 == null)
@test !(null == 1)
@test !(null != null)
@test 1 != null
@test null != 1
@test !(null < null)
@test !(null < 1)
@test !(1 < null)
@test null <= null
@test !(null <= 1)
@test !(1 <= null)
@test (null == null) === null
@test (1 == null) === null
@test (null == 1) === null
@test (null != null) === null
@test (1 != null) === null
@test (null != 1) === null
@test isequal(null, null)
@test !isequal(1, null)
@test !isequal(null, 1)
@test (null < null) === null
@test (null < 1) === null
@test (1 < null) === null
@test (null <= null) === null
@test (null <= 1) === null
@test (1 <= null) === null
@test !isless(null, null)
@test !isless(null, 1)
@test isless(1, null)
Expand Down Expand Up @@ -83,7 +86,7 @@ using Base.Test, Nulls
@test size(null, 1) == 1
@test_throws BoundsError size(null, 0)
@test ndims(null) == 0
@test null[1] == null
@test null[1] === null
@test_throws BoundsError null[2]

@test eltype([1, 2, null]) == Union{Int, Null}
Expand Down Expand Up @@ -114,25 +117,27 @@ using Base.Test, Nulls
@test Nulls.coalesce.([null, 1, null], 0) isa Vector{Int}
@test Nulls.coalesce.([null, 1, null], [0, 10, 5]) == [0, 1, 5]
@test Nulls.coalesce.([null, 1, null], [0, 10, 5]) isa Vector{Int}
@test Nulls.coalesce.([null, 1, null], [0, null, null]) == [0, 1, null]
@test isequal(Nulls.coalesce.([null, 1, null], [0, null, null]), [0, 1, null])
# Fails in Julia 0.6 and 0.7.0-DEV.1556
@test_broken Nulls.coalesce.([null, 1, null], [0, null, null]) isa Vector{Union{Null, Int}}

x = convert(Vector{Union{Int, Null}}, [1.0, null])
@test isa(x, Vector{Union{Int, Null}})
@test x == [1, null]
@test isequal(x, [1, null])
x = convert(Vector{Union{Int, Null}}, [1.0])
@test isa(x, Vector{Union{Int, Null}})
@test x == [1]
x = convert(Vector{Union{Int, Null}}, [null])
@test isa(x, Vector{Union{Int, Null}})
@test x == [null]
@test isequal(x, [null])

@test Nulls.T(Union{Int, Null}) == Int

@test nulls(1) == [null]
@test nulls(Int, 1) == (Union{Int, Null})[null]
@test nulls(Union{Int, Null}, 1, 2) == (Union{Int, Null})[null null]
@test isequal(nulls(1), [null])
@test isequal(nulls(Int, 1), [null])
@test nulls(Int, 1) isa Vector{Union{Int, Null}}
@test isequal(nulls(Union{Int, Null}, 1, 2), [null null])
@test nulls(Union{Int, Null}, 1, 2) isa Matrix{Union{Int, Null}}
@test Union{Int, Null}[1,2,3] == (Union{Int, Null})[1,2,3]

@test convert(Union{Int, Null}, 1.0) == 1
Expand Down