This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
buzhash_test.go
91 lines (76 loc) · 1.67 KB
/
buzhash_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
package chunk
import (
"bytes"
"io"
"testing"
util "github.com/ipfs/go-ipfs-util"
)
func testBuzhashChunking(t *testing.T, buf []byte) (chunkCount int) {
n, err := util.NewTimeSeededRand().Read(buf)
if n < len(buf) {
t.Fatalf("expected %d bytes, got %d", len(buf), n)
}
if err != nil {
t.Fatal(err)
}
r := NewBuzhash(bytes.NewReader(buf))
var chunks [][]byte
for {
chunk, err := r.NextBytes()
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
chunks = append(chunks, chunk)
}
chunkCount += len(chunks)
for i, chunk := range chunks {
if len(chunk) == 0 {
t.Fatalf("chunk %d/%d is empty", i+1, len(chunks))
}
}
for i, chunk := range chunks[:len(chunks)-1] {
if len(chunk) < buzMin {
t.Fatalf("chunk %d/%d is less than the minimum size", i+1, len(chunks))
}
}
unchunked := bytes.Join(chunks, nil)
if !bytes.Equal(unchunked, buf) {
t.Fatal("data was chunked incorrectly")
}
return chunkCount
}
func TestBuzhashChunking(t *testing.T) {
buf := make([]byte, 1024*1024*16)
count := testBuzhashChunking(t, buf)
t.Logf("average block size: %d\n", len(buf)/count)
}
func TestBuzhashChunkReuse(t *testing.T) {
newBuzhash := func(r io.Reader) Splitter {
return NewBuzhash(r)
}
testReuse(t, newBuzhash)
}
func BenchmarkBuzhash2(b *testing.B) {
benchmarkChunker(b, func(r io.Reader) Splitter {
return NewBuzhash(r)
})
}
func TestBuzhashBitsHashBias(t *testing.T) {
counts := make([]byte, 32)
for _, h := range bytehash {
for i := 0; i < 32; i++ {
if h&1 == 1 {
counts[i]++
}
h = h >> 1
}
}
for i, c := range counts {
if c != 128 {
t.Errorf("Bit balance in position %d broken, %d ones", i, c)
}
}
}