-
Notifications
You must be signed in to change notification settings - Fork 36
/
bugs_test.go
131 lines (106 loc) · 2.61 KB
/
bugs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// +build ignore
package xxhash_test
import (
"bytes"
"math/rand"
"reflect"
"testing"
"time"
"testing/quick"
N "github.com/OneOfOne/xxhash"
)
func TestReset64(t *testing.T) {
h := N.New64()
//
p1 := "http"
p2 := "://"
p3 := "www.marmiton.org"
p4 := "/recettes/recherche.aspx"
p5 := "?st=2&aqt=gateau&"
url := p1 + p2 + p3 + p4 + p5
// compute hash by parts
h.Write([]byte(p1))
h.Write([]byte(p2))
h.Write([]byte(p3))
h.Write([]byte(p4))
h.Write([]byte(p5))
s1 := h.Sum64()
h.Reset()
h.Write([]byte(url))
s2 := h.Sum64()
// should be the same, right ?
if s1 != s2 {
t.Errorf("s1 != s2 %x %x", s1, s2)
}
}
func TestReset32(t *testing.T) {
h := N.New32()
//
p1 := "http"
p2 := "://"
p3 := "www.marmiton.org"
p4 := "/recettes/recherche.aspx"
p5 := "?st=2&aqt=gateau&"
url := p1 + p2 + p3 + p4 + p5
// compute hash by parts
h.Write([]byte(p1))
h.Write([]byte(p2))
h.Write([]byte(p3))
h.Write([]byte(p4))
h.Write([]byte(p5))
s1 := h.Sum32()
h.Reset()
h.Write([]byte(url))
s2 := h.Sum32()
// should be the same, right ?
if s1 != s2 {
t.Errorf("s1 != s2 %x %x", s1, s2)
}
}
// issue 8
func TestDataLen(t *testing.T) {
for i := 4; i <= 8096; i += 4 {
testEquality(t, bytes.Repeat([]byte("www."), i/4))
}
}
func testEquality(t *testing.T, v []byte) {
ch64, ch32 := cxx.Checksum64(v), cxx.Checksum32(v)
if h := N.Checksum64(v); ch64 != h {
t.Fatalf("Checksum64 doesn't match, len = %d, expected 0x%X, got 0x%X", len(v), ch64, h)
}
if h := N.Checksum32(v); ch32 != h {
t.Fatalf("Checksum32 doesn't match, len = %d, expected 0x%X, got 0x%X", len(v), ch32, h)
}
h64 := N.New64()
h64.Write(v)
if h := h64.Sum64(); ch64 != h {
t.Fatalf("Sum64() doesn't match, len = %d, expected 0x%X, got 0x%X", len(v), ch64, h)
}
h32 := N.New32()
h32.Write(v)
if h := h32.Sum32(); ch32 != h {
t.Fatalf("Sum32() doesn't match, len = %d, expected 0x%X, got 0x%X", len(v), ch32, h)
}
}
func TestHulkSmash(t *testing.T) {
const C = 10000
rnd, typ := rand.New(rand.NewSource(time.Now().UnixNano())), reflect.TypeOf([]byte(nil))
for i := 0; i < C; i++ {
v, ok := quick.Value(typ, rnd)
if !ok {
t.Fatal("!ok")
}
vb := v.Bytes()
seed := uint64(rnd.Int63())
x64 := N.NewS64(seed)
x64.Write(vb)
if s1, s2 := x64.Sum64(), N.Checksum64S(vb, seed); s1 != s2 {
t.Fatalf("len(v) = %d: %d != %d, should be %d", len(vb), s1, s2, cxx.Checksum64S(vb, seed))
}
x32 := N.NewS32(uint32(seed))
x32.Write(vb)
if s1, s2 := x32.Sum32(), N.Checksum32S(vb, uint32(seed)); s1 != s2 {
t.Fatalf("len(v) = %d: %d != %d, should be %d", len(vb), s1, s2, cxx.Checksum32S(vb, uint32(seed)))
}
}
}