~chipaca/ubuntu-push/cherrypickings

« back to all changes in this revision

Viewing changes to external/murmur3/murmur_test.go

  • Committer: Tarmac
  • Author(s): Samuele Pedroni (Canonical Services Ltd.)
  • Date: 2014-03-24 16:18:08 UTC
  • mfrom: (88.1.4 gethosts)
  • Revision ID: tarmac-20140324161808-5h9lgcg76d5ngaxn
[r=chipaca] introduce package gethosts implementing finding hosts to connect to for delivery of notifications

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        {0x514e28b7, 0x4610abe56eff5cb5, 0x51622daa78f83583, ""},
 
15
        {0xbb4abcad, 0xa78ddff5adae8d10, 0x128900ef20900135, "hello"},
 
16
        {0x6f5cb2e9, 0x8b95f808840725c6, 0x1597ed5422bd493b, "hello, world"},
 
17
        {0xf50e1f30, 0x2a929de9c8f97b2f, 0x56a41d99af43a2db, "19 Jan 2038 at 3:14:07 AM"},
 
18
        {0x846f6a36, 0xfb3325171f9744da, 0xaaf8b92a5f722952, "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
                var h128 Hash128 = New128()
 
41
                h128.Write([]byte(elem.s))
 
42
                if v1, v2 := h128.Sum128(); v1 != elem.h64_1 || v2 != elem.h64_2 {
 
43
                        t.Errorf("'%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2)
 
44
                }
 
45
 
 
46
                if v1, v2 := Sum128([]byte(elem.s)); 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
}
 
51
 
 
52
func TestIncremental(t *testing.T) {
 
53
        for _, elem := range data {
 
54
                h32 := New32()
 
55
                h128 := New128()
 
56
                for i, j, k := 0, 0, len(elem.s); i < k; i = j {
 
57
                        j = 2*i + 3
 
58
                        if j > k {
 
59
                                j = k
 
60
                        }
 
61
                        s := elem.s[i:j]
 
62
                        print(s + "|")
 
63
                        h32.Write([]byte(s))
 
64
                        h128.Write([]byte(s))
 
65
                }
 
66
                println()
 
67
                if v := h32.Sum32(); v != elem.h32 {
 
68
                        t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32)
 
69
                }
 
70
                if v1, v2 := h128.Sum128(); v1 != elem.h64_1 || v2 != elem.h64_2 {
 
71
                        t.Errorf("'%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2)
 
72
                }
 
73
        }
 
74
}
 
75
 
 
76
//---
 
77
 
 
78
func bench32(b *testing.B, length int) {
 
79
        buf := make([]byte, length)
 
80
        b.SetBytes(int64(length))
 
81
        b.ResetTimer()
 
82
        for i := 0; i < b.N; i++ {
 
83
                Sum32(buf)
 
84
        }
 
85
}
 
86
 
 
87
func Benchmark32_1(b *testing.B) {
 
88
        bench32(b, 1)
 
89
}
 
90
func Benchmark32_2(b *testing.B) {
 
91
        bench32(b, 2)
 
92
}
 
93
func Benchmark32_4(b *testing.B) {
 
94
        bench32(b, 4)
 
95
}
 
96
func Benchmark32_8(b *testing.B) {
 
97
        bench32(b, 8)
 
98
}
 
99
func Benchmark32_16(b *testing.B) {
 
100
        bench32(b, 16)
 
101
}
 
102
func Benchmark32_32(b *testing.B) {
 
103
        bench32(b, 32)
 
104
}
 
105
func Benchmark32_64(b *testing.B) {
 
106
        bench32(b, 64)
 
107
}
 
108
func Benchmark32_128(b *testing.B) {
 
109
        bench32(b, 128)
 
110
}
 
111
func Benchmark32_256(b *testing.B) {
 
112
        bench32(b, 256)
 
113
}
 
114
func Benchmark32_512(b *testing.B) {
 
115
        bench32(b, 512)
 
116
}
 
117
func Benchmark32_1024(b *testing.B) {
 
118
        bench32(b, 1024)
 
119
}
 
120
func Benchmark32_2048(b *testing.B) {
 
121
        bench32(b, 2048)
 
122
}
 
123
func Benchmark32_4096(b *testing.B) {
 
124
        bench32(b, 4096)
 
125
}
 
126
func Benchmark32_8192(b *testing.B) {
 
127
        bench32(b, 8192)
 
128
}
 
129
 
 
130
//---
 
131
 
 
132
func benchPartial32(b *testing.B, length int) {
 
133
        buf := make([]byte, length)
 
134
        b.SetBytes(int64(length))
 
135
 
 
136
        start := (32 / 8) / 2
 
137
        chunks := 7
 
138
        k := length / chunks
 
139
        tail := (length - start) % k
 
140
 
 
141
        b.ResetTimer()
 
142
        for i := 0; i < b.N; i++ {
 
143
                hasher := New32()
 
144
                hasher.Write(buf[0:start])
 
145
 
 
146
                for j := start; j+k <= length; j += k {
 
147
                        hasher.Write(buf[j : j+k])
 
148
                }
 
149
 
 
150
                hasher.Write(buf[length-tail:])
 
151
                hasher.Sum32()
 
152
        }
 
153
}
 
154
 
 
155
func BenchmarkPartial32_8(b *testing.B) {
 
156
        benchPartial32(b, 8)
 
157
}
 
158
func BenchmarkPartial32_16(b *testing.B) {
 
159
        benchPartial32(b, 16)
 
160
}
 
161
func BenchmarkPartial32_32(b *testing.B) {
 
162
        benchPartial32(b, 32)
 
163
}
 
164
func BenchmarkPartial32_64(b *testing.B) {
 
165
        benchPartial32(b, 64)
 
166
}
 
167
func BenchmarkPartial32_128(b *testing.B) {
 
168
        benchPartial32(b, 128)
 
169
}
 
170
 
 
171
//---
 
172
 
 
173
func bench128(b *testing.B, length int) {
 
174
        buf := make([]byte, length)
 
175
        b.SetBytes(int64(length))
 
176
        b.ResetTimer()
 
177
        for i := 0; i < b.N; i++ {
 
178
                Sum128(buf)
 
179
        }
 
180
}
 
181
 
 
182
func Benchmark128_1(b *testing.B) {
 
183
        bench128(b, 1)
 
184
}
 
185
func Benchmark128_2(b *testing.B) {
 
186
        bench128(b, 2)
 
187
}
 
188
func Benchmark128_4(b *testing.B) {
 
189
        bench128(b, 4)
 
190
}
 
191
func Benchmark128_8(b *testing.B) {
 
192
        bench128(b, 8)
 
193
}
 
194
func Benchmark128_16(b *testing.B) {
 
195
        bench128(b, 16)
 
196
}
 
197
func Benchmark128_32(b *testing.B) {
 
198
        bench128(b, 32)
 
199
}
 
200
func Benchmark128_64(b *testing.B) {
 
201
        bench128(b, 64)
 
202
}
 
203
func Benchmark128_128(b *testing.B) {
 
204
        bench128(b, 128)
 
205
}
 
206
func Benchmark128_256(b *testing.B) {
 
207
        bench128(b, 256)
 
208
}
 
209
func Benchmark128_512(b *testing.B) {
 
210
        bench128(b, 512)
 
211
}
 
212
func Benchmark128_1024(b *testing.B) {
 
213
        bench128(b, 1024)
 
214
}
 
215
func Benchmark128_2048(b *testing.B) {
 
216
        bench128(b, 2048)
 
217
}
 
218
func Benchmark128_4096(b *testing.B) {
 
219
        bench128(b, 4096)
 
220
}
 
221
func Benchmark128_8192(b *testing.B) {
 
222
        bench128(b, 8192)
 
223
}
 
224
 
 
225
//---