diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-09-20 11:00:48 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2022-09-21 16:27:49 +0200 |
commit | 083aa54d59aecc9f9d045d4bde6cdb6c6cbe4dec (patch) | |
tree | 8c90676eb3e6cb01cf87d9ee40db4c60f14aad2c /config-model/src/test/java/com/yahoo/schema/expressiontransforms/BooleanExpressionTransformerTestCase.java | |
parent | d9db475220d68a54ba2c9f820d3bae78f80abd96 (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 'config-model/src/test/java/com/yahoo/schema/expressiontransforms/BooleanExpressionTransformerTestCase.java')
-rw-r--r-- | config-model/src/test/java/com/yahoo/schema/expressiontransforms/BooleanExpressionTransformerTestCase.java | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/config-model/src/test/java/com/yahoo/schema/expressiontransforms/BooleanExpressionTransformerTestCase.java b/config-model/src/test/java/com/yahoo/schema/expressiontransforms/BooleanExpressionTransformerTestCase.java new file mode 100644 index 00000000000..71d0657c701 --- /dev/null +++ b/config-model/src/test/java/com/yahoo/schema/expressiontransforms/BooleanExpressionTransformerTestCase.java @@ -0,0 +1,57 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.schema.expressiontransforms; + +import com.yahoo.searchlib.rankingexpression.RankingExpression; +import com.yahoo.searchlib.rankingexpression.evaluation.MapContext; +import com.yahoo.searchlib.rankingexpression.evaluation.MapTypeContext; +import com.yahoo.searchlib.rankingexpression.transform.TransformContext; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Map; + +/** + * @author bratseth + */ +public class BooleanExpressionTransformerTestCase { + + @Test + public void testTransformer() throws Exception { + assertTransformed("if (a, b, false)", "a && b"); + assertTransformed("if (a, true, b)", "a || b"); + assertTransformed("if (a, true, b + c)", "a || b + c"); + assertTransformed("if (c + a, true, b)", "c + a || b"); + assertTransformed("if (c + a, true, b + c)", "c + a || b + c"); + assertTransformed("if (a + b, true, if (c - d * e, f, false))", "a + b || c - d * e && f"); + assertTransformed("if (a, true, if (b, c, false))", "a || b && c"); + assertTransformed("if (a + b, true, if (if (c, d, false), e * f - g, false))", "a + b || c && d && e * f - g"); + assertTransformed("if(1 - 1, true, 1 - 1)", "1 - 1 || 1 - 1"); + } + + @Test + public void testIt() throws Exception { + assertTransformed("if(1 - 1, true, 1 - 1)", "1 - 1 || 1 - 1"); + } + + private void assertTransformed(String expected, String input) throws Exception { + var transformedExpression = new BooleanExpressionTransformer() + .transform(new RankingExpression(input), + new TransformContext(Map.of(), new MapTypeContext())); + + assertEquals(new RankingExpression(expected), transformedExpression, "Transformed as expected"); + + MapContext context = contextWithSingleLetterVariables(); + var inputExpression = new RankingExpression(input); + assertEquals(inputExpression.evaluate(new MapContext()).asBoolean(), + transformedExpression.evaluate(new MapContext()).asBoolean(), + "Transform and original input are equivalent"); + } + + private MapContext contextWithSingleLetterVariables() { + var context = new MapContext(); + for (int i = 0; i < 26; i++) + context.put(Character.toString(i + 97), Math.floorMod(i, 2)); + return context; + } + +} |