-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
left_pad_test.go
125 lines (118 loc) · 1.9 KB
/
left_pad_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
package text
import (
"testing"
)
func TestLeftPadMaxLine(t *testing.T) {
cases := []struct {
input, output string
maxValueLength int
leftPad int
}{
{
"foo",
"foo ",
4,
0,
},
{
"foofoofoo",
"foo…",
4,
0,
},
{
"foo",
"foo ",
10,
0,
},
{
"foo",
" f…",
4,
2,
},
{
"foofoofoo",
" foo…",
6,
2,
},
{
"foo",
" foo ",
10,
2,
},
{
"\x1b[31mbar\x1b[0m",
" \x1b[31mbar\x1b[0m ",
10,
2,
},
{
"\x1b[31mfoofoobar\x1b[0m",
" \x1b[31mfo…\x1b[0m",
5,
2,
},
}
for i, tc := range cases {
result := LeftPadMaxLine(tc.input, tc.maxValueLength, tc.leftPad)
if result != tc.output {
t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s`\n\nActual Output:\n\n`%s`",
i, tc.input, tc.output, result)
}
}
}
func BenchmarkLeftPadMaxLine(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
LeftPadMaxLine("foofoofoo", 6, 2)
}
}
func TestLeftPadLines(t *testing.T) {
cases := []struct {
input, output string
leftPad int
}{
{
"foo",
"foo",
0,
},
{
"foo\n",
"foo\n",
0,
},
{
"foo\nbar\n",
" foo\n bar\n ",
4,
},
{
"foo\n",
" foo\n ",
4,
},
{
"敏捷 A quick 的狐狸 \nfox 跳过 jumps\n over a lazy 了一只懒狗 dog。",
" 敏捷 A quick 的狐狸 \n fox 跳过 jumps\n over a lazy 了一只懒狗 dog。",
4,
},
}
for i, tc := range cases {
result := LeftPadLines(tc.input, tc.leftPad)
if result != tc.output {
t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s`\n\nActual Output:\n\n`%s`",
i, tc.input, tc.output, result)
}
}
}
func BenchmarkLeftPadLines(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
LeftPadLines("敏捷 A quick 的狐狸 \nfox 跳过 jumps\n over a lazy 了一只懒狗 dog。", 6)
}
}