]> Cypherpunks repositories - gostls13.git/commitdiff
bytes: improve test readability
authorGabriel Aszalos <gabriel.aszalos@gmail.com>
Wed, 20 Sep 2017 09:10:26 +0000 (11:10 +0200)
committerIan Lance Taylor <iant@golang.org>
Wed, 20 Sep 2017 13:41:03 +0000 (13:41 +0000)
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: Ian Lance Taylor <iant@golang.org>
src/bytes/buffer_test.go
src/bytes/reader_test.go

index 597dd130928d8f356c408f310daa705a67b29047..47ce10475a9f19e18b7463bbf741ae6faf20167a 100644 (file)
@@ -15,16 +15,16 @@ import (
        "unicode/utf8"
 )
 
-const N = 10000      // make this bigger for a larger (and slower) test
-var data string      // test data for write tests
-var testBytes []byte // test data; same as data but as a slice.
+const N = 10000       // make this bigger for a larger (and slower) test
+var testString string // test data for write tests
+var testBytes []byte  // test data; same as testString but as a slice.
 
 func init() {
        testBytes = make([]byte, N)
        for i := 0; i < N; i++ {
                testBytes[i] = 'a' + byte(i%26)
        }
-       data = string(testBytes)
+       testString = string(testBytes)
 }
 
 // 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
 
 func TestNewBuffer(t *testing.T) {
        buf := NewBuffer(testBytes)
-       check(t, "NewBuffer", buf, data)
+       check(t, "NewBuffer", buf, testString)
 }
 
 func TestNewBufferString(t *testing.T) {
-       buf := NewBufferString(data)
-       check(t, "NewBufferString", buf, data)
+       buf := NewBufferString(testString)
+       check(t, "NewBufferString", buf, testString)
 }
 
 // Empty buf through repeated reads into fub.
@@ -128,7 +128,7 @@ func TestBasicOperations(t *testing.T) {
                buf.Truncate(0)
                check(t, "TestBasicOperations (3)", &buf, "")
 
-               n, err := buf.Write([]byte(data[0:1]))
+               n, err := buf.Write(testBytes[0:1])
                if n != 1 {
                        t.Errorf("wrote 1 byte, but n == %d", n)
                }
@@ -137,30 +137,30 @@ func TestBasicOperations(t *testing.T) {
                }
                check(t, "TestBasicOperations (4)", &buf, "a")
 
-               buf.WriteByte(data[1])
+               buf.WriteByte(testString[1])
                check(t, "TestBasicOperations (5)", &buf, "ab")
 
-               n, err = buf.Write([]byte(data[2:26]))
+               n, err = buf.Write(testBytes[2:26])
                if n != 24 {
                        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)
-               check(t, "TestBasicOperations (7)", &buf, string(data[0:26]))
+               check(t, "TestBasicOperations (7)", &buf, testString[0:26])
 
                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))
 
-               buf.WriteByte(data[1])
+               buf.WriteByte(testString[1])
                c, err := buf.ReadByte()
                if err != nil {
                        t.Error("ReadByte unexpected eof")
                }
-               if c != data[1] {
+               if c != testString[1] {
                        t.Errorf("ReadByte wrong value c=%v", c)
                }
                c, err = buf.ReadByte()
@@ -177,8 +177,8 @@ func TestLargeStringWrites(t *testing.T) {
                limit = 9
        }
        for i := 3; i < limit; i += 3 {
-               s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, data)
-               empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(data)/i))
+               s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, testString)
+               empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(testString)/i))
        }
        check(t, "TestLargeStringWrites (3)", &buf, "")
 }
@@ -191,7 +191,7 @@ func TestLargeByteWrites(t *testing.T) {
        }
        for i := 3; i < limit; i += 3 {
                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, "")
 }
@@ -199,8 +199,8 @@ func TestLargeByteWrites(t *testing.T) {
 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)))
+               s := fillString(t, "TestLargeReads (1)", &buf, "", 5, testString[0:len(testString)/i])
+               empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(testString)))
        }
        check(t, "TestLargeStringReads (3)", &buf, "")
 }
@@ -209,7 +209,7 @@ func TestLargeByteReads(t *testing.T) {
        var buf Buffer
        for i := 3; i < 30; i += 3 {
                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, "")
 }
@@ -218,14 +218,14 @@ func TestMixedReadsAndWrites(t *testing.T) {
        var buf Buffer
        s := ""
        for i := 0; i < 50; i++ {
-               wlen := rand.Intn(len(data))
+               wlen := rand.Intn(len(testString))
                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 {
                        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)
                n, _ := buf.Read(fub)
                s = s[n:]
@@ -263,7 +263,7 @@ func TestReadFrom(t *testing.T) {
                s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
                var b Buffer
                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) {
                s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
                var b Buffer
                buf.WriteTo(&b)
-               empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(data)))
+               empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(testString)))
        }
 }
 
index 7b3034d4e0d90cec3e8b001f38150a5f679e7e50..8806876ff13c73fa630ec765b6179092fe65fda0 100644 (file)
@@ -140,9 +140,9 @@ func TestReaderWriteTo(t *testing.T) {
        for i := 0; i < 30; i += 3 {
                var l int
                if i > 0 {
-                       l = len(data) / i
+                       l = len(testString) / i
                }
-               s := data[:l]
+               s := testString[:l]
                r := NewReader(testBytes[:l])
                var b Buffer
                n, err := r.WriteTo(&b)