Skip to content

Commit

Permalink
Support and test equality and hasing for Benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner committed Jan 12, 2025
1 parent 41cc0cd commit 3d9f03c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ struct Benchmark
samples::Vector{Sample}
end

Base.var"=="(a::Benchmark, b::Benchmark) = a.samples == b.samples
Base.hash(a::Benchmark, h::UInt) = hash(a.samples, h (0xa1e7faab2cd3da25 % UInt))

"""
mutable struct Defaults
Expand Down
25 changes: 25 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,31 @@ else
@test startswith(t.value.msg, "syntax: keyword argument \"seconds\" repeated in call to \"")
end

@testset "Equality and hashing" begin
x = Benchmark([
Sample(time=0.1, allocs=1)
])
y = Benchmark([
Sample(time=0.1, allocs=1)
])
z = Benchmark([
Sample(time=0.1, allocs=2)
])
@test x == y
@test x !== y
@test Chairmarks.only(x.samples) === Chairmarks.only(y.samples)
@test x == x
@test x != z
@test y != z
for a in [x, y, z], b in [x, y, z]
@test (a == b) ==
(hash(a) == hash(b)) ==
(only(a.samples) == only(b.samples)) ==
(only(a.samples) === only(b.samples)) ==
(hash(a.samples) == hash(b.samples))
end
end

@testset "time_ns() close to typemax(UInt64)" begin
t0 = ccall(:jl_hrtime, UInt64, ())

Expand Down

0 comments on commit 3d9f03c

Please sign in to comment.