1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fnv
import (
"bytes"
"encoding/binary"
"hash"
"testing"
)
type golden struct {
sum []byte
text string
}
var golden32 = []golden{
{[]byte{0x81, 0x1c, 0x9d, 0xc5}, ""},
{[]byte{0x05, 0x0c, 0x5d, 0x7e}, "a"},
{[]byte{0x70, 0x77, 0x2d, 0x38}, "ab"},
{[]byte{0x43, 0x9c, 0x2f, 0x4b}, "abc"},
}
var golden32a = []golden{
{[]byte{0x81, 0x1c, 0x9d, 0xc5}, ""},
{[]byte{0xe4, 0x0c, 0x29, 0x2c}, "a"},
{[]byte{0x4d, 0x25, 0x05, 0xca}, "ab"},
{[]byte{0x1a, 0x47, 0xe9, 0x0b}, "abc"},
}
var golden64 = []golden{
{[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, ""},
{[]byte{0xaf, 0x63, 0xbd, 0x4c, 0x86, 0x01, 0xb7, 0xbe}, "a"},
{[]byte{0x08, 0x32, 0x67, 0x07, 0xb4, 0xeb, 0x37, 0xb8}, "ab"},
{[]byte{0xd8, 0xdc, 0xca, 0x18, 0x6b, 0xaf, 0xad, 0xcb}, "abc"},
}
var golden64a = []golden{
{[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, ""},
{[]byte{0xaf, 0x63, 0xdc, 0x4c, 0x86, 0x01, 0xec, 0x8c}, "a"},
{[]byte{0x08, 0x9c, 0x44, 0x07, 0xb5, 0x45, 0x98, 0x6a}, "ab"},
{[]byte{0xe7, 0x1f, 0xa2, 0x19, 0x05, 0x41, 0x57, 0x4b}, "abc"},
}
func TestGolden32(t *testing.T) {
testGolden(t, New32(), golden32)
}
func TestGolden32a(t *testing.T) {
testGolden(t, New32a(), golden32a)
}
func TestGolden64(t *testing.T) {
testGolden(t, New64(), golden64)
}
func TestGolden64a(t *testing.T) {
testGolden(t, New64a(), golden64a)
}
func testGolden(t *testing.T, hash hash.Hash, gold []golden) {
for _, g := range gold {
hash.Reset()
done, error := hash.Write([]byte(g.text))
if error != nil {
t.Fatalf("write error: %s", error)
}
if done != len(g.text) {
t.Fatalf("wrote only %d out of %d bytes", done, len(g.text))
}
if actual := hash.Sum(nil); !bytes.Equal(g.sum, actual) {
t.Errorf("hash(%q) = 0x%x want 0x%x", g.text, actual, g.sum)
}
}
}
func TestIntegrity32(t *testing.T) {
testIntegrity(t, New32())
}
func TestIntegrity32a(t *testing.T) {
testIntegrity(t, New32a())
}
func TestIntegrity64(t *testing.T) {
testIntegrity(t, New64())
}
func TestIntegrity64a(t *testing.T) {
testIntegrity(t, New64a())
}
func testIntegrity(t *testing.T, h hash.Hash) {
data := []byte{'1', '2', 3, 4, 5}
h.Write(data)
sum := h.Sum(nil)
if size := h.Size(); size != len(sum) {
t.Fatalf("Size()=%d but len(Sum())=%d", size, len(sum))
}
if a := h.Sum(nil); !bytes.Equal(sum, a) {
t.Fatalf("first Sum()=0x%x, second Sum()=0x%x", sum, a)
}
h.Reset()
h.Write(data)
if a := h.Sum(nil); !bytes.Equal(sum, a) {
t.Fatalf("Sum()=0x%x, but after Reset() Sum()=0x%x", sum, a)
}
h.Reset()
h.Write(data[:2])
h.Write(data[2:])
if a := h.Sum(nil); !bytes.Equal(sum, a) {
t.Fatalf("Sum()=0x%x, but with partial writes, Sum()=0x%x", sum, a)
}
switch h.Size() {
case 4:
sum32 := h.(hash.Hash32).Sum32()
if sum32 != binary.BigEndian.Uint32(sum) {
t.Fatalf("Sum()=0x%x, but Sum32()=0x%x", sum, sum32)
}
case 8:
sum64 := h.(hash.Hash64).Sum64()
if sum64 != binary.BigEndian.Uint64(sum) {
t.Fatalf("Sum()=0x%x, but Sum64()=0x%x", sum, sum64)
}
}
}
func BenchmarkFnv32KB(b *testing.B) {
benchmarkKB(b, New32())
}
func BenchmarkFnv32aKB(b *testing.B) {
benchmarkKB(b, New32a())
}
func BenchmarkFnv64KB(b *testing.B) {
benchmarkKB(b, New64())
}
func BenchmarkFnv64aKB(b *testing.B) {
benchmarkKB(b, New64a())
}
func benchmarkKB(b *testing.B, h hash.Hash) {
b.SetBytes(1024)
data := make([]byte, 1024)
for i := range data {
data[i] = byte(i)
}
in := make([]byte, 0, h.Size())
b.ResetTimer()
for i := 0; i < b.N; i++ {
h.Reset()
h.Write(data)
h.Sum(in)
}
}
|