From 120804574f12033999f23e6cf6a3b75961f14da1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisendo=CC=88rfer?= Date: Sun, 11 Jan 2015 16:52:41 +0100 Subject: [PATCH] test/fix: Read() may not always fill up buf --- xxd.go | 2 +- xxd_test.go | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/xxd.go b/xxd.go index 87796c2..1d8ad24 100644 --- a/xxd.go +++ b/xxd.go @@ -37,7 +37,7 @@ func XXD(r io.Reader, w io.Writer) error { r = bufio.NewReader(r) buf := make([]byte, 16) for { - n, err := r.Read(buf) + n, err := io.ReadFull(r, buf) if n == 0 || err == io.EOF { break } diff --git a/xxd_test.go b/xxd_test.go index a437f4f..4a3173a 100644 --- a/xxd_test.go +++ b/xxd_test.go @@ -28,7 +28,7 @@ func TestXXD(t *testing.T) { size := n % uint64(len(data)) fmt.Printf("%d\n", size) var out bytes.Buffer - if err := fn(bytes.NewBuffer(data[0:size]), &out); err != nil { + if err := fn(&pathologicalReader{data[0:size]}, &out); err != nil { return []string{err.Error()} } return strings.Split(out.String(), "\n") @@ -48,6 +48,26 @@ func TestXXD(t *testing.T) { } } +type pathologicalReader struct { + data []byte +} + +func (p *pathologicalReader) Read(b []byte) (int, error) { + n := len(b) + if n > len(p.data) { + n = len(p.data) + } + if n > 1 { + n-- + } + copy(b, p.data[0:n]) + p.data = p.data[n:] + if len(p.data) == 0 { + return n, io.EOF + } + return n, nil +} + func BenchmarkXXD(b *testing.B) { b.StopTimer() data := make([]byte, b.N)