github.com/batchcorp/thrift-iterator@v0.0.0-20220918180557-4c4a158fc6e9/test/level_1/list_test.go (about) 1 package test 2 3 import ( 4 "github.com/apache/thrift/lib/go/thrift" 5 "github.com/batchcorp/thrift-iterator/general" 6 "github.com/batchcorp/thrift-iterator/protocol" 7 "github.com/batchcorp/thrift-iterator/raw" 8 "github.com/batchcorp/thrift-iterator/test" 9 "github.com/stretchr/testify/require" 10 "testing" 11 ) 12 13 func Test_decode_list_by_iterator(t *testing.T) { 14 should := require.New(t) 15 for _, c := range test.Combinations { 16 buf, proto := c.CreateProtocol() 17 proto.WriteListBegin(thrift.I64, 3) 18 proto.WriteI64(1) 19 proto.WriteI64(2) 20 proto.WriteI64(3) 21 proto.WriteListEnd() 22 iter := c.CreateIterator(buf.Bytes()) 23 elemType, length := iter.ReadListHeader() 24 should.Equal(protocol.TypeI64, elemType) 25 should.Equal(3, length) 26 should.Equal(uint64(1), iter.ReadUint64()) 27 should.Equal(uint64(2), iter.ReadUint64()) 28 should.Equal(uint64(3), iter.ReadUint64()) 29 } 30 } 31 32 func Test_encode_list_by_stream(t *testing.T) { 33 should := require.New(t) 34 for _, c := range test.Combinations { 35 stream := c.CreateStream() 36 stream.WriteListHeader(protocol.TypeI64, 3) 37 stream.WriteUint64(1) 38 stream.WriteUint64(2) 39 stream.WriteUint64(3) 40 iter := c.CreateIterator(stream.Buffer()) 41 elemType, length := iter.ReadListHeader() 42 should.Equal(protocol.TypeI64, elemType) 43 should.Equal(3, length) 44 should.Equal(uint64(1), iter.ReadUint64()) 45 should.Equal(uint64(2), iter.ReadUint64()) 46 should.Equal(uint64(3), iter.ReadUint64()) 47 } 48 } 49 50 func Test_skip_list(t *testing.T) { 51 should := require.New(t) 52 for _, c := range test.Combinations { 53 buf, proto := c.CreateProtocol() 54 proto.WriteListBegin(thrift.I64, 3) 55 proto.WriteI64(1) 56 proto.WriteI64(2) 57 proto.WriteI64(3) 58 proto.WriteListEnd() 59 iter := c.CreateIterator(buf.Bytes()) 60 should.Equal(buf.Bytes(), iter.SkipList(nil)) 61 } 62 } 63 64 func Test_unmarshal_general_list(t *testing.T) { 65 should := require.New(t) 66 for _, c := range test.Combinations { 67 buf, proto := c.CreateProtocol() 68 proto.WriteListBegin(thrift.I64, 3) 69 proto.WriteI64(1) 70 proto.WriteI64(2) 71 proto.WriteI64(3) 72 proto.WriteListEnd() 73 var val general.List 74 should.NoError(c.Unmarshal(buf.Bytes(), &val)) 75 should.Equal(general.List{int64(1), int64(2), int64(3)}, val) 76 } 77 } 78 79 func Test_unmarshal_raw_list(t *testing.T) { 80 should := require.New(t) 81 for _, c := range test.Combinations { 82 buf, proto := c.CreateProtocol() 83 proto.WriteListBegin(thrift.I64, 3) 84 proto.WriteI64(1) 85 proto.WriteI64(2) 86 proto.WriteI64(3) 87 proto.WriteListEnd() 88 var val raw.List 89 should.NoError(c.Unmarshal(buf.Bytes(), &val)) 90 should.Equal(3, len(val.Elements)) 91 should.Equal(protocol.TypeI64, val.ElementType) 92 iter := c.CreateIterator(val.Elements[0]) 93 should.Equal(int64(1), iter.ReadInt64()) 94 } 95 } 96 97 func Test_unmarshal_list(t *testing.T) { 98 should := require.New(t) 99 for _, c := range test.UnmarshalCombinations { 100 buf, proto := c.CreateProtocol() 101 proto.WriteListBegin(thrift.I64, 3) 102 proto.WriteI64(1) 103 proto.WriteI64(2) 104 proto.WriteI64(3) 105 proto.WriteListEnd() 106 var val []int64 107 should.NoError(c.Unmarshal(buf.Bytes(), &val)) 108 should.Equal([]int64{int64(1), int64(2), int64(3)}, val) 109 } 110 } 111 112 func Test_marshal_general_list(t *testing.T) { 113 should := require.New(t) 114 for _, c := range test.Combinations { 115 output, err := c.Marshal(general.List{ 116 int64(1), int64(2), int64(3), 117 }) 118 should.NoError(err) 119 iter := c.CreateIterator(output) 120 elemType, length := iter.ReadListHeader() 121 should.Equal(protocol.TypeI64, elemType) 122 should.Equal(3, length) 123 should.Equal(uint64(1), iter.ReadUint64()) 124 should.Equal(uint64(2), iter.ReadUint64()) 125 should.Equal(uint64(3), iter.ReadUint64()) 126 } 127 } 128 129 func Test_marshal_raw_list(t *testing.T) { 130 should := require.New(t) 131 for _, c := range test.Combinations { 132 buf, proto := c.CreateProtocol() 133 proto.WriteListBegin(thrift.I64, 3) 134 proto.WriteI64(1) 135 proto.WriteI64(2) 136 proto.WriteI64(3) 137 proto.WriteListEnd() 138 var val raw.List 139 should.NoError(c.Unmarshal(buf.Bytes(), &val)) 140 output, err := c.Marshal(val) 141 should.NoError(err) 142 var generalVal general.List 143 should.NoError(c.Unmarshal(output, &generalVal)) 144 should.Equal(general.List{int64(1), int64(2), int64(3)}, generalVal) 145 } 146 } 147 148 func Test_marshal_list(t *testing.T) { 149 should := require.New(t) 150 for _, c := range test.MarshalCombinations { 151 output, err := c.Marshal([]int64{1, 2, 3}) 152 should.NoError(err) 153 iter := c.CreateIterator(output) 154 elemType, length := iter.ReadListHeader() 155 should.Equal(protocol.TypeI64, elemType) 156 should.Equal(3, length) 157 should.Equal(uint64(1), iter.ReadUint64()) 158 should.Equal(uint64(2), iter.ReadUint64()) 159 should.Equal(uint64(3), iter.ReadUint64()) 160 } 161 } 162 163 func Test_marshal_empty_list(t *testing.T) { 164 should := require.New(t) 165 for _, c := range test.MarshalCombinations { 166 output, err := c.Marshal([]int64{}) 167 should.NoError(err) 168 iter := c.CreateIterator(output) 169 elemType, length := iter.ReadListHeader() 170 should.Equal(protocol.TypeI64, elemType) 171 should.Equal(0, length) 172 } 173 }