This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
chunk_test.go
89 lines (77 loc) · 2.13 KB
/
chunk_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
package scat_test
import (
"io/ioutil"
"strings"
"testing"
"github.com/Roman2K/scat"
"github.com/Roman2K/scat/checksum"
assert "github.com/stretchr/testify/require"
)
func TestChunk(t *testing.T) {
c := scat.NewChunk(0, nil)
assert.Equal(t, 0, c.Num())
b, err := c.Data().Bytes()
assert.NoError(t, err)
assert.Equal(t, "", string(b))
assert.Equal(t, checksum.Hash{}, c.Hash())
assert.Equal(t, 0, c.TargetSize())
assert.Equal(t, nil, c.Meta().Get("xx"))
}
func TestChunkWithData(t *testing.T) {
c := scat.NewChunk(1, scat.BytesData("yy"))
c.SetHash(checksum.SumBytes([]byte("some hash")))
c.Meta().Set("x", "y")
cHash := c.Hash()
dup := c.WithData(scat.BytesData("zzz"))
dup.SetTargetSize(3)
b, err := dup.Data().Bytes()
assert.NoError(t, err)
assert.Equal(t, "zzz", string(b))
assert.Equal(t, cHash, dup.Hash())
assert.Equal(t, 0, c.TargetSize())
assert.Equal(t, 3, dup.TargetSize())
dup.SetHash(checksum.SumBytes([]byte("some other hash")))
assert.NotEqual(t, cHash, dup.Hash())
assert.Equal(t, cHash, c.Hash())
assert.Equal(t, "y", dup.Meta().Get("x"))
dup.Meta().Set("x", "y2")
assert.Equal(t, "y", c.Meta().Get("x"))
}
func TestChunkWithDataNoMeta(t *testing.T) {
c := scat.NewChunk(9, nil)
assert.NotPanics(t, func() {
// ...trying to access nil meta map mutex or something
c.WithData(scat.BytesData("a"))
})
}
func TestBytesData(t *testing.T) {
var bd scat.Data
bd = scat.BytesData{}
b, err := bd.Bytes()
assert.NoError(t, err)
assert.Equal(t, "", string(b))
assert.Equal(t, 0, bd.(scat.Sizer).Size())
bd = scat.BytesData{'a'}
b, err = bd.Bytes()
assert.NoError(t, err)
assert.Equal(t, "a", string(b))
assert.Equal(t, 1, bd.(scat.Sizer).Size())
}
func TestReaderData(t *testing.T) {
rd := scat.NewReaderData(strings.NewReader("a"))
buf, err := ioutil.ReadAll(rd.Reader())
assert.NoError(t, err)
assert.Equal(t, "a", string(buf))
rd = scat.NewReaderData(strings.NewReader("a"))
rd.Reader()
assert.Panics(t, func() {
rd.Reader()
})
rd = scat.NewReaderData(strings.NewReader("a"))
buf, err = rd.Bytes()
assert.NoError(t, err)
assert.Equal(t, "a", string(buf))
assert.Panics(t, func() {
rd.Bytes()
})
}