Skip to content

Commit bef0b4e

Browse files
committed
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>
1 parent d7c073e commit bef0b4e

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/hash/maphash/maphash_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,62 @@ func TestRepeat(t *testing.T) {
106106
}
107107
}
108108

109+
func TestSeedFromSum64(t *testing.T) {
110+
h1 := new(Hash)
111+
h1.WriteString("foo")
112+
x := h1.Sum64() // seed generated here
113+
h2 := new(Hash)
114+
h2.SetSeed(h1.Seed())
115+
h2.WriteString("foo")
116+
y := h2.Sum64()
117+
if x != y {
118+
t.Errorf("hashes don't match: want %x, got %x", x, y)
119+
}
120+
}
121+
122+
func TestSeedFromSeed(t *testing.T) {
123+
h1 := new(Hash)
124+
h1.WriteString("foo")
125+
_ = h1.Seed() // seed generated here
126+
x := h1.Sum64()
127+
h2 := new(Hash)
128+
h2.SetSeed(h1.Seed())
129+
h2.WriteString("foo")
130+
y := h2.Sum64()
131+
if x != y {
132+
t.Errorf("hashes don't match: want %x, got %x", x, y)
133+
}
134+
}
135+
136+
func TestSeedFromFlush(t *testing.T) {
137+
b := make([]byte, 65)
138+
h1 := new(Hash)
139+
h1.Write(b) // seed generated here
140+
x := h1.Sum64()
141+
h2 := new(Hash)
142+
h2.SetSeed(h1.Seed())
143+
h2.Write(b)
144+
y := h2.Sum64()
145+
if x != y {
146+
t.Errorf("hashes don't match: want %x, got %x", x, y)
147+
}
148+
}
149+
150+
func TestSeedFromReset(t *testing.T) {
151+
h1 := new(Hash)
152+
h1.WriteString("foo")
153+
h1.Reset() // seed generated here
154+
h1.WriteString("foo")
155+
x := h1.Sum64()
156+
h2 := new(Hash)
157+
h2.SetSeed(h1.Seed())
158+
h2.WriteString("foo")
159+
y := h2.Sum64()
160+
if x != y {
161+
t.Errorf("hashes don't match: want %x, got %x", x, y)
162+
}
163+
}
164+
109165
// Make sure a Hash implements the hash.Hash and hash.Hash64 interfaces.
110166
var _ hash.Hash = &Hash{}
111167
var _ hash.Hash64 = &Hash{}

0 commit comments

Comments
 (0)