Skip to content

Commit b666bb3

Browse files
committed
fix:add struct oneshot allocations test
1 parent 97653fc commit b666bb3

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

cng/hash_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,66 @@ func TestHash_OneShot(t *testing.T) {
181181
}
182182
}
183183

184+
func TestHashAllocations(t *testing.T) {
185+
msg := []byte("testing")
186+
n := int(testing.AllocsPerRun(10, func() {
187+
sink ^= cng.SHA1(msg)[0]
188+
sink ^= cng.SHA256(msg)[0]
189+
sink ^= cng.SHA384(msg)[0]
190+
sink ^= cng.SHA512(msg)[0]
191+
sink ^= cng.SumSHA3_256(msg)[0]
192+
sink ^= cng.SumSHA3_384(msg)[0]
193+
sink ^= cng.SumSHA3_512(msg)[0]
194+
}))
195+
want := 0
196+
if n > want {
197+
t.Errorf("allocs = %d, want %d", n, want)
198+
}
199+
}
200+
201+
func TestHashStructAllocations(t *testing.T) {
202+
msg := []byte("testing")
203+
204+
sha1Hash := cng.NewSHA1()
205+
sha256Hash := cng.NewSHA256()
206+
sha384Hash := cng.NewSHA384()
207+
sha512Hash := cng.NewSHA512()
208+
sha3_256 := cng.NewSHA3_256()
209+
sha3_384 := cng.NewSHA3_384()
210+
sha3_512 := cng.NewSHA3_512()
211+
212+
sum := make([]byte, sha512Hash.Size())
213+
n := int(testing.AllocsPerRun(10, func() {
214+
sha1Hash.Write(msg)
215+
sha256Hash.Write(msg)
216+
sha384Hash.Write(msg)
217+
sha512Hash.Write(msg)
218+
sha3_256.Write(msg)
219+
sha3_384.Write(msg)
220+
sha3_512.Write(msg)
221+
222+
sha1Hash.Sum(sum[:0])
223+
sha256Hash.Sum(sum[:0])
224+
sha384Hash.Sum(sum[:0])
225+
sha512Hash.Sum(sum[:0])
226+
sha3_256.Sum(sum[:0])
227+
sha3_384.Sum(sum[:0])
228+
sha3_512.Sum(sum[:0])
229+
230+
sha1Hash.Reset()
231+
sha256Hash.Reset()
232+
sha384Hash.Reset()
233+
sha512Hash.Reset()
234+
sha3_256.Reset()
235+
sha3_384.Reset()
236+
sha3_512.Reset()
237+
}))
238+
want := 0
239+
if n > want {
240+
t.Errorf("allocs = %d, want %d", n, want)
241+
}
242+
}
243+
184244
func BenchmarkSHA256_8Bytes(b *testing.B) {
185245
b.StopTimer()
186246
h := cng.NewSHA256()

0 commit comments

Comments
 (0)