code review 7314069: io: document and test new CopyN return behavior (issue 7314069)

27 views
Skip to first unread message

brad...@golang.org

unread,
Feb 8, 2013, 8:32:22 PM2/8/13
to r...@golang.org, golan...@googlegroups.com, re...@codereview-hr.appspotmail.com
Reviewers: rsc,

Message:
Hello rsc (cc: golan...@googlegroups.com),

I'd like you to review this change to
https://go.googlecode.com/hg/


Description:
io: document and test new CopyN return behavior

Changed accidentally in 28966b7b2f0c (CopyN using Copy).
Updating docs to be consistent with 29bf5ff5064e (ReadFull &
ReadAtLeast)

Please review this at https://codereview.appspot.com/7314069/

Affected files:
M src/pkg/io/io.go
M src/pkg/io/io_test.go


Index: src/pkg/io/io.go
===================================================================
--- a/src/pkg/io/io.go
+++ b/src/pkg/io/io.go
@@ -292,14 +292,16 @@

// CopyN copies n bytes (or until an error) from src to dst.
// It returns the number of bytes copied and the earliest
-// error encountered while copying. Because Read can
-// return the full amount requested as well as an error
-// (including EOF), so can CopyN.
+// error encountered while copying.
+// On return, written == n if and only if err == nil.
//
// If dst implements the ReaderFrom interface,
// the copy is implemented using it.
func CopyN(dst Writer, src Reader, n int64) (written int64, err error) {
written, err = Copy(dst, LimitReader(src, n))
+ if written == n {
+ return n, nil
+ }
if written < n && err == nil {
// src stopped early; must have been EOF.
err = EOF
Index: src/pkg/io/io_test.go
===================================================================
--- a/src/pkg/io/io_test.go
+++ b/src/pkg/io/io_test.go
@@ -6,6 +6,7 @@

import (
"bytes"
+ "errors"
"fmt"
. "io"
"strings"
@@ -89,6 +90,12 @@
return w.w.Write(p)
}

+type wantedAndErrReader struct{}
+
+func (wantedAndErrReader) Read(p []byte) (int, error) {
+ return len(p), errors.New("wantedAndErrReader error")
+}
+
func TestCopyNEOF(t *testing.T) {
// Test that EOF behavior is the same regardless of whether
// argument to CopyN has ReadFrom.
@@ -114,6 +121,16 @@
if n != 3 || err != EOF {
t.Errorf("CopyN(bytes.Buffer, foo, 4) = %d, %v; want 3, EOF", n, err)
}
+
+ n, err = CopyN(b, wantedAndErrReader{}, 5)
+ if n != 5 || err != nil {
+ t.Errorf("CopyN(bytes.Buffer, wantedAndErrReader, 5) = %d, %v; want 5,
nil", n, err)
+ }
+
+ n, err = CopyN(&noReadFrom{b}, wantedAndErrReader{}, 5)
+ if n != 5 || err != nil {
+ t.Errorf("CopyN(noReadFrom, wantedAndErrReader, 5) = %d, %v; want 5,
nil", n, err)
+ }
}

func TestReadAtLeast(t *testing.T) {


Russ Cox

unread,
Feb 13, 2013, 4:42:30 PM2/13/13
to Brad Fitzpatrick, Russ Cox, golang-dev, re...@codereview-hr.appspotmail.com
LGTM

brad...@golang.org

unread,
Feb 13, 2013, 4:52:03 PM2/13/13
to brad...@golang.org, r...@golang.org, golan...@googlegroups.com, re...@codereview-hr.appspotmail.com
*** Submitted as
https://code.google.com/p/go/source/detail?r=7dd660609da5 ***

io: document and test new CopyN return behavior

Changed accidentally in 28966b7b2f0c (CopyN using Copy).
Updating docs to be consistent with 29bf5ff5064e (ReadFull &
ReadAtLeast)

R=rsc
CC=golang-dev
https://codereview.appspot.com/7314069


https://codereview.appspot.com/7314069/
Reply all
Reply to author
Forward
0 new messages