golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/slog/attr_test.go (about)

     1  // Copyright 2022 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  package slog
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestAttrNoAlloc(t *testing.T) {
    13  	// Assign values just to make sure the compiler doesn't optimize away the statements.
    14  	var (
    15  		i int64
    16  		u uint64
    17  		f float64
    18  		b bool
    19  		s string
    20  		x any
    21  		p = &i
    22  		d time.Duration
    23  	)
    24  	a := int(testing.AllocsPerRun(5, func() {
    25  		i = Int64("key", 1).Value.Int64()
    26  		u = Uint64("key", 1).Value.Uint64()
    27  		f = Float64("key", 1).Value.Float64()
    28  		b = Bool("key", true).Value.Bool()
    29  		s = String("key", "foo").Value.String()
    30  		d = Duration("key", d).Value.Duration()
    31  		x = Any("key", p).Value.Any()
    32  	}))
    33  	if a != 0 {
    34  		t.Errorf("got %d allocs, want zero", a)
    35  	}
    36  	_ = u
    37  	_ = f
    38  	_ = b
    39  	_ = s
    40  	_ = x
    41  }