-
Notifications
You must be signed in to change notification settings - Fork 10
/
dbg_test.go
97 lines (82 loc) · 1.61 KB
/
dbg_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
package godbg
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
)
func TestStdoutDbg(t *testing.T) {
intType := 2
floatType := 2.1
strType := "mystring"
boolType := true
r, w, _ := os.Pipe()
os.Stdout = w
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
Dbg(intType)
Dbg(floatType)
Dbg(strType)
Dbg(boolType)
// back to normal state
w.Close()
out := <-outC
want := `[dbg_test.go:27] intType = 2
[dbg_test.go:28] floatType = 2.1
[dbg_test.go:29] strType = mystring
[dbg_test.go:30] boolType = true
`
if out != want {
t.Fail()
}
}
func TestStderrDbg(t *testing.T) {
errType := fmt.Errorf("New error")
r, w, _ := os.Pipe()
os.Stderr = w
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
Dbg(errType)
// back to normal state
w.Close()
out := <-outC
want := `[dbg_test.go:59] errType = New error
`
if out != want {
t.Fail()
}
}
func BenchmarkWithReverse(b *testing.B) {
input := "/godbg/cmd/main.go"
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := []rune(input)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
result := string(r)
_ = result
}
}
func BenchmarkWithLastIndex(b *testing.B) {
input := "/godbg/cmd/main.go"
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := input[strings.LastIndex(input, "/")+1:]
_ = result
}
}