diff options
-rw-r--r-- | container-search/src/main/java/com/yahoo/prelude/hitfield/RawBase64.java | 8 | ||||
-rw-r--r-- | container-search/src/test/java/com/yahoo/prelude/hitfield/RawBase64TestCase.java | 43 |
2 files changed, 50 insertions, 1 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/hitfield/RawBase64.java b/container-search/src/main/java/com/yahoo/prelude/hitfield/RawBase64.java index 87b073b98c5..2071e43f54c 100644 --- a/container-search/src/main/java/com/yahoo/prelude/hitfield/RawBase64.java +++ b/container-search/src/main/java/com/yahoo/prelude/hitfield/RawBase64.java @@ -1,18 +1,24 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.prelude.hitfield; +import java.util.Arrays; import java.util.Base64; /** * @author baldersheim */ -public class RawBase64 { +public class RawBase64 implements Comparable<RawBase64> { private final byte[] content; public RawBase64(byte[] content) { this.content = content; } @Override + public int compareTo(RawBase64 rhs) { + return Arrays.compareUnsigned(content, rhs.content); + } + + @Override public String toString() { return Base64.getEncoder().encodeToString(content); } diff --git a/container-search/src/test/java/com/yahoo/prelude/hitfield/RawBase64TestCase.java b/container-search/src/test/java/com/yahoo/prelude/hitfield/RawBase64TestCase.java new file mode 100644 index 00000000000..0991f73914f --- /dev/null +++ b/container-search/src/test/java/com/yahoo/prelude/hitfield/RawBase64TestCase.java @@ -0,0 +1,43 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.prelude.hitfield; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author baldersheim + */ +public class RawBase64TestCase { + private static final byte [] first = {0, 1, 2}; + private static final byte [] second = {19, 0}; + private static final byte [] last = {-127, 0}; + private static final byte [] longer = {0, 1, 2, 3}; + @Test + public void requireToStringToProvideBase64Encoding() { + assertEquals("AAEC", new RawBase64(first).toString()); + } + + private void verify(int expected, byte [] a, byte [] b) { + int diff = new RawBase64(a).compareTo(new RawBase64(b)); + if (expected == 0) { + assertEquals(expected, diff); + } else { + assertEquals(expected, diff / Math.abs(diff)); + } + } + + @Test + public void testSortOrder() { + verify(0, first, first); + verify(-1, first, longer); + verify(1, longer, first); + + verify(-1, first, second); + verify(1, second, first); + + verify(-1, first, last); + verify(1, last, first); + } +} |