-
Notifications
You must be signed in to change notification settings - Fork 4
/
writer_test.go
66 lines (58 loc) · 1.38 KB
/
writer_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
package blast_test
import (
"bytes"
"io/ioutil"
"math/rand"
"testing"
"time"
"github.com/JoshVarga/blast"
)
func TestSimpleCompress(t *testing.T) {
expected := []byte{0x00, 0x04, 0x82, 0x24, 0x25, 0x8f, 0x80, 0x7f}
var b bytes.Buffer
w := blast.NewWriter(&b, blast.Binary, blast.DictionarySize1024)
_, err := w.Write([]byte("AIAIAIAIAIAIA"))
if err != nil {
t.Errorf("failed to write: %v", err)
}
err = w.Close()
if err != nil {
t.Errorf("failed to close: %v", err)
}
if !bytes.Equal(b.Bytes(), expected) {
t.Errorf("found=%v : expected=%v", b, expected)
}
}
func TestCompressDecompress(t *testing.T) {
rand.Seed(time.Now().UnixNano())
data := randomBytes(1000, 20)
var b bytes.Buffer
w := blast.NewWriter(&b, blast.Binary, blast.DictionarySize1024)
_, err := w.Write(data)
if err != nil {
t.Errorf("error writing %v", err)
}
err = w.Close()
if err != nil {
t.Errorf("error writing %v", err)
}
reader := bytes.NewBuffer(b.Bytes())
blastReader, err := blast.NewReader(reader)
if err != nil {
t.Errorf("error reading %v", err)
}
decoded, err := ioutil.ReadAll(blastReader)
if err != nil {
t.Errorf("error decoding %v", err)
}
if !bytes.Equal(decoded, data) {
t.Errorf("found=%v\nexpected=%v", decoded, data)
}
}
func randomBytes(length, unique int) []uint8 {
b := make([]uint8, length)
for i := range b {
b[i] = uint8(rand.Intn(unique))
}
return b
}