Skip to content

Commit 3c9285f

Browse files
rofinnararslan
authored andcommitted
Add hashing support (#21)
1 parent 2c64fbd commit 3c9285f

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/FixedPointDecimals.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,6 @@ coefficient(::Type{FD{T, f}}) where {T, f} = T(10)^f
475475
coefficient(fd::FD{T, f}) where {T, f} = coefficient(FD{T, f})
476476
value(fd::FD) = fd.i
477477

478+
Base.hash(fd::FD{T, f}, h::UInt) where {T, f} = Base.hash(BigFloat(fd), h)
479+
478480
end

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,16 @@ end
938938
@test_throws ArgumentError parse(FD4, "1.2.3")
939939
@test_throws ArgumentError parse(FD4, "1.2", RoundUp)
940940
end
941+
942+
@testset "hashing" begin
943+
fd1 = FixedDecimal{Int, 4}(2.5)
944+
fd2 = FixedDecimal{Int, 5}(2.5)
945+
fd3 = FixedDecimal{Int, 4}(3.5)
946+
947+
@test hash(fd1) == hash(fd2)
948+
@test hash(fd1) != hash(fd3)
949+
@test hash(fd1) != hash(fd1.i)
950+
end
941951
end
942952

943953
end # global testset

0 commit comments

Comments
 (0)