block_test.go 3.9 KB

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