MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestCostModelValueProjections

Function TestCostModelValueProjections

cel/cel_test.go:5793–5823  ·  view source on GitHub ↗

TestCostModelValueProjections charges an element projection through a real evaluation. The two lists have the same length and the same number of literals, so every other term in the total is equal and the difference is the projection reading the elements themselves.

(t *testing.T)

Source from the content-addressed store, hash-verified

5791// lists have the same length and the same number of literals, so every other term in the total is
5792// equal and the difference is the projection reading the elements themselves.
5793func TestCostModelValueProjections(t *testing.T) {
5794 env, err := NewEnv(CostModel(
5795 cost.Overload(overloads.InList, cost.EvalCost(cost.ElemTotal(cost.Arg(1)))),
5796 ))
5797 if err != nil {
5798 t.Fatalf("NewEnv() failed: %v", err)
5799 }
5800 costOf := func(expr string) uint64 {
5801 t.Helper()
5802 ast, iss := env.Compile(expr)
5803 if iss.Err() != nil {
5804 t.Fatalf("env.Compile(%q) failed: %v", expr, iss.Err())
5805 }
5806 prg, err := env.Program(ast, CostTracking(nil))
5807 if err != nil {
5808 t.Fatalf("env.Program() failed: %v", err)
5809 }
5810 _, det, err := prg.Eval(NoVars())
5811 if err != nil {
5812 t.Fatalf("prg.Eval() failed: %v", err)
5813 }
5814 return *det.ActualCost()
5815 }
5816 // Total element sizes of 7 and 3 respectively.
5817 large := costOf(`'zz' in ['a', 'bbbb', 'cc']`)
5818 small := costOf(`'zz' in ['a', 'b', 'c']`)
5819 if large-small != 4 {
5820 t.Errorf("element projection charged %d - %d = %d, wanted a difference of 4",
5821 large, small, large-small)
5822 }
5823}
5824
5825func TestCostModelVersion(t *testing.T) {
5826 // A model with BeforeVersion(1, ...) covers the env compiling user-supplied models at the

Callers

nothing calls this directly

Calls 13

CompileMethod · 0.95
ProgramMethod · 0.95
OverloadFunction · 0.92
EvalCostFunction · 0.92
ElemTotalFunction · 0.92
ArgFunction · 0.92
CostModelFunction · 0.85
CostTrackingFunction · 0.85
NoVarsFunction · 0.85
ErrMethod · 0.80
NewEnvFunction · 0.70
EvalMethod · 0.65

Tested by

no test coverage detected