You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2019/01/29 05:55:16 UTC

[mina-sshd] 02/05: [SSHD-883] Stricter parameters checking and clearer behavior in NumberUtils#getNextPowerOf2

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

lgoldstein pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git

commit 88bbe37c211659358941affcbbbf7052bfaabb6b
Author: Lyor Goldstein <lg...@apache.org>
AuthorDate: Sun Jan 13 08:35:21 2019 +0200

    [SSHD-883] Stricter parameters checking and clearer behavior in NumberUtils#getNextPowerOf2
---
 .../org/apache/sshd/common/util/NumberUtils.java   | 40 ++++++++--------------
 .../apache/sshd/common/util/NumberUtilsTest.java   | 28 ++++++---------
 2 files changed, 24 insertions(+), 44 deletions(-)

diff --git a/sshd-common/src/main/java/org/apache/sshd/common/util/NumberUtils.java b/sshd-common/src/main/java/org/apache/sshd/common/util/NumberUtils.java
index 7c71a3d..5cfe631 100644
--- a/sshd-common/src/main/java/org/apache/sshd/common/util/NumberUtils.java
+++ b/sshd-common/src/main/java/org/apache/sshd/common/util/NumberUtils.java
@@ -23,7 +23,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
-import java.util.stream.IntStream;
 
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
@@ -37,40 +36,29 @@ public final class NumberUtils {
         GenericUtils.unmodifiableList(
             Byte.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE);
 
-    /**
-     * A {@link List} containing all the pure powers of 2 for a {@code long}
-     * value. The value at index <I>n</I> is 2 to the power of <I>n</I>
-     */
-    public static final List<Long> POWERS_OF_TWO =
-        GenericUtils.unmodifiableList(IntStream.range(0, 64).mapToObj(i -> 1L << i));
-
     private NumberUtils() {
         throw new UnsupportedOperationException("No instance");
     }
 
-    public static boolean isPowerOf2(long value) {
-        for (Long l : POWERS_OF_TWO) {
-            if (value == l) {
-                return true;
-            }
-        }
-
-        return false;
-    }
-
-    public static long getNextPowerOf2(long value) {
-        long j = 1L;
-        while (j < value && j > 0) {
-            j <<= 1;
+    /**
+     * @param value The original (non-negative) value
+     * @return The closest <U>positive</U> power of 2 that is greater or equal to the value.
+     * If none can be found then returns the original value
+     */
+    public static int getNextPowerOf2(int value) {
+        if (value < 0) {
+            throw new IllegalArgumentException("Negative value N/A: " + value);
         }
-        return j;
-    }
 
-    public static int getNextPowerOf2(int value) {
         int j = 1;
-        while (j < value && j > 0) {
+        while (j < value) {
             j <<= 1;
+            // Did we stumble onto the realm of values beyond 2GB ?
+            if (j <= 0) {
+                return value;
+            }
         }
+
         return j;
     }
 
diff --git a/sshd-common/src/test/java/org/apache/sshd/common/util/NumberUtilsTest.java b/sshd-common/src/test/java/org/apache/sshd/common/util/NumberUtilsTest.java
index b106058..16ee564 100644
--- a/sshd-common/src/test/java/org/apache/sshd/common/util/NumberUtilsTest.java
+++ b/sshd-common/src/test/java/org/apache/sshd/common/util/NumberUtilsTest.java
@@ -37,32 +37,24 @@ public class NumberUtilsTest extends JUnitTestSupport {
     }
 
     @Test
-    public void testPowersOf2List() {
-        assertEquals("Mismatched values size for " + NumberUtils.POWERS_OF_TWO, Long.SIZE, GenericUtils.size(NumberUtils.POWERS_OF_TWO));
-        long expected = 1L;
-        for (int index = 0; index < Long.SIZE; index++, expected <<= 1) {
-            Long actual = NumberUtils.POWERS_OF_TWO.get(index);
-            assertEquals("Mismatched value at index=" + index, Long.toHexString(expected), Long.toHexString(actual));
-        }
-    }
-
-    @Test
-    public void testNextPowerOf2() {
-        for (Long v : NumberUtils.POWERS_OF_TWO) {
-            long expected = v;
-            if (expected > 2L) {
-                assertEquals("Mismatched lower bound value", expected, NumberUtils.getNextPowerOf2(expected - 1L));
+    public void testIntNextPowerOf2() {
+        int expected = 1;
+        for (int index = 0; index < Integer.SIZE; expected <<= 1, index++) {
+            if (expected > 2) {
+                assertEquals("Mismatched lower bound value", expected, NumberUtils.getNextPowerOf2(expected - 1));
             }
 
-            if (expected > 0L) {    // avoid the negative value
+            if (expected > 0) {    // avoid the negative value
                 assertEquals("Mismatched exact value", expected, NumberUtils.getNextPowerOf2(expected));
             }
         }
     }
 
     @Test
-    public void testNextPowerOf2Max() {
-        assertTrue(NumberUtils.getNextPowerOf2(1073741872) < 0);
+    public void testIntNextPowerOf2Overflow() {
+        int expected = Integer.MAX_VALUE - Byte.SIZE;
+        int actual = NumberUtils.getNextPowerOf2(expected);
+        assertEquals(expected, actual);
     }
 
     @Test