2012-02-08 23:28:41 -07:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
|
|
|
package strings_test
|
|
|
|
|
|
|
|
import (
|
2012-10-11 21:43:50 -06:00
|
|
|
"bytes"
|
2012-02-14 18:58:00 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-01-22 15:15:47 -07:00
|
|
|
"io/ioutil"
|
2012-02-08 23:28:41 -07:00
|
|
|
"strings"
|
2014-04-10 16:46:07 -06:00
|
|
|
"sync"
|
2012-02-08 23:28:41 -07:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReader(t *testing.T) {
|
|
|
|
r := strings.NewReader("0123456789")
|
|
|
|
tests := []struct {
|
|
|
|
off int64
|
|
|
|
seek int
|
|
|
|
n int
|
|
|
|
want string
|
|
|
|
wantpos int64
|
2016-05-03 13:54:49 -06:00
|
|
|
readerr error
|
2012-02-08 23:28:41 -07:00
|
|
|
seekerr string
|
|
|
|
}{
|
2016-04-12 22:35:37 -06:00
|
|
|
{seek: io.SeekStart, off: 0, n: 20, want: "0123456789"},
|
|
|
|
{seek: io.SeekStart, off: 1, n: 1, want: "1"},
|
|
|
|
{seek: io.SeekCurrent, off: 1, wantpos: 3, n: 2, want: "34"},
|
|
|
|
{seek: io.SeekStart, off: -1, seekerr: "strings.Reader.Seek: negative position"},
|
2016-05-03 13:54:49 -06:00
|
|
|
{seek: io.SeekStart, off: 1 << 33, wantpos: 1 << 33, readerr: io.EOF},
|
|
|
|
{seek: io.SeekCurrent, off: 1, wantpos: 1<<33 + 1, readerr: io.EOF},
|
2016-04-12 22:35:37 -06:00
|
|
|
{seek: io.SeekStart, n: 5, want: "01234"},
|
|
|
|
{seek: io.SeekCurrent, n: 5, want: "56789"},
|
|
|
|
{seek: io.SeekEnd, off: -1, n: 1, wantpos: 9, want: "9"},
|
2012-02-08 23:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
pos, err := r.Seek(tt.off, tt.seek)
|
|
|
|
if err == nil && tt.seekerr != "" {
|
|
|
|
t.Errorf("%d. want seek error %q", i, tt.seekerr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil && err.Error() != tt.seekerr {
|
|
|
|
t.Errorf("%d. seek error = %q; want %q", i, err.Error(), tt.seekerr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tt.wantpos != 0 && tt.wantpos != pos {
|
|
|
|
t.Errorf("%d. pos = %d, want %d", i, pos, tt.wantpos)
|
|
|
|
}
|
|
|
|
buf := make([]byte, tt.n)
|
|
|
|
n, err := r.Read(buf)
|
2016-05-03 13:54:49 -06:00
|
|
|
if err != tt.readerr {
|
|
|
|
t.Errorf("%d. read = %v; want %v", i, err, tt.readerr)
|
2012-02-08 23:28:41 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
got := string(buf[:n])
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("%d. got %q; want %q", i, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-14 18:58:00 -07:00
|
|
|
|
2014-03-28 13:23:51 -06:00
|
|
|
func TestReadAfterBigSeek(t *testing.T) {
|
|
|
|
r := strings.NewReader("0123456789")
|
2016-04-12 22:35:37 -06:00
|
|
|
if _, err := r.Seek(1<<31+5, io.SeekStart); err != nil {
|
2014-03-28 13:23:51 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if n, err := r.Read(make([]byte, 10)); n != 0 || err != io.EOF {
|
|
|
|
t.Errorf("Read = %d, %v; want 0, EOF", n, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 18:58:00 -07:00
|
|
|
func TestReaderAt(t *testing.T) {
|
|
|
|
r := strings.NewReader("0123456789")
|
|
|
|
tests := []struct {
|
|
|
|
off int64
|
|
|
|
n int
|
|
|
|
want string
|
|
|
|
wanterr interface{}
|
|
|
|
}{
|
|
|
|
{0, 10, "0123456789", nil},
|
|
|
|
{1, 10, "123456789", io.EOF},
|
|
|
|
{1, 9, "123456789", nil},
|
|
|
|
{11, 10, "", io.EOF},
|
|
|
|
{0, 0, "", nil},
|
2014-04-10 22:45:41 -06:00
|
|
|
{-1, 0, "", "strings.Reader.ReadAt: negative offset"},
|
2012-02-14 18:58:00 -07:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
b := make([]byte, tt.n)
|
|
|
|
rn, err := r.ReadAt(b, tt.off)
|
|
|
|
got := string(b[:rn])
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("%d. got %q; want %q", i, got, tt.want)
|
|
|
|
}
|
|
|
|
if fmt.Sprintf("%v", err) != fmt.Sprintf("%v", tt.wanterr) {
|
|
|
|
t.Errorf("%d. got error = %v; want %v", i, err, tt.wanterr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-11 21:43:50 -06:00
|
|
|
|
2014-04-10 16:46:07 -06:00
|
|
|
func TestReaderAtConcurrent(t *testing.T) {
|
|
|
|
// Test for the race detector, to verify ReadAt doesn't mutate
|
|
|
|
// any state.
|
|
|
|
r := strings.NewReader("0123456789")
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
var buf [1]byte
|
|
|
|
r.ReadAt(buf[:], int64(i))
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2014-04-25 07:44:51 -06:00
|
|
|
func TestEmptyReaderConcurrent(t *testing.T) {
|
|
|
|
// Test for the race detector, to verify a Read that doesn't yield any bytes
|
|
|
|
// is okay to use from multiple goroutines. This was our historic behavior.
|
|
|
|
// See golang.org/issue/7856
|
|
|
|
r := strings.NewReader("")
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
var buf [1]byte
|
|
|
|
r.Read(buf[:])
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
r.Read(nil)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2012-10-11 21:43:50 -06:00
|
|
|
func TestWriteTo(t *testing.T) {
|
|
|
|
const str = "0123456789"
|
2012-11-25 10:04:13 -07:00
|
|
|
for i := 0; i <= len(str); i++ {
|
2012-10-11 21:43:50 -06:00
|
|
|
s := str[i:]
|
|
|
|
r := strings.NewReader(s)
|
|
|
|
var b bytes.Buffer
|
|
|
|
n, err := r.WriteTo(&b)
|
|
|
|
if expect := int64(len(s)); n != expect {
|
|
|
|
t.Errorf("got %v; want %v", n, expect)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2012-11-25 10:04:13 -07:00
|
|
|
t.Errorf("for length %d: got error = %v; want nil", len(s), err)
|
2012-10-11 21:43:50 -06:00
|
|
|
}
|
|
|
|
if b.String() != s {
|
|
|
|
t.Errorf("got string %q; want %q", b.String(), s)
|
|
|
|
}
|
|
|
|
if r.Len() != 0 {
|
|
|
|
t.Errorf("reader contains %v bytes; want 0", r.Len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 15:15:47 -07:00
|
|
|
|
|
|
|
// tests that Len is affected by reads, but Size is not.
|
|
|
|
func TestReaderLenSize(t *testing.T) {
|
|
|
|
r := strings.NewReader("abc")
|
|
|
|
io.CopyN(ioutil.Discard, r, 1)
|
|
|
|
if r.Len() != 2 {
|
|
|
|
t.Errorf("Len = %d; want 2", r.Len())
|
|
|
|
}
|
|
|
|
if r.Size() != 3 {
|
|
|
|
t.Errorf("Size = %d; want 3", r.Size())
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 17:05:23 -06:00
|
|
|
|
|
|
|
func TestReaderReset(t *testing.T) {
|
|
|
|
r := strings.NewReader("世界")
|
|
|
|
if _, _, err := r.ReadRune(); err != nil {
|
|
|
|
t.Errorf("ReadRune: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const want = "abcdef"
|
|
|
|
r.Reset(want)
|
|
|
|
if err := r.UnreadRune(); err == nil {
|
|
|
|
t.Errorf("UnreadRune: expected error, got nil")
|
|
|
|
}
|
|
|
|
buf, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ReadAll: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if got := string(buf); got != want {
|
|
|
|
t.Errorf("ReadAll: got %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|