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

Function TestI32EncodeDecode

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

Source from the content-addressed store, hash-verified

1070}
1071
1072func TestI32EncodeDecode(t *testing.T) {
1073
1074 indexes := []int32{1, 5, 9, 203}
1075 elts := []int32{12, 15, 19, 120}
1076
1077 cases := []struct {
1078 n int
1079 want []byte
1080 }{
1081 {
1082 0,
1083 []byte{},
1084 },
1085 {
1086 1,
1087 []byte{8, 1, 18, 1, 2, 26, 1, 0, 34},
1088 },
1089 {
1090 2,
1091 []byte{8, 2, 18, 1, 34, 26, 1, 0, 34},
1092 },
1093 }
1094
1095 for i, c := range cases {
1096
1097 a, err := array.NewI32(indexes[:c.n], elts[:c.n])
1098 if err != nil {
1099 t.Errorf("expect no error but: %s", err)
1100 }
1101
1102 rst, err := proto.Marshal(a)
1103 if err != nil {
1104 t.Errorf("expect no error but: %s", err)
1105 }
1106
1107 // build Elts part for template generated test codes
1108 var want []byte = c.want
1109 if c.n > 0 {
1110 want = append(c.want, byte(c.n*4))
1111 for i := 0; i < c.n; i++ {
1112 b := make([]byte, 4)
1113 binary.LittleEndian.PutUint32(b, uint32(elts[i]))
1114 want = append(want, b...)
1115 }
1116 }
1117
1118 if !reflect.DeepEqual(rst, want) {
1119 t.Fatalf("%d-th: n: %v; want: %v; actual: %v",
1120 i+1, c.n, want, rst)
1121 }
1122
1123 // Decode
1124
1125 b := &array.I32{}
1126 err = proto.Unmarshal(rst, b)
1127
1128 if err != nil {
1129 t.Errorf("expect no error but: %s", err)

Callers

nothing calls this directly

Calls 3

NewI32Function · 0.92
MarshalMethod · 0.80
UnmarshalMethod · 0.80

Tested by

no test coverage detected