summaryrefslogtreecommitdiffhomepage
path: root/types/strbuilder/strbuilder_test.go
blob: f21d8afacf06c5de7f0320057ef35608bd0c0436 (plain)
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
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package strbuilder

import (
	"math"
	"testing"
)

func TestBuilder(t *testing.T) {
	const want = "Hello, world 123 -456!"
	bang := []byte("!")
	var got string
	allocs := testing.AllocsPerRun(1000, func() {
		sb := Get()
		sb.WriteString("Hello, world ")
		sb.WriteUint(123)
		sb.WriteByte(' ')
		sb.WriteInt(-456)
		sb.Write(bang)
		got = sb.String()
	})
	if got != want {
		t.Errorf("got %q; want %q", got, want)
	}
	if allocs != 1 {
		t.Errorf("allocs = %v; want 1", allocs)
	}
}

// Verifies scratch buf is large enough.
func TestIntBounds(t *testing.T) {
	const want = "-9223372036854775808 9223372036854775807 18446744073709551615"
	var got string
	allocs := testing.AllocsPerRun(1000, func() {
		sb := Get()
		sb.WriteInt(math.MinInt64)
		sb.WriteByte(' ')
		sb.WriteInt(math.MaxInt64)
		sb.WriteByte(' ')
		sb.WriteUint(math.MaxUint64)
		got = sb.String()
	})
	if got != want {
		t.Errorf("got %q; want %q", got, want)
	}
	if allocs != 1 {
		t.Errorf("allocs = %v; want 1", allocs)
	}
}