Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzssss committed Oct 30, 2019
1 parent 7d1e666 commit 74a0c98
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions lz4.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
// that they were decompressed to. This limits us to using a decompression
// buffer at least this size, so we might as well actually use this as
// the block size.
// lower than 63 does not work
streamingBlockSize = 1024 * 96
)

Expand Down
49 changes: 43 additions & 6 deletions lz4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package lz4

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -231,24 +232,60 @@ func TestSimpleCompressDecompress(t *testing.T) {

func TestIOCopyStreamSimpleCompressionDecompression(t *testing.T) {
filename := "shakespeare.txt"
inputs, _ := ioutil.ReadFile(filename)
inputs, _ := os.Open(filename)

testIOCopy(t, inputs, filename)
}

func testIOCopy(t *testing.T, payload []byte, filename string) {
func testIOCopy(t *testing.T, src io.Reader, filename string) {
fname := filename + "testcom" + ".lz4"
file, err := os.Create(fname)
failOnError(t, "Failed creating to file", err)

writer := NewWriter(file)
_, err = writer.Write(payload)
failOnError(t, "Failed writing to compress object", err)

// try using io.Copy
// copied, err := io.Copy(writer, src)
// fmt.Println("// copied:", copied)
// failOnError(t, "Failed copied", err)
/////////

// try read file by chunks
BufferSize := 1024 * 96
buffer := make([]byte, BufferSize)
nCum := 0
fileCum := 0
n := 0
var err2 error
for {
bytesread, err := src.Read(buffer)
if bytesread != BufferSize {
fmt.Println("let's look at this:", bytesread)
}
fileCum += bytesread
n, err2 = writer.Write(buffer[:bytesread])
nCum += n
failOnError(t, "Failed writing to compress object", err2)

if err != nil {
if err != io.EOF {
fmt.Println(err)
}
fmt.Println("///EOF: ", bytesread)

break
}

}

failOnError(t, "Failed to close compress object", writer.Close())
stat, err := os.Stat(fname)
filenameSize, err := os.Stat(filename)
failOnError(t, "Cannot open file", err)
fmt.Println("fileCum", fileCum, "nCum: ", nCum)

t.Logf("Compressed %v -> %v bytes", len(payload), stat.Size())
// t.Logf("Compressed %v -> %v bytes", len(src), stat.Size())
t.Logf("Compressed %v -> %v bytes", filenameSize.Size(), stat.Size())

// check the compressed file is the same with the one uploaded to S3

Expand Down Expand Up @@ -285,7 +322,7 @@ func testIOCopy(t *testing.T, payload []byte, filename string) {
}
// just a check to make sure the file contents are the same
if !checkfilecontentIsSame(t, filename, fnameNew) {
t.Fatalf("Cannot compressed file and original is not the same: %s != %s", filename, fnameNew)
t.Fatalf("Original VS Compressed file contents not same: %s != %s", filename, fnameNew)

}

Expand Down

0 comments on commit 74a0c98

Please sign in to comment.