-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_test.go
127 lines (111 loc) · 2.45 KB
/
stack_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
package core
import (
"fmt"
"log"
"testing"
)
const (
MaxTestDepth = 16
MaxTestSpace = 6
)
func TestHere(t *testing.T) {
if s := fmt.Sprintf("%n", Here()); s != "TestHere" {
t.FailNow()
}
for i := 0; i < MaxDepth; i++ {
f := deepHere(i)
if s := fmt.Sprintf("%n", f); s != "hereHere" {
t.FailNow()
}
}
}
func hereHere() *Frame {
return Here()
}
func deepHere(depth int) *Frame {
if depth > 0 {
return deepHere(depth - 1)
}
return hereHere()
}
func TestStackFrame(t *testing.T) {
if s := fmt.Sprintf("%n", StackFrame(0)); s != "TestStackFrame" {
log.Print(s)
t.FailNow()
}
for i := 0; i < MaxTestDepth; i++ {
for j := 0; j < MaxTestSpace; j++ {
f := deepStackFrame(i, j)
if s := fmt.Sprintf("%n", f); s != "hereStackFrame" {
log.Print(s)
t.FailNow()
}
}
}
}
func deeperStackFrame(depth, skip int) *Frame {
if depth > 0 {
return deeperStackFrame(depth-1, skip)
}
return StackFrame(skip + 1)
}
func hereStackFrame(depth int) *Frame {
return deeperStackFrame(depth, depth)
}
func deepStackFrame(depth, space int) *Frame {
if depth > 0 {
return deepStackFrame(depth-1, space)
}
return hereStackFrame(space)
}
// revive:disable:cognitive-complexity
func TestStackTrace(t *testing.T) {
// revive:enable:cognitive-complexity
stack := StackTrace(0)
if len(stack) < 2 || fmt.Sprintf("%n", stack[0]) != "TestStackTrace" {
t.Fatalf("StackTrace(%v): %s", 0, fmt.Sprintln(stack))
}
for i := 0; i < MaxTestDepth; i++ {
for j := 0; j < MaxTestSpace; j++ {
stack := deepStackTrace(i, j)
if !checkDeepStackTrace(stack, i) {
t.Fatalf("StackTrace(%v, %v): %v: %s",
i, j, len(stack), fmt.Sprintf("%n", stack))
}
}
}
}
func deeperStackTrace(depth, skip int) Stack {
if depth > 0 {
return deeperStackTrace(depth-1, skip)
}
return StackTrace(skip)
}
func deepStackTrace(depth, space int) Stack {
if depth > 0 {
return deepStackTrace(depth-1, space)
}
return deeperStackTrace(space, space)
}
func checkStackFrameName(stack Stack, offset int, name string) bool {
if len(stack) > offset {
s := fmt.Sprintf("%n", stack[offset])
return s == name
}
return false
}
func checkDeepStackTrace(stack Stack, depth int) bool {
t0 := 2 + depth
if !checkStackFrameName(stack, t0, "TestStackTrace") {
return false
}
if !checkStackFrameName(stack, 0, "deeperStackTrace") {
return false
}
for i := 1; i < t0; i++ {
if !checkStackFrameName(stack, i, "deepStackTrace") {
return false
}
}
return true
}