1 // Copyright 2011 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.
19 var golden32
= []golden
{
20 {[]byte{0x81, 0x1c, 0x9d, 0xc5}, ""},
21 {[]byte{0x05, 0x0c, 0x5d, 0x7e}, "a"},
22 {[]byte{0x70, 0x77, 0x2d, 0x38}, "ab"},
23 {[]byte{0x43, 0x9c, 0x2f, 0x4b}, "abc"},
26 var golden32a
= []golden
{
27 {[]byte{0x81, 0x1c, 0x9d, 0xc5}, ""},
28 {[]byte{0xe4, 0x0c, 0x29, 0x2c}, "a"},
29 {[]byte{0x4d, 0x25, 0x05, 0xca}, "ab"},
30 {[]byte{0x1a, 0x47, 0xe9, 0x0b}, "abc"},
33 var golden64
= []golden
{
34 {[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, ""},
35 {[]byte{0xaf, 0x63, 0xbd, 0x4c, 0x86, 0x01, 0xb7, 0xbe}, "a"},
36 {[]byte{0x08, 0x32, 0x67, 0x07, 0xb4, 0xeb, 0x37, 0xb8}, "ab"},
37 {[]byte{0xd8, 0xdc, 0xca, 0x18, 0x6b, 0xaf, 0xad, 0xcb}, "abc"},
40 var golden64a
= []golden
{
41 {[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, ""},
42 {[]byte{0xaf, 0x63, 0xdc, 0x4c, 0x86, 0x01, 0xec, 0x8c}, "a"},
43 {[]byte{0x08, 0x9c, 0x44, 0x07, 0xb5, 0x45, 0x98, 0x6a}, "ab"},
44 {[]byte{0xe7, 0x1f, 0xa2, 0x19, 0x05, 0x41, 0x57, 0x4b}, "abc"},
47 func TestGolden32(t
*testing
.T
) {
48 testGolden(t
, New32(), golden32
)
51 func TestGolden32a(t
*testing
.T
) {
52 testGolden(t
, New32a(), golden32a
)
55 func TestGolden64(t
*testing
.T
) {
56 testGolden(t
, New64(), golden64
)
59 func TestGolden64a(t
*testing
.T
) {
60 testGolden(t
, New64a(), golden64a
)
63 func testGolden(t
*testing
.T
, hash hash
.Hash
, gold
[]golden
) {
64 for _
, g
:= range gold
{
66 done
, error
:= hash
.Write([]byte(g
.text
))
68 t
.Fatalf("write error: %s", error
)
70 if done
!= len(g
.text
) {
71 t
.Fatalf("wrote only %d out of %d bytes", done
, len(g
.text
))
73 if actual
:= hash
.Sum(nil); !bytes
.Equal(g
.sum
, actual
) {
74 t
.Errorf("hash(%q) = 0x%x want 0x%x", g
.text
, actual
, g
.sum
)
79 func TestIntegrity32(t
*testing
.T
) {
80 testIntegrity(t
, New32())
83 func TestIntegrity32a(t
*testing
.T
) {
84 testIntegrity(t
, New32a())
87 func TestIntegrity64(t
*testing
.T
) {
88 testIntegrity(t
, New64())
91 func TestIntegrity64a(t
*testing
.T
) {
92 testIntegrity(t
, New64a())
95 func testIntegrity(t
*testing
.T
, h hash
.Hash
) {
96 data
:= []byte{'1', '2', 3, 4, 5}
100 if size
:= h
.Size(); size
!= len(sum
) {
101 t
.Fatalf("Size()=%d but len(Sum())=%d", size
, len(sum
))
104 if a
:= h
.Sum(nil); !bytes
.Equal(sum
, a
) {
105 t
.Fatalf("first Sum()=0x%x, second Sum()=0x%x", sum
, a
)
110 if a
:= h
.Sum(nil); !bytes
.Equal(sum
, a
) {
111 t
.Fatalf("Sum()=0x%x, but after Reset() Sum()=0x%x", sum
, a
)
117 if a
:= h
.Sum(nil); !bytes
.Equal(sum
, a
) {
118 t
.Fatalf("Sum()=0x%x, but with partial writes, Sum()=0x%x", sum
, a
)
123 sum32
:= h
.(hash
.Hash32
).Sum32()
124 if sum32
!= binary
.BigEndian
.Uint32(sum
) {
125 t
.Fatalf("Sum()=0x%x, but Sum32()=0x%x", sum
, sum32
)
128 sum64
:= h
.(hash
.Hash64
).Sum64()
129 if sum64
!= binary
.BigEndian
.Uint64(sum
) {
130 t
.Fatalf("Sum()=0x%x, but Sum64()=0x%x", sum
, sum64
)
135 func BenchmarkFnv32KB(b
*testing
.B
) {
136 benchmarkKB(b
, New32())
139 func BenchmarkFnv32aKB(b
*testing
.B
) {
140 benchmarkKB(b
, New32a())
143 func BenchmarkFnv64KB(b
*testing
.B
) {
144 benchmarkKB(b
, New64())
147 func BenchmarkFnv64aKB(b
*testing
.B
) {
148 benchmarkKB(b
, New64a())
151 func benchmarkKB(b
*testing
.B
, h hash
.Hash
) {
153 data
:= make([]byte, 1024)
154 for i
:= range data
{
157 in
:= make([]byte, 0, h
.Size())
160 for i
:= 0; i
< b
.N
; i
++ {