GoSBHPF/SBHPFv1/serializer_test.go
2025-02-14 16:55:19 +01:00

148 lines
3.6 KiB
Go

package sbhpfv1_test
import (
"bytes"
"slices"
"testing"
sbhpfv1 "git.zervo.org/FLUX/GoSBHPF/SBHPFv1"
)
var (
prop_ser_targetbytes = []byte{
0x05, // Key length = 5
0x0c, // Value type = string
0x6f, 0x77, 0x6e, 0x65, 0x72, // Key = "owner"
0x05, 0x00, // String length = 5
0x7a, 0x65, 0x72, 0x76, 0x6f, // String = "zervo"
}
node_ser_targetbytes = []byte{
// -- ROOT NODE: HEADER
0x33, 0x00, 0x00, 0x00, // Node size = 33
0x02, 0x00, // Property count = 2
0x01, 0x00, // Child count = 1
0x06, // Name length = 6
// -- ROOT NODE: NAME
0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, // Node name = "player"
// -- ROOT NODE: PROPERTIES
// ROOT NODE -> PROPERTY A
0x06, // Key length = 6
0x0b, // Value type = bool
0x61, 0x63, 0x74, 0x69, 0x76, 0x65, // Key = "active"
0x01, // Value = true
// ROOT NODE -> PROPERTY B
0x05, // Key length = 5
0x03, // Value type = 16-bit signed int
0x6c, 0x65, 0x76, 0x65, 0x6c, // Key = "level"
0x1b, 0x00, // Value = 27
// -- ROOT NODE: CHILDREN
// ROOT NODE -> CHILD NODE
0x12, 0x00, 0x00, 0x00, // Node size = 12
0x00, 0x00, // Property count = 0
0x00, 0x00, // Child count = 0
0x09, // Name length = 9
0x69, 0x6e, 0x76, 0x65, 0x6e, 0x74, 0x6f, 0x72, 0x79, // Node name = "inventory"
}
full_ser_targetbytes = []byte{
// -- FILE HEADER
0x01, // Version flag = 1
0x00, // Feature flag = 0
// -- ROOT NODE: HEADER
0x16, 0x00, 0x00, 0x00, // Node size = 16
0x01, 0x00, // Property count = 1
0x00, 0x00, // Child count = 0
0x04, // Name length = 4
// -- ROOT NODE: NAME
0x75, 0x73, 0x65, 0x72, // Node name = "user"
// -- ROOT NODE: PROPERTIES
// ROOT NODE -> PROPERTY
0x06, // Key length = 6
0x0b, // Value type = bool
0x61, 0x63, 0x74, 0x69, 0x76, 0x65, // Key = "active"
0x01, // Value = true
}
)
func TestPropertySerialization(t *testing.T) {
prop := sbhpfv1.Property{
Key: "owner",
Type: sbhpfv1.TypeString,
Value: "zervo",
}
var buf bytes.Buffer
err := sbhpfv1.SerializeProperty(&buf, prop)
if err != nil {
t.Fatalf("Property serialization failed: %v", err)
}
if !slices.Equal(buf.Bytes(), prop_ser_targetbytes) {
t.Fatalf("Property serialization generated bad data: %v", buf.Bytes())
}
}
func TestNodeSerialization(t *testing.T) {
prop_a := sbhpfv1.Property{
Key: "active",
Type: sbhpfv1.TypeBool,
Value: true,
}
prop_b := sbhpfv1.Property{
Key: "level",
Type: sbhpfv1.TypeInt16,
Value: int16(27),
}
child_node := sbhpfv1.Node{
Name: "inventory",
}
node := sbhpfv1.Node{
Name: "player",
Properties: []sbhpfv1.Property{
prop_a,
prop_b,
},
Children: []*sbhpfv1.Node{
&child_node,
},
}
var buf bytes.Buffer
err := sbhpfv1.SerializeNodeBuffered(&buf, &node)
if err != nil {
t.Fatalf("Node serialization failed: %v", err)
}
if !slices.Equal(buf.Bytes(), node_ser_targetbytes) {
t.Fatalf("Node serialization generated bad data: %v", buf.Bytes())
}
}
func TestFullSerialization(t *testing.T) {
prop := sbhpfv1.Property{
Key: "active",
Type: sbhpfv1.TypeBool,
Value: true,
}
node := sbhpfv1.Node{
Name: "user",
Properties: []sbhpfv1.Property{
prop,
},
}
var buf bytes.Buffer
err := sbhpfv1.SerializeBuffered(&buf, &node)
if err != nil {
t.Fatalf("Full tree serialization failed: %v", err)
}
if !slices.Equal(buf.Bytes(), full_ser_targetbytes) {
t.Fatalf("Full tree serialization generated bad data: %v", buf.Bytes())
}
}