Skip to content

Commit

Permalink
Reader tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrec committed Apr 21, 2020
1 parent ac61e7d commit 3b22354
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
28 changes: 9 additions & 19 deletions block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import (
"github.com/pierrec/lz4"
)

const (
// Should match values in lz4.go
hashLog = 16
htSize = 1 << hashLog
)

type testcase struct {
file string
compressible bool
Expand Down Expand Up @@ -102,18 +96,16 @@ func TestCompressUncompressBlock(t *testing.T) {
t.Run("", func(t *testing.T) {
tc := tc
t.Run(tc.file, func(t *testing.T) {
// t.Parallel()
n = run(t, tc, func(src, dst []byte) (int, error) {
var ht [htSize]int
return lz4.CompressBlock(src, dst, ht[:])
})
})
t.Run(fmt.Sprintf("%s HC", tc.file), func(t *testing.T) {
// t.Parallel()
nhc = run(t, tc, func(src, dst []byte) (int, error) {
return lz4.CompressBlockHC(src, dst, 16, nil)
return lz4.CompressBlock(src, dst, nil)
})
})
//TODO
//t.Run(fmt.Sprintf("%s HC", tc.file), func(t *testing.T) {
// nhc = run(t, tc, func(src, dst []byte) (int, error) {
// return lz4.CompressBlockHC(src, dst, 16, nil)
// })
//})
})
if !t.Failed() {
t.Logf("%-40s: %8d / %8d / %8d\n", tc.file, n, nhc, len(src))
Expand Down Expand Up @@ -146,8 +138,7 @@ func TestCompressCornerCase_CopyDstUpperBound(t *testing.T) {
t.Run(file, func(t *testing.T) {
t.Parallel()
run(src, func(src, dst []byte) (int, error) {
var ht [htSize]int
return lz4.CompressBlock(src, dst, ht[:])
return lz4.CompressBlock(src, dst, nil)
})
})
t.Run(fmt.Sprintf("%s HC", file), func(t *testing.T) {
Expand All @@ -162,13 +153,12 @@ func TestIssue23(t *testing.T) {
compressBuf := make([]byte, lz4.CompressBlockBound(1<<16))
for j := 1; j < 16; j++ {
var buf [1 << 16]byte
var ht [htSize]int

for i := 0; i < len(buf); i += j {
buf[i] = 1
}

n, _ := lz4.CompressBlock(buf[:], compressBuf, ht[:])
n, _ := lz4.CompressBlock(buf[:], compressBuf, nil)
if got, want := n, 300; got > want {
t.Fatalf("not able to compress repeated data: got %d; want %d", got, want)
}
Expand Down
10 changes: 7 additions & 3 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

var readerStates = []aState{
noState: newState,
errorState: newState,
newState: headerState,
headerState: readState,
readState: closedState,
closedState: newState,
errorState: newState,
}

// NewReader returns a new LZ4 frame decoder.
Expand Down Expand Up @@ -64,6 +64,7 @@ func (r *Reader) Size() int {
func (r *Reader) Read(buf []byte) (n int, err error) {
defer r.state.check(&err)
switch r.state.state {
case readState:
case closedState, errorState:
return 0, r.state.err
case newState:
Expand All @@ -72,7 +73,6 @@ func (r *Reader) Read(buf []byte) (n int, err error) {
if err = r.frame.initR(r); r.state.next(err) {
return
}
r.state.next(nil)
r.data = r.frame.Descriptor.Flags.BlockSizeIndex().get()
default:
return 0, r.state.fail()
Expand All @@ -87,6 +87,7 @@ func (r *Reader) Read(buf []byte) (n int, err error) {
goto fillbuf
}
// No uncompressed data yet.
r.data = r.data[:cap(r.data)]
for len(buf) >= len(r.data) {
// Input buffer large enough and no pending data: uncompress directly into it.
switch bn, err = r.frame.Blocks.Block.uncompress(r, buf); err {
Expand All @@ -108,6 +109,7 @@ func (r *Reader) Read(buf []byte) (n int, err error) {
switch bn, err = r.frame.Blocks.Block.uncompress(r, r.data); err {
case nil:
r.handler(bn)
r.data = r.data[:bn]
goto fillbuf
case io.EOF:
default:
Expand All @@ -116,7 +118,9 @@ func (r *Reader) Read(buf []byte) (n int, err error) {
close:
r.handler(bn)
n += bn
err = r.frame.closeR(r)
if er := r.frame.closeR(r); er != nil {
err = er
}
r.frame.Descriptor.Flags.BlockSizeIndex().put(r.data)
r.reset(nil)
return
Expand Down
1 change: 1 addition & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (w *Writer) isNotConcurrent() bool {
func (w *Writer) Write(buf []byte) (n int, err error) {
defer w.state.check(&err)
switch w.state.state {
case writeState:
case closedState, errorState:
return 0, w.state.err
case newState:
Expand Down

0 comments on commit 3b22354

Please sign in to comment.