snappy_test.go 2.6 KB

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