writer_test.go 4.4 KB

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