-
Notifications
You must be signed in to change notification settings - Fork 5
/
zf_delay_test.go
173 lines (156 loc) · 4.52 KB
/
zf_delay_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
package zeroformatter_test
import (
"testing"
"time"
"reflect"
"github.com/shamaton/zeroformatter"
"github.com/shamaton/zeroformatter/char"
"github.com/shamaton/zeroformatter/datetimeoffset"
)
func TestDelayDeserialize(t *testing.T) {
type child3 struct {
Int int
}
type child2 struct {
Int2Uint map[int]uint
Float2Bool map[float32]bool
Char2String map[char.Char]string
Time2TimeOffset map[time.Time]datetimeoffset.DateTimeOffset
Duration2Struct map[time.Duration]child3
}
type child struct {
IntArray []int
UintArray []uint
FloatArray []float32
BoolArray []bool
CharArray []char.Char
StringArray []string
TimeArray []time.Time
TimeOffsetArray []datetimeoffset.DateTimeOffset
DurationArray []time.Duration
Child child2
}
type st struct {
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint8 byte
Uint16 uint16
Uint32 uint32
Uint64 uint64
Float float32
Double float64
Bool bool
Char char.Char
String string
Time time.Time
Duration time.Duration
TimeOffset datetimeoffset.DateTimeOffset
Child child
}
vSt := &st{
Int32: -32,
Int8: -8,
Int16: -16,
Int64: -64,
Uint32: 32,
Uint8: 8,
Uint16: 16,
Uint64: 64,
Float: 1.23,
Double: 2.3456,
Bool: true,
Char: char.Char('A'),
String: "Parent",
Time: now,
Duration: time.Duration(123 * time.Second),
TimeOffset: datetimeoffset.Now(),
// child
Child: child{
IntArray: []int{-1, -2, -3, -4, -5},
UintArray: []uint{1, 2, 3, 4, 5},
FloatArray: []float32{-1.2, -3.4, -5.6, -7.8},
BoolArray: []bool{true, true, false, false, true},
CharArray: []char.Char{char.Char('X'), char.Char('Y'), char.Char('Z')},
StringArray: []string{"str", "ing", "arr", "ay"},
TimeArray: []time.Time{now, now, now},
TimeOffsetArray: []datetimeoffset.DateTimeOffset{datetimeoffset.Now(), datetimeoffset.Now(), datetimeoffset.Now()},
DurationArray: []time.Duration{time.Duration(1 * time.Nanosecond), time.Duration(2 * time.Nanosecond)},
// childchild
Child: child2{
Int2Uint: map[int]uint{-1: 2, -3: 4},
Float2Bool: map[float32]bool{-1.1: true, -2.2: false},
Char2String: map[char.Char]string{char.Char('A'): "AA", char.Char('B'): "BB"},
Time2TimeOffset: map[time.Time]datetimeoffset.DateTimeOffset{now: datetimeoffset.Now()},
Duration2Struct: map[time.Duration]child3{time.Duration(1 * time.Hour): child3{Int: 1}, time.Duration(2 * time.Hour): child3{Int: 2}},
},
},
}
b, err := zeroformatter.Serialize(vSt)
if err != nil {
t.Error(err)
}
eHolder := &st{}
dds, err := zeroformatter.DelayDeserialize(eHolder, b)
if err != nil {
t.Error(err)
}
// simple
if err := dds.DeserializeByElement(&eHolder.Int8); err != nil {
t.Error(err)
}
if err := dds.DeserializeByElement(&eHolder.Uint8); err != nil {
t.Error(err)
}
if err := dds.DeserializeByElement(&eHolder.Float); err != nil {
t.Error(err)
}
if err := dds.DeserializeByElement(&eHolder.Char); err != nil {
t.Error(err)
}
if err := dds.DeserializeByElement(&eHolder.Time); err != nil {
t.Error(err)
}
if _bool, err := dds.IsDeserialized(&eHolder.Int16); err != nil || _bool {
t.Error("deserialized error")
}
// multiple
if err := dds.DeserializeByElement(&eHolder.Int16, &eHolder.Int32, &eHolder.Int64); err != nil {
t.Error(err)
}
if err := dds.DeserializeByElement(&eHolder.Uint16, &eHolder.Uint32, &eHolder.Uint64); err != nil {
t.Error(err)
}
if err := dds.DeserializeByElement(&eHolder.Double, &eHolder.String, &eHolder.Bool); err != nil {
t.Error(err)
}
if err := dds.DeserializeByElement(&eHolder.Duration, &eHolder.TimeOffset, &eHolder.Child); err != nil {
t.Error(err)
}
if _bool, err := dds.IsDeserialized(&eHolder.Int16); err != nil || !_bool {
t.Error("deserialized error")
}
// value equal ?
if !reflect.DeepEqual(vSt, eHolder) {
t.Error("value not equal!!")
}
iHolder := &st{}
dds, err = zeroformatter.DelayDeserialize(iHolder, b)
if err != nil {
t.Error(err)
}
if err := dds.DeserializeByIndex(0); err != nil {
t.Error(err)
}
if err := dds.DeserializeByIndex(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); err != nil {
t.Error(err)
}
if err := dds.DeserializeByIndex(17); err == nil {
t.Error("index error")
}
// value equal ?
if !reflect.DeepEqual(vSt, iHolder) {
t.Error("value not equal!!")
}
}