OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / gxed / hashland / murmur3 / murmur_test.go
1 package murmur3
2
3 import (
4         "hash"
5         "testing"
6 )
7
8 var data = []struct {
9         h32   uint32
10         h64_1 uint64
11         h64_2 uint64
12         s     string
13 }{
14         {0x00000000, 0x0000000000000000, 0x0000000000000000, ""},
15         {0x248bfa47, 0xcbd8a7b341bd9b02, 0x5b1e906a48ae1d19, "hello"},
16         {0x149bbb7f, 0x342fac623a5ebc8e, 0x4cdcbc079642414d, "hello, world"},
17         {0xe31e8a70, 0xb89e5988b737affc, 0x664fc2950231b2cb, "19 Jan 2038 at 3:14:07 AM"},
18         {0xd5c48bfc, 0xcd99481f9ee902c9, 0x695da1a38987b6e7, "The quick brown fox jumps over the lazy dog."},
19 }
20
21 func TestRef(t *testing.T) {
22         for _, elem := range data {
23
24                 var h32 hash.Hash32 = New32()
25                 h32.Write([]byte(elem.s))
26                 if v := h32.Sum32(); v != elem.h32 {
27                         t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32)
28                 }
29
30                 if v := Sum32([]byte(elem.s)); v != elem.h32 {
31                         t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32)
32                 }
33
34                 var h64 hash.Hash64 = New64()
35                 h64.Write([]byte(elem.s))
36                 if v := h64.Sum64(); v != elem.h64_1 {
37                         t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h64_1)
38                 }
39
40                 if v := Sum64([]byte(elem.s)); v != elem.h64_1 {
41                         t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h64_1)
42                 }
43
44                 var h128 Hash128 = New128()
45                 h128.Write([]byte(elem.s))
46                 if v1, v2 := h128.Sum128(); v1 != elem.h64_1 || v2 != elem.h64_2 {
47                         t.Errorf("'%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2)
48                 }
49
50                 if v1, v2 := Sum128([]byte(elem.s)); v1 != elem.h64_1 || v2 != elem.h64_2 {
51                         t.Errorf("'%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2)
52                 }
53         }
54 }
55
56 func TestIncremental(t *testing.T) {
57         for _, elem := range data {
58                 h32 := New32()
59                 h128 := New128()
60                 for i, j, k := 0, 0, len(elem.s); i < k; i = j {
61                         j = 2*i + 3
62                         if j > k {
63                                 j = k
64                         }
65                         s := elem.s[i:j]
66                         print(s + "|")
67                         h32.Write([]byte(s))
68                         h128.Write([]byte(s))
69                 }
70                 println()
71                 if v := h32.Sum32(); v != elem.h32 {
72                         t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32)
73                 }
74                 if v1, v2 := h128.Sum128(); v1 != elem.h64_1 || v2 != elem.h64_2 {
75                         t.Errorf("'%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2)
76                 }
77         }
78 }
79
80 //---
81
82 func bench32(b *testing.B, length int) {
83         buf := make([]byte, length)
84         b.SetBytes(int64(length))
85         b.ResetTimer()
86         for i := 0; i < b.N; i++ {
87                 Sum32(buf)
88         }
89 }
90
91 func Benchmark32_1(b *testing.B) {
92         bench32(b, 1)
93 }
94 func Benchmark32_2(b *testing.B) {
95         bench32(b, 2)
96 }
97 func Benchmark32_4(b *testing.B) {
98         bench32(b, 4)
99 }
100 func Benchmark32_8(b *testing.B) {
101         bench32(b, 8)
102 }
103 func Benchmark32_16(b *testing.B) {
104         bench32(b, 16)
105 }
106 func Benchmark32_32(b *testing.B) {
107         bench32(b, 32)
108 }
109 func Benchmark32_64(b *testing.B) {
110         bench32(b, 64)
111 }
112 func Benchmark32_128(b *testing.B) {
113         bench32(b, 128)
114 }
115 func Benchmark32_256(b *testing.B) {
116         bench32(b, 256)
117 }
118 func Benchmark32_512(b *testing.B) {
119         bench32(b, 512)
120 }
121 func Benchmark32_1024(b *testing.B) {
122         bench32(b, 1024)
123 }
124 func Benchmark32_2048(b *testing.B) {
125         bench32(b, 2048)
126 }
127 func Benchmark32_4096(b *testing.B) {
128         bench32(b, 4096)
129 }
130 func Benchmark32_8192(b *testing.B) {
131         bench32(b, 8192)
132 }
133
134 //---
135
136 func benchPartial32(b *testing.B, length int) {
137         buf := make([]byte, length)
138         b.SetBytes(int64(length))
139
140         start := (32 / 8) / 2
141         chunks := 7
142         k := length / chunks
143         tail := (length - start) % k
144
145         b.ResetTimer()
146         for i := 0; i < b.N; i++ {
147                 hasher := New32()
148                 hasher.Write(buf[0:start])
149
150                 for j := start; j+k <= length; j += k {
151                         hasher.Write(buf[j : j+k])
152                 }
153
154                 hasher.Write(buf[length-tail:])
155                 hasher.Sum32()
156         }
157 }
158
159 func BenchmarkPartial32_8(b *testing.B) {
160         benchPartial32(b, 8)
161 }
162 func BenchmarkPartial32_16(b *testing.B) {
163         benchPartial32(b, 16)
164 }
165 func BenchmarkPartial32_32(b *testing.B) {
166         benchPartial32(b, 32)
167 }
168 func BenchmarkPartial32_64(b *testing.B) {
169         benchPartial32(b, 64)
170 }
171 func BenchmarkPartial32_128(b *testing.B) {
172         benchPartial32(b, 128)
173 }
174
175 //---
176
177 func bench128(b *testing.B, length int) {
178         buf := make([]byte, length)
179         b.SetBytes(int64(length))
180         b.ResetTimer()
181         for i := 0; i < b.N; i++ {
182                 Sum128(buf)
183         }
184 }
185
186 func Benchmark128_1(b *testing.B) {
187         bench128(b, 1)
188 }
189 func Benchmark128_2(b *testing.B) {
190         bench128(b, 2)
191 }
192 func Benchmark128_4(b *testing.B) {
193         bench128(b, 4)
194 }
195 func Benchmark128_8(b *testing.B) {
196         bench128(b, 8)
197 }
198 func Benchmark128_16(b *testing.B) {
199         bench128(b, 16)
200 }
201 func Benchmark128_32(b *testing.B) {
202         bench128(b, 32)
203 }
204 func Benchmark128_64(b *testing.B) {
205         bench128(b, 64)
206 }
207 func Benchmark128_128(b *testing.B) {
208         bench128(b, 128)
209 }
210 func Benchmark128_256(b *testing.B) {
211         bench128(b, 256)
212 }
213 func Benchmark128_512(b *testing.B) {
214         bench128(b, 512)
215 }
216 func Benchmark128_1024(b *testing.B) {
217         bench128(b, 1024)
218 }
219 func Benchmark128_2048(b *testing.B) {
220         bench128(b, 2048)
221 }
222 func Benchmark128_4096(b *testing.B) {
223         bench128(b, 4096)
224 }
225 func Benchmark128_8192(b *testing.B) {
226         bench128(b, 8192)
227 }
228
229 //---