Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones committed May 6, 2021
1 parent 5b2953b commit 168b913
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/MurmurHash3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ end
function mmhash128_8_u(len::Integer, unaligned_pnt::Ptr, seed::UInt32)
# Should optimize handling of short (< 16 byte) unaligned strings
ulp = reinterpret(UInt, unaligned_pnt)
pnt = reinterpret(Ptr{UInt64}, ulp & ~u64(7))
fin = reinterpret(Ptr{UInt64}, (ulp + len + 0x7) & ~u64(7)) - 8
shft = (ulp & u64(7))<<3
pnt = reinterpret(Ptr{UInt64}, ulp & ~UInt(7))
fin = reinterpret(Ptr{UInt64}, (ulp + len + 0x7) & ~UInt(7)) - 8
shft = (ulp & UInt(7))<<3
h1 = h2 = u64(seed)
k1 = unsafe_load(pnt) # Pick up first 1-7 bytes
k2 = u64(0)
Expand Down
58 changes: 26 additions & 32 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ mh32c(str::MaybeSub, seed=0%UInt32) = mh32c(sizeof(str), pointer(str), seed)
mh32j(str::MaybeSub, seed=0%UInt32) = mmhash32(sizeof(str), pointer(str), seed)
mh32j(len::Integer, val::Unsigned, seed=0%UInt32) = mmhash32(len, val, seed)

# 32-bit hash (calling C version in Base)
function h32c(s::MaybeSub, h::UInt) ; h += mhseed32 ; mh32c(s, h) + h ; end

# 32-bit hash (calling Julia version)
function h32j(s::MaybeSub, h::UInt) ; h += mhseed32 ; mh32j(s, h) + h ; end

# 128-bit MurmurHash (either 32-bit or 64-bit implementation, C version in Base), lower 64-bits
mh128c(siz::Int, ptr::Ptr{UInt8}, seed=0) =
ccall(:memhash_seed, UInt64, (Ptr{UInt8}, Csize_t, UInt32), ptr, siz, seed)
mh128c(s::MaybeSub, h=0) = mh128c(sizeof(s), pointer(s), h%UInt32)
Expand All @@ -37,51 +32,50 @@ mh128j8a(s::MaybeSub, h=0) = mmhash128_8_a(sizeof(s), pointer(s), h%UInt32)
mh128j8u(s::MaybeSub, h=0) = mmhash128_8_u(sizeof(s), pointer(s), h%UInt32)
mh128j8c(s::MaybeSub, h=0) = mmhash128_8_c(s, h%UInt32)

# 128-bit MurmurHash (either 32-bit or 64-bit implementation, C version in Base), lower 64-bits
mh64c(siz::Int, ptr::Ptr{UInt8}, seed=0) =
ccall(:memhash_seed, UInt, (Ptr{UInt8}, Csize_t, UInt32), ptr, siz, seed%UInt32)
mh64c(str::MaybeSub, seed=0) = mh64c(sizeof(str), pointer(str), seed%UInt32)

# 128-bit MurmurHash (Julia version)
mh128j(str::MaybeSub, seed=0) = mmhash128_a(sizeof(str), pointer(str), seed%UInt32)
mh128j(len::Int, val::Unsigned, seed=0) = mmhash128_a(len, val, seed%UInt32)
mh128j_c(str::AbstractString, seed=0) = mmhash128_c(str, seed%UInt32)

# 64-bit hash (calling 128-bit MurmurHash C version in Base)
function h64c(s::MaybeSub, h::UInt) ; h += mhseed64 ; mh64c(s, h%UInt32) + h ; end

# 64-bit hash (calling 128-bit Julia version)
function h64j(s, h::UInt) ; h += mhseed64 ; last(mh128j(s, h%UInt32)) + h ; end

load_u64(p) = unsafe_load(reinterpret(Ptr{UInt64}, pointer(p1)))

const sizp2 = sizeof(p2)
const unsp2 = load_u64(p2)

pd(msg, v) = (print(stderr, msg); dump(stderr, v))

pd("32c: ", mh32c(p2))
pd("32j: ", mh32j(p2))
pd("32u: ", mh32j(sizp2, unsp2))
pd("128c: ", mh128c(p2))
pd("128j: ", mh128j(p2))
pd("128u: ", mh128j(sizp2, unsp2))
pd("128j4: ", mh128j4(p2))
pd("128ja: ", mh128j8a(p2))
pd("128ju: ", mh128j8u(p2))
pd("128jc: ", mh128j8c(p2))

@testset "MurmurHash3" begin
@static if UInt === UInt64
@testset "Aligned vs unaligned" begin
@test mh128j_c(p1) == mh128j(p2)
end
end
@testset "32-bit MurmurHash" begin
@test mh32j(p1) == mh32c(p1)
@test mh32j(p2) == mh32c(p2)
@test mh32j(sizp2, unsp2) == mh32c(p2)
end
@testset "128-bit MurmurHash" begin
@test last(mh128j(sizp2, unsp2)) == mh64c(p1)
@test last(mh128j(p1)) == mh64c(p1)
@test last(mh128j_c(p2)) == mh64c(p2)
@test last(mh128j_c(p2)) == mh64c(p1)
@test last(mh128j(sizp2, unsp2)) == mh128c(p1)
@static if UInt === UInt64
@test last(mh128j(p1)) == mh128c(p1)
@test last(mh128j_c(p2)) == mh128c(p2)
@test last(mh128j_c(p2)) == mh128c(p1)
else
@test_broken last(mh128j(p1)) == mh128c(p1)
@test_broken last(mh128j_c(p2)) == mh128c(p2)
@test_broken last(mh128j_c(p2)) == mh128c(p1)
end
end
end

print("32c: "); dump(mh32c(p2))
print("32j: "); dump(mh32j(p2))
print("32u: "); dump(mh32j(sizp2, unsp2))
print("128c: "); dump(mh128c(p2))
print("128j: "); dump(mh128j(p2))
print("128u: "); dump(mh128j(sizp2, unsp2))
print("128j4: "); dump(mh128j4(p2))
print("128ja: "); dump(mh128j8a(p2))
print("128ju: "); dump(mh128j8u(p2))
print("128jc: "); dump(mh128j8c(p2))

0 comments on commit 168b913

Please sign in to comment.