2009-02-16 17:32:30 -07: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.
|
|
|
|
|
2009-08-12 14:18:37 -06:00
|
|
|
package io_test
|
2009-02-16 17:32:30 -07:00
|
|
|
|
|
|
|
import (
|
2017-09-21 18:43:00 -06:00
|
|
|
"bytes"
|
2009-12-15 16:35:38 -07:00
|
|
|
"fmt"
|
|
|
|
. "io"
|
2017-09-21 18:43:00 -06:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2009-12-15 16:35:38 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
2009-02-16 17:32:30 -07:00
|
|
|
)
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
func checkWrite(t *testing.T, w Writer, data []byte, c chan int) {
|
2009-12-15 16:35:38 -07:00
|
|
|
n, err := w.Write(data)
|
2009-02-16 17:32:30 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("write: %v", err)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
if n != len(data) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("short write: %d != %d", n, len(data))
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
c <- 0
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test a single read/write pair.
|
|
|
|
func TestPipe1(t *testing.T) {
|
2009-12-15 16:35:38 -07:00
|
|
|
c := make(chan int)
|
|
|
|
r, w := Pipe()
|
|
|
|
var buf = make([]byte, 64)
|
2010-02-25 17:01:29 -07:00
|
|
|
go checkWrite(t, w, []byte("hello, world"), c)
|
2009-12-15 16:35:38 -07:00
|
|
|
n, err := r.Read(buf)
|
2009-02-16 17:32:30 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("read: %v", err)
|
2009-10-06 20:41:51 -06:00
|
|
|
} else if n != 12 || string(buf[0:12]) != "hello, world" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("bad read: got %q", buf[0:n])
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
<-c
|
|
|
|
r.Close()
|
|
|
|
w.Close()
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
func reader(t *testing.T, r Reader, c chan int) {
|
2009-12-15 16:35:38 -07:00
|
|
|
var buf = make([]byte, 64)
|
2009-02-16 17:32:30 -07:00
|
|
|
for {
|
2009-12-15 16:35:38 -07:00
|
|
|
n, err := r.Read(buf)
|
2011-11-01 19:48:52 -06:00
|
|
|
if err == EOF {
|
2009-12-15 16:35:38 -07:00
|
|
|
c <- 0
|
|
|
|
break
|
2009-06-22 14:26:13 -06:00
|
|
|
}
|
2009-02-16 17:32:30 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("read: %v", err)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
c <- n
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a sequence of read/write pairs.
|
|
|
|
func TestPipe2(t *testing.T) {
|
2009-12-15 16:35:38 -07:00
|
|
|
c := make(chan int)
|
|
|
|
r, w := Pipe()
|
|
|
|
go reader(t, r, c)
|
|
|
|
var buf = make([]byte, 64)
|
2009-02-16 17:32:30 -07:00
|
|
|
for i := 0; i < 5; i++ {
|
2009-12-15 16:35:38 -07:00
|
|
|
p := buf[0 : 5+i*10]
|
|
|
|
n, err := w.Write(p)
|
2009-02-16 17:32:30 -07:00
|
|
|
if n != len(p) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("wrote %d, got %d", len(p), n)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("write: %v", err)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
nn := <-c
|
2009-02-16 17:32:30 -07:00
|
|
|
if nn != n {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("wrote %d, read got %d", n, nn)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
w.Close()
|
|
|
|
nn := <-c
|
2009-02-16 17:32:30 -07:00
|
|
|
if nn != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("final read got %d", nn)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-12 14:18:37 -06:00
|
|
|
type pipeReturn struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
n int
|
2011-11-01 19:48:52 -06:00
|
|
|
err error
|
2009-08-12 14:18:37 -06:00
|
|
|
}
|
|
|
|
|
2009-02-16 17:32:30 -07:00
|
|
|
// Test a large write that requires multiple reads to satisfy.
|
2009-05-08 12:22:57 -06:00
|
|
|
func writer(w WriteCloser, buf []byte, c chan pipeReturn) {
|
2009-12-15 16:35:38 -07:00
|
|
|
n, err := w.Write(buf)
|
|
|
|
w.Close()
|
|
|
|
c <- pipeReturn{n, err}
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPipe3(t *testing.T) {
|
2009-12-15 16:35:38 -07:00
|
|
|
c := make(chan pipeReturn)
|
|
|
|
r, w := Pipe()
|
|
|
|
var wdat = make([]byte, 128)
|
2009-02-16 17:32:30 -07:00
|
|
|
for i := 0; i < len(wdat); i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
wdat[i] = byte(i)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
go writer(w, wdat, c)
|
|
|
|
var rdat = make([]byte, 1024)
|
|
|
|
tot := 0
|
2009-02-16 17:32:30 -07:00
|
|
|
for n := 1; n <= 256; n *= 2 {
|
2009-12-15 16:35:38 -07:00
|
|
|
nn, err := r.Read(rdat[tot : tot+n])
|
2011-11-01 19:48:52 -06:00
|
|
|
if err != nil && err != EOF {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Fatalf("read: %v", err)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// only final two reads should be short - 1 byte, then 0
|
2009-12-15 16:35:38 -07:00
|
|
|
expect := n
|
2009-02-16 17:32:30 -07:00
|
|
|
if n == 128 {
|
2009-11-09 13:07:39 -07:00
|
|
|
expect = 1
|
2009-02-16 17:32:30 -07:00
|
|
|
} else if n == 256 {
|
2009-12-15 16:35:38 -07:00
|
|
|
expect = 0
|
2011-11-01 19:48:52 -06:00
|
|
|
if err != EOF {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Fatalf("read at end: %v", err)
|
2009-06-22 14:26:13 -06:00
|
|
|
}
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
if nn != expect {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Fatalf("read %d, expected %d, got %d", n, expect, nn)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
tot += nn
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
pr := <-c
|
2009-02-16 17:32:30 -07:00
|
|
|
if pr.n != 128 || pr.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Fatalf("write 128: %d, %v", pr.n, pr.err)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
if tot != 128 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Fatalf("total read %d != 128", tot)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
for i := 0; i < 128; i++ {
|
|
|
|
if rdat[i] != byte(i) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Fatalf("rdat[%d] = %d", i, rdat[i])
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test read after/before writer close.
|
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
type closer interface {
|
2011-11-01 19:48:52 -06:00
|
|
|
CloseWithError(error) error
|
|
|
|
Close() error
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
type pipeTest struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
async bool
|
2011-11-01 19:48:52 -06:00
|
|
|
err error
|
2009-12-15 16:35:38 -07:00
|
|
|
closeWithError bool
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
func (p pipeTest) String() string {
|
2009-11-09 13:07:39 -07:00
|
|
|
return fmt.Sprintf("async=%v err=%v closeWithError=%v", p.async, p.err, p.closeWithError)
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
2009-02-16 17:32:30 -07:00
|
|
|
|
2009-10-06 20:41:51 -06:00
|
|
|
var pipeTests = []pipeTest{
|
2010-10-22 11:06:33 -06:00
|
|
|
{true, nil, false},
|
|
|
|
{true, nil, true},
|
|
|
|
{true, ErrShortWrite, true},
|
|
|
|
{false, nil, false},
|
|
|
|
{false, nil, true},
|
|
|
|
{false, ErrShortWrite, true},
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func delayClose(t *testing.T, cl closer, ch chan int, tt pipeTest) {
|
2011-12-12 16:42:56 -07:00
|
|
|
time.Sleep(1 * time.Millisecond)
|
2011-11-01 19:48:52 -06:00
|
|
|
var err error
|
2009-06-06 22:51:05 -06:00
|
|
|
if tt.closeWithError {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = cl.CloseWithError(tt.err)
|
2009-02-16 17:32:30 -07:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = cl.Close()
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2009-06-06 22:51:05 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("delayClose: %v", err)
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
ch <- 0
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
func TestPipeReadClose(t *testing.T) {
|
|
|
|
for _, tt := range pipeTests {
|
2009-12-15 16:35:38 -07:00
|
|
|
c := make(chan int, 1)
|
|
|
|
r, w := Pipe()
|
2009-06-06 22:51:05 -06:00
|
|
|
if tt.async {
|
2009-11-09 13:07:39 -07:00
|
|
|
go delayClose(t, w, c, tt)
|
2009-06-06 22:51:05 -06:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
delayClose(t, w, c, tt)
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
var buf = make([]byte, 64)
|
|
|
|
n, err := r.Read(buf)
|
|
|
|
<-c
|
|
|
|
want := tt.err
|
2009-06-22 14:26:13 -06:00
|
|
|
if want == nil {
|
2011-11-01 19:48:52 -06:00
|
|
|
want = EOF
|
2009-06-22 14:26:13 -06:00
|
|
|
}
|
|
|
|
if err != want {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("read from closed pipe: %v want %v", err, want)
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
|
|
|
if n != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("read on closed pipe returned %d", n)
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
|
|
|
if err = r.Close(); err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("r.Close: %v", err)
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
|
|
|
}
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 11:17:17 -06:00
|
|
|
// Test close on Read side during Read.
|
|
|
|
func TestPipeReadClose2(t *testing.T) {
|
|
|
|
c := make(chan int, 1)
|
|
|
|
r, _ := Pipe()
|
|
|
|
go delayClose(t, r, c, pipeTest{})
|
|
|
|
n, err := r.Read(make([]byte, 64))
|
|
|
|
<-c
|
2011-11-01 19:48:52 -06:00
|
|
|
if n != 0 || err != ErrClosedPipe {
|
|
|
|
t.Errorf("read from closed pipe: %v, %v want %v, %v", n, err, 0, ErrClosedPipe)
|
2010-04-27 11:17:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
// Test write after/before reader close.
|
2009-02-16 17:32:30 -07:00
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
func TestPipeWriteClose(t *testing.T) {
|
|
|
|
for _, tt := range pipeTests {
|
2009-12-15 16:35:38 -07:00
|
|
|
c := make(chan int, 1)
|
|
|
|
r, w := Pipe()
|
2009-06-06 22:51:05 -06:00
|
|
|
if tt.async {
|
2009-11-09 13:07:39 -07:00
|
|
|
go delayClose(t, r, c, tt)
|
2009-06-06 22:51:05 -06:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
delayClose(t, r, c, tt)
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
n, err := WriteString(w, "hello, world")
|
|
|
|
<-c
|
|
|
|
expect := tt.err
|
2009-06-06 22:51:05 -06:00
|
|
|
if expect == nil {
|
2011-11-01 19:48:52 -06:00
|
|
|
expect = ErrClosedPipe
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
|
|
|
if err != expect {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("write on closed pipe: %v want %v", err, expect)
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
|
|
|
if n != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("write on closed pipe returned %d", n)
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
|
|
|
if err = w.Close(); err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("w.Close: %v", err)
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
|
|
|
}
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2010-01-28 18:38:51 -07:00
|
|
|
|
2016-12-20 02:02:39 -07:00
|
|
|
// Test close on Write side during Write.
|
|
|
|
func TestPipeWriteClose2(t *testing.T) {
|
|
|
|
c := make(chan int, 1)
|
|
|
|
_, w := Pipe()
|
|
|
|
go delayClose(t, w, c, pipeTest{})
|
|
|
|
n, err := w.Write(make([]byte, 64))
|
|
|
|
<-c
|
|
|
|
if n != 0 || err != ErrClosedPipe {
|
|
|
|
t.Errorf("write to closed pipe: %v, %v want %v, %v", n, err, 0, ErrClosedPipe)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-28 18:38:51 -07:00
|
|
|
func TestWriteEmpty(t *testing.T) {
|
|
|
|
r, w := Pipe()
|
|
|
|
go func() {
|
|
|
|
w.Write([]byte{})
|
|
|
|
w.Close()
|
|
|
|
}()
|
|
|
|
var b [2]byte
|
|
|
|
ReadFull(r, b[0:2])
|
|
|
|
r.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteNil(t *testing.T) {
|
|
|
|
r, w := Pipe()
|
|
|
|
go func() {
|
|
|
|
w.Write(nil)
|
|
|
|
w.Close()
|
|
|
|
}()
|
|
|
|
var b [2]byte
|
|
|
|
ReadFull(r, b[0:2])
|
|
|
|
r.Close()
|
|
|
|
}
|
2013-08-13 12:04:09 -06:00
|
|
|
|
|
|
|
func TestWriteAfterWriterClose(t *testing.T) {
|
|
|
|
r, w := Pipe()
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
var writeErr error
|
|
|
|
go func() {
|
|
|
|
_, err := w.Write([]byte("hello"))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got error: %q; expected none", err)
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
_, writeErr = w.Write([]byte("world"))
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
buf := make([]byte, 100)
|
|
|
|
var result string
|
|
|
|
n, err := ReadFull(r, buf)
|
|
|
|
if err != nil && err != ErrUnexpectedEOF {
|
|
|
|
t.Fatalf("got: %q; want: %q", err, ErrUnexpectedEOF)
|
|
|
|
}
|
|
|
|
result = string(buf[0:n])
|
|
|
|
<-done
|
|
|
|
|
|
|
|
if result != "hello" {
|
|
|
|
t.Errorf("got: %q; want: %q", result, "hello")
|
|
|
|
}
|
|
|
|
if writeErr != ErrClosedPipe {
|
|
|
|
t.Errorf("got: %q; want: %q", writeErr, ErrClosedPipe)
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 18:43:00 -06:00
|
|
|
|
2017-11-02 16:03:28 -06:00
|
|
|
func TestPipeCloseError(t *testing.T) {
|
|
|
|
type testError1 struct{ error }
|
|
|
|
type testError2 struct{ error }
|
|
|
|
|
|
|
|
r, w := Pipe()
|
|
|
|
r.CloseWithError(testError1{})
|
|
|
|
if _, err := w.Write(nil); err != (testError1{}) {
|
|
|
|
t.Errorf("Write error: got %T, want testError1", err)
|
|
|
|
}
|
|
|
|
r.CloseWithError(testError2{})
|
|
|
|
if _, err := w.Write(nil); err != (testError2{}) {
|
|
|
|
t.Errorf("Write error: got %T, want testError2", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, w = Pipe()
|
|
|
|
w.CloseWithError(testError1{})
|
|
|
|
if _, err := r.Read(nil); err != (testError1{}) {
|
|
|
|
t.Errorf("Read error: got %T, want testError1", err)
|
|
|
|
}
|
|
|
|
w.CloseWithError(testError2{})
|
|
|
|
if _, err := r.Read(nil); err != (testError2{}) {
|
|
|
|
t.Errorf("Read error: got %T, want testError2", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 18:43:00 -06:00
|
|
|
func TestPipeConcurrent(t *testing.T) {
|
|
|
|
const (
|
|
|
|
input = "0123456789abcdef"
|
|
|
|
count = 8
|
|
|
|
readSize = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
t.Run("Write", func(t *testing.T) {
|
|
|
|
r, w := Pipe()
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Millisecond) // Increase probability of race
|
|
|
|
if n, err := w.Write([]byte(input)); n != len(input) || err != nil {
|
|
|
|
t.Errorf("Write() = (%d, %v); want (%d, nil)", n, err, len(input))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, count*len(input))
|
|
|
|
for i := 0; i < len(buf); i += readSize {
|
|
|
|
if n, err := r.Read(buf[i : i+readSize]); n != readSize || err != nil {
|
|
|
|
t.Errorf("Read() = (%d, %v); want (%d, nil)", n, err, readSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since each Write is fully gated, if multiple Read calls were needed,
|
|
|
|
// the contents of Write should still appear together in the output.
|
|
|
|
got := string(buf)
|
|
|
|
want := strings.Repeat(input, count)
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("got: %q; want: %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Read", func(t *testing.T) {
|
|
|
|
r, w := Pipe()
|
|
|
|
|
|
|
|
c := make(chan []byte, count*len(input)/readSize)
|
|
|
|
for i := 0; i < cap(c); i++ {
|
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Millisecond) // Increase probability of race
|
|
|
|
buf := make([]byte, readSize)
|
|
|
|
if n, err := r.Read(buf); n != readSize || err != nil {
|
|
|
|
t.Errorf("Read() = (%d, %v); want (%d, nil)", n, err, readSize)
|
|
|
|
}
|
|
|
|
c <- buf
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
if n, err := w.Write([]byte(input)); n != len(input) || err != nil {
|
|
|
|
t.Errorf("Write() = (%d, %v); want (%d, nil)", n, err, len(input))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since each read is independent, the only guarantee about the output
|
|
|
|
// is that it is a permutation of the input in readSized groups.
|
|
|
|
got := make([]byte, 0, count*len(input))
|
|
|
|
for i := 0; i < cap(c); i++ {
|
|
|
|
got = append(got, (<-c)...)
|
|
|
|
}
|
|
|
|
got = sortBytesInGroups(got, readSize)
|
|
|
|
want := bytes.Repeat([]byte(input), count)
|
|
|
|
want = sortBytesInGroups(want, readSize)
|
|
|
|
if string(got) != string(want) {
|
|
|
|
t.Errorf("got: %q; want: %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortBytesInGroups(b []byte, n int) []byte {
|
|
|
|
var groups [][]byte
|
|
|
|
for len(b) > 0 {
|
|
|
|
groups = append(groups, b[:n])
|
|
|
|
b = b[n:]
|
|
|
|
}
|
|
|
|
sort.Slice(groups, func(i, j int) bool { return bytes.Compare(groups[i], groups[j]) < 0 })
|
|
|
|
return bytes.Join(groups, nil)
|
|
|
|
}
|