@@ -232,6 +232,37 @@ func TestHashAllocations(t *testing.T) {
232232 }
233233}
234234
235+ func TestHashConstructorAllocations (t * testing.T ) {
236+ // Call the constructors directly inside each function so they can be
237+ // inlined and the Hash need not escape when only its size is needed.
238+ tests := []struct {
239+ h crypto.Hash
240+ size func () int
241+ }{
242+ {crypto .MD4 , func () int { return cng .NewMD4 ().Size () }},
243+ {crypto .MD5 , func () int { return cng .NewMD5 ().Size () }},
244+ {crypto .SHA1 , func () int { return cng .NewSHA1 ().Size () }},
245+ {crypto .SHA256 , func () int { return cng .NewSHA256 ().Size () }},
246+ {crypto .SHA384 , func () int { return cng .NewSHA384 ().Size () }},
247+ {crypto .SHA512 , func () int { return cng .NewSHA512 ().Size () }},
248+ {crypto .SHA3_256 , func () int { return cng .NewSHA3_256 ().Size () }},
249+ {crypto .SHA3_384 , func () int { return cng .NewSHA3_384 ().Size () }},
250+ {crypto .SHA3_512 , func () int { return cng .NewSHA3_512 ().Size () }},
251+ }
252+ for _ , tt := range tests {
253+ t .Run (tt .h .String (), func (t * testing.T ) {
254+ if ! cng .SupportsHash (tt .h ) {
255+ t .Skip ("skipping: not supported" )
256+ }
257+ if allocs := testing .AllocsPerRun (10 , func () {
258+ sink ^= byte (tt .size ())
259+ }); allocs != 0 {
260+ t .Errorf ("allocs = %v, want 0" , allocs )
261+ }
262+ })
263+ }
264+ }
265+
235266func TestHashStructAllocations (t * testing.T ) {
236267 msg := []byte ("testing" )
237268
0 commit comments