From 16d31dcc838e87d34f670ffd00e123f7a0d00d30 Mon Sep 17 00:00:00 2001 From: Austin Clements <austin@google.com> Date: Mon, 5 May 2025 17:12:42 -0400 Subject: [PATCH] hash: use testhash.TestHash in all hash functions For #69521 Change-Id: I4e056253f94ad421fcef12d21edaaaf2517b64c1 Reviewed-on: https://go-review.googlesource.com/c/go/+/670179 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Austin Clements <austin@google.com> Reviewed-by: qiu laidongfeng2 <2645477756@qq.com> Reviewed-by: Roland Shoemaker <roland@golang.org> --- src/hash/adler32/adler32_test.go | 6 ++++++ src/hash/crc32/crc32_test.go | 5 +++++ src/hash/crc64/crc64_test.go | 6 ++++++ src/hash/fnv/fnv_test.go | 27 +++++++++++++++++++++++++++ src/hash/maphash/maphash_test.go | 5 +++++ 5 files changed, 49 insertions(+) diff --git a/src/hash/adler32/adler32_test.go b/src/hash/adler32/adler32_test.go index ebb9a438a63..d42558f31df 100644 --- a/src/hash/adler32/adler32_test.go +++ b/src/hash/adler32/adler32_test.go @@ -6,11 +6,17 @@ package adler32 import ( "encoding" + "hash" + "internal/testhash" "io" "strings" "testing" ) +func TestHashInterface(t *testing.T) { + testhash.TestHash(t, func() hash.Hash { return New() }) +} + var golden = []struct { out uint32 in string diff --git a/src/hash/crc32/crc32_test.go b/src/hash/crc32/crc32_test.go index 10c28f9533b..40acd7da4f8 100644 --- a/src/hash/crc32/crc32_test.go +++ b/src/hash/crc32/crc32_test.go @@ -8,6 +8,7 @@ import ( "encoding" "fmt" "hash" + "internal/testhash" "io" "math/rand" "testing" @@ -23,6 +24,10 @@ func TestCastagnoliRace(t *testing.T) { ieee.Write([]byte("hello")) } +func TestHashInterface(t *testing.T) { + testhash.TestHash(t, func() hash.Hash { return NewIEEE() }) +} + type test struct { ieee, castagnoli uint32 in string diff --git a/src/hash/crc64/crc64_test.go b/src/hash/crc64/crc64_test.go index 06c428c81f0..d1154d5d544 100644 --- a/src/hash/crc64/crc64_test.go +++ b/src/hash/crc64/crc64_test.go @@ -6,10 +6,16 @@ package crc64 import ( "encoding" + "hash" + "internal/testhash" "io" "testing" ) +func TestCRC64Hash(t *testing.T) { + testhash.TestHash(t, func() hash.Hash { return New(MakeTable(ISO)) }) +} + type test struct { outISO uint64 outECMA uint64 diff --git a/src/hash/fnv/fnv_test.go b/src/hash/fnv/fnv_test.go index 4219460e46f..20e530032a1 100644 --- a/src/hash/fnv/fnv_test.go +++ b/src/hash/fnv/fnv_test.go @@ -9,10 +9,37 @@ import ( "encoding" "encoding/binary" "hash" + "internal/testhash" "io" "testing" ) +func TestHashInterface(t *testing.T) { + type test struct { + name string + fn func() hash.Hash + } + fn32 := func(fn func() hash.Hash32) func() hash.Hash { + return func() hash.Hash { return fn() } + } + fn64 := func(fn func() hash.Hash64) func() hash.Hash { + return func() hash.Hash { return fn() } + } + tests := []test{ + {"32", fn32(New32)}, + {"32a", fn32(New32a)}, + {"64", fn64(New64)}, + {"64a", fn64(New64a)}, + {"128", New128}, + {"128a", New128a}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + testhash.TestHash(t, test.fn) + }) + } +} + type golden struct { out []byte in string diff --git a/src/hash/maphash/maphash_test.go b/src/hash/maphash/maphash_test.go index 4a85c8a6aca..0774c1c3ced 100644 --- a/src/hash/maphash/maphash_test.go +++ b/src/hash/maphash/maphash_test.go @@ -9,6 +9,7 @@ import ( "fmt" "hash" "internal/asan" + "internal/testhash" "math" "reflect" "strings" @@ -455,6 +456,10 @@ func TestComparableAllocations(t *testing.T) { var _ hash.Hash = &Hash{} var _ hash.Hash64 = &Hash{} +func TestHashInterface(t *testing.T) { + testhash.TestHash(t, func() hash.Hash { return new(Hash) }) +} + func benchmarkSize(b *testing.B, size int) { h := &Hash{} buf := make([]byte, size) -- GitLab