-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathresult_test.go
161 lines (148 loc) · 3.61 KB
/
result_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2022 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 benchfmt
import (
"fmt"
"reflect"
"testing"
)
func TestResultSetConfig(t *testing.T) {
r := &Result{}
check := func(want ...string) {
t.Helper()
var kv []string
for _, cfg := range r.Config {
kv = append(kv, fmt.Sprintf("%s: %s", cfg.Key, cfg.Value))
}
if !reflect.DeepEqual(want, kv) {
t.Errorf("want %q, got %q", want, kv)
}
// Check the index.
for i, cfg := range r.Config {
gotI, ok := r.ConfigIndex(cfg.Key)
if !ok {
t.Errorf("key %s missing from index", cfg.Key)
} else if i != gotI {
t.Errorf("key %s index: want %d, got %d", cfg.Key, i, gotI)
}
}
if len(r.Config) != len(r.configPos) {
t.Errorf("index size mismatch: %d file configs, %d index length", len(r.Config), len(r.configPos))
}
}
// Basic key additions.
check()
r.SetConfig("a", "b")
check("a: b")
r.SetConfig("x", "y")
check("a: b", "x: y")
r.SetConfig("z", "w")
check("a: b", "x: y", "z: w")
// Update value.
r.SetConfig("x", "z")
check("a: b", "x: z", "z: w")
// Delete key.
r.SetConfig("a", "") // Check swapping
check("z: w", "x: z")
r.SetConfig("x", "") // Last key
check("z: w")
r.SetConfig("c", "") // Non-existent
check("z: w")
// Add key after deletion.
r.SetConfig("c", "d")
check("z: w", "c: d")
}
func TestResultGetConfig(t *testing.T) {
r := &Result{}
check := func(key, want string) {
t.Helper()
got := r.GetConfig(key)
if want != got {
t.Errorf("for key %s: want %s, got %s", key, want, got)
}
}
check("x", "")
r.SetConfig("x", "y")
check("x", "y")
r.SetConfig("a", "b")
check("a", "b")
check("x", "y")
r.SetConfig("a", "")
check("a", "")
check("x", "y")
// Test a literal.
r = &Result{
Config: []Config{{Key: "a", Value: []byte("b")}},
}
check("a", "b")
check("x", "")
}
func TestResultValue(t *testing.T) {
r := &Result{
Values: []Value{{42, "ns/op", 42e-9, "sec/op"}, {24, "B/op", 0, ""}},
}
check := func(unit string, want float64) {
t.Helper()
got, ok := r.Value(unit)
if !ok {
t.Errorf("missing unit %s", unit)
} else if want != got {
t.Errorf("for unit %s: want %v, got %v", unit, want, got)
}
}
check("ns/op", 42)
check("B/op", 24)
_, ok := r.Value("B/sec")
if ok {
t.Errorf("unexpectedly found unit %s", "B/sec")
}
}
func TestBaseName(t *testing.T) {
check := func(fullName string, want string) {
t.Helper()
got := string(Name(fullName).Base())
if got != want {
t.Errorf("BaseName(%q) = %q, want %q", fullName, got, want)
}
}
check("Test", "Test")
check("Test-42", "Test")
check("Test/foo", "Test")
check("Test/foo-42", "Test")
}
func TestNameParts(t *testing.T) {
check := func(fullName string, base string, parts ...string) {
t.Helper()
got, gotParts := Name(fullName).Parts()
fail := string(got) != base
if len(gotParts) != len(parts) {
fail = true
} else {
for i := range parts {
if parts[i] != string(gotParts[i]) {
fail = true
}
}
}
if fail {
t.Errorf("FullName(%q) = %q, %q, want %q, %q", fullName, got, gotParts, base, parts)
}
}
check("Test", "Test")
// Gomaxprocs
check("Test-42", "Test", "-42")
// Subtests
check("Test/foo", "Test", "/foo")
check("Test/foo=42/bar=24", "Test", "/foo=42", "/bar=24")
// Both
check("Test/foo=123-42", "Test", "/foo=123", "-42")
// Looks like gomaxprocs, but isn't
check("Test/foo-bar", "Test", "/foo-bar")
check("Test/foo-1/bar", "Test", "/foo-1", "/bar")
// Trailing slash
check("Test/foo/", "Test", "/foo", "/")
// Empty name
check("", "")
check("/a/b", "", "/a", "/b")
}