graphvent/serialize_test.go

169 lines
4.9 KiB
Go

2024-03-08 00:22:51 -07:00
package graphvent
import (
"testing"
"reflect"
"github.com/google/uuid"
)
func testTypeStack[T any](t *testing.T, ctx *Context) {
buffer := [1024]byte{}
2024-03-08 00:22:51 -07:00
reflect_type := reflect.TypeFor[T]()
written, err := TypeStack(ctx, reflect_type, buffer[:])
2024-03-08 00:22:51 -07:00
fatalErr(t, err)
stack := buffer[:written]
2024-03-08 00:22:51 -07:00
ctx.Log.Logf("test", "TypeStack[%s]: %+v", reflect_type, stack)
unwrapped_type, rest, err := UnwrapStack(ctx, stack)
fatalErr(t, err)
if len(rest) != 0 {
t.Errorf("Types remaining after unwrapping %s: %+v", unwrapped_type, stack)
}
if unwrapped_type != reflect_type {
t.Errorf("Unwrapped type[%+v] doesn't match original[%+v]", unwrapped_type, reflect_type)
}
ctx.Log.Logf("test", "Unwrapped type[%s]: %s", reflect_type, reflect_type)
}
func TestSerializeTypes(t *testing.T) {
ctx := logTestContext(t, []string{"test"})
testTypeStack[int](t, ctx)
testTypeStack[map[int]string](t, ctx)
testTypeStack[string](t, ctx)
testTypeStack[*string](t, ctx)
testTypeStack[*map[string]*map[*string]int](t, ctx)
testTypeStack[[5]int](t, ctx)
testTypeStack[uuid.UUID](t, ctx)
testTypeStack[NodeID](t, ctx)
}
func testSerializeCompare[T comparable](t *testing.T, ctx *Context, value T) {
buffer := [1024]byte{}
written, err := Serialize(ctx, value, buffer[:])
2024-03-08 00:22:51 -07:00
fatalErr(t, err)
serialized := buffer[:written]
2024-03-08 00:22:51 -07:00
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[T](), value, serialized)
deserialized, err := Deserialize[T](ctx, serialized)
2024-03-08 00:22:51 -07:00
fatalErr(t, err)
if value != deserialized {
2024-03-08 00:22:51 -07:00
t.Fatalf("Deserialized value[%+v] doesn't match original[%+v]", value, deserialized)
}
ctx.Log.Logf("test", "Deserialized Value[%+v]: %+v", value, deserialized)
2024-03-08 00:22:51 -07:00
}
func testSerializeList[L []T, T comparable](t *testing.T, ctx *Context, value L) {
buffer := [1024]byte{}
written, err := Serialize(ctx, value, buffer[:])
2024-03-08 00:22:51 -07:00
fatalErr(t, err)
serialized := buffer[:written]
2024-03-08 00:22:51 -07:00
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[L](), value, serialized)
deserialized, err := Deserialize[L](ctx, serialized)
2024-03-08 00:22:51 -07:00
fatalErr(t, err)
for i, item := range(value) {
if item != deserialized[i] {
t.Fatalf("Deserialized list %+v does not match original %+v", value, deserialized)
2024-03-08 00:22:51 -07:00
}
}
ctx.Log.Logf("test", "Deserialized Value[%+v]: %+v", value, deserialized)
2024-03-08 00:22:51 -07:00
}
func testSerializePointer[P interface {*T}, T comparable](t *testing.T, ctx *Context, value P) {
buffer := [1024]byte{}
written, err := Serialize(ctx, value, buffer[:])
2024-03-08 00:22:51 -07:00
fatalErr(t, err)
serialized := buffer[:written]
2024-03-08 00:22:51 -07:00
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[P](), value, serialized)
deserialized, err := Deserialize[P](ctx, serialized)
2024-03-08 00:22:51 -07:00
fatalErr(t, err)
if value == nil && deserialized == nil {
2024-03-08 00:22:51 -07:00
ctx.Log.Logf("test", "Deserialized nil")
} else if value == nil {
t.Fatalf("Non-nil value[%+v] returned for nil value", deserialized)
} else if deserialized == nil {
2024-03-08 00:22:51 -07:00
t.Fatalf("Nil value returned for non-nil value[%+v]", value)
} else if *deserialized != *value {
2024-03-08 00:22:51 -07:00
t.Fatalf("Deserialized value[%+v] doesn't match original[%+v]", value, deserialized)
} else {
ctx.Log.Logf("test", "Deserialized Value[%+v]: %+v", *value, *deserialized)
2024-03-08 00:22:51 -07:00
}
}
func testSerialize[T any](t *testing.T, ctx *Context, value T) {
buffer := [1024]byte{}
written, err := Serialize(ctx, value, buffer[:])
2024-03-08 00:22:51 -07:00
fatalErr(t, err)
serialized := buffer[:written]
2024-03-08 00:22:51 -07:00
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[T](), value, serialized)
deserialized, err := Deserialize[T](ctx, serialized)
2024-03-08 00:22:51 -07:00
fatalErr(t, err)
ctx.Log.Logf("test", "Deserialized Value[%+v]: %+v", value, deserialized)
2024-03-08 00:22:51 -07:00
}
func TestSerializeValues(t *testing.T) {
ctx := logTestContext(t, []string{"test"})
testSerialize(t, ctx, Extension(NewLockableExt(nil)))
2024-03-08 00:22:51 -07:00
testSerializeCompare[int8](t, ctx, -64)
testSerializeCompare[int16](t, ctx, -64)
testSerializeCompare[int32](t, ctx, -64)
testSerializeCompare[int64](t, ctx, -64)
testSerializeCompare[int](t, ctx, -64)
testSerializeCompare[uint8](t, ctx, 64)
testSerializeCompare[uint16](t, ctx, 64)
testSerializeCompare[uint32](t, ctx, 64)
testSerializeCompare[uint64](t, ctx, 64)
testSerializeCompare[uint](t, ctx, 64)
testSerializeCompare[string](t, ctx, "test")
a := 12
testSerializePointer[*int](t, ctx, &a)
b := "test"
testSerializePointer[*string](t, ctx, nil)
testSerializePointer[*string](t, ctx, &b)
testSerializeList(t, ctx, []int{1, 2, 3, 4, 5})
testSerializeCompare[bool](t, ctx, true)
testSerializeCompare[bool](t, ctx, false)
testSerializeCompare[int](t, ctx, -1)
testSerializeCompare[uint](t, ctx, 1)
testSerializeCompare[NodeID](t, ctx, RandID())
testSerializeCompare[*int](t, ctx, nil)
testSerializeCompare(t, ctx, "string")
2024-03-28 20:28:07 -06:00
testSerialize(t, ctx, NewListenerExt(10))
node, err := NewNode(ctx, nil, "Node", 100)
2024-03-08 00:22:51 -07:00
fatalErr(t, err)
testSerialize(t, ctx, node)
}