forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoded-string-at-index_test.go
executable file
·113 lines (94 loc) · 1.46 KB
/
decoded-string-at-index_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
package problem0880
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// tcs is testcase slice
var tcs = []struct {
S string
K int
ans string
}{
{
"a2345678999999999999999",
12314145213412,
"a",
},
{
"a2b3c4d5e6f7g8h9",
3,
"b",
},
{
"leet2code3for2you2",
160,
"y",
},
{
"leet2code3for2you2",
89,
"t",
},
{
"leet2code3for2you2",
13,
"l",
},
{
"leet2code3for2you2",
90,
"c",
},
{
"leet2code3",
10,
"o",
},
{
"leet2code3for2you",
16,
"t",
},
{
"leet2code3for2you2",
85,
"t",
},
{
"ha22",
5,
"h",
},
{
"a2345678999999999999999",
1,
"a",
},
// 可以有多个 testcase
}
func Test_decodeAtIndex(t *testing.T) {
ast := assert.New(t)
for _, tc := range tcs {
fmt.Printf("~~%v~~\n", tc)
ast.Equal(tc.ans, decodeAtIndex(tc.S, tc.K), "输入:%v", tc)
}
}
func Test_decodeAtIndex_2(t *testing.T) {
ast := assert.New(t)
s := "leat2code3for2you2"
ansStr := "leatleatcodeleatleatcodeleatleatcodeforleatleatcodeleatleatcodeleatleatcodeforyouleatleatcodeleatleatcodeleatleatcodeforleatleatcodeleatleatcodeleatleatcodeforyou"
for i := range ansStr {
expectd := ansStr[i : i+1]
actual := decodeAtIndex(s, i+1)
msg := fmt.Sprintf("%s 的第 %d 个字符应该是 %s", s, i+1, expectd)
ast.Equal(expectd, actual, msg)
}
}
func Benchmark_decodeAtIndex(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range tcs {
decodeAtIndex(tc.S, tc.K)
}
}
}