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-06-29 16:24:23 -06:00
|
|
|
"bytes";
|
2008-11-24 16:17:47 -07:00
|
|
|
"fmt";
|
|
|
|
"io";
|
|
|
|
"os";
|
2009-06-29 16:24:23 -06:00
|
|
|
"strings";
|
2008-11-24 16:17:47 -07:00
|
|
|
"testing";
|
2009-05-18 14:31:56 -06:00
|
|
|
"testing/iotest";
|
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-05-08 12:22:57 -06: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-01-15 17:22:57 -07:00
|
|
|
r13 := new(rot13Reader);
|
2008-10-07 13:31:31 -06:00
|
|
|
r13.r = r;
|
2008-09-12 17:42:53 -06:00
|
|
|
return r13
|
|
|
|
}
|
|
|
|
|
2009-04-17 01:08:24 -06:00
|
|
|
func (r13 *rot13Reader) Read(p []byte) (int, os.Error) {
|
2008-10-07 13:31:31 -06:00
|
|
|
n, e := r13.r.Read(p);
|
2008-09-12 17:42:53 -06:00
|
|
|
if e != nil {
|
|
|
|
return n, e
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
2009-05-18 14:31:56 -06:00
|
|
|
c := p[i] | 0x20; // lowercase byte
|
|
|
|
if 'a' <= c && c <= 'm' {
|
|
|
|
p[i] += 13;
|
|
|
|
} else if 'n' <= c && c <= 'z' {
|
|
|
|
p[i] -= 13;
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
}
|
2008-10-07 13:31:31 -06: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 {
|
|
|
|
var b [1000]byte;
|
|
|
|
nb := 0;
|
|
|
|
for {
|
|
|
|
c, e := buf.ReadByte();
|
2009-06-22 14:26:13 -06:00
|
|
|
if e == os.EOF {
|
2009-05-18 14:31:56 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if e != nil {
|
|
|
|
panic("Data: "+e.String())
|
|
|
|
}
|
|
|
|
b[nb] = c;
|
|
|
|
nb++;
|
|
|
|
}
|
|
|
|
return string(b[0:nb])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReaderSimple(t *testing.T) {
|
2009-06-29 16:24:23 -06:00
|
|
|
data := strings.Bytes("hello world");
|
2009-08-20 11:18:48 -06:00
|
|
|
b := NewReader(bytes.NewBuffer(data));
|
2009-05-18 14:31:56 -06:00
|
|
|
if s := readBytes(b); s != "hello world" {
|
|
|
|
t.Errorf("simple hello world test failed: got %q", s);
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:18:48 -06:00
|
|
|
b = NewReader(newRot13Reader(bytes.NewBuffer(data)));
|
2009-05-18 14:31:56 -06:00
|
|
|
if s := readBytes(b); s != "uryyb jbeyq" {
|
|
|
|
t.Error("rot13 hello world test failed: got %q", s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-15 17:22:57 -07:00
|
|
|
type readMaker struct {
|
2008-11-24 16:17:47 -07:00
|
|
|
name string;
|
2009-05-18 14:31:56 -06:00
|
|
|
fn func(io.Reader) io.Reader;
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
2009-03-03 09:39:12 -07:00
|
|
|
var readMakers = []readMaker {
|
2009-05-18 14:31:56 -06:00
|
|
|
readMaker{ "full", func(r io.Reader) io.Reader { return r } },
|
|
|
|
readMaker{ "byte", iotest.OneByteReader },
|
|
|
|
readMaker{ "half", iotest.HalfReader },
|
2009-08-26 17:23:34 -06:00
|
|
|
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 {
|
2008-10-07 13:31:31 -06:00
|
|
|
s := "";
|
2008-09-12 17:42:53 -06:00
|
|
|
for {
|
2009-08-27 12:20:15 -06:00
|
|
|
s1, e := b.ReadString('\n');
|
2009-06-22 14:26:13 -06:00
|
|
|
if e == os.EOF {
|
2008-09-12 17:42:53 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if e != nil {
|
|
|
|
panic("GetLines: "+e.String())
|
|
|
|
}
|
|
|
|
s += s1
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call Read to accumulate the text of a file
|
2009-05-08 12:52:39 -06:00
|
|
|
func reads(buf *Reader, m int) string {
|
2008-09-12 17:42:53 -06:00
|
|
|
var b [1000]byte;
|
2008-10-07 13:31:31 -06:00
|
|
|
nb := 0;
|
2008-09-12 17:42:53 -06:00
|
|
|
for {
|
2008-12-18 23:37:22 -07:00
|
|
|
n, e := buf.Read(b[nb:nb+m]);
|
2008-10-07 13:31:31 -06:00
|
|
|
nb += n;
|
2009-06-22 14:26:13 -06:00
|
|
|
if e == os.EOF {
|
2008-09-12 17:42:53 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2008-12-18 23:37:22 -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 {
|
2008-11-24 16:17:47 -07:00
|
|
|
name string;
|
2009-05-08 12:52:39 -06:00
|
|
|
fn func(*Reader) string;
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-03-03 09:39:12 -07:00
|
|
|
var bufreaders = []bufReader {
|
2009-05-08 12:52:39 -06:00
|
|
|
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) } },
|
2009-03-03 09:39:12 -07:00
|
|
|
bufReader{ "bytes", readBytes },
|
|
|
|
bufReader{ "lines", readLines },
|
|
|
|
}
|
2008-09-12 17:42:53 -06:00
|
|
|
|
2009-03-03 09:39:12 -07:00
|
|
|
var bufsizes = []int {
|
2008-09-12 17:42:53 -06:00
|
|
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
|
|
|
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) {
|
2008-10-07 13:31:31 -06:00
|
|
|
var texts [31]string;
|
2008-09-12 17:42:53 -06:00
|
|
|
str := "";
|
2008-10-07 13:31:31 -06:00
|
|
|
all := "";
|
2008-09-12 17:42:53 -06:00
|
|
|
for i := 0; i < len(texts)-1; i++ {
|
|
|
|
texts[i] = str + "\n";
|
|
|
|
all += texts[i];
|
|
|
|
str += string(i%26+'a')
|
|
|
|
}
|
|
|
|
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++ {
|
|
|
|
text := texts[h];
|
2009-06-29 16:24:23 -06:00
|
|
|
textbytes := strings.Bytes(text);
|
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-01-15 17:22:57 -07:00
|
|
|
readmaker := readMakers[i];
|
2008-11-24 16:17:47 -07:00
|
|
|
bufreader := bufreaders[j];
|
2008-09-12 17:42:53 -06:00
|
|
|
bufsize := bufsizes[k];
|
2009-08-20 11:18:48 -06:00
|
|
|
read := readmaker.fn(bytes.NewBuffer(textbytes));
|
2009-09-15 10:41:59 -06:00
|
|
|
buf, _ := NewReaderSize(read, bufsize);
|
2008-11-24 16:17:47 -07:00
|
|
|
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",
|
|
|
|
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 {
|
|
|
|
data []string;
|
|
|
|
step int;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *StringReader) Read (p []byte) (n int, err os.Error) {
|
|
|
|
if r.step < len(r.data) {
|
|
|
|
s := r.data[r.step];
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
p[i] = s[i];
|
|
|
|
}
|
|
|
|
n = len(s);
|
|
|
|
r.step++;
|
|
|
|
} else {
|
|
|
|
err = os.EOF;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
func readRuneSegments(t *testing.T, segments []string) {
|
|
|
|
got := "";
|
|
|
|
want := strings.Join(segments, "");
|
2009-08-12 14:19:17 -06:00
|
|
|
r := NewReader(&StringReader{data: segments});
|
2009-08-06 13:05:16 -06:00
|
|
|
for {
|
2009-09-15 10:41:59 -06:00
|
|
|
rune, _, err := r.ReadRune();
|
2009-08-06 13:05:16 -06:00
|
|
|
if err != nil {
|
|
|
|
if err != os.EOF {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
got += string(rune);
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("segments=%v got=%s want=%s", segments, got, want);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var segmentList = [][]string {
|
|
|
|
[]string{},
|
|
|
|
[]string{""},
|
|
|
|
[]string{"日", "本語"},
|
|
|
|
[]string{"\u65e5", "\u672c", "\u8a9e"},
|
|
|
|
[]string{"\U000065e5, "", \U0000672c", "\U00008a9e"},
|
|
|
|
[]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 {
|
|
|
|
readRuneSegments(t, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 14:31:56 -06:00
|
|
|
func TestWriter(t *testing.T) {
|
|
|
|
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++ {
|
|
|
|
data[i] = byte(' '+ i%('~'-' '));
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
2009-06-29 16:24:23 -06: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++ {
|
|
|
|
nwrite := bufsizes[i];
|
|
|
|
bs := bufsizes[j];
|
|
|
|
|
|
|
|
// Write nwrite bytes using buffer size bs.
|
|
|
|
// Check that the right amount makes it out
|
|
|
|
// and that the data is correct.
|
|
|
|
|
|
|
|
w.Reset();
|
|
|
|
buf, e := NewWriterSize(w, bs);
|
|
|
|
context := fmt.Sprintf("nwrite=%d bufsize=%d", nwrite, bs);
|
|
|
|
if e != nil {
|
|
|
|
t.Errorf("%s: NewWriterSize %d: %v", context, bs, e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
n, e1 := buf.Write(data[0:nwrite]);
|
|
|
|
if e1 != nil || n != nwrite {
|
|
|
|
t.Errorf("%s: buf.Write %d = %d, %v", context, nwrite, n, e1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if e = buf.Flush(); e != nil {
|
|
|
|
t.Errorf("%s: buf.Flush = %v", context, e);
|
|
|
|
}
|
2008-09-12 17:42:53 -06:00
|
|
|
|
2009-09-16 16:15:00 -06:00
|
|
|
written := w.Bytes();
|
2009-05-18 14:31:56 -06:00
|
|
|
if len(written) != nwrite {
|
|
|
|
t.Errorf("%s: %d bytes written", context, len(written));
|
|
|
|
}
|
|
|
|
for l := 0; l < len(written); l++ {
|
|
|
|
if written[i] != data[i] {
|
|
|
|
t.Errorf("%s: wrong bytes written");
|
|
|
|
t.Errorf("want=%s", data[0:len(written)]);
|
|
|
|
t.Errorf("have=%s", written);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
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) {
|
|
|
|
return len(p)*w.n/w.m, w.err;
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-05-18 14:31:56 -06: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-05-18 14:31:56 -06:00
|
|
|
buf := NewWriter(w);
|
2009-09-15 10:41:59 -06:00
|
|
|
_, e := buf.Write(strings.Bytes("hello world"));
|
2009-05-18 14:31:56 -06:00
|
|
|
if e != nil {
|
|
|
|
t.Errorf("Write hello to %v: %v", w, e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
e = buf.Flush();
|
|
|
|
if e != w.expect {
|
|
|
|
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-04-06 22:42:14 -06:00
|
|
|
const BufSize = 1000;
|
2009-08-20 11:18:48 -06:00
|
|
|
b, err := NewReaderSize(bytes.NewBuffer(strings.Bytes("hello world")), BufSize);
|
2009-04-06 22:42:14 -06:00
|
|
|
if err != nil {
|
2009-05-08 12:52:39 -06:00
|
|
|
t.Error("NewReaderSize create fail", err);
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
// Does it recognize itself?
|
2009-05-08 12:52:39 -06:00
|
|
|
b1, err2 := NewReaderSize(b, BufSize);
|
2009-04-06 22:42:14 -06:00
|
|
|
if err2 != nil {
|
2009-05-08 12:52:39 -06:00
|
|
|
t.Error("NewReaderSize #2 create fail", err2);
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
if b1 != b {
|
2009-05-08 12:52:39 -06: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-05-08 12:52:39 -06:00
|
|
|
b2, err3 := NewReaderSize(b, 2*BufSize);
|
2009-04-06 22:42:14 -06:00
|
|
|
if err3 != nil {
|
2009-05-08 12:52:39 -06:00
|
|
|
t.Error("NewReaderSize #3 create fail", err3);
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
if b2 == b {
|
2009-05-08 12:52:39 -06: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-04-06 22:42:14 -06:00
|
|
|
const BufSize = 1000;
|
2009-06-29 16:24:23 -06:00
|
|
|
b, err := NewWriterSize(new(bytes.Buffer), BufSize);
|
2009-04-06 22:42:14 -06:00
|
|
|
if err != nil {
|
2009-05-08 12:52:39 -06:00
|
|
|
t.Error("NewWriterSize create fail", err);
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
// Does it recognize itself?
|
2009-05-08 12:52:39 -06:00
|
|
|
b1, err2 := NewWriterSize(b, BufSize);
|
2009-04-06 22:42:14 -06:00
|
|
|
if err2 != nil {
|
2009-05-08 12:52:39 -06:00
|
|
|
t.Error("NewWriterSize #2 create fail", err2);
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
if b1 != b {
|
2009-05-08 12:52:39 -06: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-05-08 12:52:39 -06:00
|
|
|
b2, err3 := NewWriterSize(b, 2*BufSize);
|
2009-04-06 22:42:14 -06:00
|
|
|
if err3 != nil {
|
2009-05-08 12:52:39 -06:00
|
|
|
t.Error("NewWriterSize #3 create fail", err3);
|
2009-04-06 22:42:14 -06:00
|
|
|
}
|
|
|
|
if b2 == b {
|
2009-05-08 12:52:39 -06: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) {
|
|
|
|
const BufSize = 8;
|
|
|
|
buf := new(bytes.Buffer);
|
|
|
|
b, err := NewWriterSize(buf, BufSize);
|
|
|
|
if err != nil {
|
|
|
|
t.Error("NewWriterSize create fail", err);
|
|
|
|
}
|
|
|
|
b.WriteString("0"); // easy
|
|
|
|
b.WriteString("123456"); // still easy
|
|
|
|
b.WriteString("7890"); // easy after flush
|
|
|
|
b.WriteString("abcdefghijklmnopqrstuvwxy"); // hard
|
|
|
|
b.WriteString("z");
|
|
|
|
b.Flush();
|
|
|
|
if b.err != nil {
|
|
|
|
t.Error("WriteString", b.err);
|
|
|
|
}
|
|
|
|
s := "01234567890abcdefghijklmnopqrstuvwxyz";
|
2009-09-16 16:15:00 -06:00
|
|
|
if string(buf.Bytes()) != s {
|
|
|
|
t.Errorf("WriteString wants %q gets %q", s, string(buf.Bytes()))
|
2009-08-03 19:28:05 -06:00
|
|
|
}
|
|
|
|
}
|