writer_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 _, header := range []lz4.Header{
  25. {}, // Default header.
  26. {BlockChecksum: true},
  27. {NoChecksum: true},
  28. {BlockMaxSize: 64 << 10}, // 64Kb
  29. {CompressionLevel: 10},
  30. {Size: 123},
  31. } {
  32. label := fmt.Sprintf("%s/%s", fname, header)
  33. t.Run(label, func(t *testing.T) {
  34. fname := fname
  35. header := header
  36. t.Parallel()
  37. raw, err := ioutil.ReadFile(fname)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. r := bytes.NewReader(raw)
  42. // Compress.
  43. var zout bytes.Buffer
  44. zw := lz4.NewWriter(&zout)
  45. zw.Header = header
  46. _, err = io.Copy(zw, r)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. err = zw.Close()
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. // Uncompress.
  55. var out bytes.Buffer
  56. zr := lz4.NewReader(&zout)
  57. n, err := io.Copy(&out, zr)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. // The uncompressed data must be the same as the initial input.
  62. if got, want := int(n), len(raw); got != want {
  63. t.Errorf("invalid sizes: got %d; want %d", got, want)
  64. }
  65. if got, want := out.Bytes(), raw; !reflect.DeepEqual(got, want) {
  66. t.Fatal("uncompressed data does not match original")
  67. }
  68. })
  69. }
  70. }
  71. }
  72. func TestIssue41(t *testing.T) {
  73. r, w := io.Pipe()
  74. zw := lz4.NewWriter(w)
  75. zr := lz4.NewReader(r)
  76. data := "x"
  77. go func() {
  78. _, _ = fmt.Fprint(zw, data)
  79. _ = zw.Flush()
  80. _ = zw.Close()
  81. _ = w.Close()
  82. }()
  83. var buf bytes.Buffer
  84. _, _ = buf.ReadFrom(zr)
  85. if got, want := buf.String(), data; got != want {
  86. t.Fatal("uncompressed data does not match original")
  87. }
  88. }
  89. func TestIssue43(t *testing.T) {
  90. r, w := io.Pipe()
  91. go func() {
  92. defer w.Close()
  93. f, err := os.Open("testdata/issue43.data")
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. defer f.Close()
  98. zw := lz4.NewWriter(w)
  99. defer zw.Close()
  100. _, err = io.Copy(zw, f)
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. }()
  105. _, err := io.Copy(ioutil.Discard, lz4.NewReader(r))
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. }
  110. func TestIssue51(t *testing.T) {
  111. data, err := ioutil.ReadFile("testdata/issue51.data")
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. zbuf := make([]byte, 8192)
  116. ht := make([]int, htSize)
  117. n, err := lz4.CompressBlock(data, zbuf, ht)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. zbuf = zbuf[:n]
  122. buf := make([]byte, 8192)
  123. n, err = lz4.UncompressBlock(zbuf, buf)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. buf = buf[:n]
  128. if !bytes.Equal(data, buf) {
  129. t.Fatal("processed data does not match input")
  130. }
  131. }