From a7e3b68ccf9428288f1346a820ec5e0f5467fd16 Mon Sep 17 00:00:00 2001 From: Tristan Swadell Date: Mon, 27 Mar 2023 16:26:06 -0700 Subject: [PATCH] Additional coverage for static and runtime cost computations (#671) --- checker/cost_test.go | 22 +++++++++++++++++++++ interpreter/runtimecost_test.go | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/checker/cost_test.go b/checker/cost_test.go index 8948d690..20feff26 100644 --- a/checker/cost_test.go +++ b/checker/cost_test.go @@ -134,11 +134,33 @@ func TestCost(t *testing.T) { expr: `true || false`, wanted: zeroCost, }, + { + name: "or accumulated branch cost", + expr: `a || b || c || d`, + decls: []*exprpb.Decl{ + decls.NewVar("a", decls.Bool), + decls.NewVar("b", decls.Bool), + decls.NewVar("c", decls.Bool), + decls.NewVar("d", decls.Bool), + }, + wanted: CostEstimate{Min: 1, Max: 4}, + }, { name: "and", expr: `true && false`, wanted: zeroCost, }, + { + name: "and accumulated branch cost", + expr: `a && b && c && d`, + decls: []*exprpb.Decl{ + decls.NewVar("a", decls.Bool), + decls.NewVar("b", decls.Bool), + decls.NewVar("c", decls.Bool), + decls.NewVar("d", decls.Bool), + }, + wanted: CostEstimate{Min: 1, Max: 4}, + }, { name: "lt", expr: `1 < 2`, diff --git a/interpreter/runtimecost_test.go b/interpreter/runtimecost_test.go index ec128410..b8d0fc5e 100644 --- a/interpreter/runtimecost_test.go +++ b/interpreter/runtimecost_test.go @@ -398,6 +398,24 @@ func TestRuntimeCost(t *testing.T) { expr: `true || false`, want: 0, }, + + { + name: "or accumulated branch cost", + expr: `a || b || c || d`, + decls: []*exprpb.Decl{ + decls.NewVar("a", decls.Bool), + decls.NewVar("b", decls.Bool), + decls.NewVar("c", decls.Bool), + decls.NewVar("d", decls.Bool), + }, + in: map[string]any{ + "a": false, + "b": false, + "c": false, + "d": false, + }, + want: 4, + }, { name: "and", expr: `true && false`, @@ -408,6 +426,23 @@ func TestRuntimeCost(t *testing.T) { expr: `false && true`, want: 0, }, + { + name: "and accumulated branch cost", + expr: `a && b && c && d`, + decls: []*exprpb.Decl{ + decls.NewVar("a", decls.Bool), + decls.NewVar("b", decls.Bool), + decls.NewVar("c", decls.Bool), + decls.NewVar("d", decls.Bool), + }, + in: map[string]any{ + "a": true, + "b": true, + "c": true, + "d": true, + }, + want: 4, + }, { name: "lt", expr: `1 < 2`,