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

Function TestOptionalQuantity

common/cost/optional_test.go:79–122  ·  view source on GitHub ↗

TestOptionalQuantity checks the estimate and track of Optional, and that ElemOf recovers the value an optional holds.

(t *testing.T)

Source from the content-addressed store, hash-verified

77// TestOptionalQuantity checks the estimate and track of Optional, and that ElemOf recovers the
78// value an optional holds.
79func TestOptionalQuantity(t *testing.T) {
80 val := RangedSizeEstimate(3, 5)
81 tests := []struct {
82 name string
83 version uint32
84 expr QuantityExpr
85 want SizeEstimate
86 wantElem *SizeEstimate
87 }{
88 {name: "value", version: 1, expr: Optional(Arg(0)), want: RangedSizeEstimate(4, 6), wantElem: &val},
89 {name: "empty", version: 1, expr: Optional(Const(0)), want: FixedSizeEstimate(1), wantElem: &zeroSizeEstimate},
90 {name: "element is the value", version: 1, expr: ElemOf(Optional(Arg(0))), want: val},
91 {name: "version 0 is 1", version: 0, expr: Optional(Arg(0)), want: FixedSizeEstimate(1)},
92 }
93 for _, tc := range tests {
94 t.Run(tc.name, func(t *testing.T) {
95 m := Overload("test", argumentsOnly(), EvalCost(Const(0)), ResultSize(tc.expr))
96 est := m.FunctionEstimatorWithOptions(ModelVersion(tc.version))(nil, nil, []AstNode{typedArgNode(types.StringType, val)})
97 got := *est.ResultSize
98 if got.Min != tc.want.Min || got.Max != tc.want.Max {
99 t.Errorf("estimate = [%d, %d], want [%d, %d]", got.Min, got.Max, tc.want.Min, tc.want.Max)
100 }
101 if tc.wantElem != nil && (got.Elem == nil || *got.Elem != *tc.wantElem) {
102 t.Errorf("estimate element = %v, want %v", got.Elem, *tc.wantElem)
103 }
104 })
105 }
106
107 // The track of Optional is 1 plus the value's track, and 1 at version 0.
108 m := Overload("test", argumentsOnly(), EvalCost(Optional(Arg(0))))
109 for v, want := range map[uint32]uint64{0: 1, 1: 5} {
110 if got := *m.FunctionTrackerWithOptions(ModelVersion(v))([]ref.Val{types.String("abcd")}, nil); got != want {
111 t.Errorf("Optional(Arg(0)) track at version %d = %d, want %d", v, got, want)
112 }
113 }
114
115 // ElemOf an optional tracks the value it holds, and nothing for an empty optional.
116 elem := Overload("test", argumentsOnly(), EvalCost(ElemOf(Arg(0))))
117 for val, want := range map[ref.Val]uint64{types.OptionalOf(types.String("abcd")): 4, types.OptionalNone: 0} {
118 if got := *elem.FunctionTrackerWithOptions()([]ref.Val{val}, nil); got != want {
119 t.Errorf("ElemOf(Arg(0)) track of %v = %d, want %d", val, got, want)
120 }
121 }
122}
123
124// TestStandardOptionalModels checks the cost and result size the standard models estimate for the
125// optional functions at each cost model version.

Callers

nothing calls this directly

Calls 15

StringTypeAlias · 0.92
OptionalOfFunction · 0.92
RangedSizeEstimateFunction · 0.85
ArgFunction · 0.85
ConstFunction · 0.85
ElemOfFunction · 0.85
argumentsOnlyFunction · 0.85
EvalCostFunction · 0.85
ResultSizeFunction · 0.85
ModelVersionFunction · 0.85
typedArgNodeFunction · 0.85

Tested by

no test coverage detected