controllogic: add Value union + sizing helpers (port of arraypolicy.ts)
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package controllogic
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAsNumAsArr(t *testing.T) {
|
||||
if n, err := asNum(3.0); err != nil || n != 3 {
|
||||
t.Fatalf("asNum(3)=%v,%v", n, err)
|
||||
}
|
||||
if _, err := asNum([]Value{1.0}); err == nil {
|
||||
t.Fatal("asNum(array) should error")
|
||||
}
|
||||
if a, err := asArr([]Value{1.0, 2.0}); err != nil || len(a) != 2 {
|
||||
t.Fatalf("asArr=%v,%v", a, err)
|
||||
}
|
||||
if _, err := asArr(3.0); err == nil {
|
||||
t.Fatal("asArr(number) should error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdxResolve(t *testing.T) {
|
||||
if k, err := idxResolve(-1, 3); err != nil || k != 2 {
|
||||
t.Fatalf("idx(-1,3)=%v,%v", k, err)
|
||||
}
|
||||
if _, err := idxResolve(3, 3); err == nil {
|
||||
t.Fatal("idx(3,3) should be out of range")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeValue(t *testing.T) {
|
||||
got := normalizeValue([]interface{}{1.0, true, []interface{}{2.0}})
|
||||
want := []Value{1.0, 1.0, []Value{2.0}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("normalize=%#v want %#v", got, want)
|
||||
}
|
||||
if normalizeValue(5) != Value(5.0) {
|
||||
t.Fatalf("normalize(int) = %#v", normalizeValue(5))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseInitialArray(t *testing.T) {
|
||||
fixed := parseInitialArray(StateVar{Type: "array", Sizing: "fixed", Capacity: 3, Initial: "[1,2]"})
|
||||
if !reflect.DeepEqual(fixed, []Value{1.0, 2.0, 0.0}) {
|
||||
t.Fatalf("fixed init = %#v", fixed)
|
||||
}
|
||||
dyn := parseInitialArray(StateVar{Type: "array", Sizing: "dynamic", Initial: "[5,6,7]"})
|
||||
if !reflect.DeepEqual(dyn, []Value{5.0, 6.0, 7.0}) {
|
||||
t.Fatalf("dynamic init = %#v", dyn)
|
||||
}
|
||||
empty := parseInitialArray(StateVar{Type: "array", Sizing: "dynamic", Initial: ""})
|
||||
if len(empty) != 0 {
|
||||
t.Fatalf("empty init = %#v", empty)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplySizing(t *testing.T) {
|
||||
capped := applySizing([]Value{1.0, 2.0, 3.0, 4.0}, StateVar{Sizing: "capped", Capacity: 2})
|
||||
if !reflect.DeepEqual(capped, []Value{3.0, 4.0}) {
|
||||
t.Fatalf("capped = %#v", capped)
|
||||
}
|
||||
fixed := applySizing([]Value{1.0}, StateVar{Sizing: "fixed", Capacity: 3})
|
||||
if !reflect.DeepEqual(fixed, []Value{1.0, 0.0, 0.0}) {
|
||||
t.Fatalf("fixed = %#v", fixed)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user