From 75f6a0c759e21b3a0fd7ef7c52b73fa24d91eb2e Mon Sep 17 00:00:00 2001 From: Christian Himpel Date: Thu, 5 Aug 2010 23:11:06 +1000 Subject: 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 --- src/pkg/bytes/bytes_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/pkg/bytes/bytes_test.go') 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 { -- cgit v1.3-5-g9baa