move strings.Buffer into bytes

delete strings.Buffer
add a test for a bug not caught before (mustn't install zero-length blocks)

R=rsc
DELTA=987  (289 added, 587 deleted, 111 changed)
OCL=34850
CL=34850
This commit is contained in:
Rob Pike 2009-09-21 12:59:14 -07:00
parent fed4770685
commit 6efd7e6b8f
9 changed files with 372 additions and 671 deletions

View file

@ -11,38 +11,65 @@ import (
)
const N = 10000; // make this bigger for a larger (and slower) test
var data []byte; // test data for write tests
const N = 10000 // make this bigger for a larger (and slower) test
var data string // test data for write tests
var bytes []byte // test data; same as data but as a slice.
func init() {
data = make([]byte, N);
for i := 0; i < len(data); i++ {
data[i] = 'a' + byte(i % 26)
bytes = make([]byte, N);
for i := 0; i < N; i++ {
bytes[i] = 'a' + byte(i % 26)
}
data = string(bytes);
}
// Verify that contents of buf match the string s.
func check(t *testing.T, testname string, buf *Buffer, s string) {
if buf.Len() != len(buf.Bytes()) {
t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d\n", testname, buf.Len(), len(buf.Bytes()))
bytes := buf.Bytes();
str := buf.String();
if buf.Len() != len(bytes) {
t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d\n", testname, buf.Len(), len(bytes))
}
if buf.Len() != len(str) {
t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d\n", testname, buf.Len(), len(str))
}
if buf.Len() != len(s) {
t.Errorf("%s: buf.Len() == %d, len(s) == %d\n", testname, buf.Len(), len(s))
}
if string(buf.Bytes()) != s {
t.Errorf("%s: string(buf.Bytes()) == %q, s == %q\n", testname, string(buf.Bytes()), s)
if string(bytes) != s {
t.Errorf("%s: string(buf.Bytes()) == %q, s == %q\n", testname, string(bytes), s)
}
}
// Fill buf through n writes of fub.
// Fill buf through n writes of string fus.
// The initial contents of buf corresponds to the string s;
// the result is the final contents of buf returned as a string.
func fill(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string {
func fillString(t *testing.T, testname string, buf *Buffer, s string, n int, fus string) string {
check(t, testname + " (fill 1)", buf, s);
for ; n > 0; n-- {
m, err := buf.WriteString(fus);
if m != len(fus) {
t.Errorf(testname + " (fill 2): m == %d, expected %d\n", m, len(fus));
}
if err != nil {
t.Errorf(testname + " (fill 3): err should always be nil, found err == %s\n", err);
}
s += fus;
check(t, testname + " (fill 4)", buf, s);
}
return s;
}
// Fill buf through n writes of byte slice fub.
// The initial contents of buf corresponds to the string s;
// the result is the final contents of buf returned as a string.
func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string {
check(t, testname + " (fill 1)", buf, s);
for ; n > 0; n-- {
m, err := buf.Write(fub);
@ -59,6 +86,18 @@ func fill(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byt
}
func TestNewBuffer(t *testing.T) {
buf := NewBuffer(bytes);
check(t, "NewBuffer", buf, data);
}
func TestNewBufferString(t *testing.T) {
buf := NewBufferString(data);
check(t, "NewBufferString", buf, data);
}
// Empty buf through repeated reads into fub.
// The initial contents of buf corresponds to the string s.
func empty(t *testing.T, testname string, buf *Buffer, s string, fub []byte) {
@ -92,7 +131,7 @@ func TestBasicOperations(t *testing.T) {
buf.Truncate(0);
check(t, "TestBasicOperations (3)", &buf, "");
n, err := buf.Write(data[0 : 1]);
n, err := buf.Write(Bytes(data[0 : 1]));
if n != 1 {
t.Errorf("wrote 1 byte, but n == %d\n", n);
}
@ -104,7 +143,7 @@ func TestBasicOperations(t *testing.T) {
buf.WriteByte(data[1]);
check(t, "TestBasicOperations (5)", &buf, "ab");
n, err = buf.Write(data[2 : 26]);
n, err = buf.Write(Bytes(data[2 : 26]));
if n != 24 {
t.Errorf("wrote 25 bytes, but n == %d\n", n);
}
@ -135,23 +174,43 @@ func TestBasicOperations(t *testing.T) {
}
func TestLargeWrites(t *testing.T) {
func TestLargeStringWrites(t *testing.T) {
var buf Buffer;
for i := 3; i < 30; i += 3 {
s := fill(t, "TestLargeWrites (1)", &buf, "", 5, data);
empty(t, "TestLargeWrites (2)", &buf, s, make([]byte, len(data)/i));
s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, data);
empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(data)/i));
}
check(t, "TestLargeWrites (3)", &buf, "");
check(t, "TestLargeStringWrites (3)", &buf, "");
}
func TestLargeReads(t *testing.T) {
func TestLargeByteWrites(t *testing.T) {
var buf Buffer;
for i := 3; i < 30; i += 3 {
s := fill(t, "TestLargeReads (1)", &buf, "", 5, data[0 : len(data)/i]);
s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, bytes);
empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(data)/i));
}
check(t, "TestLargeByteWrites (3)", &buf, "");
}
func TestLargeStringReads(t *testing.T) {
var buf Buffer;
for i := 3; i < 30; i += 3 {
s := fillString(t, "TestLargeReads (1)", &buf, "", 5, data[0 : len(data)/i]);
empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)));
}
check(t, "TestLargeReads (3)", &buf, "");
check(t, "TestLargeStringReads (3)", &buf, "");
}
func TestLargeByteReads(t *testing.T) {
var buf Buffer;
for i := 3; i < 30; i += 3 {
s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, bytes[0 : len(bytes)/i]);
empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)));
}
check(t, "TestLargeByteReads (3)", &buf, "");
}
@ -160,7 +219,11 @@ func TestMixedReadsAndWrites(t *testing.T) {
s := "";
for i := 0; i < 50; i++ {
wlen := rand.Intn(len(data));
s = fill(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0 : wlen]);
if i % 2 == 0 {
s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0 : wlen]);
} else {
s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, bytes[0 : wlen]);
}
rlen := rand.Intn(len(data));
fub := make([]byte, rlen);