-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissue62407_test.go
62 lines (54 loc) · 2.28 KB
/
issue62407_test.go
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
62
// Copyright 2024 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.
package test
import (
"reflect"
"testing"
)
//go:noinline
func foo() string { return "foofoofoofoofoofo" } // len == 17
//go:noinline
func empty() string { return "" }
func TestConcatBytes(t *testing.T) {
empty := empty()
s := foo()
tests := map[string]struct {
got []byte
want []byte
}{
"two empty elements": {got: []byte(empty + empty), want: []byte{}},
"two nonempty elements": {got: []byte(s + s), want: append([]byte(foo()), foo()...)},
"one empty and one nonempty element": {got: []byte(s + empty), want: []byte(foo())},
"multiple empty elements": {got: []byte(empty + empty + empty + empty + empty + empty), want: []byte{}},
"multiple nonempty elements": {got: []byte("1" + "2" + "3" + "4" + "5" + "6"), want: []byte("123456")},
}
for name, test := range tests {
if !reflect.DeepEqual(test.got, test.want) {
t.Errorf("[%s] got: %s, want: %s", name, test.got, test.want)
}
}
}
func TestConcatBytesAllocations(t *testing.T) {
empty := empty()
s := foo()
tests := map[string]struct {
f func() []byte
allocs float64
}{
"two empty elements": {f: func() []byte { return []byte(empty + empty) }, allocs: 0},
"multiple empty elements": {f: func() []byte { return []byte(empty + empty + empty + empty + empty + empty) }, allocs: 0},
"two elements": {f: func() []byte { return []byte(s + s) }, allocs: 1},
"three elements": {f: func() []byte { return []byte(s + s + s) }, allocs: 1},
"four elements": {f: func() []byte { return []byte(s + s + s + s) }, allocs: 1},
"five elements": {f: func() []byte { return []byte(s + s + s + s + s) }, allocs: 1},
"one empty and one nonempty element": {f: func() []byte { return []byte(s + empty) }, allocs: 1},
"two empty and two nonempty element": {f: func() []byte { return []byte(s + empty + s + empty) }, allocs: 1},
}
for name, test := range tests {
allocs := testing.AllocsPerRun(100, func() { test.f() })
if allocs != test.allocs {
t.Errorf("concatbytes [%s]: %v allocs, want %v", name, allocs, test.allocs)
}
}
}