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

Function TestComparisonEstimate

common/cost/optional_test.go:189–220  ·  view source on GitHub ↗

TestComparisonEstimate checks the estimate of a comparison at each cost model version. Version 0 keeps the v0.32.0 estimate, ceil(0.1 * [m, min(lhs.Max, rhs.Max)]) where m is 1 when the smaller maximum is non-zero. From version 1 the estimate is the model's, max(1, ceil(0.1 * [min(lhs.Min, rhs.Min)

(t *testing.T)

Source from the content-addressed store, hash-verified

187// max(1, ceil(0.1 * [min(lhs.Min, rhs.Min), min(lhs.Max, rhs.Max)])), saturating for an unknown
188// size: a comparison costs at least minCallCost, even of empty operands.
189func TestComparisonEstimate(t *testing.T) {
190 unknown := UnknownSizeEstimate()
191 scaledUnknown := FixedCostEstimate(math.MaxUint64).MultiplyByCostFactor(StringTraversalCostFactor).Max
192 tests := []struct {
193 name string
194 lhs, rhs SizeEstimate
195 wantV0 CostEstimate
196 wantLatest CostEstimate
197 }{
198 {name: "possibly empty operand", lhs: RangedSizeEstimate(0, 20), rhs: FixedSizeEstimate(2),
199 wantV0: FixedCostEstimate(1), wantLatest: FixedCostEstimate(1)},
200 {name: "long operands", lhs: RangedSizeEstimate(17, 34), rhs: FixedSizeEstimate(11),
201 wantV0: RangedCostEstimate(1, 2), wantLatest: FixedCostEstimate(2)},
202 {name: "empty operands", lhs: FixedSizeEstimate(0), rhs: FixedSizeEstimate(0),
203 wantV0: FixedCostEstimate(0), wantLatest: FixedCostEstimate(1)},
204 {name: "unknown operands", lhs: unknown, rhs: unknown,
205 wantV0: RangedCostEstimate(1, scaledUnknown), wantLatest: RangedCostEstimate(1, math.MaxUint64)},
206 }
207 for _, overload := range []string{overloads.Equals, overloads.NotEquals, overloads.LessString, overloads.GreaterEqualsBytes} {
208 for _, tc := range tests {
209 t.Run(overload+"/"+tc.name, func(t *testing.T) {
210 args := []AstNode{typedArgNode(types.StringType, tc.lhs), typedArgNode(types.StringType, tc.rhs)}
211 for version, want := range map[uint32]CostEstimate{0: tc.wantV0, 1: tc.wantLatest} {
212 est := StandardOverloadEstimatorsWithOptions(ModelVersion(version))[overload](nil, nil, args)
213 if est.CostEstimate != want {
214 t.Errorf("version %d estimate = %v, want %v", version, est.CostEstimate, want)
215 }
216 }
217 })
218 }
219 }
220}

Callers

nothing calls this directly

Calls 9

RangedSizeEstimateFunction · 0.85
RangedCostEstimateFunction · 0.85
typedArgNodeFunction · 0.85
ModelVersionFunction · 0.85
UnknownSizeEstimateFunction · 0.70
FixedCostEstimateFunction · 0.70
FixedSizeEstimateFunction · 0.70
MultiplyByCostFactorMethod · 0.45

Tested by

no test coverage detected