2008-09-12 17:42:53 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2008-11-24 16:17:47 -07:00
|
|
|
package bufio
|
2008-09-12 17:42:53 -06:00
|
|
|
|
|
|
|
import (
|
2009-12-15 16:33:31 -07:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"testing/iotest"
|
2010-03-05 15:03:17 -07:00
|
|
|
"utf8"
|
2008-09-12 17:42:53 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Reads from a reader and rot13s the result.
|
2009-01-15 17:22:57 -07:00
|
|
|
type rot13Reader struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
r io.Reader
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
func newRot13Reader(r io.Reader) *rot13Reader {
|
2009-12-15 16:33:31 -07:00
|
|
|
r13 := new(rot13Reader)
|
|
|
|
r13.r = r
|
|
|
|
return r13
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-04-17 01:08:24 -06:00
|
|
|
func (r13 *rot13Reader) Read(p []byte) (int, os.Error) {
|
2009-12-15 16:33:31 -07:00
|
|
|
n, e := r13.r.Read(p)
|
2008-09-12 17:42:53 -06:00
|
|
|
if e != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return n, e
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
c := p[i] | 0x20 // lowercase byte
|
2009-05-18 14:31:56 -06:00
|
|
|
if 'a' <= c && c <= 'm' {
|
2009-11-09 13:07:39 -07:00
|
|
|
p[i] += 13
|
2009-05-18 14:31:56 -06:00
|
|
|
} else if 'n' <= c && c <= 'z' {
|
2009-11-09 13:07:39 -07:00
|
|
|
p[i] -= 13
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return n, nil
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-05-18 14:31:56 -06:00
|
|
|
// Call ReadByte to accumulate the text of a file
|
|
|
|
func readBytes(buf *Reader) string {
|
2009-12-15 16:33:31 -07:00
|
|
|
var b [1000]byte
|
|
|
|
nb := 0
|
2009-05-18 14:31:56 -06:00
|
|
|
for {
|
2009-12-15 16:33:31 -07:00
|
|
|
c, e := buf.ReadByte()
|
2009-06-22 14:26:13 -06:00
|
|
|
if e == os.EOF {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
|
|
|
if e != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
panic("Data: " + e.String())
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
b[nb] = c
|
|
|
|
nb++
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return string(b[0:nb])
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReaderSimple(t *testing.T) {
|
2009-12-15 16:33:31 -07:00
|
|
|
data := "hello world"
|
|
|
|
b := NewReader(bytes.NewBufferString(data))
|
2009-05-18 14:31:56 -06:00
|
|
|
if s := readBytes(b); s != "hello world" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("simple hello world test failed: got %q", s)
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
b = NewReader(newRot13Reader(bytes.NewBufferString(data)))
|
2009-05-18 14:31:56 -06:00
|
|
|
if s := readBytes(b); s != "uryyb jbeyq" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("rot13 hello world test failed: got %q", s)
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-15 17:22:57 -07:00
|
|
|
type readMaker struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
name string
|
|
|
|
fn func(io.Reader) io.Reader
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
2009-11-04 18:04:21 -07:00
|
|
|
|
|
|
|
var readMakers = []readMaker{
|
|
|
|
readMaker{"full", func(r io.Reader) io.Reader { return r }},
|
|
|
|
readMaker{"byte", iotest.OneByteReader},
|
|
|
|
readMaker{"half", iotest.HalfReader},
|
|
|
|
readMaker{"data+err", iotest.DataErrReader},
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2008-09-12 17:42:53 -06:00
|
|
|
|
2009-08-27 12:20:15 -06:00
|
|
|
// Call ReadString (which ends up calling everything else)
|
2008-09-12 17:42:53 -06:00
|
|
|
// to accumulate the text of a file.
|
2009-05-08 12:52:39 -06:00
|
|
|
func readLines(b *Reader) string {
|
2009-12-15 16:33:31 -07:00
|
|
|
s := ""
|
2008-09-12 17:42:53 -06:00
|
|
|
for {
|
2009-12-15 16:33:31 -07:00
|
|
|
s1, e := b.ReadString('\n')
|
2009-06-22 14:26:13 -06:00
|
|
|
if e == os.EOF {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
if e != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
panic("GetLines: " + e.String())
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
s += s1
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return s
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call Read to accumulate the text of a file
|
2009-05-08 12:52:39 -06:00
|
|
|
func reads(buf *Reader, m int) string {
|
2009-12-15 16:33:31 -07:00
|
|
|
var b [1000]byte
|
|
|
|
nb := 0
|
2008-09-12 17:42:53 -06:00
|
|
|
for {
|
2009-12-15 16:33:31 -07:00
|
|
|
n, e := buf.Read(b[nb : nb+m])
|
|
|
|
nb += n
|
2009-06-22 14:26:13 -06:00
|
|
|
if e == os.EOF {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return string(b[0:nb])
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 17:22:57 -07:00
|
|
|
type bufReader struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
name string
|
|
|
|
fn func(*Reader) string
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-11-04 18:04:21 -07:00
|
|
|
|
|
|
|
var bufreaders = []bufReader{
|
|
|
|
bufReader{"1", func(b *Reader) string { return reads(b, 1) }},
|
|
|
|
bufReader{"2", func(b *Reader) string { return reads(b, 2) }},
|
|
|
|
bufReader{"3", func(b *Reader) string { return reads(b, 3) }},
|
|
|
|
bufReader{"4", func(b *Reader) string { return reads(b, 4) }},
|
|
|
|
bufReader{"5", func(b *Reader) string { return reads(b, 5) }},
|
|
|
|
bufReader{"7", func(b *Reader) string { return reads(b, 7) }},
|
|
|
|
bufReader{"bytes", readBytes},
|
|
|
|
bufReader{"lines", readLines},
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2008-09-12 17:42:53 -06:00
|
|
|
|
2009-11-04 18:04:21 -07:00
|
|
|
var bufsizes = []int{
|
2008-09-12 17:42:53 -06:00
|
|
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
2009-11-04 18:04:21 -07:00
|
|
|
23, 32, 46, 64, 93, 128, 1024, 4096,
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2008-09-12 17:42:53 -06:00
|
|
|
|
2009-05-08 12:52:39 -06:00
|
|
|
func TestReader(t *testing.T) {
|
2009-12-15 16:33:31 -07:00
|
|
|
var texts [31]string
|
|
|
|
str := ""
|
|
|
|
all := ""
|
2008-09-12 17:42:53 -06:00
|
|
|
for i := 0; i < len(texts)-1; i++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
texts[i] = str + "\n"
|
|
|
|
all += texts[i]
|
|
|
|
str += string(i%26 + 'a')
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
texts[len(texts)-1] = all
|
2008-10-07 13:31:31 -06:00
|
|
|
|
2008-09-12 17:42:53 -06:00
|
|
|
for h := 0; h < len(texts); h++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
text := texts[h]
|
2009-01-15 17:22:57 -07:00
|
|
|
for i := 0; i < len(readMakers); i++ {
|
2008-09-12 17:42:53 -06:00
|
|
|
for j := 0; j < len(bufreaders); j++ {
|
|
|
|
for k := 0; k < len(bufsizes); k++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
readmaker := readMakers[i]
|
|
|
|
bufreader := bufreaders[j]
|
|
|
|
bufsize := bufsizes[k]
|
|
|
|
read := readmaker.fn(bytes.NewBufferString(text))
|
|
|
|
buf, _ := NewReaderSize(read, bufsize)
|
|
|
|
s := bufreader.fn(buf)
|
2008-09-12 17:42:53 -06:00
|
|
|
if s != text {
|
2008-11-24 16:17:47 -07:00
|
|
|
t.Errorf("reader=%s fn=%s bufsize=%d want=%q got=%q",
|
2009-11-09 13:07:39 -07:00
|
|
|
readmaker.name, bufreader.name, bufsize, text, s)
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-06 13:05:16 -06:00
|
|
|
// A StringReader delivers its data one string segment at a time via Read.
|
|
|
|
type StringReader struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
data []string
|
|
|
|
step int
|
2009-08-06 13:05:16 -06:00
|
|
|
}
|
|
|
|
|
2009-11-04 18:04:21 -07:00
|
|
|
func (r *StringReader) Read(p []byte) (n int, err os.Error) {
|
2009-08-06 13:05:16 -06:00
|
|
|
if r.step < len(r.data) {
|
2009-12-15 16:33:31 -07:00
|
|
|
s := r.data[r.step]
|
2009-08-06 13:05:16 -06:00
|
|
|
for i := 0; i < len(s); i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
p[i] = s[i]
|
2009-08-06 13:05:16 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
n = len(s)
|
|
|
|
r.step++
|
2009-08-06 13:05:16 -06:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = os.EOF
|
2009-08-06 13:05:16 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-08-06 13:05:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func readRuneSegments(t *testing.T, segments []string) {
|
2009-12-15 16:33:31 -07:00
|
|
|
got := ""
|
|
|
|
want := strings.Join(segments, "")
|
|
|
|
r := NewReader(&StringReader{data: segments})
|
2009-08-06 13:05:16 -06:00
|
|
|
for {
|
2009-12-15 16:33:31 -07:00
|
|
|
rune, _, err := r.ReadRune()
|
2009-08-06 13:05:16 -06:00
|
|
|
if err != nil {
|
|
|
|
if err != os.EOF {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-08-06 13:05:16 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
break
|
2009-08-06 13:05:16 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
got += string(rune)
|
2009-08-06 13:05:16 -06:00
|
|
|
}
|
|
|
|
if got != want {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("segments=%v got=%s want=%s", segments, got, want)
|
2009-08-06 13:05:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-04 18:04:21 -07:00
|
|
|
var segmentList = [][]string{
|
2009-08-06 13:05:16 -06:00
|
|
|
[]string{},
|
|
|
|
[]string{""},
|
|
|
|
[]string{"日", "本語"},
|
|
|
|
[]string{"\u65e5", "\u672c", "\u8a9e"},
|
2009-11-04 11:58:30 -07:00
|
|
|
[]string{"\U000065e5", "\U0000672c", "\U00008a9e"},
|
2009-08-06 13:05:16 -06:00
|
|
|
[]string{"\xe6", "\x97\xa5\xe6", "\x9c\xac\xe8\xaa\x9e"},
|
|
|
|
[]string{"Hello", ", ", "World", "!"},
|
|
|
|
[]string{"Hello", ", ", "", "World", "!"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadRune(t *testing.T) {
|
|
|
|
for _, s := range segmentList {
|
2009-11-09 13:07:39 -07:00
|
|
|
readRuneSegments(t, s)
|
2009-08-06 13:05:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-05 15:03:17 -07:00
|
|
|
func TestReadWriteRune(t *testing.T) {
|
|
|
|
const NRune = 1000
|
|
|
|
byteBuf := new(bytes.Buffer)
|
|
|
|
w := NewWriter(byteBuf)
|
|
|
|
// Write the runes out using WriteRune
|
|
|
|
buf := make([]byte, utf8.UTFMax)
|
|
|
|
for rune := 0; rune < NRune; rune++ {
|
|
|
|
size := utf8.EncodeRune(rune, buf)
|
|
|
|
nbytes, err := w.WriteRune(rune)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("WriteRune(0x%x) error: %s", rune, err)
|
|
|
|
}
|
|
|
|
if nbytes != size {
|
|
|
|
t.Fatalf("WriteRune(0x%x) expected %d, got %d", rune, size, nbytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
r := NewReader(byteBuf)
|
|
|
|
// Read them back with ReadRune
|
|
|
|
for rune := 0; rune < NRune; rune++ {
|
|
|
|
size := utf8.EncodeRune(rune, buf)
|
|
|
|
nr, nbytes, err := r.ReadRune()
|
|
|
|
if nr != rune || nbytes != size || err != nil {
|
|
|
|
t.Fatalf("ReadRune(0x%x) got 0x%x,%d not 0x%x,%d (err=%s)", r, nr, nbytes, r, size, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 14:31:56 -06:00
|
|
|
func TestWriter(t *testing.T) {
|
2009-12-15 16:33:31 -07:00
|
|
|
var data [8192]byte
|
2008-09-12 17:42:53 -06:00
|
|
|
|
2009-05-18 14:31:56 -06:00
|
|
|
for i := 0; i < len(data); i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
data[i] = byte(' ' + i%('~'-' '))
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
w := new(bytes.Buffer)
|
2009-05-18 14:31:56 -06:00
|
|
|
for i := 0; i < len(bufsizes); i++ {
|
|
|
|
for j := 0; j < len(bufsizes); j++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
nwrite := bufsizes[i]
|
|
|
|
bs := bufsizes[j]
|
2009-05-18 14:31:56 -06:00
|
|
|
|
|
|
|
// Write nwrite bytes using buffer size bs.
|
|
|
|
// Check that the right amount makes it out
|
|
|
|
// and that the data is correct.
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
w.Reset()
|
|
|
|
buf, e := NewWriterSize(w, bs)
|
|
|
|
context := fmt.Sprintf("nwrite=%d bufsize=%d", nwrite, bs)
|
2009-05-18 14:31:56 -06:00
|
|
|
if e != nil {
|
2009-12-15 16:33:31 -07:00
|
|
|
t.Errorf("%s: NewWriterSize %d: %v", context, bs, e)
|
|
|
|
continue
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
n, e1 := buf.Write(data[0:nwrite])
|
2009-05-18 14:31:56 -06:00
|
|
|
if e1 != nil || n != nwrite {
|
2009-12-15 16:33:31 -07:00
|
|
|
t.Errorf("%s: buf.Write %d = %d, %v", context, nwrite, n, e1)
|
|
|
|
continue
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
|
|
|
if e = buf.Flush(); e != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("%s: buf.Flush = %v", context, e)
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
2008-09-12 17:42:53 -06:00
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
written := w.Bytes()
|
2009-05-18 14:31:56 -06:00
|
|
|
if len(written) != nwrite {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("%s: %d bytes written", context, len(written))
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
|
|
|
for l := 0; l < len(written); l++ {
|
|
|
|
if written[i] != data[i] {
|
2009-12-15 16:33:31 -07:00
|
|
|
t.Errorf("%s: wrong bytes written")
|
|
|
|
t.Errorf("want=%s", data[0:len(written)])
|
|
|
|
t.Errorf("have=%s", written)
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-05-18 14:31:56 -06:00
|
|
|
// Check that write errors are returned properly.
|
2008-09-12 17:42:53 -06:00
|
|
|
|
2009-05-18 14:31:56 -06:00
|
|
|
type errorWriterTest struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
n, m int
|
|
|
|
err os.Error
|
|
|
|
expect os.Error
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-05-18 14:31:56 -06:00
|
|
|
func (w errorWriterTest) Write(p []byte) (int, os.Error) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return len(p) * w.n / w.m, w.err
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-11-04 18:04:21 -07:00
|
|
|
var errorWriterTests = []errorWriterTest{
|
|
|
|
errorWriterTest{0, 1, nil, io.ErrShortWrite},
|
|
|
|
errorWriterTest{1, 2, nil, io.ErrShortWrite},
|
|
|
|
errorWriterTest{1, 1, nil, nil},
|
|
|
|
errorWriterTest{0, 1, os.EPIPE, os.EPIPE},
|
|
|
|
errorWriterTest{1, 2, os.EPIPE, os.EPIPE},
|
|
|
|
errorWriterTest{1, 1, os.EPIPE, os.EPIPE},
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
|
2009-05-18 14:31:56 -06:00
|
|
|
func TestWriteErrors(t *testing.T) {
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, w := range errorWriterTests {
|
2009-12-15 16:33:31 -07:00
|
|
|
buf := NewWriter(w)
|
2010-02-25 17:01:29 -07:00
|
|
|
_, e := buf.Write([]byte("hello world"))
|
2009-05-18 14:31:56 -06:00
|
|
|
if e != nil {
|
2009-12-15 16:33:31 -07:00
|
|
|
t.Errorf("Write hello to %v: %v", w, e)
|
|
|
|
continue
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
e = buf.Flush()
|
2009-05-18 14:31:56 -06:00
|
|
|
if e != w.expect {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("Flush %v: got %v, wanted %v", w, e, w.expect)
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-08 12:52:39 -06:00
|
|
|
func TestNewReaderSizeIdempotent(t *testing.T) {
|
2009-12-15 16:33:31 -07:00
|
|
|
const BufSize = 1000
|
|
|
|
b, err := NewReaderSize(bytes.NewBufferString("hello world"), BufSize)
|
2009-04-06 22:42:14 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("NewReaderSize create fail", err)
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
// Does it recognize itself?
|
2009-12-15 16:33:31 -07:00
|
|
|
b1, err2 := NewReaderSize(b, BufSize)
|
2009-04-06 22:42:14 -06:00
|
|
|
if err2 != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("NewReaderSize #2 create fail", err2)
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
if b1 != b {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("NewReaderSize did not detect underlying Reader")
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
// Does it wrap if existing buffer is too small?
|
2009-12-15 16:33:31 -07:00
|
|
|
b2, err3 := NewReaderSize(b, 2*BufSize)
|
2009-04-06 22:42:14 -06:00
|
|
|
if err3 != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("NewReaderSize #3 create fail", err3)
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
if b2 == b {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("NewReaderSize did not enlarge buffer")
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-08 12:52:39 -06:00
|
|
|
func TestNewWriterSizeIdempotent(t *testing.T) {
|
2009-12-15 16:33:31 -07:00
|
|
|
const BufSize = 1000
|
|
|
|
b, err := NewWriterSize(new(bytes.Buffer), BufSize)
|
2009-04-06 22:42:14 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("NewWriterSize create fail", err)
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
// Does it recognize itself?
|
2009-12-15 16:33:31 -07:00
|
|
|
b1, err2 := NewWriterSize(b, BufSize)
|
2009-04-06 22:42:14 -06:00
|
|
|
if err2 != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("NewWriterSize #2 create fail", err2)
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
if b1 != b {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("NewWriterSize did not detect underlying Writer")
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
// Does it wrap if existing buffer is too small?
|
2009-12-15 16:33:31 -07:00
|
|
|
b2, err3 := NewWriterSize(b, 2*BufSize)
|
2009-04-06 22:42:14 -06:00
|
|
|
if err3 != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("NewWriterSize #3 create fail", err3)
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
if b2 == b {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("NewWriterSize did not enlarge buffer")
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
}
|
2009-08-03 19:28:05 -06:00
|
|
|
|
|
|
|
func TestWriteString(t *testing.T) {
|
2009-12-15 16:33:31 -07:00
|
|
|
const BufSize = 8
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
b, err := NewWriterSize(buf, BufSize)
|
2009-08-03 19:28:05 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("NewWriterSize create fail", err)
|
2009-08-03 19:28:05 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
b.WriteString("0") // easy
|
|
|
|
b.WriteString("123456") // still easy
|
|
|
|
b.WriteString("7890") // easy after flush
|
|
|
|
b.WriteString("abcdefghijklmnopqrstuvwxy") // hard
|
|
|
|
b.WriteString("z")
|
|
|
|
b.Flush()
|
2009-08-03 19:28:05 -06:00
|
|
|
if b.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("WriteString", b.err)
|
2009-08-03 19:28:05 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
s := "01234567890abcdefghijklmnopqrstuvwxyz"
|
2009-09-16 16:15:00 -06:00
|
|
|
if string(buf.Bytes()) != s {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("WriteString wants %q gets %q", s, string(buf.Bytes()))
|
2009-08-03 19:28:05 -06:00
|
|
|
}
|
|
|
|
}
|