-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex_test.go
73 lines (58 loc) · 2.04 KB
/
index_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
package gosync
import (
"testing"
"github.com/rkcloudchain/gosync/syncpb"
"github.com/stretchr/testify/assert"
)
var weakA = uint32(1)
var weakB = uint32(2)
func TestMakeChecksumIndex(t *testing.T) {
i := makeChecksumIndex([]*syncpb.ChunkChecksum{})
assert.Equal(t, 0, i.blockCount)
i = makeChecksumIndex([]*syncpb.ChunkChecksum{
{BlockIndex: 0, WeakHash: weakA, StrongHash: []byte("b")},
{BlockIndex: 1, WeakHash: weakB, StrongHash: []byte("c")},
})
assert.Equal(t, 2, i.blockCount)
}
func TestFindWeakChecksum(t *testing.T) {
i := makeChecksumIndex([]*syncpb.ChunkChecksum{
{BlockIndex: 0, WeakHash: weakA, StrongHash: []byte("b")},
{BlockIndex: 1, WeakHash: weakB, StrongHash: []byte("c")},
{BlockIndex: 2, WeakHash: weakB, StrongHash: []byte("d")},
})
result := i.FindWeakChecksum(weakA)
assert.NotNil(t, result)
assert.Len(t, result, 1)
assert.Equal(t, uint32(0), result[0].BlockIndex)
result = i.FindWeakChecksum(weakB)
assert.NotNil(t, result)
assert.Len(t, result, 2)
assert.Equal(t, uint32(1), result[0].BlockIndex)
result = i.FindWeakChecksum(uint32(3))
assert.Nil(t, result)
}
func TestFindStrongChecksum(t *testing.T) {
i := makeChecksumIndex([]*syncpb.ChunkChecksum{
{BlockIndex: 0, WeakHash: weakA, StrongHash: []byte("b")},
{BlockIndex: 1, WeakHash: weakB, StrongHash: []byte("c")},
{BlockIndex: 2, WeakHash: weakB, StrongHash: []byte("d")},
})
result := i.FindWeakChecksum(weakB)
strong := result.FindStrongChecksum([]byte("s"))
assert.Nil(t, strong)
strong = result.FindStrongChecksum([]byte("d"))
assert.NotNil(t, strong)
assert.Equal(t, uint32(2), strong.BlockIndex)
}
func TestFindStrongChecksum2(t *testing.T) {
i := makeChecksumIndex([]*syncpb.ChunkChecksum{
{BlockIndex: 0, WeakHash: weakA, StrongHash: []byte("b")},
{BlockIndex: 1, WeakHash: weakB, StrongHash: []byte("c")},
{BlockIndex: 2, WeakHash: weakB, StrongHash: []byte("d")},
})
result := i.FindWeakChecksum(weakA)
strong := i.FindStrongChecksum(result, []byte("b"))
assert.NotNil(t, strong)
assert.Equal(t, uint32(0), strong.BlockIndex)
}