aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/test
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2022-09-20 11:00:48 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2022-09-21 16:27:49 +0200
commit083aa54d59aecc9f9d045d4bde6cdb6c6cbe4dec (patch)
tree8c90676eb3e6cb01cf87d9ee40db4c60f14aad2c /searchlib/src/test
parentd9db475220d68a54ba2c9f820d3bae78f80abd96 (diff)
Short circuit boolean expressions
Short circuit boolean expressions by converting them to (nested) if expressions. This also fixes a bug in Java expression evaluation where evaluation of arithmetic operations with the same precedence would be from right to left rather than left to right.
Diffstat (limited to 'searchlib/src/test')
-rw-r--r--searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/EvaluationTestCase.java12
1 files changed, 11 insertions, 1 deletions
diff --git a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/EvaluationTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/EvaluationTestCase.java
index 19e32c23234..ad50a423eb9 100644
--- a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/EvaluationTestCase.java
+++ b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/EvaluationTestCase.java
@@ -56,6 +56,15 @@ public class EvaluationTestCase {
}
@Test
+ public void testEvaluationOrder() {
+ EvaluationTester tester = new EvaluationTester();
+ tester.assertEvaluates(-4, "1 + -2 + -3");
+ tester.assertEvaluates(2, "1 - (2 - 3)");
+ tester.assertEvaluates(-4, "(1 - 2) - 3");
+ tester.assertEvaluates(-4, "1 - 2 - 3");
+ }
+
+ @Test
public void testEvaluation() {
EvaluationTester tester = new EvaluationTester();
tester.assertEvaluates(0.5, "0.5");
@@ -78,6 +87,7 @@ public class EvaluationTestCase {
tester.assertEvaluates(3, "1 + 10 % 6 / 2");
tester.assertEvaluates(10.0, "3 ^ 2 + 1");
tester.assertEvaluates(18.0, "2 * 3 ^ 2");
+ tester.assertEvaluates(-4, "1 - 2 - 3"); // Means 1 + -2 + -3
// Conditionals
tester.assertEvaluates(2 * (3 * 4 + 3) * (4 * 5 - 4 * 200) / 10, "2*(3*4+3)*(4*5-4*200)/10");
@@ -106,7 +116,7 @@ public class EvaluationTestCase {
// Conditionals with branch probabilities
RankingExpression e = tester.assertEvaluates(3.5, "if(1.0-1.0, 2.5, 3.5, 0.3)");
- assertEquals(0.3d, (double)((IfNode) e.getRoot()).getTrueProbability(), tolerance);
+ assertEquals(0.3d, ((IfNode) e.getRoot()).getTrueProbability(), tolerance);
// Conditionals as expressions
tester.assertEvaluates(new BooleanValue(true), "2<3");