You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2023/06/13 16:29:16 UTC
[commons-collections] 01/02: Move mod tests to BitMapTest
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git
commit 1624bdb89eab7a9a0d9018982ca62d134b12af95
Author: Alex Herbert <a....@sussex.ac.uk>
AuthorDate: Tue Jun 13 17:16:21 2023 +0100
Move mod tests to BitMapTest
Consolidate the cases from EnhancedDoubleHasherTest and BitMapTest.
Use Long.remainderUnsigned as the reference result.
---
.../collections4/bloomfilter/BitMapTest.java | 34 ++++++++++++++++------
.../bloomfilter/EnhancedDoubleHasherTest.java | 11 -------
2 files changed, 25 insertions(+), 20 deletions(-)
diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapTest.java
index ab0444e99..00d5df451 100644
--- a/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapTest.java
+++ b/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapTest.java
@@ -104,18 +104,34 @@ public class BitMapTest {
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));
+ }
}
diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/EnhancedDoubleHasherTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/EnhancedDoubleHasherTest.java
index 107a06320..e4e64786f 100644
--- a/src/test/java/org/apache/commons/collections4/bloomfilter/EnhancedDoubleHasherTest.java
+++ b/src/test/java/org/apache/commons/collections4/bloomfilter/EnhancedDoubleHasherTest.java
@@ -93,15 +93,4 @@ public class EnhancedDoubleHasherTest extends AbstractHasherTest {
// 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));
- }
- }
- }
}