-
Notifications
You must be signed in to change notification settings - Fork 4
/
encode_test.go
184 lines (164 loc) · 3.98 KB
/
encode_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package ubjson
import (
"fmt"
"reflect"
"testing"
"unicode/utf8"
)
func TestMarshal(t *testing.T) {
t.Parallel()
for name, testCase := range cases {
t.Run(name, testCase.marshal)
}
}
func (tc *testCase) marshal(t *testing.T) {
if b, err := Marshal(tc.value); err != nil {
t.Fatal("failed to marshal:", err.Error())
} else if diff := firstBytesDiff(tc.binary, b); diff.index != -1 {
t.Errorf("(%T) %v:\n %s\n expected:\n %#v\n\n but got:\n %#v\n\n",
tc.value, tc.value, diff, tc.binary, b)
}
}
func TestMarshalBlock(t *testing.T) {
t.Parallel()
for name, testCase := range cases {
t.Run(name, testCase.marshalBlock)
}
}
func (tc *testCase) marshalBlock(t *testing.T) {
if b, err := MarshalBlock(tc.value); err != nil {
t.Fatal("failed to marshal block:", err.Error())
} else if diff := firstStringDiff(tc.block, string(b)); diff != noStringDiff {
t.Errorf("(%T) %v:\n %s\n expected:\n %q\n\n but got:\n %q\n\n",
tc.value, tc.value, diff, tc.block, string(b))
}
}
var noBytesDiff = bytesDiff{index: -1}
type bytesDiff struct {
index int
lens bool
b1 byte
b1Pre, b1Suf []byte
b2 byte
b2Pre, b2Suf []byte
}
func (d bytesDiff) String() string {
if d.index == -1 {
return "no difference"
} else if d.lens {
return "different lengths"
}
return fmt.Sprintf(bytesDiffF, d.index, d.b1Pre, d.b1, d.b1Suf, d.b2Pre, d.b2, d.b2Suf)
}
var bytesDiffF = "first diff at index (%d);\n" +
"%x[%x]%x\n" +
"%x[%x]%x\n"
const diffPreSufLen = 8
func firstBytesDiff(b1, b2 []byte) bytesDiff {
var diff bytesDiff
var has1, has2 bool
for {
if has1 = diff.index < len(b1); has1 {
diff.b1 = b1[diff.index]
if diff.index < diffPreSufLen {
diff.b1Pre = b1[:diff.index]
} else {
diff.b1Pre = b1[diff.index-diffPreSufLen : diff.index]
}
if diff.index+1+diffPreSufLen < len(b1) {
diff.b1Suf = b1[diff.index+1 : diff.index+1+diffPreSufLen]
} else {
diff.b1Suf = b1[diff.index+1:]
}
}
if has2 = diff.index < len(b2); has2 {
diff.b2 = b2[diff.index]
if diff.index < diffPreSufLen {
diff.b2Pre = b2[:diff.index]
} else {
diff.b2Pre = b2[diff.index-diffPreSufLen : diff.index]
}
if diff.index+1+diffPreSufLen < len(b2) {
diff.b2Suf = b2[diff.index+1 : diff.index+1+diffPreSufLen]
} else {
diff.b2Suf = b2[diff.index+1:]
}
}
if !has1 && !has2 {
return noBytesDiff
} else if !has1 || !has2 {
diff.lens = true
return diff
}
// has1 && has2 == true
if diff.b1 != diff.b2 {
return diff
}
diff.index++
}
}
var noStringDiff = stringDiff{index: -1}
type stringDiff struct {
index int
rune int
lens bool
r1 rune
r2 rune
}
func (d stringDiff) String() string {
if d.index == -1 {
return "no difference"
} else if d.lens {
return "different lengths"
}
return fmt.Sprintf("first diff at index (%d) rune (%d); %q vs. %q", d.index, d.rune, d.r1, d.r2)
}
func firstStringDiff(s1, s2 string) stringDiff {
var diff stringDiff
var has1, has2 bool
var l int
for {
if has1 = diff.index < len(s1); has1 {
diff.r1, l = utf8.DecodeRuneInString(s1[diff.index:])
}
if has2 = diff.index < len(s2); has2 {
diff.r2, _ = utf8.DecodeRuneInString(s2[diff.index:])
}
if !has1 && !has2 {
return noStringDiff
} else if !has1 || !has2 {
diff.lens = true
return diff
}
// has1 && has2 == true
if diff.r1 != diff.r2 {
return diff
}
diff.index += l
diff.rune++
}
}
func Test_elementMarkerFor(t *testing.T) {
for _, tt := range []struct {
v interface{}
m Marker
}{
{uint8(1), UInt8Marker},
{int8(2), Int8Marker},
{int16(3), Int16Marker},
{int32(4), Int32Marker},
{int64(5), Int64Marker},
{float32(1.1), Float32Marker},
{float64(2.2), Float64Marker},
{HighPrecNumber(""), HighPrecNumMarker},
{Char('a'), CharMarker},
{"", StringMarker},
} {
tt := tt
t.Run(tt.m.String(), func(t *testing.T) {
if got := elementMarkerFor(reflect.TypeOf(tt.v)); got != tt.m {
t.Errorf("expected %s for %v but got %s", tt.m, tt.v, got)
}
})
}
}