-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubstring_test.go
More file actions
34 lines (32 loc) · 978 Bytes
/
substring_test.go
File metadata and controls
34 lines (32 loc) · 978 Bytes
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
package str
import "testing"
func TestSubstring(t *testing.T) {
tests := []struct {
name string
start int
length int
input string
expected string
}{
{"normal", 0, 3, "1234567890", "123"},
{"offset", 1, 3, "1234567890", "234"},
{"negative start", -3, 2, "abcde", "cd"},
{"negative start full", -3, 3, "abcde", "cde"},
{"negative length", 0, -1, "hello", ""},
{"zero length", 0, 0, "hello", ""},
{"out of bounds start", 20, 3, "hello", ""},
{"length exceeds", 3, 100, "hello", "lo"},
{"empty input", 0, 3, "", ""},
{"negative out of bounds", -10, 3, "hello", "hel"},
{"multibyte", 0, 2, "你好世界", "你好"},
{"negative start single", -1, 3, "1234567890", "0"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Substring(tt.start, tt.length)(tt.input)
if got != tt.expected {
t.Errorf("Substring(%d, %d)(%q) = %q, want %q", tt.start, tt.length, tt.input, got, tt.expected)
}
})
}
}