(t *testing.T)
| 559 | } |
| 560 | |
| 561 | func TestCostModelEstimateAndTrackingAlignment(t *testing.T) { |
| 562 | tests := []struct { |
| 563 | name string |
| 564 | expr string |
| 565 | envOpts []EnvOption |
| 566 | hints map[string]uint64 |
| 567 | in map[string]any |
| 568 | wantEst cost.CostEstimate |
| 569 | wantCost uint64 |
| 570 | }{ |
| 571 | { |
| 572 | name: "startsWith with custom cost model", |
| 573 | expr: `str.startsWith("prefix")`, |
| 574 | envOpts: []EnvOption{ |
| 575 | Variable("str", StringType), |
| 576 | CostModel(cost.MemberOverload(overloads.StartsWithString, |
| 577 | cost.EvalCost(cost.Scale(cost.Arg(0), 1.0)), |
| 578 | )), |
| 579 | }, |
| 580 | hints: map[string]uint64{"str": 10}, |
| 581 | in: map[string]any{"str": "prefix_hello"}, |
| 582 | wantEst: cost.FixedCostEstimate(7), |
| 583 | wantCost: 7, |
| 584 | }, |
| 585 | { |
| 586 | name: "contains and string concat", |
| 587 | expr: `str1.contains(str2 + "!")`, |
| 588 | envOpts: []EnvOption{ |
| 589 | Variable("str1", StringType), |
| 590 | Variable("str2", StringType), |
| 591 | }, |
| 592 | hints: map[string]uint64{"str1": 20, "str2": 10}, |
| 593 | in: map[string]any{"str1": "hello_world_test!", "str2": "world"}, |
| 594 | // contains costs at most ⌈20 * 11 / 100⌉ = 3 (model version 1). |
| 595 | wantEst: cost.RangedCostEstimate(4, 7), |
| 596 | wantCost: 5, |
| 597 | }, |
| 598 | { |
| 599 | name: "list all comprehension", |
| 600 | expr: `list.all(x, x > 0)`, |
| 601 | envOpts: []EnvOption{ |
| 602 | Variable("list", ListType(IntType)), |
| 603 | }, |
| 604 | hints: map[string]uint64{"list": 5}, |
| 605 | in: map[string]any{"list": []int{1, 2, 3, 4, 5}}, |
| 606 | wantEst: cost.RangedCostEstimate(2, 27), |
| 607 | wantCost: 27, |
| 608 | }, |
| 609 | { |
| 610 | name: "ternary expression", |
| 611 | expr: `cond ? str1.size() : str2.size()`, |
| 612 | envOpts: []EnvOption{ |
| 613 | Variable("cond", BoolType), |
| 614 | Variable("str1", StringType), |
| 615 | Variable("str2", StringType), |
| 616 | }, |
| 617 | hints: map[string]uint64{"str1": 10, "str2": 10}, |
| 618 | in: map[string]any{"cond": true, "str1": "abc", "str2": "def"}, |
nothing calls this directly
no test coverage detected