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:15 UTC

[commons-collections] branch master updated (81834a637 -> 5b668d23c)

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git


    from 81834a637 Move EnhancedDoubleHasher.mod() to a public BitMap API (#396)
     new 1624bdb89 Move mod tests to BitMapTest
     new 5b668d23c Update BitMap.mod javadoc

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../commons/collections4/bloomfilter/BitMap.java   | 13 ++++++---
 .../collections4/bloomfilter/BitMapTest.java       | 34 ++++++++++++++++------
 .../bloomfilter/EnhancedDoubleHasherTest.java      | 11 -------
 3 files changed, 34 insertions(+), 24 deletions(-)


[commons-collections] 01/02: Move mod tests to BitMapTest

Posted by ah...@apache.org.
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));
-            }
-        }
-    }
 }


[commons-collections] 02/02: Update BitMap.mod javadoc

Posted by ah...@apache.org.
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 5b668d23c6a980290f35a848fcac05191bc23488
Author: Alex Herbert <a....@sussex.ac.uk>
AuthorDate: Tue Jun 13 17:27:28 2023 +0100

    Update BitMap.mod javadoc
    
    Add thrown exception when divisor is zero. Add link to reference
    implementation of Long.remainderUnsigned.
    
    Drop redundant @since tag as it matches the entire class @since.
---
 .../org/apache/commons/collections4/bloomfilter/BitMap.java | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/BitMap.java b/src/main/java/org/apache/commons/collections4/bloomfilter/BitMap.java
index 8180af708..58177bebc 100644
--- a/src/main/java/org/apache/commons/collections4/bloomfilter/BitMap.java
+++ b/src/main/java/org/apache/commons/collections4/bloomfilter/BitMap.java
@@ -115,14 +115,19 @@ public class BitMap {
     }
 
     /**
-     * Performs a modulus calculation on an unsigned long and an positive integer divisor.
+     * Performs a modulus calculation on an unsigned long and a positive integer divisor.
+     *
+     * <p>This method computes the same result as {@link Long#remainderUnsigned(long, long)}
+     * but assumes that the divisor is an integer in the range 1 to 2<sup>31</sup> - 1 inclusive,
+     * that is a strictly positive integer size.
      *
      * <p><em>If the divisor is negative the behavior is not defined.</em></p>
      *
-     * @param dividend a unsigned long value to calculate the modulus of.
-     * @param divisor the divisor for the modulus calculation, must be positive.
+     * @param dividend an unsigned long value to calculate the modulus of.
+     * @param divisor the divisor for the modulus calculation, must be strictly positive.
      * @return the remainder or modulus value.
-     * @since 4.5
+     * @throws ArithmeticException if the divisor is zero
+     * @see Long#remainderUnsigned(long, long)
      */
     public static int mod(final long dividend, final int divisor) {
         // See Hacker's Delight (2nd ed), section 9.3.