-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchunker_test.go
More file actions
140 lines (125 loc) · 3.35 KB
/
Copy pathchunker_test.go
File metadata and controls
140 lines (125 loc) · 3.35 KB
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
132
133
134
135
136
137
138
139
140
package core
import (
"bytes"
"errors"
"io"
"math/rand"
"testing"
)
func chunkAll(t *testing.T, data []byte, p ChunkParams) [][]byte {
t.Helper()
c, err := NewChunker(bytes.NewReader(data), p)
if err != nil {
t.Fatalf("NewChunker: %v", err)
}
var out [][]byte
for {
chunk, err := c.Next()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
t.Fatalf("Next: %v", err)
}
out = append(out, append([]byte(nil), chunk...))
}
return out
}
func TestChunkerRoundTripAndBounds(t *testing.T) {
p := DefaultChunkParams()
data := make([]byte, 1<<20)
rand.New(rand.NewSource(42)).Read(data)
chunks := chunkAll(t, data, p)
var joined []byte
for i, ch := range chunks {
if len(ch) > p.Max {
t.Fatalf("chunk %d exceeds max: %d", i, len(ch))
}
if i < len(chunks)-1 && len(ch) < p.Min {
t.Fatalf("non-final chunk %d below min: %d", i, len(ch))
}
joined = append(joined, ch...)
}
if !bytes.Equal(joined, data) {
t.Fatal("concatenated chunks differ from input")
}
if len(chunks) < 2 {
t.Fatalf("expected multiple chunks, got %d", len(chunks))
}
}
func TestChunkerDeterministic(t *testing.T) {
p := DefaultChunkParams()
data := make([]byte, 512<<10)
rand.New(rand.NewSource(7)).Read(data)
a := chunkAll(t, data, p)
b := chunkAll(t, data, p)
if len(a) != len(b) {
t.Fatalf("chunk count differs: %d vs %d", len(a), len(b))
}
for i := range a {
if !bytes.Equal(a[i], b[i]) {
t.Fatalf("chunk %d differs between runs", i)
}
}
}
func TestChunkerInsertionShiftsFewChunks(t *testing.T) {
p := DefaultChunkParams()
data := make([]byte, 1<<20)
rand.New(rand.NewSource(99)).Read(data)
ids := func(chunks [][]byte) map[BlockID]bool {
m := map[BlockID]bool{}
for _, ch := range chunks {
m[ContentID(ch)] = true
}
return m
}
before := ids(chunkAll(t, data, p))
// Insert 100 bytes near the start; most downstream boundaries must
// survive.
mutated := append(append(append([]byte(nil), data[:1000]...), make([]byte, 100)...), data[1000:]...)
after := chunkAll(t, mutated, p)
shared := 0
for _, ch := range after {
if before[ContentID(ch)] {
shared++
}
}
if ratio := float64(shared) / float64(len(after)); ratio < 0.8 {
t.Fatalf("expected most chunks shared after insertion, got %.2f (%d/%d)", ratio, shared, len(after))
}
}
func TestChunkerFixedDegenerate(t *testing.T) {
bs := 4096
p := FixedChunkParams(bs)
data := make([]byte, 3*bs+123)
rand.New(rand.NewSource(1)).Read(data)
chunks := chunkAll(t, data, p)
if len(chunks) != 4 {
t.Fatalf("expected 4 fixed chunks, got %d", len(chunks))
}
for i := 0; i < 3; i++ {
if len(chunks[i]) != bs {
t.Fatalf("chunk %d: expected %d bytes, got %d", i, bs, len(chunks[i]))
}
}
if len(chunks[3]) != 123 {
t.Fatalf("last chunk: expected 123 bytes, got %d", len(chunks[3]))
}
}
func TestChunkerEmptyInput(t *testing.T) {
c, err := NewChunker(bytes.NewReader(nil), DefaultChunkParams())
if err != nil {
t.Fatal(err)
}
if _, err := c.Next(); !errors.Is(err, io.EOF) {
t.Fatalf("expected io.EOF on empty input, got %v", err)
}
}
func TestChunkParamsValidation(t *testing.T) {
if _, err := NewChunker(bytes.NewReader(nil), ChunkParams{Min: 8, Avg: 4, Max: 16}); err == nil {
t.Fatal("expected error for min > avg")
}
if _, err := NewChunker(bytes.NewReader(nil), ChunkParams{Min: 0, Avg: 4, Max: 16}); err == nil {
t.Fatal("expected error for zero min")
}
}