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.
14 const testDataSize = 40
21 var golden32 = []golden{
22 {[]byte{0x81, 0x1c, 0x9d, 0xc5}, ""},
23 {[]byte{0x05, 0x0c, 0x5d, 0x7e}, "a"},
24 {[]byte{0x70, 0x77, 0x2d, 0x38}, "ab"},
25 {[]byte{0x43, 0x9c, 0x2f, 0x4b}, "abc"},
28 var golden32a = []golden{
29 {[]byte{0x81, 0x1c, 0x9d, 0xc5}, ""},
30 {[]byte{0xe4, 0x0c, 0x29, 0x2c}, "a"},
31 {[]byte{0x4d, 0x25, 0x05, 0xca}, "ab"},
32 {[]byte{0x1a, 0x47, 0xe9, 0x0b}, "abc"},
35 var golden64 = []golden{
36 {[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, ""},
37 {[]byte{0xaf, 0x63, 0xbd, 0x4c, 0x86, 0x01, 0xb7, 0xbe}, "a"},
38 {[]byte{0x08, 0x32, 0x67, 0x07, 0xb4, 0xeb, 0x37, 0xb8}, "ab"},
39 {[]byte{0xd8, 0xdc, 0xca, 0x18, 0x6b, 0xaf, 0xad, 0xcb}, "abc"},
42 var golden64a = []golden{
43 {[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, ""},
44 {[]byte{0xaf, 0x63, 0xdc, 0x4c, 0x86, 0x01, 0xec, 0x8c}, "a"},
45 {[]byte{0x08, 0x9c, 0x44, 0x07, 0xb5, 0x45, 0x98, 0x6a}, "ab"},
46 {[]byte{0xe7, 0x1f, 0xa2, 0x19, 0x05, 0x41, 0x57, 0x4b}, "abc"},
49 func TestGolden32(t *testing.T) {
50 testGolden(t, New32(), golden32)
53 func TestGolden32a(t *testing.T) {
54 testGolden(t, New32a(), golden32a)
57 func TestGolden64(t *testing.T) {
58 testGolden(t, New64(), golden64)
61 func TestGolden64a(t *testing.T) {
62 testGolden(t, New64a(), golden64a)
65 func testGolden(t *testing.T, hash hash.Hash, gold []golden) {
66 for _, g := range gold {
68 done, error := hash.Write([]byte(g.text))
70 t.Fatalf("write error: %s", error)
72 if done != len(g.text) {
73 t.Fatalf("wrote only %d out of %d bytes", done, len(g.text))
75 if actual := hash.Sum(nil); !bytes.Equal(g.sum, actual) {
76 t.Errorf("hash(%q) = 0x%x want 0x%x", g.text, actual, g.sum)
81 func TestIntegrity32(t *testing.T) {
82 testIntegrity(t, New32())
85 func TestIntegrity32a(t *testing.T) {
86 testIntegrity(t, New32a())
89 func TestIntegrity64(t *testing.T) {
90 testIntegrity(t, New64())
93 func TestIntegrity64a(t *testing.T) {
94 testIntegrity(t, New64a())
97 func testIntegrity(t *testing.T, h hash.Hash) {
98 data := []byte{'1', '2', 3, 4, 5}
102 if size := h.Size(); size != len(sum) {
103 t.Fatalf("Size()=%d but len(Sum())=%d", size, len(sum))
106 if a := h.Sum(nil); !bytes.Equal(sum, a) {
107 t.Fatalf("first Sum()=0x%x, second Sum()=0x%x", sum, a)
112 if a := h.Sum(nil); !bytes.Equal(sum, a) {
113 t.Fatalf("Sum()=0x%x, but after Reset() Sum()=0x%x", sum, a)
119 if a := h.Sum(nil); !bytes.Equal(sum, a) {
120 t.Fatalf("Sum()=0x%x, but with partial writes, Sum()=0x%x", sum, a)
125 sum32 := h.(hash.Hash32).Sum32()
126 if sum32 != binary.BigEndian.Uint32(sum) {
127 t.Fatalf("Sum()=0x%x, but Sum32()=0x%x", sum, sum32)
130 sum64 := h.(hash.Hash64).Sum64()
131 if sum64 != binary.BigEndian.Uint64(sum) {
132 t.Fatalf("Sum()=0x%x, but Sum64()=0x%x", sum, sum64)
137 func Benchmark32(b *testing.B) {
138 benchmark(b, New32())
141 func Benchmark32a(b *testing.B) {
142 benchmark(b, New32a())
145 func Benchmark64(b *testing.B) {
146 benchmark(b, New64())
149 func Benchmark64a(b *testing.B) {
150 benchmark(b, New64a())
153 func benchmark(b *testing.B, h hash.Hash) {
155 b.SetBytes(testDataSize)
156 data := make([]byte, testDataSize)
157 for i := range data {
158 data[i] = byte(i + 'a')
162 for todo := b.N; todo != 0; todo-- {