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

Function TestU32EncodeDecode

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

Source from the content-addressed store, hash-verified

362}
363
364func TestU32EncodeDecode(t *testing.T) {
365
366 indexes := []int32{1, 5, 9, 203}
367 elts := []uint32{12, 15, 19, 120}
368
369 cases := []struct {
370 n int
371 want []byte
372 }{
373 {
374 0,
375 []byte{},
376 },
377 {
378 1,
379 []byte{8, 1, 18, 1, 2, 26, 1, 0, 34},
380 },
381 {
382 2,
383 []byte{8, 2, 18, 1, 34, 26, 1, 0, 34},
384 },
385 }
386
387 for i, c := range cases {
388
389 a, err := array.NewU32(indexes[:c.n], elts[:c.n])
390 if err != nil {
391 t.Errorf("expect no error but: %s", err)
392 }
393
394 rst, err := proto.Marshal(a)
395 if err != nil {
396 t.Errorf("expect no error but: %s", err)
397 }
398
399 // build Elts part for template generated test codes
400 var want []byte = c.want
401 if c.n > 0 {
402 want = append(c.want, byte(c.n*4))
403 for i := 0; i < c.n; i++ {
404 b := make([]byte, 4)
405 binary.LittleEndian.PutUint32(b, elts[i])
406 want = append(want, b...)
407 }
408 }
409
410 if !reflect.DeepEqual(rst, want) {
411 t.Fatalf("%d-th: n: %v; want: %v; actual: %v",
412 i+1, c.n, want, rst)
413 }
414
415 // Decode
416
417 b := &array.U32{}
418 err = proto.Unmarshal(rst, b)
419
420 if err != nil {
421 t.Errorf("expect no error but: %s", err)

Callers

nothing calls this directly

Calls 3

NewU32Function · 0.92
MarshalMethod · 0.80
UnmarshalMethod · 0.80

Tested by

no test coverage detected