-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer_test.go
More file actions
91 lines (81 loc) · 2.06 KB
/
tokenizer_test.go
File metadata and controls
91 lines (81 loc) · 2.06 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
package chunkx
import (
"testing"
)
func TestSimpleTokenCounter(t *testing.T) {
counter := &SimpleTokenCounter{}
tests := []struct {
name string
input string
expected int
}{
{"empty string", "", 0},
{"single word", "hello", 1},
{"multiple words", "hello world foo bar", 4},
{"with newlines", "hello\nworld", 2},
{"with tabs", "hello\tworld", 2},
{"multiple spaces", "hello world", 2},
{"code snippet", "func main() { fmt.Println(\"hello\") }", 5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
count, err := counter.CountTokens(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if count != tt.expected {
t.Errorf("expected %d tokens, got %d", tt.expected, count)
}
})
}
}
func TestByteCounter(t *testing.T) {
counter := &ByteCounter{}
tests := []struct {
name string
input string
expected int
}{
{"empty string", "", 0},
{"single char", "a", 1},
{"hello world", "hello world", 11},
{"unicode", "hello 世界", 12}, // "hello " = 6 bytes, "世界" = 6 bytes (3 bytes per char)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
count, err := counter.CountTokens(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if count != tt.expected {
t.Errorf("expected %d bytes, got %d", tt.expected, count)
}
})
}
}
func TestLineCounter(t *testing.T) {
counter := &LineCounter{}
tests := []struct {
name string
input string
expected int
}{
{"empty string", "", 0},
{"single line no newline", "hello world", 1},
{"single line with newline", "hello world\n", 1},
{"multiple lines", "line1\nline2\nline3", 3},
{"multiple lines with trailing newline", "line1\nline2\nline3\n", 3},
{"empty lines", "line1\n\nline3", 3},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
count, err := counter.CountTokens(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if count != tt.expected {
t.Errorf("expected %d lines, got %d", tt.expected, count)
}
})
}
}