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

Function TestI16EncodeDecode

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

Source from the content-addressed store, hash-verified

834}
835
836func TestI16EncodeDecode(t *testing.T) {
837
838 indexes := []int32{1, 5, 9, 203}
839 elts := []int16{12, 15, 19, 120}
840
841 cases := []struct {
842 n int
843 want []byte
844 }{
845 {
846 0,
847 []byte{},
848 },
849 {
850 1,
851 []byte{8, 1, 18, 1, 2, 26, 1, 0, 34},
852 },
853 {
854 2,
855 []byte{8, 2, 18, 1, 34, 26, 1, 0, 34},
856 },
857 }
858
859 for i, c := range cases {
860
861 a, err := array.NewI16(indexes[:c.n], elts[:c.n])
862 if err != nil {
863 t.Errorf("expect no error but: %s", err)
864 }
865
866 rst, err := proto.Marshal(a)
867 if err != nil {
868 t.Errorf("expect no error but: %s", err)
869 }
870
871 // build Elts part for template generated test codes
872 var want []byte = c.want
873 if c.n > 0 {
874 want = append(c.want, byte(c.n*2))
875 for i := 0; i < c.n; i++ {
876 b := make([]byte, 2)
877 binary.LittleEndian.PutUint16(b, uint16(elts[i]))
878 want = append(want, b...)
879 }
880 }
881
882 if !reflect.DeepEqual(rst, want) {
883 t.Fatalf("%d-th: n: %v; want: %v; actual: %v",
884 i+1, c.n, want, rst)
885 }
886
887 // Decode
888
889 b := &array.I16{}
890 err = proto.Unmarshal(rst, b)
891
892 if err != nil {
893 t.Errorf("expect no error but: %s", err)

Callers

nothing calls this directly

Calls 3

NewI16Function · 0.92
MarshalMethod · 0.80
UnmarshalMethod · 0.80

Tested by

no test coverage detected