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

Function TestEstimateCostAndRuntimeCost

cel/cel_test.go:1892–1985  ·  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

1890
1891// TestEstimateCostAndRuntimeCost sanity checks that the cost systems are usable from the program API.
1892func TestEstimateCostAndRuntimeCost(t *testing.T) {
1893 intList := ListType(IntType)
1894 zeroCost := checker.CostEstimate{}
1895 cases := []struct {
1896 name string
1897 expr string
1898 decls []EnvOption
1899 hints map[string]uint64
1900 want checker.CostEstimate
1901 in any
1902 }{
1903 {
1904 name: "const",
1905 expr: `"Hello World!"`,
1906 want: zeroCost,
1907 in: map[string]any{},
1908 },
1909 {
1910 name: "identity",
1911 expr: `input`,
1912 decls: []EnvOption{Variable("input", intList)},
1913 want: checker.CostEstimate{Min: 1, Max: 1},
1914 in: map[string]any{"input": []int{1, 2}},
1915 },
1916 {
1917 name: "str concat",
1918 expr: `"abcdefg".contains(str1 + str2)`,
1919 decls: []EnvOption{
1920 Variable("str1", StringType),
1921 Variable("str2", StringType),
1922 },
1923 hints: map[string]uint64{"str1": 10, "str2": 10},
1924 want: checker.CostEstimate{Min: 2, Max: 6},
1925 in: map[string]any{"str1": "val1111111", "str2": "val2222222"},
1926 },
1927 }
1928
1929 for _, tst := range cases {
1930 tc := tst
1931 t.Run(tc.name, func(t *testing.T) {
1932 t.Parallel()
1933 if tc.hints == nil {
1934 tc.hints = map[string]uint64{}
1935 }
1936 envOpts := []EnvOption{
1937 CostEstimatorOptions(
1938 checker.OverloadCostEstimate(overloads.TimestampToYear, estimateTimestampToYear),
1939 ),
1940 }
1941 envOpts = append(envOpts, tc.decls...)
1942 env := testEnv(t, envOpts...)
1943 ast, iss := env.Compile(tc.expr)
1944 if iss.Err() != nil {
1945 t.Fatalf("env.Compile(%v) failed: %v", tc.expr, iss.Err())
1946 }
1947 est, err := env.EstimateCost(ast, testCostEstimator{hints: tc.hints})
1948 if err != nil {
1949 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