Move mod tests to BitMapTest

Consolidate the cases from EnhancedDoubleHasherTest and BitMapTest.

Use Long.remainderUnsigned as the reference result.
This commit is contained in:
Alex Herbert 2023-06-13 17:16:21 +01:00
parent 81834a637d
commit 1624bdb89e
2 changed files with 25 additions and 20 deletions

View File

@ -104,18 +104,34 @@ public class BitMapTest {
assertTrue(BitMap.contains(ary, 64)); assertTrue(BitMap.contains(ary, 64));
} }
private void assertMod(long l, int i) { @Test
assertEquals(Math.floorMod(l, i), BitMap.mod(l, i)); 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 @Test
public final void testMod() { public void testModEdgeCases() {
assertMod(Long.MAX_VALUE, Integer.MAX_VALUE); for (final long dividend : new long[] {0, -1, 1, Long.MAX_VALUE}) {
assertMod(Long.MAX_VALUE, Integer.MAX_VALUE-1); assertThrows(ArithmeticException.class, () -> BitMap.mod(dividend, 0));
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);
assertNotEquals(Math.floorMod(5, -1), BitMap.mod(5, -1)); 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));
}
} }

View File

@ -93,15 +93,4 @@ public class EnhancedDoubleHasherTest extends AbstractHasherTest {
// test empty buffer // test empty buffer
assertThrows(IllegalArgumentException.class, () -> new EnhancedDoubleHasher(new byte[0])); 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));
}
}
}
} }