Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Create a thorough unit test for KeyComparator #10753

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public int compare(final Key first, final Key second) {
if (first == second) return 0;
else if (first == null) return -1;
else if (second == null) return 1;
// Note, record defines equals, but it uses reference equality for reference type members.
// We must not use reference equality here, so we cannot use that.
else if (first.key() == null) return second.key() == null ? 0 : -1;
else if (second.key() == null) return 1;

Expand Down Expand Up @@ -91,20 +93,21 @@ private int compareId(final ContractID leftId, final ContractID rightId) {
final Bytes evmTwo = rightId.evmAddress();
final Long leftNum = leftId.contractNum();
final Long rightNum = rightId.contractNum();
final long firstId = leftNum != null ? leftNum.longValue() : 0L;
final long secondId = rightNum != null ? rightNum.longValue() : 0L;
// default -1 so contractNum sorts "before" evm address
final long firstId = leftNum != null ? leftNum.longValue() : -1L;
final long secondId = rightNum != null ? rightNum.longValue() : -1L;
if (realmOne == realmTwo) {
if (shardOne == shardTwo) {
if (firstId == secondId) {
return compareBytes(evmOne, evmTwo);
} else {
return firstId > secondId ? -1 : 1;
return Long.compare(firstId, secondId);
}
} else {
return shardOne > shardTwo ? -1 : 1;
return Long.compare(shardOne, shardTwo);
}
} else {
return realmOne > realmTwo ? -1 : 1;
return Long.compare(realmOne, realmTwo);
}
}

Expand All @@ -129,7 +132,7 @@ private int compareThreshold(final Key first, final Key second) {

final int leftThreshold = lhs.threshold();
final int rightThreshold = rhs.threshold();
if (leftThreshold != rightThreshold) return leftThreshold - rightThreshold;
if (leftThreshold != rightThreshold) return leftThreshold > rightThreshold ? 1 : -1;

final KeyList leftList = lhs.keys();
final KeyList rightList = rhs.keys();
Expand Down Expand Up @@ -188,10 +191,9 @@ private int compareBytes(final Bytes lhs, final Bytes rhs) {
else {
// left and right length are equal.
for (long offset = 0L; offset < leftLength; offset++) {
// cast because java hates byte (it promotes to int for everything anyway)
final int left = (int) lhs.getByte(offset);
final int right = (int) rhs.getByte(offset);
if (left != right) return left - right;
final byte left = lhs.getByte(offset);
final byte right = rhs.getByte(offset);
if (left != right) return Byte.compareUnsigned(left, right) > 0 ? 1 : -1;
}
}
// nothing differed, so these are equal.
Expand All @@ -203,10 +205,11 @@ private int compareBytes(final Bytes lhs, final Bytes rhs) {
* The "natural" order, in this case, is just the type order in the proto file.
* @param firstKeyType The OneOfType for the first (left hand) key.
* @param secondKeyType The OneOfType for the second (right hand) key.
* @return a value less than, equal to, or greater than 0 indicating if the first key type should be
* sorted before, the same, or after, the second key type.
* @return a value greater than, equal to, or less than 0 indicating if the first key type is
* "greater than" (sorted after), the same as, or "less than" (sorted before),
* the second key type.
*/
private int compareCrossType(final KeyOneOfType firstKeyType, final KeyOneOfType secondKeyType) {
return firstKeyType.protoOrdinal() - secondKeyType.protoOrdinal();
return Integer.compare(firstKeyType.protoOrdinal(), secondKeyType.protoOrdinal());
}
}
Loading
Loading