-
Notifications
You must be signed in to change notification settings - Fork 0
/
zrle_test.go
74 lines (61 loc) · 1.33 KB
/
zrle_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
package zrle
import (
"bytes"
"compress/zlib"
"encoding/binary"
"io"
"reflect"
"testing"
)
func TestReadsLength(t *testing.T) {
bs := make([]byte, 4)
binary.BigEndian.PutUint32(bs, 123)
buf := &bytes.Buffer{}
buf.Write(bs)
length, _ := getLength(buf)
if length != 123 {
t.Errorf("expected 123, got: %v", length)
}
}
func TestDecodesZlib(t *testing.T) {
bs := []byte("testtesttest")
buf := &bytes.Buffer{}
w := zlib.NewWriter(buf)
n, err := w.Write(bs)
if err != nil {
t.Error(err)
}
w.Flush()
encoded_length := uint32(n)
decoded, err := decode(buf, encoded_length)
// no error should be raised
if err != nil {
t.Error(err)
}
// buf should now be empty
if buf.Len() != 0 {
t.Errorf("buffer should be empty but has length: %d", buf.Len())
}
// decoded should be original
if !bytes.Equal(decoded, bs) {
t.Errorf("expected %v, got %v", bs, decoded)
}
}
func TestZRLERead(t *testing.T) {
enc := ZRLEEncoding{
getLength: func(buf io.Reader) (uint32, error) { return 1, nil },
decode: func(buf io.Reader, length uint32) ([]byte, error) {
p := make([]byte, length)
_, err := buf.Read(p)
if err != nil {
return p, err
}
return p, nil
},
}
buf := bytes.NewBuffer([]byte{1})
enc.Read(buf)
if !reflect.DeepEqual(enc.data, []byte{1}) {
t.Errorf("expected %v, got %v", []byte{1}, enc.data)
}
}