mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes: add IndexRune, FieldsFunc and To*Special
Basically these functions are implemented the same way as the corresponding functions in the strings package. Test functions are implemented for IndexRune and FieldsFunc. Additionally two typos are fixed in packages bytes and strings. R=r CC=golang-dev https://golang.org/cl/1696062
This commit is contained in:
parent
895c5db6df
commit
75f6a0c759
3 changed files with 83 additions and 5 deletions
|
|
@ -125,6 +125,15 @@ var indexAnyTests = []BinOpTest{
|
|||
BinOpTest{dots + dots + dots, " ", -1},
|
||||
}
|
||||
|
||||
var indexRuneTests = []BinOpTest{
|
||||
BinOpTest{"", "a", -1},
|
||||
BinOpTest{"", "☺", -1},
|
||||
BinOpTest{"foo", "☹", -1},
|
||||
BinOpTest{"foo", "o", 1},
|
||||
BinOpTest{"foo☺bar", "☺", 3},
|
||||
BinOpTest{"foo☺☻☹bar", "☹", 9},
|
||||
}
|
||||
|
||||
// Execute f on each test case. funcName should be the name of f; it's used
|
||||
// in failure reports.
|
||||
func runIndexTests(t *testing.T, f func(s, sep []byte) int, funcName string, testCases []BinOpTest) {
|
||||
|
|
@ -168,6 +177,17 @@ func TestIndexByte(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIndexRune(t *testing.T) {
|
||||
for _, tt := range indexRuneTests {
|
||||
a := []byte(tt.a)
|
||||
r, _ := utf8.DecodeRuneInString(tt.b)
|
||||
pos := IndexRune(a, r)
|
||||
if pos != tt.i {
|
||||
t.Errorf(`IndexRune(%q, '%c') = %v`, tt.a, r, pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkIndexByte4K(b *testing.B) { bmIndex(b, IndexByte, 4<<10) }
|
||||
|
||||
func BenchmarkIndexByte4M(b *testing.B) { bmIndex(b, IndexByte, 4<<20) }
|
||||
|
|
@ -336,6 +356,23 @@ func TestFields(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestFieldsFunc(t *testing.T) {
|
||||
pred := func(c int) bool { return c == 'X' }
|
||||
var fieldsFuncTests = []FieldsTest{
|
||||
FieldsTest{"", []string{}},
|
||||
FieldsTest{"XX", []string{}},
|
||||
FieldsTest{"XXhiXXX", []string{"hi"}},
|
||||
FieldsTest{"aXXbXXXcX", []string{"a", "b", "c"}},
|
||||
}
|
||||
for _, tt := range fieldsFuncTests {
|
||||
a := FieldsFunc([]byte(tt.s), pred)
|
||||
result := arrayOfString(a)
|
||||
if !eq(result, tt.a) {
|
||||
t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test case for any function which accepts and returns a byte array.
|
||||
// For ease of creation, we write the byte arrays as strings.
|
||||
type StringTest struct {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue