-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_test.go
More file actions
150 lines (142 loc) · 4.18 KB
/
reader_test.go
File metadata and controls
150 lines (142 loc) · 4.18 KB
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
package binary
import (
"testing"
)
func TestReader_Slice(t *testing.T) {
r := newSliceReader([]byte("0123456789"))
out, err := r.Slice(3)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(out) != 3 {
t.Errorf("Expected length 3, got %d", len(out))
}
if string(out) != "012" {
t.Errorf("Expected '012', got '%s'", string(out))
}
if r.Len() != 7 {
t.Errorf("Expected 7, got %d", r.Len())
}
}
// TestReaderEOF commented out since it uses bigStruct which contains maps.
// func TestReaderEOF(t *testing.T) {
// tb := New()
// b, _ := tb.Encode(newBigStruct())
//
// for size := 0; size < len(b)-1; size++ {
// var output bigStruct
// if err := tb.Decode(b[0:size], &output); err == nil {
// t.Error("Expected error, got nil")
// }
// }
// }
// TestStreamReader and bigStruct commented out since it uses maps, which are not supported in Binary.
// func TestStreamReader(t *testing.T) {
// input := newBigStruct()
// tb := New()
// b, _ := tb.Encode(input)
//
// dec := newDecoder(newNetworkSource(b))
// out := new(bigStruct)
// if err := dec.Decode(out); err != nil {
// t.Fatalf("Unexpected error: %v", err)
// }
// }
// --------------------------------------- Big Structure (Every Field Type) ---------------------------------------
// structure with every possible codec type - commented out since it uses maps.
// type bigStruct struct {
// String string
// Uint8 uint8
// Uint16 uint16
// Uint32 uint32
// Uint64 uint64
// Int8 int8
// Int16 int16
// Int32 int32
// Int64 int64
// Float32 float32
// Float64 float64
// Strings []string
// Bytes []byte
// Bools []bool
// Uint8s []uint8
// Uint16s []uint16
// Uint32s []uint32
// Uint64s []uint64
// Int8s []int8
// Int16s []int16
// Int32s []int32
// Int64s []int64
// Float32s []float32
// Float64s []float64
// MapStr map[string]simpleStruct
// MapPtr map[string]*simpleStruct
// MapUint8 map[uint8]uint8
// MapUint16 map[uint16]uint16
// MapUint32 map[uint32]uint32
// MapUint64 map[uint64]uint64
// MapInt8 map[int8]int8
// MapInt16 map[int16]int16
// MapInt32 map[int32]int32
// MapInt64 map[int64]int64
// Time *time.Time
// Nil *time.Time
// Pointer *simpleStruct
// Value simpleStruct
// Array [6]byte
// Byte byte
// Bool bool
// }
// func newBigStruct() *bigStruct {
// timestamp := time.Date(2013, 1, 2, 3, 4, 5, 6, time.UTC)
// child := simpleStruct{
// Name: "Roman",
// Timestamp: timestamp,
// Payload: []byte("hi"),
// Ssid: []uint32{1, 2, 3},
// }
//
// return &bigStruct{
// String: "hello",
// Byte: 0x3a,
// Bool: true,
// Uint8: math.MaxUint8,
// Uint16: math.MaxUint16,
// Uint32: math.MaxUint32,
// Uint64: math.MaxUint64,
// Int8: math.MaxInt8,
// Int16: math.MaxInt16,
// Int32: math.MaxInt32,
// Int64: math.MaxInt64,
// Float32: math.MaxFloat32,
// Float64: math.MaxFloat64,
// Strings: []string{"a", "b", "c"},
// Bytes: []byte("hello-bytes"),
// Bools: []bool{true, false, true},
// Uint8s: []uint8{0, math.MaxUint8},
// Uint16s: []uint16{0, math.MaxUint16},
// Uint32s: []uint32{0, math.MaxUint32},
// Uint64s: []uint64{0, math.MaxUint64},
// Int8s: []int8{math.MinInt8, math.MaxInt8},
// Int16s: []int16{math.MinInt16, math.MaxInt16},
// Int32s: []int32{math.MinInt32, math.MaxInt32},
// Int64s: []int64{math.MinInt64, math.MaxInt64},
// Float32s: []float32{0, math.MaxFloat32},
// Float64s: []float64{0, math.MaxFloat64},
// MapStr: map[string]simpleStruct{"a": child, "b": child},
// MapPtr: map[string]*simpleStruct{"a": &child, "b": nil},
// MapUint8: map[uint8]uint8{1: 1},
// MapUint16: map[uint16]uint16{1: 1},
// MapUint32: map[uint32]uint32{1: 1},
// MapUint64: map[uint64]uint64{1: 1},
// MapInt8: map[int8]int8{1: 1},
// MapInt16: map[int16]int16{1: 1},
// MapInt32: map[int32]int32{1: 1},
// MapInt64: map[int64]int64{1: 1},
// Array: [6]byte{1, 2, 3, 4, 5, 6},
// Time: ×tamp,
// Nil: nil,
// Pointer: &child,
// Value: child,
// }
// }