diff options
author | Jon Bratseth <bratseth@oath.com> | 2018-04-30 15:12:24 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@oath.com> | 2018-04-30 15:12:24 +0200 |
commit | b2ce89c7d12d0b77d7aaf273c8f4c0516ded7bd5 (patch) | |
tree | f37f50633caa356d64ddb7b7e2cc6571661e2394 /searchlib/src/test/java/com | |
parent | 2b18385e21151985edb7a9d0a85daa2332daa4a5 (diff) |
Remove usage of junit.framework
Diffstat (limited to 'searchlib/src/test/java/com')
15 files changed, 357 insertions, 197 deletions
diff --git a/searchlib/src/test/java/com/yahoo/searchlib/aggregation/AggregationTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/aggregation/AggregationTestCase.java index 1c0212babf8..cbd6c02cd26 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/aggregation/AggregationTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/aggregation/AggregationTestCase.java @@ -9,13 +9,19 @@ import com.yahoo.vespa.objects.BufferSerializer; import com.yahoo.vespa.objects.Identifiable; import com.yahoo.vespa.objects.ObjectOperation; import com.yahoo.vespa.objects.ObjectPredicate; -import junit.framework.TestCase; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; /** * @author baldersheim */ -public class AggregationTestCase extends TestCase { +public class AggregationTestCase { + + private static final double delta = 0.0000000001; + @Test public void testSumAggregationResult() { SumAggregationResult a = new SumAggregationResult(); a.setExpression(new AttributeNode("attributeA")); @@ -27,6 +33,7 @@ public class AggregationTestCase extends TestCase { assertEquals(b.getSum().getInteger(), 14); } + @Test public void testXorAggregationResult() { XorAggregationResult a = new XorAggregationResult(6); a.setExpression(new AttributeNode("attributeA")); @@ -39,6 +46,7 @@ public class AggregationTestCase extends TestCase { assertEquals(b.getXor(), 0); } + @Test public void testCountAggregationResult() { CountAggregationResult a = new CountAggregationResult(6); a.setExpression(new AttributeNode("attributeA")); @@ -51,6 +59,7 @@ public class AggregationTestCase extends TestCase { assertEquals(b.getCount(), 14); } + @Test public void testMinAggregationResult() { MinAggregationResult a = new MinAggregationResult(new IntegerResultNode(6)); a.setExpression(new AttributeNode("attributeA")); @@ -64,6 +73,7 @@ public class AggregationTestCase extends TestCase { assertEquals(b.getMin().getInteger(), 6); } + @Test public void testMaxAggregationResult() { MaxAggregationResult a = new MaxAggregationResult(new IntegerResultNode(6)); a.setExpression(new AttributeNode("attributeA")); @@ -77,6 +87,7 @@ public class AggregationTestCase extends TestCase { assertEquals(b.getMax().getInteger(), 7); } + @Test public void testAverageAggregationResult() { AverageAggregationResult a = new AverageAggregationResult(new FloatResultNode(72), 6); a.setExpression(new AttributeNode("attributeA")); @@ -99,6 +110,7 @@ public class AggregationTestCase extends TestCase { return new GlobalId((new DocumentId("doc:test:" + docId)).getGlobalId()); } + @Test public void testFs4HitsAggregationResult() { double rank1 = 1; double rank2 = 2; @@ -118,10 +130,10 @@ public class AggregationTestCase extends TestCase { assertEquals(a, b); a.postMerge(); assertEquals(2, a.getHits().size()); - assertEquals(2.0, a.getHits().get(0).getRank()); + assertEquals(2.0, a.getHits().get(0).getRank(), delta); a.setMaxHits(1).postMerge(); assertEquals(1, a.getHits().size()); - assertEquals(2.0, a.getHits().get(0).getRank()); + assertEquals(2.0, a.getHits().get(0).getRank(), delta); HitsAggregationResult hits = new HitsAggregationResult(3) .addHit(new FS4Hit(1, createGlobalId(3), 1)) @@ -160,6 +172,7 @@ public class AggregationTestCase extends TestCase { assertFS4Hits(request, 4, 5, 0); } + @Test public void testVdsHitsAggregationResult() { double rank1 = 1; double rank2 = 2; @@ -176,8 +189,6 @@ public class AggregationTestCase extends TestCase { assertEquals(0, a.getHits().size()); a.setExpression(new AttributeNode("attributeA")); a.addHit(new VdsHit("1", s2, rank1)); -// a.addHit(new VdsHit("5", s7, rank2)); -// assertEquals(2, a.getHits().size()); HitsAggregationResult b = (HitsAggregationResult)serializeDeserialize(a); assertEquals(a, b); @@ -218,7 +229,6 @@ public class AggregationTestCase extends TestCase { assertVdsHits(request, 4, 5, 0); } - private void assertFS4Hits(Grouping request, int firstLevel, int lastLevel, int expected) { CountFS4Hits obj = new CountFS4Hits(); request.setFirstLevel(firstLevel); @@ -255,6 +265,7 @@ public class AggregationTestCase extends TestCase { } } + @Test public void testGroup() { Group a = new Group(); a.setId(new IntegerResultNode(17)); @@ -262,6 +273,7 @@ public class AggregationTestCase extends TestCase { serializeDeserialize1(a); } + @Test public void testGrouping() { Grouping a = new Grouping(); GroupingLevel level = new GroupingLevel(); @@ -293,7 +305,6 @@ public class AggregationTestCase extends TestCase { a.getRoot().addChild(g); serializeDeserialize1(a); - Grouping foo = new Grouping(); foo.addLevel(level); int hashBefore = foo.hashCode(); @@ -320,7 +331,7 @@ public class AggregationTestCase extends TestCase { buf.flip(); Identifiable b = Identifiable.create(buf); assertEquals(a.getClass(), b.getClass()); - assertEquals(buf.getBuf().hasRemaining(), false); + assertFalse(buf.getBuf().hasRemaining()); Identifiable c = b.clone(); assertEquals(b.getClass(), c.getClass()); BufferSerializer bb = new BufferSerializer(new GrowableByteBuffer()); @@ -343,4 +354,5 @@ public class AggregationTestCase extends TestCase { assertEquals(a.getExpression().getClass(), b.getExpression().getClass()); return b; } + } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/aggregation/ForceLoadTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/aggregation/ForceLoadTestCase.java index f2abc2069d7..347c38898ee 100755 --- a/searchlib/src/test/java/com/yahoo/searchlib/aggregation/ForceLoadTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/aggregation/ForceLoadTestCase.java @@ -1,19 +1,22 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.aggregation; -public class ForceLoadTestCase extends junit.framework.TestCase { +import org.junit.Test; - public ForceLoadTestCase(String name) { - super(name); - } +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class ForceLoadTestCase { + @Test public void testLoadClasses() { try { new com.yahoo.searchlib.aggregation.ForceLoad(); assertTrue(com.yahoo.searchlib.aggregation.ForceLoad.forceLoad()); } catch (com.yahoo.system.ForceLoadError e) { e.printStackTrace(); - assertTrue(false); + fail("Load failed"); } } + } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/aggregation/MergeTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/aggregation/MergeTestCase.java index f94806fb00a..83b7e81a539 100755 --- a/searchlib/src/test/java/com/yahoo/searchlib/aggregation/MergeTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/aggregation/MergeTestCase.java @@ -3,22 +3,32 @@ package com.yahoo.searchlib.aggregation; import com.yahoo.document.DocumentId; import com.yahoo.document.GlobalId; -import com.yahoo.searchlib.expression.*; -import junit.framework.TestCase; +import com.yahoo.searchlib.expression.AggregationRefNode; +import com.yahoo.searchlib.expression.AttributeNode; +import com.yahoo.searchlib.expression.ConstantNode; +import com.yahoo.searchlib.expression.FloatBucketResultNode; +import com.yahoo.searchlib.expression.FloatResultNode; +import com.yahoo.searchlib.expression.IntegerResultNode; +import com.yahoo.searchlib.expression.MultiplyFunctionNode; +import com.yahoo.searchlib.expression.StringResultNode; +import org.junit.Test; import java.util.Arrays; import java.util.List; +import static org.junit.Assert.assertEquals; + /** - * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + * @author Simon Thoresen */ -public class MergeTestCase extends TestCase { +public class MergeTestCase { private GlobalId createGlobalId(int docId) { return new GlobalId((new DocumentId("doc:test:" + docId)).getGlobalId()); } // Test merging of hits. + @Test public void testMergeHits() { Grouping request = new Grouping() .setFirstLevel(0) @@ -33,7 +43,7 @@ public class MergeTestCase extends TestCase { .addHit(new FS4Hit(10, createGlobalId(10), 10)) .addHit(new FS4Hit(5, createGlobalId(9), 9)) .addHit(new FS4Hit(6, createGlobalId(8), 8)) - .setExpression(new ConstantNode( new IntegerResultNode(0)))); + .setExpression(new ConstantNode(new IntegerResultNode(0)))); Group a = new Group() .addAggregationResult(new HitsAggregationResult() @@ -68,6 +78,7 @@ public class MergeTestCase extends TestCase { } // Test merging the sum of the values from a single attribute vector that was collected directly into the root node. + @Test public void testMergeSimpleSum() { Grouping lhs = new Grouping() .setRoot(new Group() @@ -90,6 +101,7 @@ public class MergeTestCase extends TestCase { } // Test merging of the value from a single attribute vector in level 1. + @Test public void testMergeSingleChild() { Grouping lhs = new Grouping() .setFirstLevel(0) @@ -119,6 +131,7 @@ public class MergeTestCase extends TestCase { } // Test merging of the value from a multiple attribute vectors in level 1. + @Test public void testMergeMultiChild() { Grouping lhs = new Grouping() .setFirstLevel(0) @@ -171,6 +184,7 @@ public class MergeTestCase extends TestCase { } // Verify that frozen levels are not touched during merge. + @Test public void testMergeLevels() { Grouping request = new Grouping() .addLevel(new GroupingLevel() @@ -340,6 +354,7 @@ public class MergeTestCase extends TestCase { // Verify that the number of groups for a level is pruned down to maxGroups, that the remaining groups are the // highest ranked ones, and that they are sorted by group id. + @Test public void testMergeGroups() { Grouping request = new Grouping() .addLevel(new GroupingLevel() @@ -393,6 +408,7 @@ public class MergeTestCase extends TestCase { assertMerge(request, rhs, lhs, expectAll); } + @Test public void testMergeBuckets() { Grouping lhs = new Grouping() .setRoot(new Group().setTag(0) @@ -423,6 +439,7 @@ public class MergeTestCase extends TestCase { } // Merge two trees that are ordered by an expression, and verify that the resulting order after merge is correct. + @Test public void testMergeExpressions() { Grouping a = new Grouping() .setFirstLevel(0) @@ -465,6 +482,7 @@ public class MergeTestCase extends TestCase { } // Merge two relatively complex tree structures and verify that the end result is as expected. + @Test public void testMergeTrees() { Grouping request = new Grouping() .addLevel(new GroupingLevel() @@ -732,4 +750,5 @@ public class MergeTestCase extends TestCase { assertEquals(expect.toString(), tmp.getRoot().toString()); assertEquals(expect, tmp.getRoot()); } + } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/expression/ExpressionTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/expression/ExpressionTestCase.java index d76db4e8147..12a4cacaa97 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/expression/ExpressionTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/expression/ExpressionTestCase.java @@ -5,16 +5,24 @@ import com.yahoo.io.GrowableByteBuffer; import com.yahoo.text.Utf8; import com.yahoo.vespa.objects.BufferSerializer; import com.yahoo.vespa.objects.Identifiable; -import junit.framework.TestCase; +import org.junit.Test; import java.nio.ByteBuffer; import java.util.Arrays; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + /** * @author baldersheim */ -public class ExpressionTestCase extends TestCase { +public class ExpressionTestCase { + + private static final double delta = 0.00000001; + @Test public void testRangeBucketPreDefFunctionNode() { assertMultiArgFunctionNode(new RangeBucketPreDefFunctionNode(new StringBucketResultNodeVector().add(new StringBucketResultNode("10", "20")), new AttributeNode("foo"))); assertEquals(new RangeBucketPreDefFunctionNode(), new RangeBucketPreDefFunctionNode()); @@ -26,6 +34,7 @@ public class ExpressionTestCase extends TestCase { new RangeBucketPreDefFunctionNode(new StringBucketResultNodeVector().add(new StringBucketResultNode("10", "20")), new AttributeNode("bar"))); } + @Test public void testFixedWidthBucketFunctionNode() { assertMultiArgFunctionNode(new FixedWidthBucketFunctionNode()); assertEquals(new FixedWidthBucketFunctionNode(), new FixedWidthBucketFunctionNode()); @@ -37,6 +46,7 @@ public class ExpressionTestCase extends TestCase { new FixedWidthBucketFunctionNode(new IntegerResultNode(5), new AttributeNode("bar"))); } + @Test public void testIntegerBucketResultNodeVector() { assertResultNode(new IntegerBucketResultNodeVector().add(new IntegerBucketResultNode(10, 20))); assertEquals(new IntegerBucketResultNodeVector().add(new IntegerBucketResultNode(10, 20)), @@ -47,6 +57,7 @@ public class ExpressionTestCase extends TestCase { new IntegerBucketResultNodeVector().add(new IntegerBucketResultNode(11, 20))); } + @Test public void testFloatBucketResultNodeVector() { assertResultNode(new FloatBucketResultNodeVector().add(new FloatBucketResultNode(10, 20))); assertEquals(new FloatBucketResultNodeVector().add(new FloatBucketResultNode(10, 20)), @@ -57,6 +68,7 @@ public class ExpressionTestCase extends TestCase { new FloatBucketResultNodeVector().add(new FloatBucketResultNode(11, 20))); } + @Test public void testStringBucketResultNodeVector() { assertResultNode(new StringBucketResultNodeVector().add(new StringBucketResultNode("10", "20"))); assertEquals(new StringBucketResultNodeVector().add(new StringBucketResultNode("10", "20")), @@ -67,6 +79,7 @@ public class ExpressionTestCase extends TestCase { new StringBucketResultNodeVector().add(new StringBucketResultNode("11", "20"))); } + @Test public void testIntegerBucketResultNode() { assertResultNode(new IntegerBucketResultNode(10, 20)); assertEquals(new IntegerBucketResultNode(10, 20), new IntegerBucketResultNode(10, 20)); @@ -74,6 +87,7 @@ public class ExpressionTestCase extends TestCase { assertNotEquals(new IntegerBucketResultNode(10, 20), new IntegerBucketResultNode(10, 21)); } + @Test public void testFloatBucketResultNode() { assertResultNode(new FloatBucketResultNode(10.0, 20.0)); assertEquals(new FloatBucketResultNode(10.0, 20.0), new FloatBucketResultNode(10.0, 20.0)); @@ -81,6 +95,7 @@ public class ExpressionTestCase extends TestCase { assertNotEquals(new FloatBucketResultNode(10.0, 20.0), new FloatBucketResultNode(10.0, 21.0)); } + @Test public void testStringBucketResultNode() { assertResultNode(new StringBucketResultNode("10.0", "20.0")); assertEquals(new StringBucketResultNode("10.0", "20.0"), new StringBucketResultNode("10.0", "20.0")); @@ -99,6 +114,7 @@ public class ExpressionTestCase extends TestCase { new StringBucketResultNode("11.0", "19.0"), new StringBucketResultNode("11.0", "20.0")); } + @Test public void testPositiveInfinity() { PositiveInfinityResultNode inf = new PositiveInfinityResultNode(); PositiveInfinityResultNode inf2 = new PositiveInfinityResultNode(); @@ -106,6 +122,7 @@ public class ExpressionTestCase extends TestCase { assertEquals(inf, inf2); } + @Test public void testAddFunctionNode() { assertMultiArgFunctionNode(new AddFunctionNode()); assertFunctionNode(new AddFunctionNode().addArg(new ConstantNode(new IntegerResultNode(2))) @@ -119,6 +136,7 @@ public class ExpressionTestCase extends TestCase { 5, 5.0, "5.0", doubleAsRaw(5.0)); } + @Test public void testAndFunctionNode() { assertMultiArgFunctionNode(new AndFunctionNode()); assertFunctionNode(new AndFunctionNode().addArg(new ConstantNode(new IntegerResultNode(3))) @@ -126,11 +144,13 @@ public class ExpressionTestCase extends TestCase { 3, 3.0, "3", longAsRaw(3)); } + @Test public void testZCurveFunctionNode() { assertMultiArgFunctionNode( new ZCurveFunctionNode(new ConstantNode(new IntegerResultNode(7)), ZCurveFunctionNode.Dimension.Y)); } + @Test public void testTimeStampFunctionNode() { assertMultiArgFunctionNode(new TimeStampFunctionNode(new AttributeNode("testattribute"), TimeStampFunctionNode.TimePart.Hour, true)); assertEquals(new TimeStampFunctionNode(new AttributeNode("testattribute"), TimeStampFunctionNode.TimePart.Hour, true), @@ -152,11 +172,13 @@ public class ExpressionTestCase extends TestCase { false)); } + @Test public void testExpressionRefNode() { AggregationRefNode ref = new AggregationRefNode(3); assertEquals(3, ref.getIndex()); } + @Test public void testAttributeNode() { try { new AttributeNode(null); @@ -192,6 +214,7 @@ public class ExpressionTestCase extends TestCase { assertFalse(b.equals(c)); } + @Test public void testInterpolatedLookupNode() { ExpressionNode argA = new ConstantNode(new FloatResultNode(2.71828182846)); ExpressionNode argB = new ConstantNode(new FloatResultNode(3.14159265359)); @@ -236,6 +259,7 @@ public class ExpressionTestCase extends TestCase { assertFalse(a1.equals(a2)); } + @Test public void testCatFunctionNode() { assertMultiArgFunctionNode(new CatFunctionNode()); assertFunctionNode(new CatFunctionNode().addArg(new ConstantNode(new RawResultNode(asRaw('1', '2')))) @@ -243,6 +267,7 @@ public class ExpressionTestCase extends TestCase { 0, 0.0, "1234", asRaw('1', '2', '3', '4')); } + @Test public void testStrCatFunctionNode() { assertMultiArgFunctionNode(new StrCatFunctionNode()); assertFunctionNode(new StrCatFunctionNode().addArg(new ConstantNode(new StringResultNode("foo"))) @@ -250,6 +275,7 @@ public class ExpressionTestCase extends TestCase { 0, 0.0, "foobar", stringAsRaw("foobar")); } + @Test public void testDivideFunctionNode() { assertMultiArgFunctionNode(new DivideFunctionNode()); assertFunctionNode(new DivideFunctionNode().addArg(new ConstantNode(new IntegerResultNode(10))) @@ -263,6 +289,7 @@ public class ExpressionTestCase extends TestCase { 1, 0.5, "0.5", doubleAsRaw(0.5)); } + @Test public void testDocumentFieldNode() { try { new DocumentFieldNode(null); @@ -298,10 +325,11 @@ public class ExpressionTestCase extends TestCase { assertFalse(b.equals(c)); } + @Test public void testFloatResultNode() { FloatResultNode a = new FloatResultNode(7.3); assertEquals(a.getInteger(), 7); - assertEquals(a.getFloat(), 7.3); + assertEquals(a.getFloat(), 7.3, delta); assertEquals(a.getString(), "7.3"); assertEquals(a.getNumber(), new Double(7.3)); byte[] raw = a.getRaw(); @@ -313,11 +341,12 @@ public class ExpressionTestCase extends TestCase { FloatResultNode b = new FloatResultNode(7.5); assertEquals(b.getInteger(), 8); - assertEquals(b.getFloat(), 7.5); + assertEquals(b.getFloat(), 7.5, delta); assertEquals(b.getString(), "7.5"); assertEquals(b.getNumber(), new Double(7.5)); } + @Test public void testGetDocIdNamespaceSpecificFunctionNode() { GetDocIdNamespaceSpecificFunctionNode a = new GetDocIdNamespaceSpecificFunctionNode(new IntegerResultNode(7)); assertTrue(a.getResult() instanceof IntegerResultNode); @@ -340,6 +369,7 @@ public class ExpressionTestCase extends TestCase { } } + @Test public void testGetYMUMChecksumFunctionNode() { GetYMUMChecksumFunctionNode a = new GetYMUMChecksumFunctionNode(); assertTrue(a.getResult() instanceof IntegerResultNode); @@ -358,10 +388,11 @@ public class ExpressionTestCase extends TestCase { } } + @Test public void testIntegerResultNode() { IntegerResultNode a = new IntegerResultNode(7); assertEquals(a.getInteger(), 7); - assertEquals(a.getFloat(), 7.0); + assertEquals(a.getFloat(), 7.0, delta); assertEquals(a.getString(), "7"); assertEquals(a.getNumber(), new Long(7)); byte[] raw = a.getRaw(); @@ -371,6 +402,7 @@ public class ExpressionTestCase extends TestCase { compare(new IntegerResultNode(-1), new IntegerResultNode(0), new IntegerResultNode(0x80000000L)); } + @Test public void testMaxFunctionNode() { assertMultiArgFunctionNode(new MaxFunctionNode()); assertFunctionNode(new MaxFunctionNode().addArg(new ConstantNode(new IntegerResultNode(3))) @@ -381,6 +413,7 @@ public class ExpressionTestCase extends TestCase { 5, 5.0, "5.0", doubleAsRaw(5.0)); } + @Test public void testMD5BitFunctionNode() { try { new MD5BitFunctionNode(null, 64); @@ -403,6 +436,7 @@ public class ExpressionTestCase extends TestCase { assertUnaryBitFunctionNode(new MD5BitFunctionNode()); } + @Test public void testMinFunctionNode() { assertMultiArgFunctionNode(new MinFunctionNode()); assertFunctionNode(new MinFunctionNode().addArg(new ConstantNode(new IntegerResultNode(3))) @@ -413,6 +447,7 @@ public class ExpressionTestCase extends TestCase { 5, 4.9999999, "4.9999999", doubleAsRaw(4.9999999)); } + @Test public void testModuloFunctionNode() { assertMultiArgFunctionNode(new ModuloFunctionNode()); assertFunctionNode(new ModuloFunctionNode().addArg(new ConstantNode(new IntegerResultNode(13))) @@ -423,6 +458,7 @@ public class ExpressionTestCase extends TestCase { 5, 4.9999999, "4.9999999", doubleAsRaw(4.9999999)); } + @Test public void testMultiplyFunctionNode() { assertMultiArgFunctionNode(new MultiplyFunctionNode()); assertFunctionNode(new MultiplyFunctionNode().addArg(new ConstantNode(new IntegerResultNode(3))) @@ -433,6 +469,7 @@ public class ExpressionTestCase extends TestCase { 23, 22.5, "22.5", doubleAsRaw(22.5)); } + @Test public void testNegateFunctionNode() { assertMultiArgFunctionNode(new NegateFunctionNode()); assertFunctionNode(new NegateFunctionNode().addArg(new ConstantNode(new IntegerResultNode(3))), @@ -441,73 +478,80 @@ public class ExpressionTestCase extends TestCase { -3, -3.0, "-3.0", doubleAsRaw(-3.0)); } + @Test public void testSortFunctionNode() { assertMultiArgFunctionNode(new SortFunctionNode()); - } + @Test public void testReverseFunctionNode() { assertMultiArgFunctionNode(new ReverseFunctionNode()); } + @Test public void testToIntFunctionNode() { assertMultiArgFunctionNode(new ToIntFunctionNode()); assertFunctionNode(new ToIntFunctionNode().addArg(new ConstantNode(new StringResultNode("1337"))), 1337, 1337.0, "1337", longAsRaw(1337)); } + @Test public void testToFloatFunctionNode() { assertMultiArgFunctionNode(new ToFloatFunctionNode()); assertFunctionNode(new ToFloatFunctionNode().addArg(new ConstantNode(new FloatResultNode(3.14))), 3, 3.14, "3.14", doubleAsRaw(3.14)); } + @Test public void testMathFunctionNode() { assertMultiArgFunctionNode(new MathFunctionNode(MathFunctionNode.Function.LOG10)); assertFunctionNode(new MathFunctionNode(MathFunctionNode.Function.LOG10).addArg(new ConstantNode(new IntegerResultNode(100000))), 5, 5.0, "5.0", doubleAsRaw(5.0)); } + @Test public void testStrLenFunctionNode() { assertMultiArgFunctionNode(new StrLenFunctionNode()); assertFunctionNode(new StrLenFunctionNode().addArg(new ConstantNode(new StringResultNode("foo"))), 3, 3.0, "3", longAsRaw(3)); } + @Test public void testNormalizeSubjectFunctionNode() { assertMultiArgFunctionNode(new NormalizeSubjectFunctionNode()); assertFunctionNode(new NormalizeSubjectFunctionNode().addArg(new ConstantNode(new StringResultNode("Re: Your mail"))), 0, 0, "Your mail", stringAsRaw("Your mail")); } + @Test public void testNormalizeSubjectFunctionNode2() { assertMultiArgFunctionNode(new NormalizeSubjectFunctionNode()); assertFunctionNode(new NormalizeSubjectFunctionNode().addArg(new ConstantNode(new StringResultNode("Your mail"))), 0, 0, "Your mail", stringAsRaw("Your mail")); } + @Test public void testNumElemFunctionNode() { assertMultiArgFunctionNode(new NumElemFunctionNode()); assertFunctionNode(new NumElemFunctionNode().addArg(new ConstantNode(new IntegerResultNode(1337))), 1, 1.0, "1", longAsRaw(1)); } + @Test public void testToStringFunctionNode() { assertMultiArgFunctionNode(new ToStringFunctionNode()); assertFunctionNode(new ToStringFunctionNode().addArg(new ConstantNode(new IntegerResultNode(1337))), 1337, 1337.0, "1337", stringAsRaw("1337")); } + @Test public void testToRawFunctionNode() { assertMultiArgFunctionNode(new ToRawFunctionNode()); assertFunctionNode(new ToRawFunctionNode().addArg(new ConstantNode(new IntegerResultNode(1337))), 1337, 1337.0, "1337", longAsRaw(1337)); } - public void testNullResultNode() { - // TODO: Implement. - } - + @Test public void testOrFunctionNode() { assertMultiArgFunctionNode(new OrFunctionNode()); assertFunctionNode(new OrFunctionNode().addArg(new ConstantNode(new IntegerResultNode(2))) @@ -515,6 +559,7 @@ public class ExpressionTestCase extends TestCase { 6, 6.0, "6", longAsRaw(6)); } + @Test public void testDebugWaitFunctionNode() { assertFunctionNode( new DebugWaitFunctionNode(new OrFunctionNode().addArg(new ConstantNode(new IntegerResultNode(2))) @@ -543,6 +588,7 @@ public class ExpressionTestCase extends TestCase { assertTrue(end - start > 450); } + @Test public void testRawResultNode() { try { new RawResultNode(null); @@ -564,7 +610,7 @@ public class ExpressionTestCase extends TestCase { assertEquals(raw[1], '.'); assertEquals(raw[2], '4'); assertEquals(a.getInteger(), 0); - assertEquals(a.getFloat(), 0.0); + assertEquals(a.getFloat(), 0.0, delta); assertEquals(a.getString(), "7.4"); assertResultNode(a); compare(new RawResultNode(), new RawResultNode(new byte [] {'z'}), new RawResultNode(new byte [] {'z', 'z'})); @@ -647,6 +693,7 @@ public class ExpressionTestCase extends TestCase { assertEquals(0, large.compareTo(large)); } + @Test public void testStringResultNode() { try { new StringResultNode(null); @@ -662,7 +709,7 @@ public class ExpressionTestCase extends TestCase { } StringResultNode a = new StringResultNode("7.3"); assertEquals(a.getInteger(), 0); - assertEquals(a.getFloat(), 7.3); + assertEquals(a.getFloat(), 7.3, delta); assertEquals(a.getString(), "7.3"); byte[] raw = a.getRaw(); assertEquals(raw.length, 3); @@ -672,6 +719,7 @@ public class ExpressionTestCase extends TestCase { compare(new StringResultNode("a"), new StringResultNode("zz"), new PositiveInfinityResultNode()); } + @Test public void testXorBitFunctionNode() { try { new XorBitFunctionNode(null, 64); @@ -694,6 +742,7 @@ public class ExpressionTestCase extends TestCase { assertUnaryBitFunctionNode(new XorBitFunctionNode()); } + @Test public void testUcaFunctionNode() { try { new UcaFunctionNode(null, "foo"); @@ -716,6 +765,7 @@ public class ExpressionTestCase extends TestCase { assertUcaFunctionNode(new UcaFunctionNode(new ConstantNode(new IntegerResultNode(1337)), "foo", "bar")); } + @Test public void testNestedFunctions() { assertFunctionNode(new AddFunctionNode() .addArg(new MultiplyFunctionNode().addArg(new ConstantNode(new IntegerResultNode(3))) @@ -725,6 +775,7 @@ public class ExpressionTestCase extends TestCase { 14, 14.0, "14.0", doubleAsRaw(14.0)); } + @Test public void testArithmeticNodes() { ExpressionNode i1 = new ConstantNode(new IntegerResultNode(1)); ExpressionNode i2 = new ConstantNode(new IntegerResultNode(2)); @@ -761,6 +812,7 @@ public class ExpressionTestCase extends TestCase { assertTrue(add4.getResult() instanceof IntegerResultNode); } + @Test public void testArithmeticOperations() { ExpressionNode i1 = new ConstantNode(new IntegerResultNode(1793253241)); ExpressionNode i2 = new ConstantNode(new IntegerResultNode(1676521321)); @@ -860,14 +912,14 @@ public class ExpressionTestCase extends TestCase { node.prepare(); node.execute(); assertEquals(lexpected, node.getResult().getInteger()); - assertEquals(dexpected, node.getResult().getFloat()); + assertEquals(dexpected, node.getResult().getFloat(), delta); } public void assertFunctionNode(FunctionNode node, long lexpected, double dexpected, String sexpected, byte[] rexpected) { node.prepare(); node.execute(); assertEquals(lexpected, node.getResult().getInteger()); - assertEquals(dexpected, node.getResult().getFloat()); + assertEquals(dexpected, node.getResult().getFloat(), delta); assertEquals(sexpected, node.getResult().getString()); assertTrue(Arrays.equals(rexpected, node.getResult().getRaw())); } @@ -882,7 +934,7 @@ public class ExpressionTestCase extends TestCase { buf.flip(); node.deserialize(buf); assertEquals(oldInteger, node.getInteger()); - assertEquals(oldFloat, node.getFloat()); + assertEquals(oldFloat, node.getFloat(), delta); assertEquals(oldString, node.getString()); assertEquals(oldRaw.length, node.getRaw().length); @@ -891,7 +943,7 @@ public class ExpressionTestCase extends TestCase { buf.flip(); node.deserializeWithId(buf); assertEquals(oldInteger, node.getInteger()); - assertEquals(oldFloat, node.getFloat()); + assertEquals(oldFloat, node.getFloat(), delta); assertEquals(oldString, node.getString()); assertEquals(oldRaw.length, node.getRaw().length); @@ -900,7 +952,7 @@ public class ExpressionTestCase extends TestCase { buf.flip(); ResultNode obj = (ResultNode)Identifiable.create(buf); assertEquals(oldInteger, obj.getInteger()); - assertEquals(oldFloat, obj.getFloat()); + assertEquals(oldFloat, obj.getFloat(), delta); assertEquals(oldString, obj.getString()); assertEquals(oldRaw.length, obj.getRaw().length); @@ -913,7 +965,7 @@ public class ExpressionTestCase extends TestCase { buf.flip(); Identifiable created = Identifiable.create(buf); assertEquals(node, created); - assertEquals(buf.getBuf().hasRemaining(), false); + assertFalse(buf.getBuf().hasRemaining()); Identifiable cloned = created.clone(); assertEquals(node, cloned); BufferSerializer createdBuffer = new BufferSerializer(new GrowableByteBuffer()); @@ -929,4 +981,5 @@ public class ExpressionTestCase extends TestCase { } return created; } + } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/expression/ForceLoadTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/expression/ForceLoadTestCase.java index a5d216ff089..fa2c9cf1b1e 100755 --- a/searchlib/src/test/java/com/yahoo/searchlib/expression/ForceLoadTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/expression/ForceLoadTestCase.java @@ -1,19 +1,22 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.expression; -public class ForceLoadTestCase extends junit.framework.TestCase { +import org.junit.Test; - public ForceLoadTestCase(String name) { - super(name); - } +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class ForceLoadTestCase { + @Test public void testLoadClasses() { try { new com.yahoo.searchlib.expression.ForceLoad(); assertTrue(com.yahoo.searchlib.expression.ForceLoad.forceLoad()); } catch (com.yahoo.system.ForceLoadError e) { e.printStackTrace(); - assertTrue(false); + fail("Load failed"); } } + } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/expression/ObjectVisitorTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/expression/ObjectVisitorTestCase.java index 1d1fa8251fd..286a6a702db 100755 --- a/searchlib/src/test/java/com/yahoo/searchlib/expression/ObjectVisitorTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/expression/ObjectVisitorTestCase.java @@ -2,18 +2,18 @@ package com.yahoo.searchlib.expression; import com.yahoo.vespa.objects.ObjectDumper; -import com.yahoo.searchlib.expression.FixedWidthBucketFunctionNode; -import com.yahoo.searchlib.expression.IntegerResultNode; -import com.yahoo.searchlib.expression.AttributeNode; -import junit.framework.TestCase; +import org.junit.Test; import java.util.Arrays; +import static org.junit.Assert.assertEquals; + /** - * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + * @author Simon Thoresen */ -public class ObjectVisitorTestCase extends TestCase { +public class ObjectVisitorTestCase { + @Test public void testObjectDumper() { assertDump("test: <NULL>\n", null); assertDump("test: 1\n", 1); @@ -58,4 +58,5 @@ public class ObjectVisitorTestCase extends TestCase { dump.visit("test", obj); assertEquals(expected, dump.toString()); } + } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/SemanticDistanceTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/SemanticDistanceTestCase.java index 68db4867cf7..eb0e46ad6dc 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/SemanticDistanceTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/SemanticDistanceTestCase.java @@ -1,11 +1,15 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.ranking.features.fieldmatch; -import com.yahoo.searchlib.ranking.features.fieldmatch.FieldMatchMetricsComputer; +import org.junit.Before; +import org.junit.Test; import java.util.Set; import java.util.HashSet; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + /** * The "semantic distance" refers to the non-continuous distance from a token * to the next token used by the string match metrics algorithm. This class @@ -14,24 +18,21 @@ import java.util.HashSet; * * @author bratseth */ -public class SemanticDistanceTestCase extends junit.framework.TestCase { +public class SemanticDistanceTestCase { FieldMatchMetricsComputer c; - public SemanticDistanceTestCase(String name) { - super(name); - } - - @Override + @Before public void setUp() { c=new FieldMatchMetricsComputer(); - StringBuilder field=new StringBuilder(); - for (int i=0; i<150; i++) + StringBuilder field = new StringBuilder(); + for (int i = 0; i < 150; i++) field.append("t" + i + " "); - c.compute("query",field.toString()); // Just to set the field value + c.compute("query", field.toString()); // Just to set the field value } /** Must be true using any semantic distance function */ + @Test public void testBothWayConversionProducesOriginalValue() { assertBothWayConversionProducesOriginalValue(50); assertBothWayConversionProducesOriginalValue(10); @@ -43,6 +44,7 @@ public class SemanticDistanceTestCase extends junit.framework.TestCase { } /** Must be true using any semantic distance function */ + @Test public void testFunctionsAreOneToOne() { assertFunctionsAreOneToOne(50); assertFunctionsAreOneToOne(10); @@ -54,52 +56,54 @@ public class SemanticDistanceTestCase extends junit.framework.TestCase { } /** Specific to this particular distance function */ + @Test public void testFunction() { - int zeroJ=50; - assertFunction(50,0,zeroJ); - assertFunction(59,9,zeroJ); - assertFunction(49,10,zeroJ); - assertFunction(40,19,zeroJ); - assertFunction(60,20,zeroJ); - assertFunction(149,109,zeroJ); - assertFunction(39,110,zeroJ); - assertFunction(0,149,zeroJ); - - zeroJ=0; - assertFunction(0,0,zeroJ); - assertFunction(10,10,zeroJ); - assertFunction(20,20,zeroJ); - assertFunction(149,149,zeroJ); - - zeroJ=5; - assertFunction(5,0,zeroJ); - assertFunction(10,5,zeroJ); - assertFunction(14,9,zeroJ); - assertFunction(4,10,zeroJ); - assertFunction(0,14,zeroJ); - assertFunction(15,15,zeroJ); - assertFunction(25,25,zeroJ); - assertFunction(149,149,zeroJ); - - zeroJ=149; - assertFunction(149,0,zeroJ); - assertFunction(140,9,zeroJ); - assertFunction(130,19,zeroJ); - assertFunction(0,149,zeroJ); - - zeroJ=145; - assertFunction(145,0,zeroJ); - assertFunction(149,4,zeroJ); - assertFunction(144,5,zeroJ); - assertFunction(140,9,zeroJ); - assertFunction(135,14,zeroJ); - assertFunction(125,24,zeroJ); - assertFunction(0,149,zeroJ); + int zeroJ = 50; + assertFunction(50,0, zeroJ); + assertFunction(59,9, zeroJ); + assertFunction(49,10, zeroJ); + assertFunction(40,19, zeroJ); + assertFunction(60,20, zeroJ); + assertFunction(149,109, zeroJ); + assertFunction(39,110, zeroJ); + assertFunction(0,149, zeroJ); + + zeroJ = 0; + assertFunction(0,0, zeroJ); + assertFunction(10,10, zeroJ); + assertFunction(20,20, zeroJ); + assertFunction(149,149, zeroJ); + + zeroJ = 5; + assertFunction(5,0, zeroJ); + assertFunction(10,5, zeroJ); + assertFunction(14,9, zeroJ); + assertFunction(4,10, zeroJ); + assertFunction(0,14, zeroJ); + assertFunction(15,15, zeroJ); + assertFunction(25,25, zeroJ); + assertFunction(149,149, zeroJ); + + zeroJ = 149; + assertFunction(149,0, zeroJ); + assertFunction(140,9, zeroJ); + assertFunction(130,19, zeroJ); + assertFunction(0,149, zeroJ); + + zeroJ = 145; + assertFunction(145,0, zeroJ); + assertFunction(149,4, zeroJ); + assertFunction(144,5, zeroJ); + assertFunction(140,9, zeroJ); + assertFunction(135,14, zeroJ); + assertFunction(125,24, zeroJ); + assertFunction(0,149, zeroJ); } /** Hits both limits at once */ + @Test public void testSmallField() { - c=new FieldMatchMetricsComputer(); + c = new FieldMatchMetricsComputer(); c.compute("query","my field value four"); // Just to set the field value assertBothWayConversionProducesOriginalValue(2); assertBothWayConversionProducesOriginalValue(0); @@ -109,33 +113,34 @@ public class SemanticDistanceTestCase extends junit.framework.TestCase { assertFunctionsAreOneToOne(3); int zeroJ=2; - assertFunction(2,0,zeroJ); - assertFunction(3,1,zeroJ); - assertFunction(1,2,zeroJ); - assertFunction(0,3,zeroJ); + assertFunction(2,0, zeroJ); + assertFunction(3,1, zeroJ); + assertFunction(1,2, zeroJ); + assertFunction(0,3, zeroJ); } private void assertBothWayConversionProducesOriginalValue(int zeroJ) { // Starting point in the middle - for (int j=0; j<c.getField().terms().size(); j++) { - int semanticDistance=c.fieldIndexToSemanticDistance(j,zeroJ); + for (int j = 0; j < c.getField().terms().size(); j++) { + int semanticDistance=c.fieldIndexToSemanticDistance(j, zeroJ); assertTrue("Using zeroJ=" + zeroJ + ": " + semanticDistance +">=0", semanticDistance >= 0); - int backConvertedJ=c.semanticDistanceToFieldIndex(semanticDistance,zeroJ); + int backConvertedJ=c.semanticDistanceToFieldIndex(semanticDistance, zeroJ); assertEquals("Using zeroJ=" + zeroJ + ": " + j + "->" + semanticDistance + "->" + backConvertedJ,j, backConvertedJ); } } private void assertFunctionsAreOneToOne(int zeroJ) { - Set<Integer> distances=new HashSet<Integer>(); - for (int j=0; j<c.getField().terms().size(); j++) { - int semanticDistance=c.fieldIndexToSemanticDistance(j,zeroJ); - assertTrue("Using zeroJ=" + zeroJ + ": " + j + "->" + semanticDistance + " is unique", ! distances.contains(semanticDistance)); + Set<Integer> distances = new HashSet<>(); + for (int j = 0; j < c.getField().terms().size(); j++) { + int semanticDistance = c.fieldIndexToSemanticDistance(j,zeroJ); + assertTrue("Using zeroJ=" + zeroJ + ": " + j + "->" + semanticDistance + " is unique", + ! distances.contains(semanticDistance)); distances.add(semanticDistance); } } - private void assertFunction(int j,int semanticDistance,int zeroJ) { - assertEquals(j + "->" + semanticDistance,semanticDistance,c.fieldIndexToSemanticDistance(j,zeroJ)); + private void assertFunction(int j,int semanticDistance, int zeroJ) { + assertEquals(j + "->" + semanticDistance,semanticDistance, c.fieldIndexToSemanticDistance(j,zeroJ)); } } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/reference/test/OptimalStringAlignmentTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/reference/test/OptimalStringAlignmentTestCase.java index b2db0d0f805..c9b812894ba 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/reference/test/OptimalStringAlignmentTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/reference/test/OptimalStringAlignmentTestCase.java @@ -2,20 +2,22 @@ package com.yahoo.searchlib.ranking.features.fieldmatch.reference.test; import com.yahoo.searchlib.ranking.features.fieldmatch.reference.OptimalStringAlignmentDistance; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; /** * @author bratseth */ -public class OptimalStringAlignmentTestCase extends junit.framework.TestCase { +public class OptimalStringAlignmentTestCase { - public OptimalStringAlignmentTestCase(String name) { - super(name); - } + private final double delta = 0.0000000001; + @Test public void testEditDistance() { // Edit distance, substitution, deletion, insertion, transposition, query, field, print? - boolean print=false; + boolean print = false; assertEditDistance(0,0,0,0,0,"niels bohr","niels bohr",print); assertEditDistance(1,1,0,0,0,"niels","bohr",print); assertEditDistance(1,0,0,1,0,"niels","niels bohr",print); @@ -27,8 +29,9 @@ public class OptimalStringAlignmentTestCase extends junit.framework.TestCase { assertEditDistance(3,2,0,1,0,"niels bohr i kopenhagen","niels henrik bor i stockholm",print); } + @Test public void testEditDistanceAsRelevance() { - boolean print=false; + boolean print = false; assertEditDistance(2,0,0,2,0,"niels bohr","niels blah blah bohr",print); assertEditDistance(4,0,1,3,0,"niels bohr","bohr blah blah niels",print); // Not desired assertEditDistance(4,2,0,2,0,"niels bohr","koko blah blah bahia",print); @@ -48,11 +51,11 @@ public class OptimalStringAlignmentTestCase extends junit.framework.TestCase { System.out.println(); } - assertEquals("Substitutions",(float)substitution,e.getSubstitutions()); - assertEquals("Deletions",(float)deletion,e.getDeletions()); - assertEquals("Insertions",(float)insertion,e.getInsertions()); - assertEquals("Transpositions",(float)transposition,e.getTranspositions()); - assertEquals("Total",(float)total,e.getTotal()); + assertEquals("Substitutions",(float)substitution,e.getSubstitutions(), delta); + assertEquals("Deletions",(float)deletion,e.getDeletions(), delta); + assertEquals("Insertions",(float)insertion,e.getInsertions(), delta); + assertEquals("Transpositions",(float)transposition,e.getTranspositions(), delta); + assertEquals("Total",(float)total,e.getTotal(), delta); } } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java index 6f9b0f03b6b..4918ee5bdcd 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java @@ -8,21 +8,21 @@ import com.yahoo.searchlib.ranking.features.fieldmatch.FieldMatchMetricsComputer import com.yahoo.searchlib.ranking.features.fieldmatch.FieldMatchMetricsParameters; import com.yahoo.searchlib.ranking.features.fieldmatch.QueryTerm; import com.yahoo.searchlib.ranking.features.fieldmatch.Query; +import org.junit.Test; import java.util.List; +import static org.junit.Assert.assertEquals; + /** * Tests of calculation of all the string match metrics. * Add true as the fourth parameter to assertMetrics to see a trace of what the test is doing. * * @author bratseth */ -public class FieldMatchMetricsTestCase extends junit.framework.TestCase { - - public FieldMatchMetricsTestCase(String name) { - super(name); - } +public class FieldMatchMetricsTestCase { + @Test public void testOutOfOrder() { assertMetrics("outOfOrder:0","a","a"); assertMetrics("outOfOrder:0","a b c","a b c"); @@ -33,6 +33,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("outOfOrder:2", "a b c d e", "c x a b x x x x x e x x d"); } + @Test public void testSegments() { assertMetrics("segments:1","a","a"); assertMetrics("segments:1","a b c","a b c"); @@ -45,6 +46,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("segments:2 gaps:1","a y y b c","x x x b x x c x x x x x x x x x x x x x x x x x x x a x x"); } + @Test public void testGaps() { assertMetrics("gaps:0","a","a"); assertMetrics("gaps:0","x�a","a"); @@ -59,6 +61,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("gaps:0","y a b c","a b c x"); } + @Test public void testHead() { assertMetrics("head:0","a","a"); assertMetrics("head:0","y","a"); @@ -68,6 +71,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("head:2", "a b c", "x x c x x x x x x x x x x x x x x x a b"); } + @Test public void testTail() { assertMetrics("tail:0","a","a"); assertMetrics("tail:0","y","a"); @@ -77,6 +81,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("tail:0","a b c","x x c x x x x x x x x x x x x x x x a b"); } + @Test public void testLongestSequence() { assertMetrics("longestSequence:1","a","a"); assertMetrics("longestSequence:1","a","a b c"); @@ -89,6 +94,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("longestSequence:4 segments:1","a b c d","x x a b x x x x x x x x x x x x x x x x x c d x x a b c d"); } + @Test public void testMatches() { assertMetrics("matches:1 queryCompleteness:1 fieldCompleteness:1", "a","a"); assertMetrics("matches:3 queryCompleteness:1 fieldCompleteness:1", "a b c","a b c"); @@ -96,6 +102,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("matches:3 queryCompleteness:0.5 fieldCompleteness:0.25","a y y b c y","a x x b c x a x a b x x"); } + @Test public void testCompleteness() { assertMetrics("completeness:1 queryCompleteness:1 fieldCompleteness:1", "a","a"); assertMetrics("completeness:0 queryCompleteness:0 fieldCompleteness:0", "a","x"); @@ -109,6 +116,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("completeness:0.97 queryCompleteness:1 fieldCompleteness:0.4","a b","a b b b b"); } + @Test public void testOrderness() { assertMetrics("orderness:1", "a","a"); assertMetrics("orderness:1", "a","x"); @@ -120,6 +128,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("orderness:1", "a b","b x x x x x x x x x x x x x x x x x x x x x a"); } + @Test public void testRelatedness() { assertMetrics("relatedness:1", "a","a"); assertMetrics("relatedness:0", "a","x"); @@ -129,6 +138,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("relatedness:0.5","a y b y y y c","a b x x x x x x x x x x x x x x x x x x x x x x x c"); } + @Test public void testLongestSequenceRatio() { assertMetrics("longestSequenceRatio:1", "a","a"); assertMetrics("longestSequenceRatio:0", "a","x"); @@ -140,6 +150,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("longestSequenceRatio:0.75","a b c d","x x a b x a x c d a b c x d x"); } + @Test public void testEarliness() { assertMetrics("earliness:1", "a","a"); assertMetrics("earliness:0", "a","x"); @@ -151,6 +162,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("earliness:0.2", "a b c","x b c a x x x x a x x x x x x x a b c x x"); } + @Test public void testWeight() { assertMetrics("weight:1", "a","a"); assertMetrics("weight:0", "y","a"); @@ -216,6 +228,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { } /** Calculated the same way as weight */ + @Test public void testSignificance() { assertMetrics("significance:1", "a","a"); assertMetrics("significance:0", "a","x"); @@ -280,6 +293,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("significance:0","a%0 b%0","",0.3f); } + @Test public void testImportance() { assertMetrics("importance:0.75","a b c", "a x x b x c c c",600); assertMetrics("importance:0.85","a b!500 c","a x x b x c c c",1000); @@ -290,6 +304,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("importance:0.85","a b!500%0.5 c","a x x b x c c c",1000); } + @Test public void testOccurrence() { assertMetrics("occurrence:0","a","x"); assertMetrics("occurrence:1","a","a"); @@ -313,6 +328,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("occurrence:1", "a b","a a a a a a a a a a a b b b b b b b b b b b",false,c); // Field is too large to consider field length } + @Test public void testAbsoluteOccurrence() { assertMetrics("absoluteOccurrence:0", "a","x"); assertMetrics("absoluteOccurrence:0.01","a","a"); @@ -336,6 +352,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("absoluteOccurrence:1", "a b","a a a a a a a a a a a b b b b b b b b b b b",false,c); // Field is too large to consider field length } + @Test public void testWeightedOccurrence() { assertMetrics("weightedOccurrence:0","a!200","x"); assertMetrics("weightedOccurrence:1","a!200","a"); @@ -378,6 +395,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("weightedOccurrence:1", "a!200 b","a a a a a a a a a a a b b b b b b b b b b b",false,c); // Field is too large to consider field length } + @Test public void testWeightedAbsoluteOccurrence() { assertMetrics("weightedAbsoluteOccurrence:0", "a!200","x"); assertMetrics("weightedAbsoluteOccurrence:0.01", "a!200","a"); @@ -420,6 +438,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("weightedAbsoluteOccurrence:1", "a!200 b","a a a a a a a a a a a b b b b b b b b b b b",false,c); // Field is too large to consider field length } + @Test public void testSignificantOccurrence() { assertMetrics("significantOccurrence:0","a%0.2","x"); assertMetrics("significantOccurrence:1","a%0.2","a"); @@ -462,6 +481,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("significantOccurrence:1", "a%0.2 b","a a a a a a a a a a a b b b b b b b b b b b",false,c); // Field is too large to consider field length } + @Test public void testUnweightedProximity() { assertMetrics("unweightedProximity:1", "a","a"); assertMetrics("unweightedProximity:1", "a b c","a b c"); @@ -476,6 +496,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("unweightedProximity:0.5", "y a b c","a x x b x x c x"); } + @Test public void testReverseProximity() { assertMetrics("unweightedProximity:0.33", "a b","b a"); assertMetrics("unweightedProximity:0.62", "a b c","c a b"); @@ -485,6 +506,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("unweightedProximity:0.9275","a b c d e","a b x c d e"); } + @Test public void testProximity() { assertMetrics("absoluteProximity:0.1 proximity:1", "a b","a b"); assertMetrics("absoluteProximity:0.3 proximity:1", "a 0.3:b","a b"); @@ -509,6 +531,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { * Tests exactness (using field exactness only - nothing additional of interest to test with query exactness * as that is just another number multiplied with the term exactness) */ + @Test public void testExactness() { assertMetrics("exactness:1", "a b c","a x b x x c"); assertMetrics("exactness:0.9", "a b c","a x b:0.7 x x c"); @@ -517,12 +540,14 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("exactness:0.65", "a b c!200","a x b:0.6 x x c:0.5"); } + @Test public void testMultiSegmentProximity() { assertMetrics("absoluteProximity:0.1 proximity:1", "a b c", "a b x x x x x x x x x x x x x x x x x x x x x x c"); assertMetrics("absoluteProximity:0.05 proximity:0.5","a b c", "a x x b x x x x x x x x x x x x x x x x x x x x x x c"); assertMetrics("absoluteProximity:0.075 proximity:0.75","a b c d","a x x b x x x x x x x x x x x x x x x x x x x x x x c d"); } + @Test public void testSegmentDistance() { assertMetrics("segmentDistance:13 absoluteProximity:0.1", "a b c","a b x x x x x x x x x x c"); assertMetrics("segmentDistance:13 absoluteProximity:0.5", "a 0.5:b c","a b x x x x x x x x x x c"); @@ -534,6 +559,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("segmentDistance:25 absoluteProximity:0.1", "a b c","c x x x x x x x x x x x b x x x x x x x x x x a"); } + @Test public void testSegmentProximity() { assertMetrics("segmentProximity:1", "a","a"); assertMetrics("segmentProximity:0", "a","x"); @@ -546,6 +572,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { } /** Test cases where we choose between multiple different segmentations */ + @Test public void testSegmentSelection() { assertMetrics("segments:2 absoluteProximity:0.1 proximity:1 segmentStarts:19,41", "a b c d e","x a b x c x x x x x x x x x x x x x x a b c x x x x x x x x x e x d x c d x x x c d e"); @@ -567,12 +594,14 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("segments:1 segmentStarts:0","a b c d","a b x x x x x x x x c d x x x x x x x x x x x a b x x x x x x x x x x x c d"); } + @Test public void testMoreThanASegmentLengthOfUnmatchedQuery() { assertMetrics("absoluteProximity:0.1 proximity:1","a b y y y y y y y y y y y y y y y","a b"); assertMetrics("segments:2 absoluteProximity:0.1 proximity:1","a b c d y y y y y y y y y y y y y y y","a b x x x x x x x x x x x x x x x x x x c d"); assertMetrics("segments:2 absoluteProximity:0.1 proximity:1","a b y y y y y y y y y y y y y y y c d","a b x x x x x x x x x x x x x x x x x x c d"); } + @Test public void testQueryRepeats() { // Not really handled perfectly, but good enough assertMetrics("absoluteProximity:0.1 proximity:1 head:0 tail:0", "a a a","a"); @@ -588,6 +617,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("matches:2 fieldCompleteness:1","a b b b","a b"); } + @Test public void testZeroCases() { assertMetrics("absoluteProximity:0.1 proximity:1 matches:0 exactness:0","y","a"); assertMetrics("absoluteProximity:0.1 proximity:1 matches:0 exactness:0","a","x"); @@ -596,8 +626,8 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("absoluteProximity:0.1 proximity:1 matches:0 exactness:0","",""); } + @Test public void testExceedingIterationLimit() { - { // Segments found: a x x b and c d FieldMatchMetricsParameters p=new FieldMatchMetricsParameters(); p.setMaxAlternativeSegmentations(0); @@ -620,6 +650,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { } } + @Test public void testMatch() { // Ordered by decreasing match score per query assertMetrics("match:1", "a","a"); @@ -647,6 +678,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { assertMetrics("match:0.2927","a b!200 c","x x a x b:0.7 x x x x x x x x x x x x x x x x x x x x x x"); } + @Test public void testRepeatedMatch() { // gap==1 caused by finding two possible segments due to repeated matching assertMetrics("fieldCompleteness:1 queryCompleteness:0.6667 segments:1 earliness:1 gaps:1", @@ -654,6 +686,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { } /** Three segments - improving the score on the first should impact the last */ + @Test public void testNestedAlternatives() { assertMetrics("segmentStarts:6,19,32 proximity:1", "a b c d e f", @@ -664,6 +697,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { } /** Nice demonstration of the limitations of this algorithm: Segment end points are determined greedily */ + @Test public void testSegmentationGreedyness() { assertMetrics("match:0.3717","a b c","a x b x x x x x x x x b c"); assertMetrics("match:0.4981","a b c","a x z x x x x x x x x b c"); @@ -715,7 +749,7 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { } else { float correctValue=Float.parseFloat(correctValueString); - assertEquals(metricName, correctValue, (float)Math.round(metrics.get(metricName)*10000)/10000 ); + assertEquals(metricName, correctValue, (float)Math.round(metrics.get(metricName)*10000)/10000, 0.000000001); } } } @@ -754,4 +788,5 @@ public class FieldMatchMetricsTestCase extends junit.framework.TestCase { } return new Field(terms.build()); } + } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/ReferenceTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/ReferenceTestCase.java index f275f95ca8e..4298beb5233 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/ReferenceTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/ReferenceTestCase.java @@ -5,9 +5,9 @@ import com.yahoo.searchlib.rankingexpression.rule.NameNode; import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode; import org.junit.Test; -import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author bratseth diff --git a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/ContextReuseTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/ContextReuseTestCase.java index 3d5710b81e0..3ad5c0f7316 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/ContextReuseTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/ContextReuseTestCase.java @@ -6,19 +6,22 @@ import com.yahoo.searchlib.rankingexpression.RankingExpression; import com.yahoo.searchlib.rankingexpression.evaluation.ArrayContext; import com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer; import com.yahoo.searchlib.rankingexpression.parser.ParseException; +import org.junit.Test; import java.io.File; import java.io.IOException; +import static org.junit.Assert.assertEquals; + /** * This tests reuse of a optimized context which is not initialized with * all values referenced in the expression. * * @author bratseth */ -public class ContextReuseTestCase extends junit.framework.TestCase { +public class ContextReuseTestCase { - private String contextString= + private String contextString = "CONCEPTTYPE = 0.0\n" + "REGEXTYPE = 0.0\n" + "POS_18 = 0.0\n" + @@ -43,6 +46,7 @@ public class ContextReuseTestCase extends junit.framework.TestCase { "EXTENDEDTYPE = 0.0\n" + "ENTITYPLACETYPE = 0.0\n"; + @Test public void testIt() throws ParseException, IOException { // Prepare RankingExpression expression=new RankingExpression(IOUtils.readFile(new File("src/test/files/s-expression.vre"))); @@ -55,7 +59,7 @@ public class ContextReuseTestCase extends junit.framework.TestCase { String[] contextValueParts = contextValueString.split("="); context.put(contextValueParts[0].trim(), Double.valueOf(contextValueParts[1].trim())); } - assertEquals(-2.3450294999999994, expression.evaluate(context).asDouble()); + assertEquals(-2.3450294999999994, expression.evaluate(context).asDouble(), 0.000000000001); } } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTForestOptimizerTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTForestOptimizerTestCase.java index 420819a8b2a..ce78703f842 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTForestOptimizerTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTForestOptimizerTestCase.java @@ -4,12 +4,17 @@ package com.yahoo.searchlib.rankingexpression.evaluation.gbdtoptimization; import com.yahoo.searchlib.rankingexpression.RankingExpression; import com.yahoo.searchlib.rankingexpression.evaluation.*; import com.yahoo.searchlib.rankingexpression.parser.ParseException; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author bratseth */ -public class GBDTForestOptimizerTestCase extends junit.framework.TestCase { +public class GBDTForestOptimizerTestCase { + @Test public void testForestOptimization() throws ParseException { String gbdtString = "if (LW_NEWS_SEARCHES_RATIO < 1.72971, 0.0697159, if (LW_USERS < 0.10496, if (SEARCHES < 0.0329127, 0.151257, 0.117501), if (SUGG_OVERLAP < 18.5, 0.0897622, 0.0756903))) + \n" + @@ -55,6 +60,7 @@ public class GBDTForestOptimizerTestCase extends junit.framework.TestCase { assertEqualish(result3, oResult3); } + @Test public void testForestOptimizationWithSetMembershipConditions() throws ParseException { String gbdtString = "if (MYSTRING in [\"string 1\",\"string 2\"], 0.0697159, if (LW_USERS < 0.10496, if (SEARCHES < 0.0329127, 0.151257, 0.117501), if (MYSTRING in [\"string 2\"], 0.0897622, 0.0756903))) + \n" + diff --git a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTOptimizerTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTOptimizerTestCase.java index 1cbd19b667e..4b7462505fc 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTOptimizerTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTOptimizerTestCase.java @@ -7,96 +7,105 @@ import com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer; import com.yahoo.searchlib.rankingexpression.evaluation.MapContext; import com.yahoo.searchlib.rankingexpression.evaluation.OptimizationReport; import com.yahoo.searchlib.rankingexpression.parser.ParseException; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; /** * @author bratseth */ -public class GBDTOptimizerTestCase extends junit.framework.TestCase { +public class GBDTOptimizerTestCase { + + private final double delta = 0.00000000001; + @Test public void testSimpleNodeOptimization() throws ParseException { - RankingExpression gbdt=new RankingExpression("if (a < 2, if (b < 2, 5, 6), 4) + if (a < 3, 7, 8)"); + RankingExpression gbdt = new RankingExpression("if (a < 2, if (b < 2, 5, 6), 4) + if (a < 3, 7, 8)"); // Optimized evaluation - ArrayContext arguments=new ArrayContext(gbdt); - ExpressionOptimizer optimizer=new ExpressionOptimizer(); + ArrayContext arguments = new ArrayContext(gbdt); + ExpressionOptimizer optimizer = new ExpressionOptimizer(); optimizer.getOptimizer(GBDTForestOptimizer.class).setEnabled(false); - OptimizationReport report=optimizer.optimize(gbdt,arguments); - assertEquals(2,report.getMetric("Optimized GDBT trees")); - arguments.put("a",1d); - arguments.put("b",2d); - assertEquals(13.0,gbdt.evaluate(arguments).asDouble()); + OptimizationReport report = optimizer.optimize(gbdt,arguments); + assertEquals(2, report.getMetric("Optimized GDBT trees")); + arguments.put("a", 1d); + arguments.put("b", 2d); + assertEquals(13.0, gbdt.evaluate(arguments).asDouble(), delta); } + @Test public void testNodeOptimization() throws ParseException { - String gbdtString= + String gbdtString = "if (LW_NEWS_SEARCHES_RATIO < 1.72971, 0.0697159, if (LW_USERS < 0.10496, if (SEARCHES < 0.0329127, 0.151257, 0.117501), if (SUGG_OVERLAP < 18.5, 0.0897622, 0.0756903))) + \n" + "if (LW_NEWS_SEARCHES_RATIO < 1.73156, if (NEWS_USERS < 0.0737993, -0.00481646, 0.00110018), if (LW_USERS < 0.0844616, 0.0488919, if (SUGG_OVERLAP < 32.5, 0.0136917, 9.85328E-4))) + \n" + "if (LW_NEWS_SEARCHES_RATIO < 1.74451, -0.00298257, if (LW_USERS < 0.116207, if (SEARCHES < 0.0329127, 0.0676105, 0.0340198), if (NUM_WORDS < 1.5, -8.55514E-5, 0.0112406))) + \n" + "if (LW_NEWS_SEARCHES_RATIO < 1.72995, if (NEWS_USERS < 0.0737993, -0.00407515, 0.00139088), if (LW_USERS == 0.0509035, 0.0439466, if (LW_USERS < 0.325818, 0.0187156, 0.00236949)))"; - RankingExpression gbdt=new RankingExpression(gbdtString); + RankingExpression gbdt = new RankingExpression(gbdtString); // Regular evaluation - MapContext arguments=new MapContext(); - arguments.put("LW_NEWS_SEARCHES_RATIO",1d); - arguments.put("SUGG_OVERLAP",17d); - double result1=gbdt.evaluate(arguments).asDouble(); - arguments.put("LW_NEWS_SEARCHES_RATIO",2d); - arguments.put("SUGG_OVERLAP",20d); - double result2=gbdt.evaluate(arguments).asDouble(); - arguments.put("LW_NEWS_SEARCHES_RATIO",2d); - arguments.put("SUGG_OVERLAP",40d); - double result3=gbdt.evaluate(arguments).asDouble(); + MapContext arguments = new MapContext(); + arguments.put("LW_NEWS_SEARCHES_RATIO", 1d); + arguments.put("SUGG_OVERLAP", 17d); + double result1 = gbdt.evaluate(arguments).asDouble(); + arguments.put("LW_NEWS_SEARCHES_RATIO", 2d); + arguments.put("SUGG_OVERLAP", 20d); + double result2 = gbdt.evaluate(arguments).asDouble(); + arguments.put("LW_NEWS_SEARCHES_RATIO", 2d); + arguments.put("SUGG_OVERLAP", 40d); + double result3 = gbdt.evaluate(arguments).asDouble(); // Optimized evaluation - ArrayContext fArguments=new ArrayContext(gbdt); - ExpressionOptimizer optimizer=new ExpressionOptimizer(); + ArrayContext fArguments = new ArrayContext(gbdt); + ExpressionOptimizer optimizer = new ExpressionOptimizer(); optimizer.getOptimizer(GBDTForestOptimizer.class).setEnabled(false); - OptimizationReport report=optimizer.optimize(gbdt,fArguments); - assertEquals(4,report.getMetric("Optimized GDBT trees")); - fArguments.put("LW_NEWS_SEARCHES_RATIO",1d); - fArguments.put("SUGG_OVERLAP",17d); - double oResult1=gbdt.evaluate(fArguments).asDouble(); - fArguments.put("LW_NEWS_SEARCHES_RATIO",2d); - fArguments.put("SUGG_OVERLAP",20d); - double oResult2=gbdt.evaluate(fArguments).asDouble(); - fArguments.put("LW_NEWS_SEARCHES_RATIO",2d); - fArguments.put("SUGG_OVERLAP",40d); - double oResult3=gbdt.evaluate(fArguments).asDouble(); + OptimizationReport report = optimizer.optimize(gbdt,fArguments); + assertEquals(4, report.getMetric("Optimized GDBT trees")); + fArguments.put("LW_NEWS_SEARCHES_RATIO", 1d); + fArguments.put("SUGG_OVERLAP", 17d); + double oResult1 = gbdt.evaluate(fArguments).asDouble(); + fArguments.put("LW_NEWS_SEARCHES_RATIO", 2d); + fArguments.put("SUGG_OVERLAP", 20d); + double oResult2 = gbdt.evaluate(fArguments).asDouble(); + fArguments.put("LW_NEWS_SEARCHES_RATIO", 2d); + fArguments.put("SUGG_OVERLAP", 40d); + double oResult3 = gbdt.evaluate(fArguments).asDouble(); // Assert the same results are produced - assertEquals(result1,oResult1); - assertEquals(result2,oResult2); - assertEquals(result3,oResult3); + assertEquals(result1, oResult1, delta); + assertEquals(result2, oResult2, delta); + assertEquals(result3, oResult3, delta); } + @Test public void testFeatureNamesWithDots() throws ParseException { - String gbdtString= + String gbdtString = "if (a.b < 1.72971, 0.0697159, if (a.b.c < 0.10496, if (a.c < 0.0329127, 0.151257, 0.117501), if (a < 18.5, 0.0897622, 0.0756903))) + 1"; - RankingExpression gbdt=new RankingExpression(gbdtString); + RankingExpression gbdt = new RankingExpression(gbdtString); // Regular evaluation - MapContext arguments=new MapContext(); - arguments.put("a.b",1d); - arguments.put("a.b.c",0.1d); - arguments.put("a.c",0.01d); - arguments.put("a",19d); - double result=gbdt.evaluate(arguments).asDouble(); + MapContext arguments = new MapContext(); + arguments.put("a.b", 1d); + arguments.put("a.b.c", 0.1d); + arguments.put("a.c", 0.01d); + arguments.put("a", 19d); + double result = gbdt.evaluate(arguments).asDouble(); // Optimized evaluation - ArrayContext fArguments=new ArrayContext(gbdt); - OptimizationReport report=new OptimizationReport(); - new GBDTOptimizer().optimize(gbdt,fArguments,report); - assertEquals("Optimization result is as expected:\n" + report,1,report.getMetric("Optimized GDBT trees")); - fArguments.put("a.b",1d); - fArguments.put("a.b.c",0.1d); - fArguments.put("a.c",0.01d); - fArguments.put("a",19d); - double oResult=gbdt.evaluate(fArguments).asDouble(); + ArrayContext fArguments = new ArrayContext(gbdt); + OptimizationReport report = new OptimizationReport(); + new GBDTOptimizer().optimize(gbdt, fArguments, report); + assertEquals("Optimization result is as expected:\n" + report, 1, report.getMetric("Optimized GDBT trees")); + fArguments.put("a.b", 1d); + fArguments.put("a.b.c", 0.1d); + fArguments.put("a.c", 0.01d); + fArguments.put("a", 19d); + double oResult = gbdt.evaluate(fArguments).asDouble(); // Assert the same results are produced - assertEquals(result,oResult); + assertEquals(result, oResult, delta); } + @Test public void testBug4009433() throws ParseException { RankingExpression exp = new RankingExpression("10*if(two>35,if(two>one,if(two>=670,4,8),if(two>8000,5,3)),if(two==478,90,91))"); new GBDTOptimizer().optimize(exp, new ArrayContext(exp), new OptimizationReport()); diff --git a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/integration/tensorflow/DimensionRenamerTest.java b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/integration/tensorflow/DimensionRenamerTest.java index ebcfde54c70..74b0d11f1d6 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/integration/tensorflow/DimensionRenamerTest.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/integration/tensorflow/DimensionRenamerTest.java @@ -3,7 +3,7 @@ package com.yahoo.searchlib.rankingexpression.integration.tensorflow; import com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.DimensionRenamer; import org.junit.Test; -import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertTrue; public class DimensionRenamerTest { @@ -43,7 +43,6 @@ public class DimensionRenamerTest { assertTrue(secondDimensionOfXName.compareTo(firstDimensionOfWName) == 0); assertTrue(firstDimensionOfXName.compareTo(secondDimensionOfWName) < 0); assertTrue(secondDimensionOfWName.compareTo(firstDimensionOfBName) == 0); - - } + } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/treenet/TreeNetParserTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/treenet/TreeNetParserTestCase.java index cd7643b9f74..5f48109d4c6 100755 --- a/searchlib/src/test/java/com/yahoo/searchlib/treenet/TreeNetParserTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/treenet/TreeNetParserTestCase.java @@ -4,17 +4,24 @@ package com.yahoo.searchlib.treenet; import com.yahoo.searchlib.rankingexpression.RankingExpression; import com.yahoo.searchlib.treenet.parser.ParseException; import com.yahoo.searchlib.treenet.parser.TreeNetParser; -import junit.framework.TestCase; +import org.junit.Test; -import java.io.*; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringReader; + +import static org.junit.Assert.assertEquals; /** - * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + * @author Simon Thoresen */ -public class TreeNetParserTestCase extends TestCase { +public class TreeNetParserTestCase { private static final boolean WRITE_FILES = false; + @Test public void testRankingExpression() { for (int i = 1; i <= 8; ++i) { String inputFile = String.format("src/test/files/treenet%02d.model", i); @@ -76,4 +83,5 @@ public class TreeNetParserTestCase extends TestCase { throw new AssertionError(e); } } + } |