tape: Implement option type in the dynamic system
This commit is contained in:
@@ -3,39 +3,10 @@ package tape
|
||||
import "bytes"
|
||||
import "testing"
|
||||
import "reflect"
|
||||
import "git.tebibyte.media/sashakoshka/go-util/container"
|
||||
import tu "git.tebibyte.media/sashakoshka/hopp/internal/testutil"
|
||||
|
||||
var samplePayloads = [][]byte {
|
||||
/* int8 */ []byte { byte(LSI.WithCN(0)), 0x45 },
|
||||
/* int16 */ []byte { byte(LSI.WithCN(1)), 0x45, 0x67 },
|
||||
/* int32 */ []byte { byte(LSI.WithCN(3)), 0x45, 0x67, 0x89, 0xAB },
|
||||
/* int64 */ []byte { byte(LSI.WithCN(7)), 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23 },
|
||||
/* uint5 */ []byte { byte(SI.WithCN(12)) },
|
||||
/* uint8 */ []byte { byte(LI.WithCN(0)), 0x45 },
|
||||
/* uint16 */ []byte { byte(LI.WithCN(1)), 0x45, 0x67 },
|
||||
/* uint32 */ []byte { byte(LI.WithCN(3)), 0x45, 0x67, 0x89, 0xAB },
|
||||
/* uint64 */ []byte { byte(LI.WithCN(7)), 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23 },
|
||||
/* bool */ []byte { byte(SI.WithCN(0)) },
|
||||
/* bool */ []byte { byte(SI.WithCN(1)) },
|
||||
/* string */ []byte { byte(SBA.WithCN(7)), 'p', 'u', 'p', 'e', 'v', 'e', 'r' },
|
||||
/* []byte */ []byte { byte(SBA.WithCN(5)), 'b', 'l', 'a', 'r', 'g' },
|
||||
/* []string */ []byte {
|
||||
byte(OTA.WithCN(0)), 2, byte(LBA.WithCN(0)),
|
||||
0x08, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23,
|
||||
0x05, 0x11, 0x11, 0x11, 0x11, 0x11,
|
||||
},
|
||||
/* map[uint16] any */ []byte {
|
||||
byte(KTV.WithCN(0)), 2,
|
||||
0x02, 0x23, byte(LSI.WithCN(1)), 0x45, 0x67,
|
||||
0x02, 0x24, byte(LI.WithCN(3)), 0x45, 0x67, 0x89, 0xAB,
|
||||
},
|
||||
/* map[uint16] any */ []byte {
|
||||
byte(KTV.WithCN(0)), 3,
|
||||
0x00, 0x01, 0x63, 0x43, 0xF4, 0xC0, 0x00,
|
||||
0x00, 0x02, 0x82, 'h', 'i',
|
||||
0x00, 0x03, 0x21, 0x39, 0x92,
|
||||
},
|
||||
}
|
||||
var samplePayloads [][]byte
|
||||
|
||||
var sampleValues = []any {
|
||||
/* int8 */ int8(0x45),
|
||||
@@ -64,6 +35,64 @@ var sampleValues = []any {
|
||||
0x0002: "hi",
|
||||
0x0003: uint16(0x3992),
|
||||
},
|
||||
// IMPORTANT: ONLY ADD AT THE END!!!! DO NOT MOVE WHAT IS ALREADY HERE!
|
||||
// IMPORTANT: ONLY ADD AT THE END!!!! DO NOT MOVE WHAT IS ALREADY HERE!
|
||||
}
|
||||
|
||||
type sample struct {
|
||||
t Tag
|
||||
v any
|
||||
s tu.Snake
|
||||
}
|
||||
|
||||
var samples = []sample {
|
||||
/* int8 */ sample { t: LSI.WithCN(0), s: tu.S(0x45) },
|
||||
/* int16 */ sample { t: LSI.WithCN(1), s: tu.S(0x45, 0x67) },
|
||||
/* int32 */ sample { t: LSI.WithCN(3), s: tu.S(0x45, 0x67, 0x89, 0xAB) },
|
||||
/* int64 */ sample { t: LSI.WithCN(7), s: tu.S(0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23) },
|
||||
/* uint5 */ sample { t: SI.WithCN(12), s: tu.S() },
|
||||
/* uint8 */ sample { t: LI.WithCN(0), s: tu.S(0x45) },
|
||||
/* uint16 */ sample { t: LI.WithCN(1), s: tu.S(0x45, 0x67) },
|
||||
/* uint32 */ sample { t: LI.WithCN(3), s: tu.S(0x45, 0x67, 0x89, 0xAB) },
|
||||
/* uint64 */ sample { t: LI.WithCN(7), s: tu.S(0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23) },
|
||||
/* bool */ sample { t: SI.WithCN(0), s: tu.S() },
|
||||
/* bool */ sample { t: SI.WithCN(1), s: tu.S() },
|
||||
/* string */ sample { t: SBA.WithCN(7), s: tu.S('p', 'u', 'p', 'e', 'v', 'e', 'r') },
|
||||
/* []byte */ sample { t: SBA.WithCN(5), s: tu.S('b', 'l', 'a', 'r', 'g') },
|
||||
/* []string */ sample {
|
||||
t: OTA.WithCN(0),
|
||||
s: tu.S(2, byte(LBA.WithCN(0)),
|
||||
0x08, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23,
|
||||
0x05, 0x11, 0x11, 0x11, 0x11, 0x11),
|
||||
},
|
||||
/* map[uint16] any */ sample {
|
||||
t: KTV.WithCN(0),
|
||||
s: tu.S(2).AddVar(
|
||||
[]byte { 0x02, 0x23, byte(LSI.WithCN(1)), 0x45, 0x67 },
|
||||
[]byte { 0x02, 0x24, byte(LI.WithCN(3)), 0x45, 0x67, 0x89, 0xAB }),
|
||||
},
|
||||
/* map[uint16] any */ sample {
|
||||
t: KTV.WithCN(0),
|
||||
s: tu.S(3).AddVar(
|
||||
[]byte { 0x00, 0x01, 0x63, 0x43, 0xF4, 0xC0, 0x00 },
|
||||
[]byte { 0x00, 0x02, 0x82, 'h', 'i' },
|
||||
[]byte { 0x00, 0x03, 0x21, 0x39, 0x92 }),
|
||||
},
|
||||
}
|
||||
|
||||
var sampleOptionValues []any
|
||||
|
||||
func init() {
|
||||
sampleOptionValues = make([]any, len(sampleValues))
|
||||
for index, value := range sampleValues {
|
||||
sampleOptionValues[index] = ucontainer.O(value)
|
||||
samples[index].v = value
|
||||
}
|
||||
samplePayloads = make([][]byte, len(samples))
|
||||
for index, sample := range samples {
|
||||
item := append([]byte { byte(sample.t) }, sample.s.Flatten()...)
|
||||
samplePayloads[index] = item
|
||||
}
|
||||
}
|
||||
|
||||
type userDefinedInteger int16
|
||||
@@ -271,6 +300,20 @@ func TestEncodeDecodeAnyDestination(test *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeOption(test *testing.T) {
|
||||
for _, sample := range samples {
|
||||
snake := sample.s
|
||||
tag := sample.t
|
||||
value := sample.v
|
||||
if _, ok := value.(bool); tag.Is(SI) && !ok {
|
||||
// we will never encode an SI unless its a bool
|
||||
continue
|
||||
}
|
||||
err := testEncodeAny(test, value, tag, snake)
|
||||
if err != nil { test.Fatal(err) }
|
||||
}
|
||||
}
|
||||
|
||||
func TestPeekSlice(test *testing.T) {
|
||||
buffer := bytes.NewBuffer([]byte {
|
||||
2, byte(OTA.WithCN(3)),
|
||||
|
||||
Reference in New Issue
Block a user