summaryrefslogtreecommitdiffstats
path: root/searchlib/src/test/java/com/yahoo
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@oath.com>2018-02-01 12:53:32 +0100
committerJon Bratseth <bratseth@oath.com>2018-02-01 12:53:32 +0100
commit237dfb95f61f572bcc45cf98fcb2c1b3af473cac (patch)
tree3efc15c5c054aba38e718e27ec4c70fe04ea5263 /searchlib/src/test/java/com/yahoo
parent97a57faf30866ff14d2bb35b5b58ba4e88e64c9f (diff)
Allow type generalizations in if
Diffstat (limited to 'searchlib/src/test/java/com/yahoo')
-rw-r--r--searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/EvaluationTestCase.java1
-rw-r--r--searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/TypeResolutionTestCase.java54
2 files changed, 55 insertions, 0 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 3aa2d144f1f..6c7643b37b3 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
@@ -10,6 +10,7 @@ import com.yahoo.searchlib.rankingexpression.rule.ArithmeticOperator;
import com.yahoo.searchlib.rankingexpression.rule.ConstantNode;
import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode;
import com.yahoo.searchlib.rankingexpression.rule.IfNode;
+import com.yahoo.tensor.TensorType;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
diff --git a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/TypeResolutionTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/TypeResolutionTestCase.java
new file mode 100644
index 00000000000..d1ea0fcf2e4
--- /dev/null
+++ b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/TypeResolutionTestCase.java
@@ -0,0 +1,54 @@
+// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+package com.yahoo.searchlib.rankingexpression.evaluation;
+
+import com.yahoo.searchlib.rankingexpression.RankingExpression;
+import com.yahoo.searchlib.rankingexpression.parser.ParseException;
+import com.yahoo.tensor.TensorType;
+import com.yahoo.tensor.evaluation.TypeContext;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/**
+ * @author bratseth
+ */
+public class TypeResolutionTestCase {
+
+ @Test
+ public void testTypeResolution() {
+ TypeMapContext context = new TypeMapContext();
+ context.setType("query('x1')", TensorType.fromSpec("tensor(x[])"));
+ context.setType("query('x2')", TensorType.fromSpec("tensor(x[10])"));
+ context.setType("query('y1')", TensorType.fromSpec("tensor(y[])"));
+
+ assertType("tensor(x[])", "query(x1)", context);
+ assertType("tensor(x[])", "if (1>0, query(x1), query(x2))", context);
+ assertIncompatibleType("if (1>0, query(x1), query(y1))", context);
+ }
+
+ private void assertType(String type, String expression, TypeContext context) {
+ try {
+ assertEquals(TensorType.fromSpec(type), new RankingExpression(expression).type(context));
+ }
+ catch (ParseException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private void assertIncompatibleType(String expression, TypeContext context) {
+ try {
+ new RankingExpression(expression).type(context);
+ fail("Expected type incompatibility exception");
+ }
+ catch (IllegalArgumentException expected) {
+ assertEquals("An if expression must produce compatible types in both alternatives, but the 'true' type is tensor(x[]) while the 'false' type is tensor(y[])",
+ expected.getMessage());
+ }
+ catch (ParseException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}