-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_build_iter_test.go
260 lines (227 loc) · 9.23 KB
/
custom_build_iter_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2019 Karl Stenerud
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
package concise_encoding
import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
"strings"
"testing"
"github.com/kstenerud/go-concise-encoding/ce"
"github.com/kstenerud/go-concise-encoding/configuration"
"github.com/kstenerud/go-describe"
"github.com/kstenerud/go-equivalence"
)
// Demonstration of the Concise Encoding "custom" data type.
// See https://github.com/kstenerud/concise-encoding/blob/master/cbe-specification.md#custom
// See https://github.com/kstenerud/concise-encoding/blob/master/cte-specification.md#custom
// ============================================================================
// Implementation Code
// ============================================================================
// Assume the following custom serialized formats for complex64 and 128:
//
// | Offset | Size | Description |
// | ------ | ---- | ------------------------------------------------------- |
// | 0 | 4 | Real portion (float32, little endian) |
// | 4 | 4 | Imaginary portion (float32, little endian) |
//
// | Offset | Size | Description |
// | ------ | ---- | ------------------------------------------------------- |
// | 0 | 8 | Real portion (float64, little endian) |
// | 8 | 8 | Imaginary portion (float64, little endian) |
// We'll assign the following custom type codes:
const (
typeCodeComplex64 = 0
typeCodeComplex128 = 1
)
// First piece: functions to convert from complex type to custom bytes.
// These functions each handle a single type only.
func convertComplex64ToCustomBinary(rv reflect.Value) (customType uint64, asBytes []byte, err error) {
cplx := complex64(rv.Complex())
buff := bytes.Buffer{}
if err = binary.Write(&buff, binary.LittleEndian, real(cplx)); err != nil {
return
}
if err = binary.Write(&buff, binary.LittleEndian, imag(cplx)); err != nil {
return
}
customType = typeCodeComplex64
asBytes = buff.Bytes()
return
}
func convertComplex128ToCustomBinary(rv reflect.Value) (customType uint64, asBytes []byte, err error) {
cplx := rv.Complex()
buff := bytes.Buffer{}
if err = binary.Write(&buff, binary.LittleEndian, real(cplx)); err != nil {
return
}
if err = binary.Write(&buff, binary.LittleEndian, imag(cplx)); err != nil {
return
}
customType = typeCodeComplex128
asBytes = buff.Bytes()
return
}
// Second piece: converter function to fill in an object from custom data.
// This same function will be used for ALL custom types.
func convertFromCustomBinary(customType uint64, src []byte, dst reflect.Value) error {
buff := bytes.NewBuffer(src)
switch customType {
case typeCodeComplex64:
var realPart float32
var imagPart float32
if err := binary.Read(buff, binary.LittleEndian, &realPart); err != nil {
return err
}
if err := binary.Read(buff, binary.LittleEndian, &imagPart); err != nil {
return err
}
dst.SetComplex(complex128(complex(realPart, imagPart)))
return nil
case typeCodeComplex128:
var realPart float64
var imagPart float64
if err := binary.Read(buff, binary.LittleEndian, &realPart); err != nil {
return err
}
if err := binary.Read(buff, binary.LittleEndian, &imagPart); err != nil {
return err
}
dst.SetComplex(complex(realPart, imagPart))
return nil
default:
return fmt.Errorf("unknown custom type [0x%02x]", customType)
}
}
// ============================================================================
// Test Code
// ============================================================================
func assertCBEMarshalUnmarshalComplexFromBinary(t *testing.T, value interface{}) {
marshalConfig := configuration.New()
marshalConfig.Iterator.CustomBinaryConverters[reflect.TypeOf(complex(float32(0), float32(0)))] = convertComplex64ToCustomBinary
marshalConfig.Iterator.CustomBinaryConverters[reflect.TypeOf(complex(float64(0), float64(0)))] = convertComplex128ToCustomBinary
marshaler := ce.NewCBEMarshaler(marshalConfig)
document, err := marshaler.MarshalToDocument(value)
if err != nil {
t.Error(err)
return
}
template := value
unmarshalConfig := configuration.New()
unmarshalConfig.Builder.CustomBinaryBuildFunction = convertFromCustomBinary
unmarshalConfig.Builder.CustomBuiltTypes = append(unmarshalConfig.Builder.CustomBuiltTypes, reflect.TypeOf(value))
unmarshaler := ce.NewCBEUnmarshaler(unmarshalConfig)
actual, err := unmarshaler.UnmarshalFromDocument(document, template)
if err != nil {
t.Error(err)
return
}
if !equivalence.IsEquivalent(actual, value) {
t.Errorf("Expected %v but got %v", describe.D(value), describe.D(actual))
}
}
func assertCTEMarshalUnmarshalComplexFromBinary(t *testing.T, value interface{}) {
marshalConfig := configuration.New()
marshalConfig.Iterator.CustomBinaryConverters[reflect.TypeOf(complex(float32(0), float32(0)))] = convertComplex64ToCustomBinary
marshalConfig.Iterator.CustomBinaryConverters[reflect.TypeOf(complex(float64(0), float64(0)))] = convertComplex128ToCustomBinary
marshaler := ce.NewCTEMarshaler(marshalConfig)
document, err := marshaler.MarshalToDocument(value)
if err != nil {
t.Error(err)
return
}
template := value
unmarshalConfig := configuration.New()
unmarshalConfig.Builder.CustomBinaryBuildFunction = convertFromCustomBinary
unmarshalConfig.Builder.CustomBuiltTypes = append(unmarshalConfig.Builder.CustomBuiltTypes, reflect.TypeOf(value))
unmarshaler := ce.NewCTEUnmarshaler(unmarshalConfig)
actual, err := unmarshaler.UnmarshalFromDocument(document, template)
if err != nil {
t.Error(err)
return
}
if !equivalence.IsEquivalent(actual, value) {
t.Errorf("Expected %v but got %v", describe.D(value), describe.D(actual))
}
}
func assertMarshalUnmarshalComplexFromBinary(t *testing.T, value interface{}) {
assertCBEMarshalUnmarshalComplexFromBinary(t, value)
assertCTEMarshalUnmarshalComplexFromBinary(t, value)
}
// ============================================================================
func convertComplexToCustomText(rv reflect.Value) (customType uint64, asString []byte, err error) {
cplx := rv.Complex()
switch rv.Kind() {
case reflect.Complex64:
customType = typeCodeComplex64
case reflect.Complex128:
customType = typeCodeComplex128
}
builder := strings.Builder{}
builder.WriteString(fmt.Sprintf("%g+%gi", real(cplx), imag(cplx)))
asString = []byte(builder.String())
return
}
func convertFromCustomText(customType uint64, src string, dst reflect.Value) error {
var r, i float64
if _, err := fmt.Sscanf(src, "%f+%fi", &r, &i); err != nil {
return err
}
dst.SetComplex(complex(r, i))
return nil
}
func assertCTEMarshalUnmarshalComplexFromText(t *testing.T, value interface{}) {
marshalConfig := configuration.New()
marshalConfig.Iterator.CustomTextConverters[reflect.TypeOf(complex(float32(0), float32(0)))] = convertComplexToCustomText
marshalConfig.Iterator.CustomTextConverters[reflect.TypeOf(complex(float64(0), float64(0)))] = convertComplexToCustomText
marshaler := ce.NewCTEMarshaler(marshalConfig)
document, err := marshaler.MarshalToDocument(value)
if err != nil {
t.Error(err)
return
}
template := value
unmarshalConfig := configuration.New()
unmarshalConfig.Builder.CustomTextBuildFunction = convertFromCustomText
unmarshalConfig.Builder.CustomBuiltTypes = append(unmarshalConfig.Builder.CustomBuiltTypes, reflect.TypeOf(value))
unmarshaler := ce.NewCTEUnmarshaler(unmarshalConfig)
actual, err := unmarshaler.UnmarshalFromDocument(document, template)
if err != nil {
t.Error(err)
return
}
if !equivalence.IsEquivalent(actual, value) {
t.Errorf("Expected %v but got %v", describe.D(value), describe.D(actual))
}
}
func assertMarshalUnmarshalComplexFromText(t *testing.T, value interface{}) {
assertCTEMarshalUnmarshalComplexFromText(t, value)
}
func assertMarshalUnmarshalComplex(t *testing.T, value interface{}) {
assertMarshalUnmarshalComplexFromBinary(t, value)
assertMarshalUnmarshalComplexFromText(t, value)
}
// ============================================================================
// Tests
func TestCustomBuildIter(t *testing.T) {
assertMarshalUnmarshalComplex(t, complex(1, 1))
assertMarshalUnmarshalComplex(t, complex(float64(1.0000000000000000000000000001), float64(1)))
}