(t *testing.T)
| 5823 | } |
| 5824 | |
| 5825 | func TestCostModelVersion(t *testing.T) { |
| 5826 | // A model with BeforeVersion(1, ...) covers the env compiling user-supplied models at the |
| 5827 | // pinned revision rather than at the latest one. |
| 5828 | model := cost.MemberOverload(overloads.StartsWithString, |
| 5829 | cost.EvalCost(cost.Min(cost.Target(), cost.Arg(0))), |
| 5830 | cost.BeforeVersion(1, cost.EvalCost(cost.Ranged(cost.Const(1), cost.Min(cost.Target(), cost.Arg(0))))), |
| 5831 | ) |
| 5832 | const expr = `"abcdef".startsWith("abcdef")` |
| 5833 | |
| 5834 | tests := []struct { |
| 5835 | name string |
| 5836 | opts []EnvOption |
| 5837 | want cost.CostEstimate |
| 5838 | }{ |
| 5839 | { |
| 5840 | name: "latest by default", |
| 5841 | want: cost.FixedCostEstimate(6), |
| 5842 | }, |
| 5843 | { |
| 5844 | // Version 0 reports a floor of 1 regardless of the operands' lower bounds. |
| 5845 | name: "pinned to v0", |
| 5846 | opts: []EnvOption{CostModelVersion(0)}, |
| 5847 | want: cost.RangedCostEstimate(1, 6), |
| 5848 | }, |
| 5849 | { |
| 5850 | name: "pinned to v1", |
| 5851 | opts: []EnvOption{CostModelVersion(1)}, |
| 5852 | want: cost.FixedCostEstimate(6), |
| 5853 | }, |
| 5854 | } |
| 5855 | var runtimeCosts []uint64 |
| 5856 | for _, tc := range tests { |
| 5857 | t.Run(tc.name, func(t *testing.T) { |
| 5858 | env, err := NewEnv(append([]EnvOption{CostModel(model)}, tc.opts...)...) |
| 5859 | if err != nil { |
| 5860 | t.Fatalf("NewEnv() failed: %v", err) |
| 5861 | } |
| 5862 | ast, iss := env.Compile(expr) |
| 5863 | if iss.Err() != nil { |
| 5864 | t.Fatalf("env.Compile(%q) failed: %v", expr, iss.Err()) |
| 5865 | } |
| 5866 | est, err := env.EstimateCost(ast, nil) |
| 5867 | if err != nil { |
| 5868 | t.Fatalf("env.EstimateCost() failed: %v", err) |
| 5869 | } |
| 5870 | if est != tc.want { |
| 5871 | t.Errorf("env.EstimateCost() got %v, wanted %v", est, tc.want) |
| 5872 | } |
| 5873 | prg, err := env.Program(ast, CostTracking(nil)) |
| 5874 | if err != nil { |
| 5875 | t.Fatalf("env.Program() failed: %v", err) |
| 5876 | } |
| 5877 | _, det, err := prg.Eval(NoVars()) |
| 5878 | if err != nil { |
| 5879 | t.Fatalf("prg.Eval() failed: %v", err) |
| 5880 | } |
| 5881 | runtimeCosts = append(runtimeCosts, *det.ActualCost()) |
| 5882 | }) |
nothing calls this directly
no test coverage detected