package tape import "fmt" import "bytes" import "testing" import "reflect" import tu "git.tebibyte.media/sashakoshka/hopp/internal/testutil" func TestEncodeAnyInt(test *testing.T) { err := testEncodeAny(test, uint8(0xCA), LI.WithCN(0), tu.S(0xCA)) if err != nil { test.Fatal(err) } err = testEncodeAny(test, 400, LI.WithCN(3), tu.S( 0, 0, 0x1, 0x90, )) if err != nil { test.Fatal(err) } } func TestEncodeAnyTable(test *testing.T) { err := testEncodeAny(test, map[uint16] any { 0xF3B9: 1, 0x0102: 2, 0x0000: "hi!", 0xFFFF: []uint16 { 0xBEE5, 0x7777 }, 0x1234: [][]uint16 { []uint16 { 0x5 }, []uint16 { 0x17, 0xAAAA} }, }, KTV.WithCN(0), tu.S(5).AddVar( []byte { 0xF3, 0xB9, byte(LI.WithCN(3)), 0, 0, 0, 1, }, []byte { 0x01, 0x02, byte(LI.WithCN(3)), 0, 0, 0, 2, }, []byte { 0, 0, byte(SBA.WithCN(3)), 'h', 'i', '!', }, []byte { 0xFF, 0xFF, byte(OTA.WithCN(0)), 2, byte(LI.WithCN(1)), 0xBE, 0xE5, 0x77, 0x77, }, []byte { 0x12, 0x34, byte(OTA.WithCN(0)), 2, byte(OTA.WithCN(0)), 1, byte(LI.WithCN(1)), 0, 0x5, 2, byte(LI.WithCN(1)), 0, 0x17, 0xAA, 0xAA, }, )) if err != nil { test.Fatal(err) } } func TestEncodeDecodeAnyMap(test *testing.T) { err := testEncodeDecodeAny(test, map[uint16] any { 0xF3B9: 1, 0x0102: 2, 0x0000: "hi!", 0xFFFF: []uint16 { 0xBEE5, 0x7777 }, 0x1234: [][]uint16 { []uint16 { 0x5 }, []uint16 { 0x17, 0xAAAA} }, }, nil) if err != nil { test.Fatal(err) } } func TestPeekSlice(test *testing.T) { buffer := bytes.NewBuffer([]byte { 2, byte(OTA.WithCN(3)), 0, 0, 0, 1, byte(LI.WithCN(1)), 0, 0x5, 2, byte(LI.WithCN(1)), 0, 0x17, 0xAA, 0xAA, }) decoder := NewDecoder(buffer) elem, dimension, err := peekSlice(decoder, OTA.WithCN(0)) if err != nil { test.Fatal(err) } if elem != LI.WithCN(1) { test.Fatalf("wrong element tag: %v %02X", elem, byte(elem)) } if got, correct := dimension, 2; got != correct { test.Fatalf("wrong dimension: %d != %d", got, correct) } } func TestPeekSliceOnce(test *testing.T) { buffer := bytes.NewBuffer([]byte { 2, byte(OTA.WithCN(3)), 0, 0, 0, 1, byte(LI.WithCN(1)), 0, 0x5, 2, byte(LI.WithCN(1)), 0, 0x17, 0xAA, 0xAA, }) decoder := NewDecoder(buffer) test.Log("--- stage 1") elem, populated, n, err := peekSliceOnce(decoder, OTA.WithCN(0), 0) if err != nil { test.Fatal(err) } if elem != OTA.WithCN(3) { test.Fatal("wrong element tag:", elem) } if !populated { test.Fatal("wrong populated:", populated) } if got, correct := n, 2; got != correct { test.Fatalf("wrong n: %d != %d", got, correct) } test.Log("--- stage 2") elem, populated, n, err = peekSliceOnce(decoder, elem, n) if err != nil { test.Fatal(err) } if elem != LI.WithCN(1) { test.Fatal("wrong element tag:", elem) } if !populated { test.Fatal("wrong populated:", populated) } if got, correct := n, 7; got != correct { test.Fatalf("wrong n: %d != %d", got, correct) } } func encAny(value any) ([]byte, Tag, int, error) { tag, err := TagAny(value) if err != nil { return nil, 0, 0, err } buffer := bytes.Buffer { } encoder := NewEncoder(&buffer) n, err := EncodeAny(encoder, value, tag) if err != nil { return nil, 0, n, err } encoder.Flush() return buffer.Bytes(), tag, n, nil } func decAny(data []byte) (Tag, any, int, error) { destination := map[uint16] any { } tag, err := TagAny(destination) if err != nil { return 0, nil, 0, err } n, err := DecodeAny(NewDecoder(bytes.NewBuffer(data)), &destination, tag) if err != nil { return 0, nil, n, err } return tag, destination, n, nil } func testEncodeAny(test *testing.T, value any, correctTag Tag, correctBytes tu.Snake) error { bytes, tag, n, err := encAny(value) if err != nil { return err } test.Log("n: ", n) test.Log("tag: ", tag) test.Log("got: ", tu.HexBytes(bytes)) test.Log("correct:", correctBytes) if tag != correctTag { return fmt.Errorf("tag not equal") } if ok, n := correctBytes.Check(bytes); !ok { return fmt.Errorf("bytes not equal: %d", n) } if n != len(bytes) { return fmt.Errorf("n not equal: %d != %d", n, len(bytes)) } return nil } func testEncodeDecodeAny(test *testing.T, value, correctValue any) error { if correctValue == nil { correctValue = value } test.Log("encoding...") bytes, tag, n, err := encAny(value) if err != nil { return err } test.Log("n: ", n) test.Log("tag:", tag) test.Log("got:", tu.HexBytes(bytes)) test.Log("decoding...", tag) if n != len(bytes) { return fmt.Errorf("n not equal: %d != %d", n, len(bytes)) } _, decoded, n, err := decAny(bytes) if err != nil { return err } test.Log("got: ", decoded) test.Log("correct:", correctValue) if !reflect.DeepEqual(decoded, correctValue) { return fmt.Errorf("values not equal") } if n != len(bytes) { return fmt.Errorf("n not equal: %d != %d", n, len(bytes)) } return nil }