forked from ipfs/go-ipfs-chunker
-
Notifications
You must be signed in to change notification settings - Fork 3
/
buzhash_test.go
85 lines (70 loc) · 1.54 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
package chunk
import (
"bytes"
"io"
"testing"
util "github.com/ipfs/go-ipfs-util"
)
func TestBuzhashChunking(t *testing.T) {
data := make([]byte, 1024*1024*16)
chunkCount := 0
rounds := 100
for i := 0; i < rounds; i++ {
util.NewTimeSeededRand().Read(data)
r := NewBuzhash(bytes.NewReader(data))
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, data) {
t.Fatal("data was chunked incorrectly")
}
}
t.Logf("average block size: %d\n", len(data)*rounds/chunkCount)
}
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)
}
}
}