OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [hash/] [fnv/] [fnv_test.go] - Blame information for rev 861

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
package fnv
6
 
7
import (
8
        "bytes"
9
        "encoding/binary"
10
        "hash"
11
        "testing"
12
)
13
 
14
const testDataSize = 40
15
 
16
type golden struct {
17
        sum  []byte
18
        text string
19
}
20
 
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"},
26
}
27
 
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"},
33
}
34
 
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"},
40
}
41
 
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"},
47
}
48
 
49
func TestGolden32(t *testing.T) {
50
        testGolden(t, New32(), golden32)
51
}
52
 
53
func TestGolden32a(t *testing.T) {
54
        testGolden(t, New32a(), golden32a)
55
}
56
 
57
func TestGolden64(t *testing.T) {
58
        testGolden(t, New64(), golden64)
59
}
60
 
61
func TestGolden64a(t *testing.T) {
62
        testGolden(t, New64a(), golden64a)
63
}
64
 
65
func testGolden(t *testing.T, hash hash.Hash, gold []golden) {
66
        for _, g := range gold {
67
                hash.Reset()
68
                done, error := hash.Write([]byte(g.text))
69
                if error != nil {
70
                        t.Fatalf("write error: %s", error)
71
                }
72
                if done != len(g.text) {
73
                        t.Fatalf("wrote only %d out of %d bytes", done, len(g.text))
74
                }
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)
77
                }
78
        }
79
}
80
 
81
func TestIntegrity32(t *testing.T) {
82
        testIntegrity(t, New32())
83
}
84
 
85
func TestIntegrity32a(t *testing.T) {
86
        testIntegrity(t, New32a())
87
}
88
 
89
func TestIntegrity64(t *testing.T) {
90
        testIntegrity(t, New64())
91
}
92
 
93
func TestIntegrity64a(t *testing.T) {
94
        testIntegrity(t, New64a())
95
}
96
 
97
func testIntegrity(t *testing.T, h hash.Hash) {
98
        data := []byte{'1', '2', 3, 4, 5}
99
        h.Write(data)
100
        sum := h.Sum(nil)
101
 
102
        if size := h.Size(); size != len(sum) {
103
                t.Fatalf("Size()=%d but len(Sum())=%d", size, len(sum))
104
        }
105
 
106
        if a := h.Sum(nil); !bytes.Equal(sum, a) {
107
                t.Fatalf("first Sum()=0x%x, second Sum()=0x%x", sum, a)
108
        }
109
 
110
        h.Reset()
111
        h.Write(data)
112
        if a := h.Sum(nil); !bytes.Equal(sum, a) {
113
                t.Fatalf("Sum()=0x%x, but after Reset() Sum()=0x%x", sum, a)
114
        }
115
 
116
        h.Reset()
117
        h.Write(data[:2])
118
        h.Write(data[2:])
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)
121
        }
122
 
123
        switch h.Size() {
124
        case 4:
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)
128
                }
129
        case 8:
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)
133
                }
134
        }
135
}
136
 
137
func Benchmark32(b *testing.B) {
138
        benchmark(b, New32())
139
}
140
 
141
func Benchmark32a(b *testing.B) {
142
        benchmark(b, New32a())
143
}
144
 
145
func Benchmark64(b *testing.B) {
146
        benchmark(b, New64())
147
}
148
 
149
func Benchmark64a(b *testing.B) {
150
        benchmark(b, New64a())
151
}
152
 
153
func benchmark(b *testing.B, h hash.Hash) {
154
        b.ResetTimer()
155
        b.SetBytes(testDataSize)
156
        data := make([]byte, testDataSize)
157
        for i := range data {
158
                data[i] = byte(i + 'a')
159
        }
160
 
161
        b.StartTimer()
162
        for todo := b.N; todo != 0; todo-- {
163
                h.Reset()
164
                h.Write(data)
165
                h.Sum(nil)
166
        }
167
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.