writer_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package zlib
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "os"
  11. "testing"
  12. )
  13. var filenames = []string{
  14. "../testdata/gettysburg.txt",
  15. "../testdata/e.txt",
  16. "../testdata/pi.txt",
  17. }
  18. var data = []string{
  19. "test a reasonable sized string that can be compressed",
  20. }
  21. // Tests that compressing and then decompressing the given file at the given compression level and dictionary
  22. // yields equivalent bytes to the original file.
  23. func testFileLevelDict(t *testing.T, fn string, level int, d string) {
  24. // Read the file, as golden output.
  25. golden, err := os.Open(fn)
  26. if err != nil {
  27. t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
  28. return
  29. }
  30. defer golden.Close()
  31. b0, err0 := ioutil.ReadAll(golden)
  32. if err0 != nil {
  33. t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err0)
  34. return
  35. }
  36. testLevelDict(t, fn, b0, level, d)
  37. }
  38. func testLevelDict(t *testing.T, fn string, b0 []byte, level int, d string) {
  39. // Make dictionary, if given.
  40. var dict []byte
  41. if d != "" {
  42. dict = []byte(d)
  43. }
  44. // Push data through a pipe that compresses at the write end, and decompresses at the read end.
  45. piper, pipew := io.Pipe()
  46. defer piper.Close()
  47. go func() {
  48. defer pipew.Close()
  49. zlibw, err := NewWriterLevelDict(pipew, level, dict)
  50. if err != nil {
  51. t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
  52. return
  53. }
  54. defer zlibw.Close()
  55. _, err = zlibw.Write(b0)
  56. if err != nil {
  57. t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
  58. return
  59. }
  60. }()
  61. zlibr, err := NewReaderDict(piper, dict)
  62. if err != nil {
  63. t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
  64. return
  65. }
  66. defer zlibr.Close()
  67. // Compare the decompressed data.
  68. b1, err1 := ioutil.ReadAll(zlibr)
  69. if err1 != nil {
  70. t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err1)
  71. return
  72. }
  73. if len(b0) != len(b1) {
  74. t.Errorf("%s (level=%d, dict=%q): length mismatch %d versus %d", fn, level, d, len(b0), len(b1))
  75. return
  76. }
  77. for i := 0; i < len(b0); i++ {
  78. if b0[i] != b1[i] {
  79. t.Errorf("%s (level=%d, dict=%q): mismatch at %d, 0x%02x versus 0x%02x\n", fn, level, d, i, b0[i], b1[i])
  80. return
  81. }
  82. }
  83. }
  84. func testFileLevelDictReset(t *testing.T, fn string, level int, dict []byte) {
  85. var b0 []byte
  86. var err error
  87. if fn != "" {
  88. b0, err = ioutil.ReadFile(fn)
  89. if err != nil {
  90. t.Errorf("%s (level=%d): %v", fn, level, err)
  91. return
  92. }
  93. }
  94. // Compress once.
  95. buf := new(bytes.Buffer)
  96. var zlibw *Writer
  97. if dict == nil {
  98. zlibw, err = NewWriterLevel(buf, level)
  99. } else {
  100. zlibw, err = NewWriterLevelDict(buf, level, dict)
  101. }
  102. if err == nil {
  103. _, err = zlibw.Write(b0)
  104. }
  105. if err == nil {
  106. err = zlibw.Close()
  107. }
  108. if err != nil {
  109. t.Errorf("%s (level=%d): %v", fn, level, err)
  110. return
  111. }
  112. out := buf.String()
  113. // Reset and compress again.
  114. buf2 := new(bytes.Buffer)
  115. zlibw.Reset(buf2)
  116. _, err = zlibw.Write(b0)
  117. if err == nil {
  118. err = zlibw.Close()
  119. }
  120. if err != nil {
  121. t.Errorf("%s (level=%d): %v", fn, level, err)
  122. return
  123. }
  124. out2 := buf2.String()
  125. if out2 != out {
  126. t.Errorf("%s (level=%d): different output after reset (got %d bytes, expected %d",
  127. fn, level, len(out2), len(out))
  128. }
  129. }
  130. func TestWriter(t *testing.T) {
  131. for i, s := range data {
  132. b := []byte(s)
  133. tag := fmt.Sprintf("#%d", i)
  134. testLevelDict(t, tag, b, DefaultCompression, "")
  135. testLevelDict(t, tag, b, NoCompression, "")
  136. testLevelDict(t, tag, b, HuffmanOnly, "")
  137. for level := BestSpeed; level <= BestCompression; level++ {
  138. testLevelDict(t, tag, b, level, "")
  139. }
  140. }
  141. }
  142. func TestWriterBig(t *testing.T) {
  143. for _, fn := range filenames {
  144. testFileLevelDict(t, fn, DefaultCompression, "")
  145. testFileLevelDict(t, fn, NoCompression, "")
  146. testFileLevelDict(t, fn, HuffmanOnly, "")
  147. for level := BestSpeed; level <= BestCompression; level++ {
  148. testFileLevelDict(t, fn, level, "")
  149. }
  150. }
  151. }
  152. func TestWriterDict(t *testing.T) {
  153. const dictionary = "0123456789."
  154. for _, fn := range filenames {
  155. testFileLevelDict(t, fn, DefaultCompression, dictionary)
  156. testFileLevelDict(t, fn, NoCompression, dictionary)
  157. testFileLevelDict(t, fn, HuffmanOnly, dictionary)
  158. for level := BestSpeed; level <= BestCompression; level++ {
  159. testFileLevelDict(t, fn, level, dictionary)
  160. }
  161. }
  162. }
  163. func TestWriterReset(t *testing.T) {
  164. const dictionary = "0123456789."
  165. for _, fn := range filenames {
  166. testFileLevelDictReset(t, fn, NoCompression, nil)
  167. testFileLevelDictReset(t, fn, DefaultCompression, nil)
  168. testFileLevelDictReset(t, fn, HuffmanOnly, nil)
  169. testFileLevelDictReset(t, fn, NoCompression, []byte(dictionary))
  170. testFileLevelDictReset(t, fn, DefaultCompression, []byte(dictionary))
  171. testFileLevelDictReset(t, fn, HuffmanOnly, []byte(dictionary))
  172. if testing.Short() {
  173. break
  174. }
  175. for level := BestSpeed; level <= BestCompression; level++ {
  176. testFileLevelDictReset(t, fn, level, nil)
  177. }
  178. }
  179. }
  180. func TestWriterDictIsUsed(t *testing.T) {
  181. var input = []byte("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
  182. var buf bytes.Buffer
  183. compressor, err := NewWriterLevelDict(&buf, BestCompression, input)
  184. if err != nil {
  185. t.Errorf("error in NewWriterLevelDict: %s", err)
  186. return
  187. }
  188. compressor.Write(input)
  189. compressor.Close()
  190. const expectedMaxSize = 25
  191. output := buf.Bytes()
  192. if len(output) > expectedMaxSize {
  193. t.Errorf("result too large (got %d, want <= %d bytes). Is the dictionary being used?", len(output), expectedMaxSize)
  194. }
  195. }