-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_test.go
More file actions
450 lines (403 loc) · 11.5 KB
/
count_test.go
File metadata and controls
450 lines (403 loc) · 11.5 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package wordcounter_test
import (
"strings"
"testing"
"github.com/100gle/wordcounter"
)
func TestCounter_Count(t *testing.T) {
tests := []struct {
name string
input any
wantErr bool
}{
{
name: "Empty string",
input: "",
wantErr: true,
},
{
name: "Valid string",
input: "Hello 世界",
wantErr: false,
},
{
name: "Valid byte slice",
input: []byte("Hello 世界"),
wantErr: false,
},
{
name: "Invalid input type - int",
input: 42,
wantErr: true,
},
{
name: "Invalid input type - float",
input: 3.14,
wantErr: true,
},
{
name: "Invalid input type - bool",
input: true,
wantErr: true,
},
{
name: "Invalid input type - nil",
input: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := wordcounter.NewCounter()
err := tc.Count(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("Counter.Count() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if tc.TotalChars != 8 {
t.Errorf("Counter.Count() total chars = %d, want 8", tc.TotalChars)
}
if tc.ChineseChars != 2 {
t.Errorf("Counter.Count() chinese chars = %d, want 2", tc.ChineseChars)
}
if tc.Lines != 1 {
t.Errorf("Counter.Count() space chars = %d, want 1", tc.Lines)
}
}
})
}
}
func TestCounter_CountBytes_Comprehensive(t *testing.T) {
tests := []struct {
name string
input string
expectedLines int
expectedChinese int
expectedNonChinese int
expectedTotal int
}{
{
name: "Simple Chinese text",
input: "你好世界",
expectedLines: 1,
expectedChinese: 4,
expectedNonChinese: 0,
expectedTotal: 4,
},
{
name: "Mixed Chinese and English",
input: "Hello 你好 World 世界",
expectedLines: 1,
expectedChinese: 4,
expectedNonChinese: 13,
expectedTotal: 17,
},
{
name: "Multiple lines with Chinese",
input: "第一行\n第二行\n第三行",
expectedLines: 3,
expectedChinese: 9,
expectedNonChinese: 0,
expectedTotal: 9,
},
{
name: "Text with Chinese punctuation",
input: "你好,世界!",
expectedLines: 1,
expectedChinese: 6, // Including Chinese punctuation
expectedNonChinese: 0,
expectedTotal: 6,
},
{
name: "Empty lines",
input: "第一行\n\n第三行",
expectedLines: 3,
expectedChinese: 6,
expectedNonChinese: 0,
expectedTotal: 6,
},
{
name: "Only English",
input: "Hello World",
expectedLines: 1,
expectedChinese: 0,
expectedNonChinese: 11,
expectedTotal: 11,
},
{
name: "Numbers and symbols",
input: "123 + 456 = 579",
expectedLines: 1,
expectedChinese: 0,
expectedNonChinese: 15,
expectedTotal: 15,
},
{
name: "Unicode symbols",
input: "😀😃😄 emoji test",
expectedLines: 1,
expectedChinese: 0,
expectedNonChinese: 14, // Emojis count as non-Chinese (3 emojis + 11 other chars)
expectedTotal: 14,
},
{
name: "Traditional Chinese",
input: "繁體中文測試",
expectedLines: 1,
expectedChinese: 6,
expectedNonChinese: 0,
expectedTotal: 6,
},
{
name: "Single character",
input: "中",
expectedLines: 1,
expectedChinese: 1,
expectedNonChinese: 0,
expectedTotal: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := wordcounter.NewCounter()
err := tc.CountBytes([]byte(tt.input))
if err != nil {
t.Errorf("Counter.CountBytes() error = %v", err)
return
}
stats := tc.GetStats()
if stats.Lines != tt.expectedLines {
t.Errorf("Lines = %d, want %d", stats.Lines, tt.expectedLines)
}
if stats.ChineseChars != tt.expectedChinese {
t.Errorf("ChineseChars = %d, want %d", stats.ChineseChars, tt.expectedChinese)
}
if stats.NonChineseChars != tt.expectedNonChinese {
t.Errorf("NonChineseChars = %d, want %d", stats.NonChineseChars, tt.expectedNonChinese)
}
if stats.TotalChars != tt.expectedTotal {
t.Errorf("TotalChars = %d, want %d", stats.TotalChars, tt.expectedTotal)
}
})
}
}
func TestCounter_CountBytes_EdgeCases(t *testing.T) {
tests := []struct {
name string
input []byte
wantErr bool
}{
{
name: "Empty byte slice",
input: []byte{},
wantErr: false,
},
{
name: "Nil byte slice",
input: nil,
wantErr: false,
},
{
name: "Single newline",
input: []byte("\n"),
wantErr: false,
},
{
name: "Multiple newlines",
input: []byte("\n\n\n"),
wantErr: false,
},
{
name: "Invalid UTF-8 sequence",
input: []byte{0xFF, 0xFE, 0xFD},
wantErr: false, // Should handle gracefully
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := wordcounter.NewCounter()
err := tc.CountBytes(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("Counter.CountBytes() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestCounter_CountBytes_EmptyData(t *testing.T) {
tests := []struct {
name string
input []byte
}{
{
name: "Empty byte slice",
input: []byte{},
},
{
name: "Nil byte slice",
input: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := wordcounter.NewCounter()
err := tc.CountBytes(tt.input)
if err != nil {
t.Errorf("Counter.CountBytes() error = %v, expected no error for empty data", err)
return
}
stats := tc.GetStats()
if stats.Lines != 0 || stats.ChineseChars != 0 || stats.NonChineseChars != 0 || stats.TotalChars != 0 {
t.Errorf("Counter.CountBytes() for empty data, expected all zero stats, got: Lines=%d, ChineseChars=%d, NonChineseChars=%d, TotalChars=%d",
stats.Lines, stats.ChineseChars, stats.NonChineseChars, stats.TotalChars)
}
})
}
}
func TestCounter_LargeText(t *testing.T) {
// Test with large text to ensure performance
largeText := strings.Repeat("这是一个测试文本。", 10000) // 10,000 repetitions
tc := wordcounter.NewCounter()
err := tc.Count(largeText)
if err != nil {
t.Errorf("Counter.Count() error = %v", err)
return
}
stats := tc.GetStats()
// "这是一个测试文本。" has 9 characters total:
// 8 Chinese characters: 这是一个测试文本
// 1 Chinese punctuation: 。(U+3002) - now counted as Chinese
expectedChinese := 90000 // 9 Chinese chars (including punctuation) * 10,000 repetitions
expectedNonChinese := 0 // No non-Chinese characters
expectedTotal := 90000
if stats.ChineseChars != expectedChinese {
t.Errorf("ChineseChars = %d, want %d", stats.ChineseChars, expectedChinese)
}
if stats.NonChineseChars != expectedNonChinese {
t.Errorf("NonChineseChars = %d, want %d", stats.NonChineseChars, expectedNonChinese)
}
if stats.TotalChars != expectedTotal {
t.Errorf("TotalChars = %d, want %d", stats.TotalChars, expectedTotal)
}
}
// Benchmark tests for Counter performance
// BenchmarkCounter_CountBytes_SmallText benchmarks counting small text
func BenchmarkCounter_CountBytes_SmallText(b *testing.B) {
text := "Hello 世界! This is a test 这是一个测试。"
data := []byte(text)
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc := wordcounter.NewCounter()
tc.CountBytes(data)
}
}
// BenchmarkCounter_CountBytes_MediumText benchmarks counting medium-sized text
func BenchmarkCounter_CountBytes_MediumText(b *testing.B) {
// Create a medium-sized text (about 1KB)
text := strings.Repeat("这是一个中文测试文本,包含了各种字符。Hello World! 123456789.\n", 20)
data := []byte(text)
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc := wordcounter.NewCounter()
tc.CountBytes(data)
}
}
// BenchmarkCounter_CountBytes_LargeText benchmarks counting large text
func BenchmarkCounter_CountBytes_LargeText(b *testing.B) {
// Create a large text (about 100KB)
text := strings.Repeat("这是一个中文测试文本,包含了各种字符。Hello World! 123456789.\n", 2000)
data := []byte(text)
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc := wordcounter.NewCounter()
tc.CountBytes(data)
}
}
// BenchmarkCounter_CountBytes_VeryLargeText benchmarks counting very large text
func BenchmarkCounter_CountBytes_VeryLargeText(b *testing.B) {
// Create a very large text (about 1MB)
text := strings.Repeat("这是一个中文测试文本,包含了各种字符。Hello World! 123456789.\n", 20000)
data := []byte(text)
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc := wordcounter.NewCounter()
tc.CountBytes(data)
}
}
// BenchmarkCounter_CountBytes_ChineseOnly benchmarks counting Chinese-only text
func BenchmarkCounter_CountBytes_ChineseOnly(b *testing.B) {
text := strings.Repeat("这是一个完全由中文组成的测试文本,用于测试中文字符识别的性能。\n", 1000)
data := []byte(text)
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc := wordcounter.NewCounter()
tc.CountBytes(data)
}
}
// BenchmarkCounter_CountBytes_EnglishOnly benchmarks counting English-only text
func BenchmarkCounter_CountBytes_EnglishOnly(b *testing.B) {
text := strings.Repeat("This is a test text composed entirely of English characters for performance testing.\n", 1000)
data := []byte(text)
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc := wordcounter.NewCounter()
tc.CountBytes(data)
}
}
// BenchmarkCounter_CountBytes_MixedContent benchmarks counting mixed content
func BenchmarkCounter_CountBytes_MixedContent(b *testing.B) {
text := strings.Repeat("Mixed content 混合内容: English 英文, Numbers 123456, Symbols !@#$%^&*(), Emojis 😀😃😄\n", 1000)
data := []byte(text)
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc := wordcounter.NewCounter()
tc.CountBytes(data)
}
}
// BenchmarkCounter_Count_String benchmarks the Count method with string input
func BenchmarkCounter_Count_String(b *testing.B) {
text := strings.Repeat("这是一个测试文本 This is a test text.\n", 1000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc := wordcounter.NewCounter()
tc.Count(text)
}
}
// BenchmarkCounter_Count_ByteSlice benchmarks the Count method with byte slice input
func BenchmarkCounter_Count_ByteSlice(b *testing.B) {
text := strings.Repeat("这是一个测试文本 This is a test text.\n", 1000)
data := []byte(text)
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc := wordcounter.NewCounter()
tc.Count(data)
}
}
// BenchmarkCounter_MultipleOperations benchmarks multiple counting operations on the same counter
func BenchmarkCounter_MultipleOperations(b *testing.B) {
texts := []string{
"第一段文本 First text segment",
"第二段文本 Second text segment",
"第三段文本 Third text segment",
"第四段文本 Fourth text segment",
"第五段文本 Fifth text segment",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc := wordcounter.NewCounter()
for _, text := range texts {
tc.Count(text)
}
}
}
// BenchmarkIsChinese benchmarks the isChinese function indirectly through character counting
func BenchmarkIsChinese(b *testing.B) {
// Create text with various character types to test isChinese performance
text := "中文English123!@#😀"
data := []byte(text)
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc := wordcounter.NewCounter()
tc.CountBytes(data)
}
}