MCPcopy
hub / github.com/openacid/slim / TestU16EncodeDecode

Function TestU16EncodeDecode

array/int_test.go:128–207  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

126}
127
128func TestU16EncodeDecode(t *testing.T) {
129
130 indexes := []int32{1, 5, 9, 203}
131 elts := []uint16{12, 15, 19, 120}
132
133 cases := []struct {
134 n int
135 want []byte
136 }{
137 {
138 0,
139 []byte{},
140 },
141 {
142 1,
143 []byte{8, 1, 18, 1, 2, 26, 1, 0, 34},
144 },
145 {
146 2,
147 []byte{8, 2, 18, 1, 34, 26, 1, 0, 34},
148 },
149 }
150
151 for i, c := range cases {
152
153 a, err := array.NewU16(indexes[:c.n], elts[:c.n])
154 if err != nil {
155 t.Errorf("expect no error but: %s", err)
156 }
157
158 rst, err := proto.Marshal(a)
159 if err != nil {
160 t.Errorf("expect no error but: %s", err)
161 }
162
163 // build Elts part for template generated test codes
164 var want []byte = c.want
165 if c.n > 0 {
166 want = append(c.want, byte(c.n*2))
167 for i := 0; i < c.n; i++ {
168 b := make([]byte, 2)
169 binary.LittleEndian.PutUint16(b, elts[i])
170 want = append(want, b...)
171 }
172 }
173
174 if !reflect.DeepEqual(rst, want) {
175 t.Fatalf("%d-th: n: %v; want: %v; actual: %v",
176 i+1, c.n, want, rst)
177 }
178
179 // Decode
180
181 b := &array.U16{}
182 err = proto.Unmarshal(rst, b)
183
184 if err != nil {
185 t.Errorf("expect no error but: %s", err)

Callers

nothing calls this directly

Calls 3

NewU16Function · 0.92
MarshalMethod · 0.80
UnmarshalMethod · 0.80

Tested by

no test coverage detected