TestStandardOptionalModels checks the cost and result size the standard models estimate for the optional functions at each cost model version.
(t *testing.T)
| 124 | // TestStandardOptionalModels checks the cost and result size the standard models estimate for the |
| 125 | // optional functions at each cost model version. |
| 126 | func TestStandardOptionalModels(t *testing.T) { |
| 127 | value := typedArgNode(types.StringType, RangedSizeEstimate(3, 5)) |
| 128 | alt := typedArgNode(types.StringType, FixedSizeEstimate(7)) |
| 129 | held := RangedSizeEstimate(3, 5) |
| 130 | optional := optionalSizeOf(held) |
| 131 | optTarget := typedArgNode(types.NewOptionalType(types.StringType), optional) |
| 132 | optAlt := typedArgNode(types.NewOptionalType(types.StringType), optionalSizeOf(FixedSizeEstimate(7))) |
| 133 | tests := []struct { |
| 134 | overload string |
| 135 | target AstNode |
| 136 | args []AstNode |
| 137 | version uint32 |
| 138 | wantCost CostEstimate |
| 139 | // wantSize is nil where the model reports no result size. |
| 140 | wantSize *SizeEstimate |
| 141 | }{ |
| 142 | {overload: overloads.OptionalNone, version: 1, wantCost: FixedCostEstimate(1), wantSize: ptr(FixedSizeEstimate(1))}, |
| 143 | {overload: overloads.OptionalNone, version: 0, wantCost: FixedCostEstimate(1)}, |
| 144 | {overload: overloads.OptionalOf, args: []AstNode{value}, version: 1, wantCost: FixedCostEstimate(1), wantSize: ptr(RangedSizeEstimate(4, 6))}, |
| 145 | {overload: overloads.OptionalOf, args: []AstNode{value}, version: 0, wantCost: FixedCostEstimate(1)}, |
| 146 | // ofNonZeroValue may be empty, so its value may be 0. |
| 147 | {overload: overloads.OptionalOfNonZeroValue, args: []AstNode{value}, version: 1, wantCost: FixedCostEstimate(1), wantSize: ptr(RangedSizeEstimate(1, 6))}, |
| 148 | {overload: overloads.OptionalValue, target: optTarget, version: 1, wantCost: FixedCostEstimate(1), wantSize: &held}, |
| 149 | {overload: overloads.OptionalValue, target: optTarget, version: 0, wantCost: FixedCostEstimate(1)}, |
| 150 | // orValue returns the receiver's value or the alternative; or returns either optional. |
| 151 | {overload: overloads.OptionalOrValueValue, target: optTarget, args: []AstNode{alt}, version: 1, wantCost: FixedCostEstimate(0), wantSize: ptr(RangedSizeEstimate(3, 7))}, |
| 152 | {overload: overloads.OptionalOrOptional, target: optTarget, args: []AstNode{optAlt}, version: 1, wantCost: FixedCostEstimate(0), wantSize: ptr(RangedSizeEstimate(4, 8))}, |
| 153 | // Before version 1 these were unmodeled calls, which v0.32.0 estimated at 1. |
| 154 | {overload: overloads.OptionalOrValueValue, target: optTarget, args: []AstNode{alt}, version: 0, wantCost: FixedCostEstimate(1)}, |
| 155 | {overload: overloads.OptionalOrOptional, target: optTarget, args: []AstNode{optAlt}, version: 0, wantCost: FixedCostEstimate(1)}, |
| 156 | } |
| 157 | for _, tc := range tests { |
| 158 | t.Run(fmt.Sprintf("%s/v%d", tc.overload, tc.version), func(t *testing.T) { |
| 159 | var target *AstNode |
| 160 | if tc.target != nil { |
| 161 | target = &tc.target |
| 162 | } |
| 163 | est := StandardOverloadEstimatorsWithOptions(ModelVersion(tc.version))[tc.overload](nil, target, tc.args) |
| 164 | if est.CostEstimate != tc.wantCost { |
| 165 | t.Errorf("cost = %v, want %v", est.CostEstimate, tc.wantCost) |
| 166 | } |
| 167 | switch { |
| 168 | case tc.wantSize == nil && est.ResultSize != nil: |
| 169 | t.Errorf("result size = %v, want none", *est.ResultSize) |
| 170 | case tc.wantSize != nil && est.ResultSize == nil: |
| 171 | t.Errorf("result size = none, want [%d, %d]", tc.wantSize.Min, tc.wantSize.Max) |
| 172 | case tc.wantSize != nil && (est.ResultSize.Min != tc.wantSize.Min || est.ResultSize.Max != tc.wantSize.Max): |
| 173 | t.Errorf("result size = [%d, %d], want [%d, %d]", est.ResultSize.Min, est.ResultSize.Max, tc.wantSize.Min, tc.wantSize.Max) |
| 174 | } |
| 175 | }) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func ptr[T any](v T) *T { |
| 180 | return &v |
nothing calls this directly
no test coverage detected