|
| 1 | +package arrow_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "github.com/tarantool/go-tarantool/v2/arrow" |
| 10 | + "github.com/vmihailenco/msgpack/v5" |
| 11 | +) |
| 12 | + |
| 13 | +var longArrow, _ = hex.DecodeString("ffffffff70000000040000009effffff0400010004000000" + |
| 14 | + "b6ffffff0c00000004000000000000000100000004000000daffffff140000000202" + |
| 15 | + "000004000000f0ffffff4000000001000000610000000600080004000c0010000400" + |
| 16 | + "080009000c000c000c0000000400000008000a000c00040006000800ffffffff8800" + |
| 17 | + "0000040000008affffff0400030010000000080000000000000000000000acffffff" + |
| 18 | + "01000000000000003400000008000000000000000200000000000000000000000000" + |
| 19 | + "00000000000000000000000000000800000000000000000000000100000001000000" + |
| 20 | + "0000000000000000000000000a00140004000c0010000c0014000400060008000c00" + |
| 21 | + "00000000000000000000") |
| 22 | + |
| 23 | +var tests = []struct { |
| 24 | + name string |
| 25 | + arr []byte |
| 26 | + enc []byte |
| 27 | +}{ |
| 28 | + { |
| 29 | + "abc", |
| 30 | + []byte{'a', 'b', 'c'}, |
| 31 | + []byte{0xc7, 0x3, 0x8, 'a', 'b', 'c'}, |
| 32 | + }, |
| 33 | + { |
| 34 | + "empty", |
| 35 | + []byte{}, |
| 36 | + []byte{0xc7, 0x0, 0x8}, |
| 37 | + }, |
| 38 | + { |
| 39 | + "one", |
| 40 | + []byte{1}, |
| 41 | + []byte{0xd4, 0x8, 0x1}, |
| 42 | + }, |
| 43 | + { |
| 44 | + "long", |
| 45 | + longArrow, |
| 46 | + []byte{ |
| 47 | + 0xc8, 0x1, 0x10, 0x8, 0xff, 0xff, 0xff, 0xff, 0x70, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, |
| 48 | + 0x0, 0x9e, 0xff, 0xff, 0xff, 0x4, 0x0, 0x1, 0x0, 0x4, 0x0, 0x0,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0, |
| 60 | + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, |
| 61 | + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x14, 0x0, |
| 62 | + 0x4, 0x0, 0xc, 0x0, 0x10, 0x0, 0xc, 0x0, 0x14, 0x0, 0x4, 0x0, 0x6, 0x0, 0x8, 0x0, 0xc, |
| 63 | + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 64 | + }, |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +func TestEncodeArrow(t *testing.T) { |
| 69 | + for _, tt := range tests { |
| 70 | + t.Run(tt.name, func(t *testing.T) { |
| 71 | + buf := bytes.NewBuffer([]byte{}) |
| 72 | + enc := msgpack.NewEncoder(buf) |
| 73 | + |
| 74 | + arr, err := arrow.MakeArrow(tt.arr) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + err = enc.Encode(arr) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + require.Equal(t, tt.enc, buf.Bytes()) |
| 81 | + }) |
| 82 | + |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestDecodeArrow(t *testing.T) { |
| 87 | + for _, tt := range tests { |
| 88 | + t.Run(tt.name, func(t *testing.T) { |
| 89 | + |
| 90 | + buf := bytes.NewBuffer(tt.enc) |
| 91 | + dec := msgpack.NewDecoder(buf) |
| 92 | + |
| 93 | + var arr arrow.Arrow |
| 94 | + err := dec.Decode(&arr) |
| 95 | + require.NoError(t, err) |
| 96 | + |
| 97 | + require.Equal(t, tt.arr, arr.Raw()) |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
0 commit comments