writer_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package lz4_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "reflect"
  9. "testing"
  10. "github.com/pierrec/lz4"
  11. )
  12. func TestWriter(t *testing.T) {
  13. goldenFiles := []string{
  14. "testdata/e.txt",
  15. "testdata/gettysburg.txt",
  16. "testdata/Mark.Twain-Tom.Sawyer.txt",
  17. "testdata/Mark.Twain-Tom.Sawyer_long.txt",
  18. "testdata/pg1661.txt",
  19. "testdata/pi.txt",
  20. "testdata/random.data",
  21. "testdata/repeat.txt",
  22. }
  23. for _, fname := range goldenFiles {
  24. for _, size := range []int{0, 4} {
  25. for _, header := range []lz4.Header{
  26. {}, // Default header.
  27. {BlockChecksum: true},
  28. {NoChecksum: true},
  29. {BlockMaxSize: 64 << 10}, // 64Kb
  30. {CompressionLevel: 10},
  31. {Size: 123},
  32. } {
  33. label := fmt.Sprintf("%s/%s", fname, header)
  34. t.Run(label, func(t *testing.T) {
  35. fname := fname
  36. header := header
  37. t.Parallel()
  38. raw, err := ioutil.ReadFile(fname)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. r := bytes.NewReader(raw)
  43. // Compress.
  44. var zout bytes.Buffer
  45. zw := lz4.NewWriter(&zout)
  46. zw.Header = header
  47. zw.WithConcurrency(size)
  48. _, err = io.Copy(zw, r)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. err = zw.Close()
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. // Uncompress.
  57. var out bytes.Buffer
  58. zr := lz4.NewReader(&zout)
  59. n, err := io.Copy(&out, zr)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. // The uncompressed data must be the same as the initial input.
  64. if got, want := int(n), len(raw); got != want {
  65. t.Errorf("invalid sizes: got %d; want %d", got, want)
  66. }
  67. if got, want := out.Bytes(), raw; !reflect.DeepEqual(got, want) {
  68. t.Fatal("uncompressed data does not match original")
  69. }
  70. })
  71. }
  72. }
  73. }
  74. }
  75. func TestIssue41(t *testing.T) {
  76. r, w := io.Pipe()
  77. zw := lz4.NewWriter(w)
  78. zr := lz4.NewReader(r)
  79. data := "x"
  80. go func() {
  81. _, _ = fmt.Fprint(zw, data)
  82. _ = zw.Flush()
  83. _ = zw.Close()
  84. _ = w.Close()
  85. }()
  86. var buf bytes.Buffer
  87. _, _ = buf.ReadFrom(zr)
  88. if got, want := buf.String(), data; got != want {
  89. t.Fatal("uncompressed data does not match original")
  90. }
  91. }
  92. func TestIssue43(t *testing.T) {
  93. r, w := io.Pipe()
  94. go func() {
  95. defer w.Close()
  96. f, err := os.Open("testdata/issue43.data")
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. defer f.Close()
  101. zw := lz4.NewWriter(w)
  102. defer zw.Close()
  103. _, err = io.Copy(zw, f)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. }()
  108. _, err := io.Copy(ioutil.Discard, lz4.NewReader(r))
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. }
  113. func TestIssue51(t *testing.T) {
  114. data, err := ioutil.ReadFile("testdata/issue51.data")
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. zbuf := make([]byte, 8192)
  119. ht := make([]int, htSize)
  120. n, err := lz4.CompressBlock(data, zbuf, ht)
  121. if err != nil {
  122. t.Fatal(err)
  123. }
  124. zbuf = zbuf[:n]
  125. buf := make([]byte, 8192)
  126. n, err = lz4.UncompressBlock(zbuf, buf)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. buf = buf[:n]
  131. if !bytes.Equal(data, buf) {
  132. t.Fatal("processed data does not match input")
  133. }
  134. }
  135. func TestIssue71(t *testing.T) {
  136. for _, tc := range []string{
  137. "abc", // < mfLimit
  138. "abcdefghijklmnopq", // > mfLimit
  139. } {
  140. t.Run(tc, func(t *testing.T) {
  141. src := []byte(tc)
  142. bound := lz4.CompressBlockBound(len(tc))
  143. // Small buffer.
  144. zSmall := make([]byte, bound-1)
  145. n, err := lz4.CompressBlock(src, zSmall, nil)
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. if n != 0 {
  150. t.Fatal("should be incompressible")
  151. }
  152. // Large enough buffer.
  153. zLarge := make([]byte, bound)
  154. n, err = lz4.CompressBlock(src, zLarge, nil)
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. if n == 0 {
  159. t.Fatal("should be compressible")
  160. }
  161. })
  162. }
  163. }