Skip to content

Commit

Permalink
Merge pull request #24963 from JuliaLang/nl/missingtuple
Browse files Browse the repository at this point in the history
 Use three-valued logic with missing values in ==(::Tuple, ::Tuple)
  • Loading branch information
nalimilan authored Dec 14, 2017
2 parents a9bb7d3 + be38d94 commit df2616e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
12 changes: 8 additions & 4 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,16 @@ function ==(t1::Tuple, t2::Tuple)
if length(t1) != length(t2)
return false
end
anymissing = false
for i = 1:length(t1)
if !(t1[i] == t2[i])
return false
end
eq = (t1[i] == t2[i])
if ismissing(eq)
anymissing = true
elseif !eq
return false
end
end
return true
return anymissing ? missing : true
end

const tuplehash_seed = UInt === UInt64 ? 0x77cfa1eef01bca90 : 0xf01bca90
Expand Down
16 changes: 16 additions & 0 deletions test/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ end
@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 !([missing, 1] == [missing, 2])
@test !(Union{Int, Missing}[1] == [2])
@test !([1] == Union{Int, Missing}[2])
@test !(Union{Int, Missing}[1] == Union{Int, Missing}[2])
Expand All @@ -217,11 +218,26 @@ end
@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 [missing, 1] != [missing, 2]
@test Union{Int, Missing}[1] != [2]
@test [1] != Union{Int, Missing}[2]
@test Union{Int, Missing}[1] != Union{Int, Missing}[2]
end

@testset "== and != on tuples" begin
@test ismissing((1, missing) == (1, missing))
@test ismissing(("a", missing) == ("a", missing))
@test ismissing((missing,) == (missing,))
@test ismissing((missing, 2) == (1, missing))
@test !((missing, 1) == (missing, 2))

@test ismissing((1, missing) != (1, missing))
@test ismissing(("a", missing) != ("a", missing))
@test ismissing((missing,) != (missing,))
@test ismissing((missing, 2) != (1, missing))
@test (missing, 1) != (missing, 2)
end

@testset "any & all" begin
@test any([true, missing])
@test any(x -> x == 1, [1, missing])
Expand Down

0 comments on commit df2616e

Please sign in to comment.