a6fa4e7c7c
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package controllogic
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestCompileInitsLocalsFromDecls(t *testing.T) {
|
|
g := Graph{
|
|
ID: "g", Name: "n",
|
|
StateVars: []StateVar{
|
|
{Name: "count", Type: "number", Initial: "7"},
|
|
{Name: "flag", Type: "bool", Initial: "true"},
|
|
{Name: "buf", Type: "array", Initial: "[1,2,3]", Sizing: "capped", Capacity: 4},
|
|
},
|
|
}
|
|
cg := compile(g)
|
|
if got := cg.getLocal("count"); got != Value(7.0) {
|
|
t.Fatalf("count = %#v", got)
|
|
}
|
|
if got := cg.getLocal("flag"); got != Value(1.0) {
|
|
t.Fatalf("flag = %#v", got)
|
|
}
|
|
if got := cg.getLocal("buf"); !reflect.DeepEqual(got, []Value{1.0, 2.0, 3.0}) {
|
|
t.Fatalf("buf = %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestSetLocalAppliesSizing(t *testing.T) {
|
|
g := Graph{ID: "g", Name: "n", StateVars: []StateVar{
|
|
{Name: "buf", Type: "array", Initial: "[]", Sizing: "capped", Capacity: 2},
|
|
}}
|
|
cg := compile(g)
|
|
cg.setLocal("buf", []Value{1.0, 2.0, 3.0, 4.0})
|
|
if got := cg.getLocal("buf"); !reflect.DeepEqual(got, []Value{3.0, 4.0}) {
|
|
t.Fatalf("sized buf = %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestResolverReturnsLocalValue(t *testing.T) {
|
|
g := Graph{ID: "g", Name: "n", StateVars: []StateVar{
|
|
{Name: "buf", Type: "array", Initial: "[10,20]", Sizing: "dynamic"},
|
|
}}
|
|
cg := compile(g)
|
|
R := func(ds, name string) Value {
|
|
if ds == "local" {
|
|
return cg.getLocal(name)
|
|
}
|
|
return 0.0
|
|
}
|
|
if v := EvalExpr("buf[1]", R); v != 20 {
|
|
t.Fatalf("buf[1] = %v", v)
|
|
}
|
|
if v := EvalExpr("sum(buf)", R); v != 30 {
|
|
t.Fatalf("sum(buf) = %v", v)
|
|
}
|
|
}
|