Skip to content
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
2 changes: 1 addition & 1 deletion src/Unitful.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Unitful

import Base: ==, <, <=, +, -, *, /, //, ^, isequal
import Base: ==, <, <=, +, -, *, /, //, ^, isequal, hash
import Base: show, convert
import Base: abs, abs2, angle, big, float, fma, muladd, inv, sqrt, cbrt
import Base: min, max, floor, ceil, real, imag, conj
Expand Down
5 changes: 5 additions & 0 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ for cmp in [:(==), :isequal]
@eval $cmp(x::Number, y::AbstractQuantity) = $cmp(y,x)
end

# For now, hashes of quantities that are equal but have different units are not equal
function hash(x::AbstractQuantity, h::UInt)
hash(x.val, hash(FreeUnits(unit(x)), h))
end

_dimerr(f) = error("$f can only be well-defined for dimensionless ",
"numbers. For dimensionful numbers, different input units yield physically ",
"different results.")
Expand Down
7 changes: 7 additions & 0 deletions test/dates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@
@test !isequal(Day(4), 4u"hr")
@test !isequal(4u"cm", Day(4))

@test isequal(CompoundPeriod(), 0s)
@test isequal(CompoundPeriod(Day(365), Hour(6)), 1u"yr")
@test isequal(1u"yr", CompoundPeriod(Day(365), Hour(6)))
@test !isequal(CompoundPeriod(), -0.0u"s") # !isequal(0.0, -0.0)
Expand All @@ -629,6 +630,12 @@
@test !isequal(CompoundPeriod(Year(1)), 1u"yr")
@test !isequal(CompoundPeriod(Month(12)), 1u"yr")

# hash
@test_broken hash(Second(2)) === hash(2.0s)
@test_broken hash(72hr) === hash(Day(3))
@test_broken hash(CompoundPeriod()) === hash(0s)
@test_broken hash(CompoundPeriod(Hour(6))) === hash(6hr)

# <
@test Second(1) < 1001u"ms"
@test 3u"minute" < Minute(4)
Expand Down
14 changes: 12 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,16 @@ Unitful.uconvert(U::Unitful.Units, q::QQQ) = uconvert(U, Quantity(q.val, cm))
end
end

@testset "Hashing" begin
@test hash(big(1.0)m) === hash(big(1.0)m)
@test hash(1.0m) === hash(1m)
@test hash(0.5m) === hash((1//2)m)
@test hash(2.0m) === hash(2.0ContextUnits(m, cm))
@test hash(3.0m) === hash(3.0FixedUnits(m))
@test_broken hash(0.5m) === hash(500mm)
@test_broken hash(1rad) === hash(1)
end

@testset "Unit string parsing" begin
@test uparse("m") == m
@test uparse("m,s") == (m,s)
Expand Down Expand Up @@ -2100,11 +2110,11 @@ end
@testset ">> Level" begin
@test big(3.0)dBm == big(3.0)dBm
@test isequal(big(3.0)dBm, big(3.0)dBm)
@test_broken hash(big(3.0)dBm) == hash(big(3.0)dBm)
@test hash(big(3.0)dBm) == hash(big(3.0)dBm)

@test @dB(3.0V/2.0V) == @dB(3V/V)
@test isequal(@dB(3.0V/2.0V), @dB(3V/V))
@test_broken hash(@dB(3.0V/2.0V)) == hash(@dB(3V/V))
@test hash(@dB(3.0V/2.0V)) == hash(@dB(3V/V))
end

@testset ">> Gain" begin
Expand Down
Loading