limage - xcf/rle_test.go
1 package xcf
2
3 import (
4 "bytes"
5 "io"
6 "strings"
7 "testing"
8
9 "vimagination.zapto.org/byteio"
10 "vimagination.zapto.org/memio"
11 )
12
13 func TestReads(t *testing.T) {
14 tests := []struct {
15 Input, Output string
16 }{
17 {},
18 {
19 "\x00A",
20 "A",
21 },
22 {
23 "\x01A",
24 "AA",
25 },
26 {
27 "\x7eA",
28 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
29 },
30 {
31 "\x7f\x00\x00A",
32 "",
33 },
34 {
35 "\x7f\x00\x01A",
36 "A",
37 },
38 {
39 "\x7f\x00\x0aA",
40 "AAAAAAAAAA",
41 },
42 {
43 "\x7f\x01\x00A",
44 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
45 },
46 {
47 "\x80\x00\x01A",
48 "A",
49 },
50 {
51 "\x80\x00\x02AA",
52 "AA",
53 },
54 {
55 "\xffA",
56 "A",
57 },
58 {
59 "\xfeAB",
60 "AB",
61 },
62 {
63 "\x00A\x01B\x7f\x00\x01C\x7f\x00\x0aD\x80\x00\x0a1234567890",
64 "ABBCDDDDDDDDDD1234567890",
65 },
66 }
67 for n, test := range tests {
68 var buf bytes.Buffer
69 _, err := io.Copy(&buf, &rle{
70 Reader: &byteio.StickyBigEndianReader{
71 Reader: strings.NewReader(test.Input),
72 },
73 })
74 if err != nil {
75 t.Errorf("test %d: unexpected error: %q", n+1, err)
76 } else if str := buf.String(); str != test.Output {
77 t.Errorf("test %d: expecting %q, got %q", n+1, test.Output, str)
78 }
79 }
80 }
81
82 func TestWrites(t *testing.T) {
83 tests := []struct {
84 Input, Output string
85 }{
86 {},
87 {
88 "A",
89 "\x00A",
90 },
91 {
92 "AA",
93 "\x01A",
94 },
95 {
96 "AAA",
97 "\x02A",
98 },
99 {
100 "AB",
101 "\xfeAB",
102 },
103 {
104 "AAAAABBCCCCCFFGGHHIIII",
105 "\x04A\x01B\x04C\xfaFFGGHH\x03I",
106 },
107 {
108 "ABVGGFHSDFGHDFHGFHDGZDBGNHJSDGSDFGHHTGHDDVFVDBFBDBVF",
109 "\xccABVGGFHSDFGHDFHGFHDGZDBGNHJSDGSDFGHHTGHDDVFVDBFBDBVF",
110 },
111 }
112 d := make([]byte, 0, 2048)
113 for n, test := range tests {
114 w := newWriter(memio.Create(&d))
115 w.WriteRLE([]byte(test.Input))
116 if w.Err != nil {
117 t.Errorf("test %d: unexpected error: %q", n+1, w.Err)
118 } else if str := string(d); str != test.Output {
119 t.Errorf("test %d: expecting %q, got %q", n+1, test.Output, str)
120 }
121 }
122 }
123