| 1 | package bare |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "math" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | ) |
| 10 | |
| 11 | func TestWriteUint(t *testing.T) { |
| 12 | b := bytes.NewBuffer([]byte{}) |
| 13 | w := NewWriter(b) |
| 14 | |
| 15 | err := w.WriteUint(0x7F) |
| 16 | assert.Nil(t, err) |
| 17 | err = w.WriteUint(0x1337) |
| 18 | assert.Nil(t, err) |
| 19 | assert.Equal(t, []byte{0x7F, 0xB7, 0x26}, b.Bytes()) |
| 20 | } |
| 21 | |
| 22 | func TestWriteU8(t *testing.T) { |
| 23 | b := bytes.NewBuffer([]byte{}) |
| 24 | w := NewWriter(b) |
| 25 | err := w.WriteU8(uint8(0x42)) |
| 26 | assert.Nil(t, err) |
| 27 | assert.Equal(t, []byte{0x42}, b.Bytes()) |
| 28 | } |
| 29 | |
| 30 | func TestWriteU16(t *testing.T) { |
| 31 | b := bytes.NewBuffer([]byte{}) |
| 32 | w := NewWriter(b) |
| 33 | err := w.WriteU16(uint16(0xCAFE)) |
| 34 | assert.Nil(t, err) |
| 35 | assert.Equal(t, []byte{0xFE, 0xCA}, b.Bytes()) |
| 36 | } |
| 37 | |
| 38 | func TestWriteU32(t *testing.T) { |
| 39 | b := bytes.NewBuffer([]byte{}) |
| 40 | w := NewWriter(b) |
| 41 | err := w.WriteU32(uint32(0xDEADBEEF)) |
| 42 | assert.Nil(t, err) |
| 43 | assert.Equal(t, []byte{0xEF, 0xBE, 0xAD, 0xDE}, b.Bytes()) |
| 44 | } |
| 45 | |
| 46 | func TestWriteU64(t *testing.T) { |
| 47 | b := bytes.NewBuffer([]byte{}) |
| 48 | w := NewWriter(b) |
| 49 | err := w.WriteU64(uint64(0xCAFEBABEDEADBEEF)) |
| 50 | assert.Nil(t, err) |
| 51 | assert.Equal(t, []byte{0xEF, 0xBE, 0xAD, 0xDE, 0xBE, 0xBA, 0xFE, 0xCA}, b.Bytes()) |
| 52 | } |
| 53 | |
| 54 | func TestWriteInt(t *testing.T) { |
| 55 | b := bytes.NewBuffer([]byte{}) |
| 56 | w := NewWriter(b) |
| 57 | |
| 58 | err := w.WriteInt(42) |
| 59 | assert.Nil(t, err) |
| 60 | err = w.WriteInt(-1337) |
| 61 | assert.Nil(t, err) |
| 62 | assert.Equal(t, []byte{0x54, 0xf1, 0x14}, b.Bytes()) |
| 63 | } |
| 64 | |
| 65 | func TestWriteI8(t *testing.T) { |
| 66 | b := bytes.NewBuffer([]byte{}) |
| 67 | w := NewWriter(b) |
| 68 | err := w.WriteI8(int8(-42)) |
| 69 | assert.Nil(t, err) |
| 70 | assert.Equal(t, []byte{0xD6}, b.Bytes()) |
| 71 | } |
| 72 | |
| 73 | func TestWriteI16(t *testing.T) { |
| 74 | b := bytes.NewBuffer([]byte{}) |
| 75 | w := NewWriter(b) |
| 76 | err := w.WriteI16(int16(-1234)) |
| 77 | assert.Nil(t, err) |
| 78 | assert.Equal(t, []byte{0x2E, 0xFB}, b.Bytes()) |
| 79 | } |
| 80 | |
| 81 | func TestWriteI32(t *testing.T) { |
| 82 | b := bytes.NewBuffer([]byte{}) |
| 83 | w := NewWriter(b) |
| 84 | err := w.WriteI32(int32(-12345678)) |
| 85 | assert.Nil(t, err) |
| 86 | assert.Equal(t, []byte{0xB2, 0x9E, 0x43, 0xFF}, b.Bytes()) |
| 87 | } |
| 88 | |
| 89 | func TestWriteI64(t *testing.T) { |
| 90 | b := bytes.NewBuffer([]byte{}) |
| 91 | w := NewWriter(b) |
| 92 | err := w.WriteI64(int64(-12345678987654321)) |
| 93 | assert.Nil(t, err) |
| 94 | assert.Equal(t, []byte{0x4F, 0x0B, 0x6E, 0x9D, 0xAB, 0x23, 0xD4, 0xFF}, b.Bytes()) |
| 95 | } |
| 96 | |
| 97 | func TestWriteF32(t *testing.T) { |
| 98 | b := bytes.NewBuffer([]byte{}) |
| 99 | w := NewWriter(b) |
| 100 | err := w.WriteF32(float32(1337.42)) |
| 101 | assert.Nil(t, err) |
| 102 | assert.Equal(t, []byte{0x71, 0x2D, 0xA7, 0x44}, b.Bytes()) |
| 103 | |
| 104 | err = w.WriteF32(float32(math.NaN())) |
| 105 | assert.NotNil(t, err) |
| 106 | } |
| 107 | |
| 108 | func TestWriteF64(t *testing.T) { |
| 109 | b := bytes.NewBuffer([]byte{}) |
| 110 | w := NewWriter(b) |
| 111 | err := w.WriteF64(float64(133713371337.42424242)) |
| 112 | assert.Nil(t, err) |
| 113 | assert.Equal(t, []byte{0x9B, 0x6C, 0xC9, 0x20, 0xF0, 0x21, 0x3F, 0x42}, b.Bytes()) |
| 114 | |
| 115 | err = w.WriteF64(math.NaN()) |
| 116 | assert.NotNil(t, err) |
| 117 | } |
| 118 | |
| 119 | func TestWriteBool(t *testing.T) { |
| 120 | b := bytes.NewBuffer([]byte{}) |
| 121 | w := NewWriter(b) |
| 122 | err := w.WriteBool(false) |
| 123 | assert.Nil(t, err) |
| 124 | err = w.WriteBool(true) |
| 125 | assert.Nil(t, err) |
| 126 | assert.Equal(t, []byte{0x00, 0x01}, b.Bytes()) |
| 127 | } |
| 128 | |
| 129 | func TestWriteString(t *testing.T) { |
| 130 | b := bytes.NewBuffer([]byte{}) |
| 131 | w := NewWriter(b) |
| 132 | err := w.WriteString("こんにちは、世界!") |
| 133 | assert.Nil(t, err) |
| 134 | assert.Equal(t, []byte{0x1B, 0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, |
| 135 | 0x81, 0xAB, 0xE3, 0x81, 0xA1, 0xE3, 0x81, 0xAF, 0xE3, 0x80, 0x81, 0xE4, |
| 136 | 0xB8, 0x96, 0xE7, 0x95, 0x8C, 0xEF, 0xBC, 0x81}, |
| 137 | b.Bytes()) |
| 138 | } |
| 139 | |
| 140 | func TestWriteDataFixed(t *testing.T) { |
| 141 | b := bytes.NewBuffer([]byte{}) |
| 142 | w := NewWriter(b) |
| 143 | err := w.WriteDataFixed([]byte{0x13, 0x37, 0x42}) |
| 144 | assert.Nil(t, err) |
| 145 | assert.Equal(t, []byte{0x13, 0x37, 0x42}, b.Bytes()) |
| 146 | } |
| 147 | |
| 148 | func TestWriteData(t *testing.T) { |
| 149 | b := bytes.NewBuffer([]byte{}) |
| 150 | w := NewWriter(b) |
| 151 | err := w.WriteData([]byte{0x13, 0x37, 0x42}) |
| 152 | assert.Nil(t, err) |
| 153 | assert.Equal(t, []byte{0x03, 0x13, 0x37, 0x42}, b.Bytes()) |
| 154 | } |