tape: In progress testing of dynamic encoding/decoding
This commit is contained in:
parent
37eccc91c0
commit
e1f58a194a
136
tape/dynamic_test.go
Normal file
136
tape/dynamic_test.go
Normal file
@ -0,0 +1,136 @@
|
||||
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 encAny(value any) ([]byte, Tag, int, error) {
|
||||
tag, err := TagAny(value)
|
||||
if err != nil { return nil, 0, 0, err }
|
||||
buffer := bytes.Buffer { }
|
||||
n, err := EncodeAny(&Encoder {
|
||||
Writer: &buffer,
|
||||
}, value, tag)
|
||||
if err != nil { return nil, 0, n, err }
|
||||
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(&Decoder {
|
||||
Reader: 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("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("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
|
||||
}
|
Loading…
Reference in New Issue
Block a user