Commit 97757881 authored by Gabriel Aszalos's avatar Gabriel Aszalos Committed by Ian Lance Taylor

bytes: improve test readability

This CL improves the readability of the tests in the bytes package by
naming the `data` test variable `testString`, using the same convention
as its counterpart, `testBytes`.

It additionally removes some type casting which was unnecessary.

Change-Id: If38b5606ce8bda0306bae24498f21cb8dbbb6377
Reviewed-on: https://go-review.googlesource.com/64931
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 1b548dc5
...@@ -16,15 +16,15 @@ import ( ...@@ -16,15 +16,15 @@ import (
) )
const N = 10000 // make this bigger for a larger (and slower) test const N = 10000 // make this bigger for a larger (and slower) test
var data string // test data for write tests var testString string // test data for write tests
var testBytes []byte // test data; same as data but as a slice. var testBytes []byte // test data; same as testString but as a slice.
func init() { func init() {
testBytes = make([]byte, N) testBytes = make([]byte, N)
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
testBytes[i] = 'a' + byte(i%26) testBytes[i] = 'a' + byte(i%26)
} }
data = string(testBytes) testString = string(testBytes)
} }
// Verify that contents of buf match the string s. // Verify that contents of buf match the string s.
...@@ -88,12 +88,12 @@ func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub ...@@ -88,12 +88,12 @@ func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub
func TestNewBuffer(t *testing.T) { func TestNewBuffer(t *testing.T) {
buf := NewBuffer(testBytes) buf := NewBuffer(testBytes)
check(t, "NewBuffer", buf, data) check(t, "NewBuffer", buf, testString)
} }
func TestNewBufferString(t *testing.T) { func TestNewBufferString(t *testing.T) {
buf := NewBufferString(data) buf := NewBufferString(testString)
check(t, "NewBufferString", buf, data) check(t, "NewBufferString", buf, testString)
} }
// Empty buf through repeated reads into fub. // Empty buf through repeated reads into fub.
...@@ -128,7 +128,7 @@ func TestBasicOperations(t *testing.T) { ...@@ -128,7 +128,7 @@ func TestBasicOperations(t *testing.T) {
buf.Truncate(0) buf.Truncate(0)
check(t, "TestBasicOperations (3)", &buf, "") check(t, "TestBasicOperations (3)", &buf, "")
n, err := buf.Write([]byte(data[0:1])) n, err := buf.Write(testBytes[0:1])
if n != 1 { if n != 1 {
t.Errorf("wrote 1 byte, but n == %d", n) t.Errorf("wrote 1 byte, but n == %d", n)
} }
...@@ -137,30 +137,30 @@ func TestBasicOperations(t *testing.T) { ...@@ -137,30 +137,30 @@ func TestBasicOperations(t *testing.T) {
} }
check(t, "TestBasicOperations (4)", &buf, "a") check(t, "TestBasicOperations (4)", &buf, "a")
buf.WriteByte(data[1]) buf.WriteByte(testString[1])
check(t, "TestBasicOperations (5)", &buf, "ab") check(t, "TestBasicOperations (5)", &buf, "ab")
n, err = buf.Write([]byte(data[2:26])) n, err = buf.Write(testBytes[2:26])
if n != 24 { if n != 24 {
t.Errorf("wrote 24 bytes, but n == %d", n) t.Errorf("wrote 24 bytes, but n == %d", n)
} }
check(t, "TestBasicOperations (6)", &buf, string(data[0:26])) check(t, "TestBasicOperations (6)", &buf, testString[0:26])
buf.Truncate(26) buf.Truncate(26)
check(t, "TestBasicOperations (7)", &buf, string(data[0:26])) check(t, "TestBasicOperations (7)", &buf, testString[0:26])
buf.Truncate(20) buf.Truncate(20)
check(t, "TestBasicOperations (8)", &buf, string(data[0:20])) check(t, "TestBasicOperations (8)", &buf, testString[0:20])
empty(t, "TestBasicOperations (9)", &buf, string(data[0:20]), make([]byte, 5)) empty(t, "TestBasicOperations (9)", &buf, testString[0:20], make([]byte, 5))
empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100)) empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100))
buf.WriteByte(data[1]) buf.WriteByte(testString[1])
c, err := buf.ReadByte() c, err := buf.ReadByte()
if err != nil { if err != nil {
t.Error("ReadByte unexpected eof") t.Error("ReadByte unexpected eof")
} }
if c != data[1] { if c != testString[1] {
t.Errorf("ReadByte wrong value c=%v", c) t.Errorf("ReadByte wrong value c=%v", c)
} }
c, err = buf.ReadByte() c, err = buf.ReadByte()
...@@ -177,8 +177,8 @@ func TestLargeStringWrites(t *testing.T) { ...@@ -177,8 +177,8 @@ func TestLargeStringWrites(t *testing.T) {
limit = 9 limit = 9
} }
for i := 3; i < limit; i += 3 { for i := 3; i < limit; i += 3 {
s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, data) s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, testString)
empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(data)/i)) empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(testString)/i))
} }
check(t, "TestLargeStringWrites (3)", &buf, "") check(t, "TestLargeStringWrites (3)", &buf, "")
} }
...@@ -191,7 +191,7 @@ func TestLargeByteWrites(t *testing.T) { ...@@ -191,7 +191,7 @@ func TestLargeByteWrites(t *testing.T) {
} }
for i := 3; i < limit; i += 3 { for i := 3; i < limit; i += 3 {
s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, testBytes) s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, testBytes)
empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(data)/i)) empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(testString)/i))
} }
check(t, "TestLargeByteWrites (3)", &buf, "") check(t, "TestLargeByteWrites (3)", &buf, "")
} }
...@@ -199,8 +199,8 @@ func TestLargeByteWrites(t *testing.T) { ...@@ -199,8 +199,8 @@ func TestLargeByteWrites(t *testing.T) {
func TestLargeStringReads(t *testing.T) { func TestLargeStringReads(t *testing.T) {
var buf Buffer var buf Buffer
for i := 3; i < 30; i += 3 { for i := 3; i < 30; i += 3 {
s := fillString(t, "TestLargeReads (1)", &buf, "", 5, data[0:len(data)/i]) s := fillString(t, "TestLargeReads (1)", &buf, "", 5, testString[0:len(testString)/i])
empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data))) empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(testString)))
} }
check(t, "TestLargeStringReads (3)", &buf, "") check(t, "TestLargeStringReads (3)", &buf, "")
} }
...@@ -209,7 +209,7 @@ func TestLargeByteReads(t *testing.T) { ...@@ -209,7 +209,7 @@ func TestLargeByteReads(t *testing.T) {
var buf Buffer var buf Buffer
for i := 3; i < 30; i += 3 { for i := 3; i < 30; i += 3 {
s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data))) empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(testString)))
} }
check(t, "TestLargeByteReads (3)", &buf, "") check(t, "TestLargeByteReads (3)", &buf, "")
} }
...@@ -218,14 +218,14 @@ func TestMixedReadsAndWrites(t *testing.T) { ...@@ -218,14 +218,14 @@ func TestMixedReadsAndWrites(t *testing.T) {
var buf Buffer var buf Buffer
s := "" s := ""
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
wlen := rand.Intn(len(data)) wlen := rand.Intn(len(testString))
if i%2 == 0 { if i%2 == 0 {
s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0:wlen]) s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testString[0:wlen])
} else { } else {
s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testBytes[0:wlen]) s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testBytes[0:wlen])
} }
rlen := rand.Intn(len(data)) rlen := rand.Intn(len(testString))
fub := make([]byte, rlen) fub := make([]byte, rlen)
n, _ := buf.Read(fub) n, _ := buf.Read(fub)
s = s[n:] s = s[n:]
...@@ -263,7 +263,7 @@ func TestReadFrom(t *testing.T) { ...@@ -263,7 +263,7 @@ func TestReadFrom(t *testing.T) {
s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
var b Buffer var b Buffer
b.ReadFrom(&buf) b.ReadFrom(&buf)
empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data))) empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(testString)))
} }
} }
...@@ -273,7 +273,7 @@ func TestWriteTo(t *testing.T) { ...@@ -273,7 +273,7 @@ func TestWriteTo(t *testing.T) {
s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
var b Buffer var b Buffer
buf.WriteTo(&b) buf.WriteTo(&b)
empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(data))) empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(testString)))
} }
} }
......
...@@ -140,9 +140,9 @@ func TestReaderWriteTo(t *testing.T) { ...@@ -140,9 +140,9 @@ func TestReaderWriteTo(t *testing.T) {
for i := 0; i < 30; i += 3 { for i := 0; i < 30; i += 3 {
var l int var l int
if i > 0 { if i > 0 {
l = len(data) / i l = len(testString) / i
} }
s := data[:l] s := testString[:l]
r := NewReader(testBytes[:l]) r := NewReader(testBytes[:l])
var b Buffer var b Buffer
n, err := r.WriteTo(&b) n, err := r.WriteTo(&b)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment