File: allocations_test.go

package info (click to toggle)
golang-go.crypto 1%3A0.25.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 6,768 kB
  • sloc: asm: 6,518; ansic: 258; sh: 25; makefile: 6
file content (61 lines) | stat: -rw-r--r-- 1,327 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !noopt

package sha3_test

import (
	"runtime"
	"testing"

	"golang.org/x/crypto/sha3"
)

var sink byte

func TestAllocations(t *testing.T) {
	want := 0.0

	if runtime.GOARCH == "s390x" {
		// On s390x the returned hash.Hash is conditional so it escapes.
		want = 3.0
	}

	t.Run("New", func(t *testing.T) {
		if allocs := testing.AllocsPerRun(10, func() {
			h := sha3.New256()
			b := []byte("ABC")
			h.Write(b)
			out := make([]byte, 0, 32)
			out = h.Sum(out)
			sink ^= out[0]
		}); allocs > want {
			t.Errorf("expected zero allocations, got %0.1f", allocs)
		}
	})
	t.Run("NewShake", func(t *testing.T) {
		if allocs := testing.AllocsPerRun(10, func() {
			h := sha3.NewShake128()
			b := []byte("ABC")
			h.Write(b)
			out := make([]byte, 0, 32)
			out = h.Sum(out)
			sink ^= out[0]
			h.Read(out)
			sink ^= out[0]
		}); allocs > want {
			t.Errorf("expected zero allocations, got %0.1f", allocs)
		}
	})
	t.Run("Sum", func(t *testing.T) {
		if allocs := testing.AllocsPerRun(10, func() {
			b := []byte("ABC")
			out := sha3.Sum256(b)
			sink ^= out[0]
		}); allocs > want {
			t.Errorf("expected zero allocations, got %0.1f", allocs)
		}
	})
}