reader_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2012 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 flate
  5. import (
  6. "bytes"
  7. "io"
  8. "io/ioutil"
  9. "runtime"
  10. "strings"
  11. "testing"
  12. )
  13. func TestNlitOutOfRange(t *testing.T) {
  14. // Trying to decode this bogus flate data, which has a Huffman table
  15. // with nlit=288, should not panic.
  16. io.Copy(ioutil.Discard, NewReader(strings.NewReader(
  17. "\xfc\xfe\x36\xe7\x5e\x1c\xef\xb3\x55\x58\x77\xb6\x56\xb5\x43\xf4"+
  18. "\x6f\xf2\xd2\xe6\x3d\x99\xa0\x85\x8c\x48\xeb\xf8\xda\x83\x04\x2a"+
  19. "\x75\xc4\xf8\x0f\x12\x11\xb9\xb4\x4b\x09\xa0\xbe\x8b\x91\x4c")))
  20. }
  21. const (
  22. digits = iota
  23. twain
  24. random
  25. )
  26. var testfiles = []string{
  27. // Digits is the digits of the irrational number e. Its decimal representation
  28. // does not repeat, but there are only 10 possible digits, so it should be
  29. // reasonably compressible.
  30. digits: "../testdata/e.txt",
  31. // Twain is Project Gutenberg's edition of Mark Twain's classic English novel.
  32. twain: "../testdata/Mark.Twain-Tom.Sawyer.txt",
  33. // Random bytes
  34. random: "../testdata/sharnd.out",
  35. }
  36. func benchmarkDecode(b *testing.B, testfile, level, n int) {
  37. b.ReportAllocs()
  38. b.StopTimer()
  39. b.SetBytes(int64(n))
  40. buf0, err := ioutil.ReadFile(testfiles[testfile])
  41. if err != nil {
  42. b.Fatal(err)
  43. }
  44. if len(buf0) == 0 {
  45. b.Fatalf("test file %q has no data", testfiles[testfile])
  46. }
  47. compressed := new(bytes.Buffer)
  48. w, err := NewWriter(compressed, level)
  49. if err != nil {
  50. b.Fatal(err)
  51. }
  52. for i := 0; i < n; i += len(buf0) {
  53. if len(buf0) > n-i {
  54. buf0 = buf0[:n-i]
  55. }
  56. io.Copy(w, bytes.NewReader(buf0))
  57. }
  58. w.Close()
  59. buf1 := compressed.Bytes()
  60. buf0, compressed, w = nil, nil, nil
  61. runtime.GC()
  62. b.StartTimer()
  63. r := NewReader(bytes.NewReader(buf1))
  64. res := r.(Resetter)
  65. for i := 0; i < b.N; i++ {
  66. res.Reset(bytes.NewReader(buf1), nil)
  67. io.Copy(ioutil.Discard, r)
  68. }
  69. }
  70. // These short names are so that gofmt doesn't break the BenchmarkXxx function
  71. // bodies below over multiple lines.
  72. const (
  73. constant = ConstantCompression
  74. speed = BestSpeed
  75. default_ = DefaultCompression
  76. compress = BestCompression
  77. )
  78. func BenchmarkDecodeDigitsSpeed1e4(b *testing.B) { benchmarkDecode(b, digits, speed, 1e4) }
  79. func BenchmarkDecodeDigitsSpeed1e5(b *testing.B) { benchmarkDecode(b, digits, speed, 1e5) }
  80. func BenchmarkDecodeDigitsSpeed1e6(b *testing.B) { benchmarkDecode(b, digits, speed, 1e6) }
  81. func BenchmarkDecodeDigitsDefault1e4(b *testing.B) { benchmarkDecode(b, digits, default_, 1e4) }
  82. func BenchmarkDecodeDigitsDefault1e5(b *testing.B) { benchmarkDecode(b, digits, default_, 1e5) }
  83. func BenchmarkDecodeDigitsDefault1e6(b *testing.B) { benchmarkDecode(b, digits, default_, 1e6) }
  84. func BenchmarkDecodeDigitsCompress1e4(b *testing.B) { benchmarkDecode(b, digits, compress, 1e4) }
  85. func BenchmarkDecodeDigitsCompress1e5(b *testing.B) { benchmarkDecode(b, digits, compress, 1e5) }
  86. func BenchmarkDecodeDigitsCompress1e6(b *testing.B) { benchmarkDecode(b, digits, compress, 1e6) }
  87. func BenchmarkDecodeTwainSpeed1e4(b *testing.B) { benchmarkDecode(b, twain, speed, 1e4) }
  88. func BenchmarkDecodeTwainSpeed1e5(b *testing.B) { benchmarkDecode(b, twain, speed, 1e5) }
  89. func BenchmarkDecodeTwainSpeed1e6(b *testing.B) { benchmarkDecode(b, twain, speed, 1e6) }
  90. func BenchmarkDecodeTwainDefault1e4(b *testing.B) { benchmarkDecode(b, twain, default_, 1e4) }
  91. func BenchmarkDecodeTwainDefault1e5(b *testing.B) { benchmarkDecode(b, twain, default_, 1e5) }
  92. func BenchmarkDecodeTwainDefault1e6(b *testing.B) { benchmarkDecode(b, twain, default_, 1e6) }
  93. func BenchmarkDecodeTwainCompress1e4(b *testing.B) { benchmarkDecode(b, twain, compress, 1e4) }
  94. func BenchmarkDecodeTwainCompress1e5(b *testing.B) { benchmarkDecode(b, twain, compress, 1e5) }
  95. func BenchmarkDecodeTwainCompress1e6(b *testing.B) { benchmarkDecode(b, twain, compress, 1e6) }
  96. func BenchmarkDecodeRandomSpeed1e4(b *testing.B) { benchmarkDecode(b, random, speed, 1e4) }
  97. func BenchmarkDecodeRandomSpeed1e5(b *testing.B) { benchmarkDecode(b, random, speed, 1e5) }
  98. func BenchmarkDecodeRandomSpeed1e6(b *testing.B) { benchmarkDecode(b, random, speed, 1e6) }