Skip to content

Commit

Permalink
Move mod tests to BitMapTest
Browse files Browse the repository at this point in the history
Consolidate the cases from EnhancedDoubleHasherTest and BitMapTest.

Use Long.remainderUnsigned as the reference result.
  • Loading branch information
aherbert committed Jun 13, 2023
1 parent 81834a6 commit 1624bdb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,34 @@ public final void testContains() {
assertTrue(BitMap.contains(ary, 64));
}

private void assertMod(long l, int i) {
assertEquals(Math.floorMod(l, i), BitMap.mod(l, i));
@Test
public void testMod() {
for (final long dividend : new long[] {0, -1, -2, -3, -6378683, -23567468136887892L,
Long.MIN_VALUE, 345, 678686, 67868768686878924L, Long.MAX_VALUE, Long.MAX_VALUE - 1}) {
for (final int divisor : new int[] {1, 2, 3, 5, 13, Integer.MAX_VALUE, Integer.MAX_VALUE - 1}) {
assertMod(dividend, divisor);
}
}
}

@Test
public final void testMod() {
assertMod(Long.MAX_VALUE, Integer.MAX_VALUE);
assertMod(Long.MAX_VALUE, Integer.MAX_VALUE-1);
assertThrows(ArithmeticException.class, () -> BitMap.mod(Long.MAX_VALUE, 0));
assertMod(Long.MAX_VALUE-1, Integer.MAX_VALUE);
assertMod(Long.MAX_VALUE-1, Integer.MAX_VALUE-1);
assertMod(0, Integer.MAX_VALUE);
public void testModEdgeCases() {
for (final long dividend : new long[] {0, -1, 1, Long.MAX_VALUE}) {
assertThrows(ArithmeticException.class, () -> BitMap.mod(dividend, 0));
}
assertNotEquals(Math.floorMod(5, -1), BitMap.mod(5, -1));
}

/**
* Assert the {@link BitMap#mod(long, int)} method functions as an unsigned modulus.
*
* @param dividend the dividend
* @param divisor the divisor
*/
private void assertMod(long dividend, int divisor) {
assertTrue(divisor > 0 && divisor <= Integer.MAX_VALUE,
"Incorrect usage. Divisor must be strictly positive.");
assertEquals((int) Long.remainderUnsigned(dividend, divisor), BitMap.mod(dividend, divisor),
() -> String.format("failure with dividend=%s and divisor=%s.", dividend, divisor));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,4 @@ public void testByteConstructor() {
// test empty buffer
assertThrows(IllegalArgumentException.class, () -> new EnhancedDoubleHasher(new byte[0]));
}

@Test
void testModEdgeCases() {
for (final long dividend : new long[] {-1, -2, -3, -6378683, -23567468136887892L, Long.MIN_VALUE, 345, 678686,
67868768686878924L, Long.MAX_VALUE}) {
for (final int divisor : new int[] {1, 2, 3, 5, 13, Integer.MAX_VALUE}) {
assertEquals((int) Long.remainderUnsigned(dividend, divisor), BitMap.mod(dividend, divisor),
() -> String.format("failure with dividend=%s and divisor=%s.", dividend, divisor));
}
}
}
}

0 comments on commit 1624bdb

Please sign in to comment.