block_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //+build go1.9
  2. package lz4_test
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "reflect"
  7. "testing"
  8. "github.com/pierrec/lz4"
  9. )
  10. type testcase struct {
  11. file string
  12. compressible bool
  13. src []byte
  14. }
  15. var rawFiles = []testcase{
  16. // {"testdata/207326ba-36f8-11e7-954a-aca46ba8ca73.png", true, nil},
  17. {"testdata/e.txt", true, nil},
  18. {"testdata/gettysburg.txt", true, nil},
  19. {"testdata/Mark.Twain-Tom.Sawyer.txt", true, nil},
  20. {"testdata/pg1661.txt", true, nil},
  21. {"testdata/pi.txt", true, nil},
  22. {"testdata/random.data", false, nil},
  23. {"testdata/repeat.txt", true, nil},
  24. }
  25. func TestCompressUncompressBlock(t *testing.T) {
  26. type compressor func(s, d []byte) (int, error)
  27. run := func(tc testcase, compress compressor) int {
  28. t.Helper()
  29. src := tc.src
  30. // Compress the data.
  31. zbuf := make([]byte, lz4.CompressBlockBound(len(src)))
  32. n, err := compress(src, zbuf)
  33. if err != nil {
  34. t.Error(err)
  35. return 0
  36. }
  37. zbuf = zbuf[:n]
  38. // Make sure that it was actually compressed unless not compressible.
  39. if !tc.compressible {
  40. return 0
  41. }
  42. if n == 0 || n >= len(src) {
  43. t.Errorf("data not compressed: %d/%d", n, len(src))
  44. return 0
  45. }
  46. // Uncompress the data.
  47. buf := make([]byte, len(src))
  48. n, err = lz4.UncompressBlock(zbuf, buf)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. buf = buf[:n]
  53. if !reflect.DeepEqual(src, buf) {
  54. t.Error("uncompressed compressed data not matching initial input")
  55. return 0
  56. }
  57. return len(zbuf)
  58. }
  59. for _, tc := range rawFiles {
  60. src, err := ioutil.ReadFile(tc.file)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. tc.src = src
  65. var n, nhc int
  66. t.Run("", func(t *testing.T) {
  67. tc := tc
  68. t.Run(tc.file, func(t *testing.T) {
  69. t.Parallel()
  70. n = run(tc, func(src, dst []byte) (int, error) {
  71. var ht [1 << 16]int
  72. return lz4.CompressBlock(src, dst, ht[:])
  73. })
  74. })
  75. t.Run(fmt.Sprintf("%s HC", tc.file), func(t *testing.T) {
  76. t.Parallel()
  77. nhc = run(tc, func(src, dst []byte) (int, error) {
  78. return lz4.CompressBlockHC(src, dst, -1)
  79. })
  80. })
  81. })
  82. fmt.Printf("%-40s: %8d / %8d / %8d\n", tc.file, n, nhc, len(src))
  83. }
  84. }
  85. func TestCompressCornerCase_CopyDstUpperBound(t *testing.T) {
  86. type compressor func(s, d []byte) (int, error)
  87. run := func(src []byte, compress compressor) {
  88. t.Helper()
  89. // Compress the data.
  90. zbuf := make([]byte, int(float64(len(src))*0.85))
  91. _, err := compress(src, zbuf)
  92. if err != lz4.ErrInvalidSourceShortBuffer {
  93. t.Fatal("err should be ErrInvalidSourceShortBuffer")
  94. }
  95. }
  96. file := "testdata/upperbound.data"
  97. src, err := ioutil.ReadFile(file)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. t.Run(file, func(t *testing.T) {
  102. t.Parallel()
  103. run(src, func(src, dst []byte) (int, error) {
  104. var ht [1 << 16]int
  105. return lz4.CompressBlock(src, dst, ht[:])
  106. })
  107. })
  108. t.Run(fmt.Sprintf("%s HC", file), func(t *testing.T) {
  109. t.Parallel()
  110. run(src, func(src, dst []byte) (int, error) {
  111. return lz4.CompressBlockHC(src, dst, -1)
  112. })
  113. })
  114. }