-
Notifications
You must be signed in to change notification settings - Fork 48
/
utils_test.go
147 lines (139 loc) · 3.29 KB
/
utils_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
package html2article
import (
"testing"
"time"
)
func TestCompress(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test0",
args: args{
str: "test ",
},
want: "test ",
},
{
name: "test1",
args: args{
str: " test ",
},
want: " test ",
},
{
name: "test2",
args: args{
str: "test 2 ",
},
want: "test 2 ",
},
{
name: "test3",
args: args{
str: "test 3 \n ",
},
want: "test 3 ",
},
{
name: "test4",
args: args{
str: "test4",
},
want: "test4",
},
{
name: "test5",
args: args{
str: "test5 test5 \n test ",
},
want: "test5 test5 test ",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Compress(tt.args.str); got != tt.want {
t.Errorf("Compress() = %v, want %v", got, tt.want)
}
})
}
}
func TestDistance(t *testing.T) {
tests := []struct {
name string
a string
b string
maxValue int
want int
ok bool
}{
{"1", "abc", "ab", 10, 1, true},
{"2", "abc", "abd", 10, 1, true},
{"3", "ab", "abcef", 10, 3, true},
{"4", "我爱中国", "我是中国人", 10, 2, true},
{"5", "abcdefg", "hijklmnxfdfd", 3, (1 << 31) - 1, false},
{"6", "36氪-创业学院教授:创业者都应该学习亚马逊,敢于颠覆市场 | 创投圈", "36氪-创业学院教授:创业者都应该学习亚马逊,敢于颠覆市场", 100, 6, true},
{"7", "提示", "36氪-创业学院教授:创业者都应该学习亚马逊,敢于颠覆市场 | 创投圈", 117, 35, true},
{"8", "提示", "36氪-创业学院教授:创业者都应该学习亚马逊,敢于颠覆市场 | 创投圈", 11, 35, false},
{"9", "本文作者:三川 2017-01-20 18:42 导语: Torch 的新生还是终结?", "Facebook 发布开源框架 PyTorch orch 终于被移植到 Python 生态圈 | 雷锋网", 15, 100, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := distanceExit(tt.a, tt.b, tt.maxValue)
if ok != tt.ok {
t.Errorf("Not ok equal %v, want %v", ok, tt.ok)
}
if ok {
if got != tt.want {
t.Errorf("distanceExit() = %v, want %v", got, tt.want)
}
}
})
}
}
func TestDiffstr(t *testing.T) {
tests := []struct {
name string
a string
b string
want int
}{
{"1", "abc", "ab", 1},
{"2", "abc", "abd", 2},
{"3", "ab", "abcef", 3},
{"4", "我的站长之路 丨 伊成Blog", "伊成Blog", 8},
{"5", "我的站长之路 丨 伊成Blog", "我的站长之路", 8},
{"6", "abc", "abef", 3},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := diffString(tt.a, tt.b)
if got != tt.want {
t.Errorf("diffString() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetTime(t *testing.T) {
tests := []struct {
name string
a string
want int64
}{
{"1", "fdaf5小时前 ggagg", time.Now().Add(-(5-8)*time.Hour).Unix() / 3600 * 3600},
{"2", "hgha3天前fdsa", (time.Now().Add(-3*time.Hour*24 + time.Hour*8).Unix()) / int64(24*3600) * int64(24*3600)},
{"3", "2017-02-14 05:48", 1487051280},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getTime(tt.a); got != tt.want {
t.Errorf("Time = %v, want %v", got, tt.want)
}
})
}
}