diff options
author | Keith Randall <khr@golang.org> | 2020-02-23 19:23:15 -0800 |
---|---|---|
committer | Keith Randall <khr@golang.org> | 2020-03-02 17:15:20 +0000 |
commit | bef0b4ea8fd0dbda6f29412a841c176d2fc2f2eb (patch) | |
tree | 40f1923ba03c7301cfe6de9c3629fdd04b746b84 /src/hash | |
parent | d7c073ecbfc3ecc506bfc753b271973b47f8bc15 (diff) | |
download | go-bef0b4ea8fd0dbda6f29412a841c176d2fc2f2eb.tar.gz go-bef0b4ea8fd0dbda6f29412a841c176d2fc2f2eb.zip |
hash/maphash: add more tests for seed generation
Test all the paths by which a Hash picks its seed.
Make sure they all behave identically to a preset seed.
Change-Id: I2f7950857697f2f07226b96655574c36931b2aae
Reviewed-on: https://go-review.googlesource.com/c/go/+/220686
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Vladimir Evgrafov <evgrafov.vladimir@gmail.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/hash')
-rw-r--r-- | src/hash/maphash/maphash_test.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/hash/maphash/maphash_test.go b/src/hash/maphash/maphash_test.go index 0164a9e20a..caea43a8c8 100644 --- a/src/hash/maphash/maphash_test.go +++ b/src/hash/maphash/maphash_test.go @@ -106,6 +106,62 @@ func TestRepeat(t *testing.T) { } } +func TestSeedFromSum64(t *testing.T) { + h1 := new(Hash) + h1.WriteString("foo") + x := h1.Sum64() // seed generated here + h2 := new(Hash) + h2.SetSeed(h1.Seed()) + h2.WriteString("foo") + y := h2.Sum64() + if x != y { + t.Errorf("hashes don't match: want %x, got %x", x, y) + } +} + +func TestSeedFromSeed(t *testing.T) { + h1 := new(Hash) + h1.WriteString("foo") + _ = h1.Seed() // seed generated here + x := h1.Sum64() + h2 := new(Hash) + h2.SetSeed(h1.Seed()) + h2.WriteString("foo") + y := h2.Sum64() + if x != y { + t.Errorf("hashes don't match: want %x, got %x", x, y) + } +} + +func TestSeedFromFlush(t *testing.T) { + b := make([]byte, 65) + h1 := new(Hash) + h1.Write(b) // seed generated here + x := h1.Sum64() + h2 := new(Hash) + h2.SetSeed(h1.Seed()) + h2.Write(b) + y := h2.Sum64() + if x != y { + t.Errorf("hashes don't match: want %x, got %x", x, y) + } +} + +func TestSeedFromReset(t *testing.T) { + h1 := new(Hash) + h1.WriteString("foo") + h1.Reset() // seed generated here + h1.WriteString("foo") + x := h1.Sum64() + h2 := new(Hash) + h2.SetSeed(h1.Seed()) + h2.WriteString("foo") + y := h2.Sum64() + if x != y { + t.Errorf("hashes don't match: want %x, got %x", x, y) + } +} + // Make sure a Hash implements the hash.Hash and hash.Hash64 interfaces. var _ hash.Hash = &Hash{} var _ hash.Hash64 = &Hash{} |