Add full tree serialization test

This commit is contained in:
zervo 2025-02-14 16:55:19 +01:00
parent b2cc3359a8
commit 60635cce46

View file

@ -18,12 +18,14 @@ var (
} }
node_ser_targetbytes = []byte{ node_ser_targetbytes = []byte{
// ROOT NODE // -- ROOT NODE: HEADER
0x33, 0x00, 0x00, 0x00, // Node size = 33 0x33, 0x00, 0x00, 0x00, // Node size = 33
0x02, 0x00, // Property count = 2 0x02, 0x00, // Property count = 2
0x01, 0x00, // Child count = 1 0x01, 0x00, // Child count = 1
0x06, // Name length = 6 0x06, // Name length = 6
// -- ROOT NODE: NAME
0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, // Node name = "player" 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, // Node name = "player"
// -- ROOT NODE: PROPERTIES
// ROOT NODE -> PROPERTY A // ROOT NODE -> PROPERTY A
0x06, // Key length = 6 0x06, // Key length = 6
0x0b, // Value type = bool 0x0b, // Value type = bool
@ -34,6 +36,7 @@ var (
0x03, // Value type = 16-bit signed int 0x03, // Value type = 16-bit signed int
0x6c, 0x65, 0x76, 0x65, 0x6c, // Key = "level" 0x6c, 0x65, 0x76, 0x65, 0x6c, // Key = "level"
0x1b, 0x00, // Value = 27 0x1b, 0x00, // Value = 27
// -- ROOT NODE: CHILDREN
// ROOT NODE -> CHILD NODE // ROOT NODE -> CHILD NODE
0x12, 0x00, 0x00, 0x00, // Node size = 12 0x12, 0x00, 0x00, 0x00, // Node size = 12
0x00, 0x00, // Property count = 0 0x00, 0x00, // Property count = 0
@ -41,6 +44,25 @@ var (
0x09, // Name length = 9 0x09, // Name length = 9
0x69, 0x6e, 0x76, 0x65, 0x6e, 0x74, 0x6f, 0x72, 0x79, // Node name = "inventory" 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) { func TestPropertySerialization(t *testing.T) {
@ -50,8 +72,8 @@ func TestPropertySerialization(t *testing.T) {
Value: "zervo", Value: "zervo",
} }
buf := &bytes.Buffer{} var buf bytes.Buffer
err := sbhpfv1.SerializeProperty(buf, prop) err := sbhpfv1.SerializeProperty(&buf, prop)
if err != nil { if err != nil {
t.Fatalf("Property serialization failed: %v", err) t.Fatalf("Property serialization failed: %v", err)
} }
@ -89,13 +111,38 @@ func TestNodeSerialization(t *testing.T) {
}, },
} }
var w bytes.Buffer var buf bytes.Buffer
err := sbhpfv1.SerializeNodeStream(&w, &node) err := sbhpfv1.SerializeNodeBuffered(&buf, &node)
if err != nil { if err != nil {
t.Fatalf("Node serialization failed: %v", err) t.Fatalf("Node serialization failed: %v", err)
} }
if !slices.Equal(w.Bytes(), node_ser_targetbytes) { if !slices.Equal(buf.Bytes(), node_ser_targetbytes) {
t.Fatalf("Node serialization generated bad data: %v", w.Bytes()) 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())
} }
} }