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) {
|
|
|
|
reflect_type := reflect.TypeFor[T]()
|
|
|
|
stack, err := TypeStack(ctx, reflect_type)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
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) {
|
2024-03-08 14:35:23 -07:00
|
|
|
serialized, err := Serialize(ctx, value)
|
2024-03-08 00:22:51 -07:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[T](), value, serialized)
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
deserialized, err := Deserialize[T](ctx, serialized)
|
2024-03-08 00:22:51 -07:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
if value != deserialized {
|
2024-03-08 00:22:51 -07:00
|
|
|
t.Fatalf("Deserialized value[%+v] doesn't match original[%+v]", value, deserialized)
|
|
|
|
}
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
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) {
|
2024-03-08 14:35:23 -07:00
|
|
|
serialized, err := Serialize(ctx, value)
|
2024-03-08 00:22:51 -07:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[L](), value, serialized)
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
deserialized, err := Deserialize[L](ctx, serialized)
|
2024-03-08 00:22:51 -07:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
for i, item := range(value) {
|
2024-03-08 14:35:23 -07:00
|
|
|
if item != deserialized[i] {
|
|
|
|
t.Fatalf("Deserialized list %+v does not match original %+v", value, deserialized)
|
2024-03-08 00:22:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 14:35:23 -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) {
|
2024-03-08 14:35:23 -07:00
|
|
|
serialized, err := Serialize(ctx, value)
|
2024-03-08 00:22:51 -07:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[P](), value, serialized)
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
deserialized, err := Deserialize[P](ctx, serialized)
|
2024-03-08 00:22:51 -07:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
if value == nil && deserialized == nil {
|
2024-03-08 00:22:51 -07:00
|
|
|
ctx.Log.Logf("test", "Deserialized nil")
|
|
|
|
} else if value == nil {
|
2024-03-08 14:35:23 -07:00
|
|
|
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)
|
2024-03-08 14:35:23 -07:00
|
|
|
} 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 {
|
2024-03-08 14:35:23 -07:00
|
|
|
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) {
|
2024-03-08 14:35:23 -07:00
|
|
|
serialized, err := Serialize(ctx, value)
|
2024-03-08 00:22:51 -07:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[T](), value, serialized)
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
deserialized, err := Deserialize[T](ctx, serialized)
|
2024-03-08 00:22:51 -07:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2024-03-08 14:35:23 -07:00
|
|
|
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"})
|
|
|
|
|
2024-03-10 15:41:13 -06:00
|
|
|
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")
|
|
|
|
|
|
|
|
node, err := NewNode(ctx, nil, "Base", 100)
|
|
|
|
fatalErr(t, err)
|
|
|
|
testSerialize(t, ctx, node)
|
|
|
|
}
|