|
| 1 | +package y3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestD2EncodeUInt32(t *testing.T) { |
| 8 | + testD2EncodeUInt32(t, 0x03, 6, []byte{0x03, 0x01, 0x06}) |
| 9 | + testD2EncodeUInt32(t, 0x06, 127, []byte{0x06, 0x02, 0x80, 0x7F}) |
| 10 | +} |
| 11 | + |
| 12 | +func TestD2EncodeInt32(t *testing.T) { |
| 13 | + testD2EncodeInt(t, 0x03, -1, []byte{0x03, 0x01, 0x7F}) |
| 14 | + testD2EncodeInt(t, 0x06, -65, []byte{0x06, 0x02, 0xFF, 0x3F}) |
| 15 | + testD2EncodeInt(t, 0x09, 255, []byte{0x09, 0x02, 0x81, 0x7F}) |
| 16 | +} |
| 17 | + |
| 18 | +func TestD2EncodeUInt64(t *testing.T) { |
| 19 | + testD2EncodeUInt64(t, 0x03, 0, []byte{0x03, 0x01, 0x00}) |
| 20 | + testD2EncodeUInt64(t, 0x06, 1, []byte{0x06, 0x01, 0x01}) |
| 21 | + testD2EncodeUInt64(t, 0x09, 18446744073709551615, []byte{0x09, 0x01, 0x7F}) |
| 22 | +} |
| 23 | + |
| 24 | +func TestD2EncodeInt64(t *testing.T) { |
| 25 | + testD2EncodeInt64(t, 0x03, 0, []byte{0x03, 0x01, 0x00}) |
| 26 | + testD2EncodeInt64(t, 0x06, 1, []byte{0x06, 0x01, 0x01}) |
| 27 | + testD2EncodeInt64(t, 0x09, -1, []byte{0x09, 0x01, 0x7F}) |
| 28 | +} |
| 29 | + |
| 30 | +func TestD2EncodeFloat32(t *testing.T) { |
| 31 | + testD2EncodeFloat32(t, 0x03, -2, []byte{0x03, 0x01, 0xC0}) |
| 32 | + testD2EncodeFloat32(t, 0x06, 0.25, []byte{0x06, 0x02, 0x3E, 0x80}) |
| 33 | + testD2EncodeFloat32(t, 0x09, 68.123, []byte{0x09, 0x04, 0x42, 0x88, 0x3E, 0xFA}) |
| 34 | +} |
| 35 | + |
| 36 | +func TestD2EncodeFloat64(t *testing.T) { |
| 37 | + testD2EncodeFloat64(t, 0x03, 23, []byte{0x03, 0x02, 0x40, 0x37}) |
| 38 | + testD2EncodeFloat64(t, 0x06, 2, []byte{0x06, 0x01, 0x40}) |
| 39 | + testD2EncodeFloat64(t, 0x09, 0.01171875, []byte{0x09, 0x02, 0x3F, 0x88}) |
| 40 | +} |
| 41 | + |
| 42 | +func TestD2EncodeString(t *testing.T) { |
| 43 | + p, _ := EncodeString(0x01, "C") |
| 44 | + compareTwoBytes(t, p, []byte{0x01, 0x01, 0x43}) |
| 45 | + p, _ = EncodeString(0x01, "CC") |
| 46 | + compareTwoBytes(t, p, []byte{0x01, 0x02, 0x43, 0x43}) |
| 47 | + p, _ = EncodeString(0x01, "Yona") |
| 48 | + compareTwoBytes(t, p, []byte{0x01, 0x04, 0x59, 0x6F, 0x6E, 0x61}) |
| 49 | + p, _ = EncodeString(0x01, "https://yomo.run") |
| 50 | + compareTwoBytes(t, p, []byte{0x01, 0x10, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3A, 0x2F, 0x2F, 0x79, 0x6F, 0x6D, 0x6F, 0x2E, 0x72, 0x75, 0x6E}) |
| 51 | +} |
| 52 | + |
| 53 | +func TestD2EncodeBytes(t *testing.T) { |
| 54 | + p, _ := EncodeBytes(0x01, []byte{0x03, 0x06, 0x09, 0x0C, 0x0F}) |
| 55 | + compareTwoBytes(t, p, []byte{0x01, 0x05, 0x03, 0x06, 0x09, 0x0C, 0x0F}) |
| 56 | +} |
| 57 | + |
| 58 | +func TestD2EncodeBool(t *testing.T) { |
| 59 | + p, _ := EncodeBool(0x01, true) |
| 60 | + compareTwoBytes(t, p, []byte{0x01, 0x01, 0x01}) |
| 61 | + p, _ = EncodeBool(0x01, false) |
| 62 | + compareTwoBytes(t, p, []byte{0x01, 0x01, 0x00}) |
| 63 | +} |
| 64 | + |
| 65 | +func testD2EncodeUInt32(t *testing.T, tag int, val uint, expected []byte) { |
| 66 | + p, _ := EncodeUInt(tag, val) |
| 67 | + compareTwoBytes(t, p, expected) |
| 68 | +} |
| 69 | + |
| 70 | +func testD2EncodeInt(t *testing.T, tag int, val int, expected []byte) { |
| 71 | + p, _ := EncodeInt(tag, val) |
| 72 | + compareTwoBytes(t, p, expected) |
| 73 | +} |
| 74 | + |
| 75 | +func testD2EncodeUInt64(t *testing.T, tag int, val uint64, expected []byte) { |
| 76 | + p, _ := EncodeUInt64(tag, val) |
| 77 | + compareTwoBytes(t, p, expected) |
| 78 | +} |
| 79 | + |
| 80 | +func testD2EncodeInt64(t *testing.T, tag int, val int64, expected []byte) { |
| 81 | + p, _ := EncodeInt64(tag, val) |
| 82 | + compareTwoBytes(t, p, expected) |
| 83 | +} |
| 84 | + |
| 85 | +func testD2EncodeFloat32(t *testing.T, tag int, val float32, expected []byte) { |
| 86 | + p, _ := EncodeFloat32(tag, val) |
| 87 | + compareTwoBytes(t, p, expected) |
| 88 | +} |
| 89 | + |
| 90 | +func testD2EncodeFloat64(t *testing.T, tag int, val float64, expected []byte) { |
| 91 | + p, _ := EncodeFloat64(tag, val) |
| 92 | + compareTwoBytes(t, p, expected) |
| 93 | +} |
| 94 | + |
| 95 | +func compareTwoBytes(t *testing.T, result []byte, expected []byte) { |
| 96 | + for i, p := range result { |
| 97 | + if p != expected[i] { |
| 98 | + t.Errorf("\nexpected:[% X]\n actual:[% X]\n", expected, result) |
| 99 | + break |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments