(t *testing.T)
| 25 | ) |
| 26 | |
| 27 | func TestCostModelTrackerOptions(t *testing.T) { |
| 28 | tests := []struct { |
| 29 | name string |
| 30 | expr string |
| 31 | envOpts []EnvOption |
| 32 | in any |
| 33 | wantResult ref.Val |
| 34 | wantCost uint64 |
| 35 | }{ |
| 36 | { |
| 37 | name: "custom_global_function_cost_model", |
| 38 | expr: `custom_len(str)`, |
| 39 | envOpts: []EnvOption{ |
| 40 | Variable("str", StringType), |
| 41 | Function("custom_len", |
| 42 | Overload("custom_len_string", []*Type{StringType}, IntType, |
| 43 | UnaryBinding(func(val ref.Val) ref.Val { |
| 44 | return types.Int(len(val.Value().(string))) |
| 45 | }), |
| 46 | ), |
| 47 | ), |
| 48 | CostModel( |
| 49 | cost.Overload("custom_len_string", |
| 50 | cost.EvalCost(cost.Const(42)), |
| 51 | ), |
| 52 | ), |
| 53 | }, |
| 54 | in: map[string]any{"str": "hello"}, |
| 55 | wantResult: types.Int(5), |
| 56 | // 1 for variable 'str' + 42 for custom_len |
| 57 | wantCost: 43, |
| 58 | }, |
| 59 | { |
| 60 | name: "custom_member_function_cost_model", |
| 61 | expr: `str.custom_transform("prefix_")`, |
| 62 | envOpts: []EnvOption{ |
| 63 | Variable("str", StringType), |
| 64 | Function("custom_transform", |
| 65 | MemberOverload("string_custom_transform_string", []*Type{StringType, StringType}, StringType, |
| 66 | BinaryBinding(func(target, arg ref.Val) ref.Val { |
| 67 | return types.String(arg.Value().(string) + target.Value().(string)) |
| 68 | }), |
| 69 | ), |
| 70 | ), |
| 71 | CostModel( |
| 72 | cost.MemberOverload("string_custom_transform_string", |
| 73 | cost.EvalCost(cost.Sum(cost.Scale(cost.Target(), 2.0), cost.Scale(cost.Arg(0), 1.0))), |
| 74 | ), |
| 75 | ), |
| 76 | }, |
| 77 | in: map[string]any{"str": "abc"}, |
| 78 | wantResult: types.String("prefix_abc"), |
| 79 | // 1 for variable 'str' + target size (3)*2 + arg0 size (7)*1 = 1 + 6 + 7 = 14 |
| 80 | wantCost: 14, |
| 81 | }, |
| 82 | { |
| 83 | name: "override_standard_overload_cost_model", |
| 84 | expr: `str.startsWith("prefix")`, |
nothing calls this directly
no test coverage detected