diff options
| author | Christian Himpel <chressie@googlemail.com> | 2010-08-05 23:11:06 +1000 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2010-08-05 23:11:06 +1000 |
| commit | 75f6a0c759e21b3a0fd7ef7c52b73fa24d91eb2e (patch) | |
| tree | 962b49ca35368ff0970fa3c7cba7c81daa2b42cf /src/pkg/bytes/bytes_test.go | |
| parent | 895c5db6df786e9312d187f27d6a4538afd4b0b1 (diff) | |
| download | go-75f6a0c759e21b3a0fd7ef7c52b73fa24d91eb2e.tar.xz | |
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
Diffstat (limited to 'src/pkg/bytes/bytes_test.go')
| -rw-r--r-- | src/pkg/bytes/bytes_test.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/pkg/bytes/bytes_test.go b/src/pkg/bytes/bytes_test.go index b91ae5734d..de503878cd 100644 --- a/src/pkg/bytes/bytes_test.go +++ b/src/pkg/bytes/bytes_test.go @@ -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 { |
