diff options
Diffstat (limited to 'predicate-search-core/src/test/java/com/yahoo/document/predicate/BinaryFormatTest.java')
-rw-r--r-- | predicate-search-core/src/test/java/com/yahoo/document/predicate/BinaryFormatTest.java | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/predicate-search-core/src/test/java/com/yahoo/document/predicate/BinaryFormatTest.java b/predicate-search-core/src/test/java/com/yahoo/document/predicate/BinaryFormatTest.java new file mode 100644 index 00000000000..44477423fdb --- /dev/null +++ b/predicate-search-core/src/test/java/com/yahoo/document/predicate/BinaryFormatTest.java @@ -0,0 +1,124 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.document.predicate; + +import com.yahoo.slime.Inspector; +import com.yahoo.slime.Slime; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a> + */ +public class BinaryFormatTest { + + @Test + public void requireThatEncodeNullThrows() { + try { + BinaryFormat.encode(null); + fail(); + } catch (NullPointerException e) { + assertEquals("predicate", e.getMessage()); + } + } + + @Test + public void requireThatDecodeNullThrows() { + try { + BinaryFormat.decode(null); + fail(); + } catch (NullPointerException e) { + assertEquals("buf", e.getMessage()); + } + } + + @Test + public void requireThatDecodeEmptyThrows() { + try { + BinaryFormat.decode(new byte[0]); + fail(); + } catch (UnsupportedOperationException e) { + assertEquals("0", e.getMessage()); + } + } + + @Test + public void requireThatConjunctionCanBeSerialized() { + assertSerialize(new Conjunction(new FeatureSet("foo", "bar"), new FeatureSet("baz", "cox"))); + } + + @Test + public void requireThatDisjunctionCanBeSerialized() { + assertSerialize(new Disjunction(new FeatureSet("foo", "bar"), new FeatureSet("baz", "cox"))); + } + + @Test + public void requireThatFeatureRangeCanBeSerialized() { + assertSerialize(new FeatureRange("foo", null, null)); + assertSerialize(new FeatureRange("foo", null, 9L)); + assertSerialize(new FeatureRange("foo", 6L, null)); + assertSerialize(new FeatureRange("foo", 6L, 9L)); + } + + @Test + public void requireThatPartitionedFeatureRangeCanBeSerialized() { + FeatureRange expected = new FeatureRange("foo", 8L, 20L); + FeatureRange f = new FeatureRange("foo", 8L, 20L); + f.addPartition(new RangeEdgePartition("foo=0", 0, 8, -1)); + f.addPartition(new RangeEdgePartition("foo=20", 20, 0, 0)); + f.addPartition(new RangePartition("foo", 10, 19, false)); + assertSerializesTo(expected, f); + Slime slime = com.yahoo.slime.BinaryFormat.decode(BinaryFormat.encode(f)); + assertEquals(BinaryFormat.TYPE_FEATURE_RANGE, slime.get().field(BinaryFormat.NODE_TYPE).asLong()); + Inspector in1 = slime.get().field(BinaryFormat.HASHED_PARTITIONS); + assertEquals(1, in1.entries()); + assertEquals(0xf2b6d1cc6322cb99L, in1.entry(0).asLong()); + Inspector in2 = slime.get().field(BinaryFormat.HASHED_EDGE_PARTITIONS); + assertEquals(2, in2.entries()); + Inspector obj1 = in2.entry(0); + assertEquals(0xb2b301e26efffdc2L, obj1.field(BinaryFormat.HASH).asLong()); + assertEquals(0, obj1.field(BinaryFormat.VALUE).asLong()); + assertEquals(0x80000008L, obj1.field(BinaryFormat.PAYLOAD).asLong()); + Inspector obj2 = in2.entry(1); + assertEquals(0x22acb2ed72523c36L, obj2.field(BinaryFormat.HASH).asLong()); + assertEquals(20, obj2.field(BinaryFormat.VALUE).asLong()); + assertEquals(0x40000001L, obj2.field(BinaryFormat.PAYLOAD).asLong()); + } + + @Test + public void requireThatFeatureSetCanBeSerialized() { + assertSerialize(new FeatureSet("foo")); + assertSerialize(new FeatureSet("foo", "bar")); + assertSerialize(new FeatureSet("foo", "bar", "baz")); + } + + @Test + public void requireThatNegationCanBeSerialized() { + assertSerialize(new Negation(new FeatureSet("foo", "bar"))); + } + + @Test + public void requireThatBooleanCanBeSerialized() { + assertSerialize(new BooleanPredicate(true)); + assertSerialize(new BooleanPredicate(false)); + } + + @Test + public void requireThatUnknownNodeThrows() { + try { + BinaryFormat.encode(SimplePredicates.newString("foo")); + fail(); + } catch (UnsupportedOperationException e) { + + } + } + + private static void assertSerializesTo(Predicate expected, Predicate predicate) { + assertEquals(expected, BinaryFormat.decode(BinaryFormat.encode(predicate))); + } + + private static void assertSerialize(Predicate predicate) { + assertSerializesTo(predicate, predicate); + } +} |