| 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 buffer |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/race" |
| 9 |
"internal/testenv" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func Test(t *testing.T) { |
| 14 |
b := New() |
| 15 |
defer b.Free() |
| 16 |
b.WriteString("hello") |
| 17 |
b.WriteByte(',') |
| 18 |
b.Write([]byte(" world")) |
| 19 |
|
| 20 |
got := b.String() |
| 21 |
want := "hello, world" |
| 22 |
if got != want { |
| 23 |
t.Errorf("got %q, want %q", got, want) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func TestAlloc(t *testing.T) { |
| 28 |
if race.Enabled { |
| 29 |
t.Skip("skipping test in race mode") |
| 30 |
} |
| 31 |
testenv.SkipIfOptimizationOff(t) |
| 32 |
got := int(testing.AllocsPerRun(5, func() { |
| 33 |
b := New() |
| 34 |
defer b.Free() |
| 35 |
b.WriteString("not 1K worth of bytes") |
| 36 |
})) |
| 37 |
if got != 0 { |
| 38 |
t.Errorf("got %d allocs, want 0", got) |
| 39 |
} |
| 40 |
} |