...

Source file src/golang.org/x/crypto/sha3/allocations_test.go

Documentation: golang.org/x/crypto/sha3

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !noopt
     6  
     7  package sha3_test
     8  
     9  import (
    10  	"runtime"
    11  	"testing"
    12  
    13  	"golang.org/x/crypto/sha3"
    14  )
    15  
    16  var sink byte
    17  
    18  func TestAllocations(t *testing.T) {
    19  	want := 0.0
    20  
    21  	if runtime.GOARCH == "s390x" {
    22  		// On s390x the returned hash.Hash is conditional so it escapes.
    23  		want = 3.0
    24  	}
    25  
    26  	t.Run("New", func(t *testing.T) {
    27  		if allocs := testing.AllocsPerRun(10, func() {
    28  			h := sha3.New256()
    29  			b := []byte("ABC")
    30  			h.Write(b)
    31  			out := make([]byte, 0, 32)
    32  			out = h.Sum(out)
    33  			sink ^= out[0]
    34  		}); allocs > want {
    35  			t.Errorf("expected zero allocations, got %0.1f", allocs)
    36  		}
    37  	})
    38  	t.Run("NewShake", func(t *testing.T) {
    39  		if allocs := testing.AllocsPerRun(10, func() {
    40  			h := sha3.NewShake128()
    41  			b := []byte("ABC")
    42  			h.Write(b)
    43  			out := make([]byte, 0, 32)
    44  			out = h.Sum(out)
    45  			sink ^= out[0]
    46  			h.Read(out)
    47  			sink ^= out[0]
    48  		}); allocs > want {
    49  			t.Errorf("expected zero allocations, got %0.1f", allocs)
    50  		}
    51  	})
    52  	t.Run("Sum", func(t *testing.T) {
    53  		if allocs := testing.AllocsPerRun(10, func() {
    54  			b := []byte("ABC")
    55  			out := sha3.Sum256(b)
    56  			sink ^= out[0]
    57  		}); allocs > want {
    58  			t.Errorf("expected zero allocations, got %0.1f", allocs)
    59  		}
    60  	})
    61  }
    62  

View as plain text