aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/bytes/bytes_test.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-11-18 19:23:08 -0800
committerRob Pike <r@golang.org>2009-11-18 19:23:08 -0800
commit8c1a627e5cf1621cf44387b13d9a0631e44236ae (patch)
tree198fa6a2f7c90a5489a169be3d9cc0b5870b9d04 /src/pkg/bytes/bytes_test.go
parent1a8ebcc4b8502460d61fafb09d89a8275f98e547 (diff)
downloadgo-8c1a627e5cf1621cf44387b13d9a0631e44236ae.tar.xz
add bytes.IndexByte; common case we can make fast later.
also pick off the special case in strings.Index. don't want strings.IndexByte because the call site will very rarely need to allocate and we can handle the test in the code itself. bytes.IndexByte can avoid a common allocation. R=rsc CC=golang-dev https://golang.org/cl/156091
Diffstat (limited to 'src/pkg/bytes/bytes_test.go')
-rw-r--r--src/pkg/bytes/bytes_test.go76
1 files changed, 59 insertions, 17 deletions
diff --git a/src/pkg/bytes/bytes_test.go b/src/pkg/bytes/bytes_test.go
index 1b197e1dfb..b7f8262931 100644
--- a/src/pkg/bytes/bytes_test.go
+++ b/src/pkg/bytes/bytes_test.go
@@ -39,41 +39,83 @@ var faces = "☺☻☹"
var commas = "1,2,3,4"
var dots = "1....2....3....4"
-type CompareTest struct {
+type BinOpTest struct {
a string;
b string;
- cmp int;
+ i int;
}
-var comparetests = []CompareTest{
- CompareTest{"", "", 0},
- CompareTest{"a", "", 1},
- CompareTest{"", "a", -1},
- CompareTest{"abc", "abc", 0},
- CompareTest{"ab", "abc", -1},
- CompareTest{"abc", "ab", 1},
- CompareTest{"x", "ab", 1},
- CompareTest{"ab", "x", -1},
- CompareTest{"x", "a", 1},
- CompareTest{"b", "x", -1},
+var comparetests = []BinOpTest{
+ BinOpTest{"", "", 0},
+ BinOpTest{"a", "", 1},
+ BinOpTest{"", "a", -1},
+ BinOpTest{"abc", "abc", 0},
+ BinOpTest{"ab", "abc", -1},
+ BinOpTest{"abc", "ab", 1},
+ BinOpTest{"x", "ab", 1},
+ BinOpTest{"ab", "x", -1},
+ BinOpTest{"x", "a", 1},
+ BinOpTest{"b", "x", -1},
}
func TestCompare(t *testing.T) {
- for i := 0; i < len(comparetests); i++ {
- tt := comparetests[i];
+ for _, tt := range comparetests {
a := strings.Bytes(tt.a);
b := strings.Bytes(tt.b);
cmp := Compare(a, b);
eql := Equal(a, b);
- if cmp != tt.cmp {
+ if cmp != tt.i {
t.Errorf(`Compare(%q, %q) = %v`, tt.a, tt.b, cmp)
}
- if eql != (tt.cmp == 0) {
+ if eql != (tt.i == 0) {
t.Errorf(`Equal(%q, %q) = %v`, tt.a, tt.b, eql)
}
}
}
+var indextests = []BinOpTest{
+ BinOpTest{"", "", 0},
+ BinOpTest{"a", "", 0},
+ BinOpTest{"", "a", -1},
+ BinOpTest{"abc", "abc", 0},
+ BinOpTest{"ab", "abc", -1},
+ BinOpTest{"abc", "bc", 1},
+ BinOpTest{"x", "ab", -1},
+ // one-byte tests for IndexByte
+ BinOpTest{"ab", "x", -1},
+ BinOpTest{"", "a", -1},
+ BinOpTest{"x", "a", -1},
+ BinOpTest{"x", "x", 0},
+ BinOpTest{"abc", "a", 0},
+ BinOpTest{"abc", "b", 1},
+ BinOpTest{"abc", "c", 2},
+ BinOpTest{"abc", "x", -1},
+}
+
+func TestIndex(t *testing.T) {
+ for _, tt := range indextests {
+ a := strings.Bytes(tt.a);
+ b := strings.Bytes(tt.b);
+ pos := Index(a, b);
+ if pos != tt.i {
+ t.Errorf(`Index(%q, %q) = %v`, tt.a, tt.b, pos)
+ }
+ }
+}
+
+func TestIndexByte(t *testing.T) {
+ for _, tt := range indextests {
+ if len(tt.b) != 1 {
+ continue
+ }
+ a := strings.Bytes(tt.a);
+ b := tt.b[0];
+ pos := IndexByte(a, b);
+ if pos != tt.i {
+ t.Errorf(`IndexByte(%q, '%c') = %v`, tt.a, b, pos)
+ }
+ }
+}
type ExplodeTest struct {
s string;