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

Function TestCostModelVersion

cel/cel_test.go:5825–5891  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

5823}
5824
5825func TestCostModelVersion(t *testing.T) {
5826 // A model with BeforeVersion(1, ...) covers the env compiling user-supplied models at the
5827 // pinned revision rather than at the latest one.
5828 model := cost.MemberOverload(overloads.StartsWithString,
5829 cost.EvalCost(cost.Min(cost.Target(), cost.Arg(0))),
5830 cost.BeforeVersion(1, cost.EvalCost(cost.Ranged(cost.Const(1), cost.Min(cost.Target(), cost.Arg(0))))),
5831 )
5832 const expr = `"abcdef".startsWith("abcdef")`
5833
5834 tests := []struct {
5835 name string
5836 opts []EnvOption
5837 want cost.CostEstimate
5838 }{
5839 {
5840 name: "latest by default",
5841 want: cost.FixedCostEstimate(6),
5842 },
5843 {
5844 // Version 0 reports a floor of 1 regardless of the operands' lower bounds.
5845 name: "pinned to v0",
5846 opts: []EnvOption{CostModelVersion(0)},
5847 want: cost.RangedCostEstimate(1, 6),
5848 },
5849 {
5850 name: "pinned to v1",
5851 opts: []EnvOption{CostModelVersion(1)},
5852 want: cost.FixedCostEstimate(6),
5853 },
5854 }
5855 var runtimeCosts []uint64
5856 for _, tc := range tests {
5857 t.Run(tc.name, func(t *testing.T) {
5858 env, err := NewEnv(append([]EnvOption{CostModel(model)}, tc.opts...)...)
5859 if err != nil {
5860 t.Fatalf("NewEnv() failed: %v", err)
5861 }
5862 ast, iss := env.Compile(expr)
5863 if iss.Err() != nil {
5864 t.Fatalf("env.Compile(%q) failed: %v", expr, iss.Err())
5865 }
5866 est, err := env.EstimateCost(ast, nil)
5867 if err != nil {
5868 t.Fatalf("env.EstimateCost() failed: %v", err)
5869 }
5870 if est != tc.want {
5871 t.Errorf("env.EstimateCost() got %v, wanted %v", est, tc.want)
5872 }
5873 prg, err := env.Program(ast, CostTracking(nil))
5874 if err != nil {
5875 t.Fatalf("env.Program() failed: %v", err)
5876 }
5877 _, det, err := prg.Eval(NoVars())
5878 if err != nil {
5879 t.Fatalf("prg.Eval() failed: %v", err)
5880 }
5881 runtimeCosts = append(runtimeCosts, *det.ActualCost())
5882 })

Callers

nothing calls this directly

Calls 15

CompileMethod · 0.95
EstimateCostMethod · 0.95
ProgramMethod · 0.95
MemberOverloadFunction · 0.92
EvalCostFunction · 0.92
MinFunction · 0.92
TargetFunction · 0.92
ArgFunction · 0.92
BeforeVersionFunction · 0.92
RangedFunction · 0.92
ConstFunction · 0.92
FixedCostEstimateFunction · 0.92

Tested by

no test coverage detected