block_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //+build go1.9
  2. package lz4_test
  3. import (
  4. "bytes"
  5. "fmt"
  6. "io/ioutil"
  7. "testing"
  8. "github.com/pierrec/lz4"
  9. )
  10. const (
  11. // Should match values in lz4.go
  12. hashLog = 16
  13. htSize = 1 << hashLog
  14. )
  15. type testcase struct {
  16. file string
  17. compressible bool
  18. src []byte
  19. }
  20. var rawFiles = []testcase{
  21. // {"testdata/207326ba-36f8-11e7-954a-aca46ba8ca73.png", true, nil},
  22. {"testdata/e.txt", false, nil},
  23. {"testdata/gettysburg.txt", true, nil},
  24. {"testdata/Mark.Twain-Tom.Sawyer.txt", true, nil},
  25. {"testdata/pg1661.txt", true, nil},
  26. {"testdata/pi.txt", false, nil},
  27. {"testdata/random.data", false, nil},
  28. {"testdata/repeat.txt", true, nil},
  29. {"testdata/pg1661.txt", true, nil},
  30. }
  31. func TestCompressUncompressBlock(t *testing.T) {
  32. type compressor func(s, d []byte) (int, error)
  33. run := func(t *testing.T, tc testcase, compress compressor) int {
  34. t.Helper()
  35. src := tc.src
  36. // Compress the data.
  37. zbuf := make([]byte, lz4.CompressBlockBound(len(src)))
  38. n, err := compress(src, zbuf)
  39. if err != nil {
  40. t.Error(err)
  41. return 0
  42. }
  43. zbuf = zbuf[:n]
  44. // Make sure that it was actually compressed unless not compressible.
  45. if !tc.compressible {
  46. return 0
  47. }
  48. if n == 0 || n >= len(src) {
  49. t.Errorf("data not compressed: %d/%d", n, len(src))
  50. return 0
  51. }
  52. // Uncompress the data.
  53. buf := make([]byte, len(src))
  54. n, err = lz4.UncompressBlock(zbuf, buf)
  55. if err != nil {
  56. t.Fatal(err)
  57. } else if n < 0 || n > len(buf) {
  58. t.Fatalf("returned written bytes > len(buf): n=%d available=%d", n, len(buf))
  59. } else if n != len(src) {
  60. t.Errorf("expected to decompress into %d bytes got %d", len(src), n)
  61. }
  62. buf = buf[:n]
  63. if !bytes.Equal(src, buf) {
  64. var c int
  65. for i, b := range buf {
  66. if c > 10 {
  67. break
  68. }
  69. if src[i] != b {
  70. t.Errorf("%d: exp(%x) != got(%x)", i, src[i], buf[i])
  71. c++
  72. }
  73. }
  74. t.Fatal("uncompressed compressed data not matching initial input")
  75. return 0
  76. }
  77. return len(zbuf)
  78. }
  79. for _, tc := range rawFiles {
  80. src, err := ioutil.ReadFile(tc.file)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. tc.src = src
  85. var n, nhc int
  86. t.Run("", func(t *testing.T) {
  87. tc := tc
  88. t.Run(tc.file, func(t *testing.T) {
  89. // t.Parallel()
  90. n = run(t, tc, func(src, dst []byte) (int, error) {
  91. var ht [htSize]int
  92. return lz4.CompressBlock(src, dst, ht[:])
  93. })
  94. })
  95. t.Run(fmt.Sprintf("%s HC", tc.file), func(t *testing.T) {
  96. // t.Parallel()
  97. nhc = run(t, tc, func(src, dst []byte) (int, error) {
  98. return lz4.CompressBlockHC(src, dst, -1)
  99. })
  100. })
  101. })
  102. if !t.Failed() {
  103. t.Logf("%-40s: %8d / %8d / %8d\n", tc.file, n, nhc, len(src))
  104. }
  105. }
  106. }
  107. func TestCompressCornerCase_CopyDstUpperBound(t *testing.T) {
  108. type compressor func(s, d []byte) (int, error)
  109. run := func(src []byte, compress compressor) {
  110. t.Helper()
  111. // Compress the data.
  112. // We provide a destination that is too small to trigger an out-of-bounds,
  113. // which makes it return the error we want.
  114. zbuf := make([]byte, int(float64(len(src))*0.40))
  115. _, err := compress(src, zbuf)
  116. if err != lz4.ErrInvalidSourceShortBuffer {
  117. t.Fatal("err should be ErrInvalidSourceShortBuffer, was", err)
  118. }
  119. }
  120. file := "testdata/upperbound.data"
  121. src, err := ioutil.ReadFile(file)
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. t.Run(file, func(t *testing.T) {
  126. t.Parallel()
  127. run(src, func(src, dst []byte) (int, error) {
  128. var ht [htSize]int
  129. return lz4.CompressBlock(src, dst, ht[:])
  130. })
  131. })
  132. t.Run(fmt.Sprintf("%s HC", file), func(t *testing.T) {
  133. t.Parallel()
  134. run(src, func(src, dst []byte) (int, error) {
  135. return lz4.CompressBlockHC(src, dst, -1)
  136. })
  137. })
  138. }
  139. func TestIssue23(t *testing.T) {
  140. compressBuf := make([]byte, lz4.CompressBlockBound(1<<16))
  141. for j := 1; j < 16; j++ {
  142. var buf [1 << 16]byte
  143. var ht [htSize]int
  144. for i := 0; i < len(buf); i += j {
  145. buf[i] = 1
  146. }
  147. n, _ := lz4.CompressBlock(buf[:], compressBuf, ht[:])
  148. if got, want := n, 300; got > want {
  149. t.Fatalf("not able to compress repeated data: got %d; want %d", got, want)
  150. }
  151. }
  152. }