snappy_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2011 The Snappy-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 snappy
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io/ioutil"
  9. "math/rand"
  10. "strings"
  11. "testing"
  12. )
  13. func roundtrip(b, ebuf, dbuf []byte) error {
  14. e, err := Encode(ebuf, b)
  15. if err != nil {
  16. return fmt.Errorf("encoding error: %v", err)
  17. }
  18. d, err := Decode(dbuf, e)
  19. if err != nil {
  20. return fmt.Errorf("decoding error: %v", err)
  21. }
  22. if !bytes.Equal(b, d) {
  23. return fmt.Errorf("roundtrip mismatch:\n\twant %v\n\tgot %v", b, d)
  24. }
  25. return nil
  26. }
  27. func TestEmpty(t *testing.T) {
  28. if err := roundtrip(nil, nil, nil); err != nil {
  29. t.Fatal(err)
  30. }
  31. }
  32. func TestSmallCopy(t *testing.T) {
  33. for _, ebuf := range [][]byte{nil, make([]byte, 20), make([]byte, 64)} {
  34. for _, dbuf := range [][]byte{nil, make([]byte, 20), make([]byte, 64)} {
  35. for i := 0; i < 32; i++ {
  36. s := "aaaa" + strings.Repeat("b", i) + "aaaabbbb"
  37. if err := roundtrip([]byte(s), ebuf, dbuf); err != nil {
  38. t.Errorf("len(ebuf)=%d, len(dbuf)=%d, i=%d: %v", len(ebuf), len(dbuf), i, err)
  39. }
  40. }
  41. }
  42. }
  43. }
  44. func TestSmallRand(t *testing.T) {
  45. rand.Seed(27354294)
  46. for n := 1; n < 20000; n += 23 {
  47. b := make([]byte, n)
  48. for i, _ := range b {
  49. b[i] = uint8(rand.Uint32())
  50. }
  51. if err := roundtrip(b, nil, nil); err != nil {
  52. t.Fatal(err)
  53. }
  54. }
  55. }
  56. func TestSmallRegular(t *testing.T) {
  57. for n := 1; n < 20000; n += 23 {
  58. b := make([]byte, n)
  59. for i, _ := range b {
  60. b[i] = uint8(i%10 + 'a')
  61. }
  62. if err := roundtrip(b, nil, nil); err != nil {
  63. t.Fatal(err)
  64. }
  65. }
  66. }
  67. func benchWords(b *testing.B, n int, decode bool) {
  68. b.StopTimer()
  69. // Make src, a []byte of length n containing copies of the words file.
  70. words, err := ioutil.ReadFile("/usr/share/dict/words")
  71. if err != nil {
  72. panic(err)
  73. }
  74. if len(words) == 0 {
  75. panic("/usr/share/dict/words has zero length")
  76. }
  77. src := make([]byte, n)
  78. for x := src; len(x) > 0; {
  79. n := copy(x, words)
  80. x = x[n:]
  81. }
  82. // If benchmarking decoding, encode the src.
  83. if decode {
  84. src, err = Encode(nil, src)
  85. if err != nil {
  86. panic(err)
  87. }
  88. }
  89. b.SetBytes(int64(len(src)))
  90. // Allocate a sufficiently large dst buffer.
  91. var dst []byte
  92. if decode {
  93. dst = make([]byte, n)
  94. } else {
  95. dst = make([]byte, MaxEncodedLen(n))
  96. }
  97. // Run the loop.
  98. b.StartTimer()
  99. for i := 0; i < b.N; i++ {
  100. if decode {
  101. Decode(dst, src)
  102. } else {
  103. Encode(dst, src)
  104. }
  105. }
  106. }
  107. func BenchmarkDecodeWords1e3(b *testing.B) { benchWords(b, 1e3, true) }
  108. func BenchmarkDecodeWords1e4(b *testing.B) { benchWords(b, 1e4, true) }
  109. func BenchmarkDecodeWords1e5(b *testing.B) { benchWords(b, 1e5, true) }
  110. func BenchmarkDecodeWords1e6(b *testing.B) { benchWords(b, 1e6, true) }
  111. func BenchmarkEncodeWords1e3(b *testing.B) { benchWords(b, 1e3, false) }
  112. func BenchmarkEncodeWords1e4(b *testing.B) { benchWords(b, 1e4, false) }
  113. func BenchmarkEncodeWords1e5(b *testing.B) { benchWords(b, 1e5, false) }
  114. func BenchmarkEncodeWords1e6(b *testing.B) { benchWords(b, 1e6, false) }