Skip to content

Commit

Permalink
test/fix: Read() may not always fill up buf
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge committed Jan 11, 2015
1 parent 473330a commit 1208045
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion xxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 21 additions & 1 deletion xxd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit 1208045

Please sign in to comment.