-
Notifications
You must be signed in to change notification settings - Fork 46
/
runlengthbytewriter_test.go
86 lines (82 loc) · 1.67 KB
/
runlengthbytewriter_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
package orc
import (
"bytes"
"math/rand"
"reflect"
"testing"
)
func TestRunLengthByteWriter(t *testing.T) {
testCases := []struct {
input []byte
expect func([]byte)
}{
{
input: []byte{0x44, 0x45},
expect: func(output []byte) {
expected := []byte{0xfe, 0x44, 0x45}
if !reflect.DeepEqual(expected, output) {
t.Errorf("Test failed, got %v expected %v", output, expected)
}
},
},
{
input: []byte{0x01, 0x01, 0x01, 0x01},
expect: func(output []byte) {
expected := []byte{0x01, 0x01}
if !reflect.DeepEqual(expected, output) {
t.Errorf("Test failed, got %v expected %v", output, expected)
}
},
},
{
input: make([]byte, 100),
expect: func(output []byte) {
expected := []byte{0x61, 0x00}
if !reflect.DeepEqual(expected, output) {
t.Errorf("Test failed, got %v expected %v", output, expected)
}
},
},
}
for _, tc := range testCases {
var buf bytes.Buffer
w := NewRunLengthByteWriter(&buf)
for i := range tc.input {
err := w.WriteByte(tc.input[i])
if err != nil {
t.Fatal(err)
}
}
err := w.Close()
if err != nil {
t.Fatal(err)
}
tc.expect(buf.Bytes())
}
}
func TestWriteReadBytes(t *testing.T) {
var buf bytes.Buffer
w := NewRunLengthByteWriter(&buf)
var input []byte
for i := 0; i < 10000; i++ {
b := uint8(rand.Intn(2))
input = append(input, b)
err := w.WriteByte(b)
if err != nil {
t.Fatal(err)
}
}
err := w.Close()
if err != nil {
t.Fatal(err)
}
r := NewRunLengthByteReader(&buf)
var index int
for r.Next() {
b := r.Byte()
if input[index] != b {
t.Errorf("Test failed, %v does not equal %v at index %v", b, input[index], index)
}
index++
}
}