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

Function TestEstimateCostAndRuntimeCost

cel/cel_test.go:1788–1881  ·  view source on GitHub ↗

TestEstimateCostAndRuntimeCost sanity checks that the cost systems are usable from the program API.

(t *testing.T)

Source from the content-addressed store, hash-verified

1786
1787// TestEstimateCostAndRuntimeCost sanity checks that the cost systems are usable from the program API.
1788func TestEstimateCostAndRuntimeCost(t *testing.T) {
1789 intList := ListType(IntType)
1790 zeroCost := checker.CostEstimate{}
1791 cases := []struct {
1792 name string
1793 expr string
1794 decls []EnvOption
1795 hints map[string]uint64
1796 want checker.CostEstimate
1797 in any
1798 }{
1799 {
1800 name: "const",
1801 expr: `"Hello World!"`,
1802 want: zeroCost,
1803 in: map[string]any{},
1804 },
1805 {
1806 name: "identity",
1807 expr: `input`,
1808 decls: []EnvOption{Variable("input", intList)},
1809 want: checker.CostEstimate{Min: 1, Max: 1},
1810 in: map[string]any{"input": []int{1, 2}},
1811 },
1812 {
1813 name: "str concat",
1814 expr: `"abcdefg".contains(str1 + str2)`,
1815 decls: []EnvOption{
1816 Variable("str1", StringType),
1817 Variable("str2", StringType),
1818 },
1819 hints: map[string]uint64{"str1": 10, "str2": 10},
1820 want: checker.CostEstimate{Min: 2, Max: 6},
1821 in: map[string]any{"str1": "val1111111", "str2": "val2222222"},
1822 },
1823 }
1824
1825 for _, tst := range cases {
1826 tc := tst
1827 t.Run(tc.name, func(t *testing.T) {
1828 t.Parallel()
1829 if tc.hints == nil {
1830 tc.hints = map[string]uint64{}
1831 }
1832 envOpts := []EnvOption{
1833 CostEstimatorOptions(
1834 checker.OverloadCostEstimate(overloads.TimestampToYear, estimateTimestampToYear),
1835 ),
1836 }
1837 envOpts = append(envOpts, tc.decls...)
1838 env := testEnv(t, envOpts...)
1839 ast, iss := env.Compile(tc.expr)
1840 if iss.Err() != nil {
1841 t.Fatalf("env.Compile(%v) failed: %v", tc.expr, iss.Err())
1842 }
1843 est, err := env.EstimateCost(ast, testCostEstimator{hints: tc.hints})
1844 if err != nil {
1845 t.Fatalf("Env.EstimateCost(ast *Ast, estimator checker.CostEstimator) failed to estimate cost: %s\n", err)

Callers

nothing calls this directly

Calls 14

OverloadCostEstimateFunction · 0.92
OverloadCostTrackerFunction · 0.92
CostEstimatorOptionsFunction · 0.85
CostTrackingFunction · 0.85
CostTrackerOptionsFunction · 0.85
ErrMethod · 0.80
EstimateCostMethod · 0.80
CheckMethod · 0.80
ProgramMethod · 0.80
VariableFunction · 0.70
testEnvFunction · 0.70
CompileMethod · 0.65

Tested by

no test coverage detected