Skip to content

Commit

Permalink
Merge branch 'master' into ecr-2708
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-timofeev committed Dec 12, 2018
2 parents bc5936d + dfba941 commit 28ba8ab
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 21 deletions.
Expand Up @@ -24,6 +24,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -45,21 +46,19 @@ void commonPrefixCommutative(DbKey firstKey, DbKey secondKey, DbKey expectedResu
assertThat(actualCommonPrefixKey, equalTo(expectedResultKey));
}

@ParameterizedTest(name = "{index} => description={3}")
@MethodSource("testData")
void commonPrefixOfSelf(DbKey firstKey, DbKey secondKey, DbKey expectedResultKey,
String description) {
DbKey commonPrefix = firstKey.commonPrefix(firstKey);
assertThat(commonPrefix, sameInstance(firstKey));
@ParameterizedTest
@MethodSource("uniqueTestKeys")
void commonPrefixOfSelf(DbKey key) {
DbKey commonPrefix = key.commonPrefix(key);
assertThat(commonPrefix, sameInstance(key));
}

@ParameterizedTest(name = "{index} => description={3}")
@MethodSource("testData")
void commonPrefixOfEqualKey(DbKey firstKey, DbKey secondKey, DbKey expectedResultKey,
String description) {
DbKey firstKeyClone = DbKey.fromBytes(firstKey.getRawDbKey());
DbKey commonPrefix = firstKey.commonPrefix(firstKeyClone);
assertThat(commonPrefix, equalTo(firstKey));
@ParameterizedTest
@MethodSource("uniqueTestKeys")
void commonPrefixOfEqualKey(DbKey key) {
DbKey keyClone = DbKey.fromBytes(key.getRawDbKey());
DbKey commonPrefix = key.commonPrefix(keyClone);
assertThat(commonPrefix, equalTo(key));
}

private static List<Arguments> testData() {
Expand Down Expand Up @@ -148,4 +147,12 @@ private static List<Arguments> testData() {
"[1111 1111 | 10_11] | [1111 1111 | 10] -> [1111 1111 | 10]")
);
}

private static Stream<DbKey> uniqueTestKeys() {
return testData().stream()
.flatMap(args ->
Stream.of(args.get()[0], args.get()[1]))
.map(o -> (DbKey) o)
.distinct();
}
}
Expand Up @@ -16,7 +16,11 @@

package com.exonum.binding.common.proofs.map;

import static com.exonum.binding.common.proofs.map.DbKeyComparisonParameterizedTest.ComparisonResult.EQUAL;
import static com.exonum.binding.common.proofs.map.DbKeyComparisonParameterizedTest.ComparisonResult.GREATER;
import static com.exonum.binding.common.proofs.map.DbKeyComparisonParameterizedTest.ComparisonResult.LESS;
import static com.exonum.binding.common.proofs.map.DbKeyTestUtils.branchKeyFromPrefix;
import static com.exonum.binding.common.proofs.map.DbKeyTestUtils.leafKeyFromPrefix;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

Expand All @@ -28,13 +32,23 @@

class DbKeyComparisonParameterizedTest {

@ParameterizedTest(name = "{index} => description={3}")
@ParameterizedTest(name = "[{index}] => {3}")
@MethodSource("testData")
void dbKeyCompareTest(DbKey firstKey, DbKey secondKey, boolean expectedResult,
void dbKeyCompareTest(DbKey firstKey, DbKey secondKey, ComparisonResult expectedResult,
String description) {
int comparisonResult = firstKey.compareTo(secondKey);

assertThat(comparisonResult > 0, equalTo(expectedResult));
assertThat(ComparisonResult.fromInt(comparisonResult), equalTo(expectedResult));
}

@ParameterizedTest
@MethodSource("testData")
void dbKeyComparatorSymmetric(DbKey firstKey, DbKey secondKey, ComparisonResult firstToSecond,
String description) {
int comparisonResult = secondKey.compareTo(firstKey);

ComparisonResult expectedResult = firstToSecond.opposite();
assertThat(ComparisonResult.fromInt(comparisonResult), equalTo(expectedResult));
}

private static List<Arguments> testData() {
Expand All @@ -43,23 +57,62 @@ private static List<Arguments> testData() {
Arguments.of(
branchKeyFromPrefix("1100"),
branchKeyFromPrefix("0"),
true,
GREATER,
"[1100] > [0]"),
Arguments.of(
branchKeyFromPrefix("1100"),
branchKeyFromPrefix("001101"),
true,
GREATER,
"[1100] > [001101]"),
Arguments.of(
branchKeyFromPrefix("001101"),
branchKeyFromPrefix("00110101"),
false,
LESS,
"[001101] < [00110101]"),
Arguments.of(
branchKeyFromPrefix("101"),
branchKeyFromPrefix("110"),
false,
"[101] < [110]")
LESS,
"[101] < [110]"),
Arguments.of(
branchKeyFromPrefix("1"),
branchKeyFromPrefix("1"),
EQUAL,
"[1] = [1] (branches)"),
Arguments.of(
leafKeyFromPrefix("0"),
leafKeyFromPrefix("0"),
EQUAL,
"[0] = [0] (leaves)")
);
}

enum ComparisonResult {
GREATER,
LESS,
EQUAL;

static ComparisonResult fromInt(int compareCode) {
if (compareCode < 0) {
return LESS;
} else if (0 < compareCode) {
return GREATER;
} else {
return EQUAL;
}
}

/**
* Returns the opposite comparison result — the one you would obtain if you swap
* the arguments positions.
*/
ComparisonResult opposite() {
switch (this) {
case GREATER: return LESS;
case LESS: return GREATER;
case EQUAL: return EQUAL;
default: throw new AssertionError("unreachable");
}
}
}
}
Expand Up @@ -70,6 +70,9 @@ private static List<Arguments> testData() {
return Arrays.asList(
// "A <- B" reads "A is a prefix of B"
// "!P" reads "not P"
// TODO: Consider using bitstrings (e.g, DbKeyTestUtils#keyFromString) to simplify
// these parameters: (1) get rid of explicit length; (2) use sane order.
// With that these parameters could be specified as a @CsvSource: ECR-2744
Arguments.of(bytes(), 0, bytes(), 0, true, "[] <- []"),
Arguments.of(bytes(), 0, bytes(), 2, true, "[] <- [00]"),
Arguments.of(bytes(), 0, bytes(0xE), 4, true, "[] <- [1110]"),
Expand Down

0 comments on commit 28ba8ab

Please sign in to comment.