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 2021/11/24 13:05:54 UTC
[commons-numbers] 03/04: Test isPowerOfTwo with negative arguments and all true powers up to 2^62
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-numbers.git
commit 41d4a3fa6d982167b47d62f0f7b2ac2383141dcc
Author: aherbert <ah...@apache.org>
AuthorDate: Wed Nov 24 12:54:45 2021 +0000
Test isPowerOfTwo with negative arguments and all true powers up to 2^62
---
.../commons/numbers/core/ArithmeticUtilsTest.java | 19 +++++++++++++++++--
src/changes/changes.xml | 3 ++-
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
index 5f466a2..9652c5b 100644
--- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
+++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
@@ -442,6 +442,10 @@ class ArithmeticUtilsTest {
@Test
void testIsPowerOfTwo() {
+ Assertions.assertFalse(ArithmeticUtils.isPowerOfTwo(-1));
+ Assertions.assertFalse(ArithmeticUtils.isPowerOfTwo(Integer.MIN_VALUE));
+
+ // Small numbers in [0, 1024]
final int n = 1025;
final boolean[] expected = new boolean[n];
Arrays.fill(expected, false);
@@ -449,8 +453,19 @@ class ArithmeticUtilsTest {
expected[i] = true;
}
for (int i = 0; i < expected.length; i++) {
- final boolean actual = ArithmeticUtils.isPowerOfTwo(i);
- Assertions.assertEquals(expected[i], actual, Integer.toString(i));
+ final int value = i;
+ final boolean actual = ArithmeticUtils.isPowerOfTwo(value);
+ Assertions.assertEquals(expected[i], actual, () -> Integer.toString(value));
+ }
+
+ // All powers up to 2^62
+ for (int i = 0; i <= 62; i++) {
+ final long value = 1L << i;
+ Assertions.assertTrue(ArithmeticUtils.isPowerOfTwo(value), () -> Long.toString(value));
+ if (value >= 4) {
+ Assertions.assertFalse(ArithmeticUtils.isPowerOfTwo(value + 1), () -> Long.toString(value + 1));
+ Assertions.assertFalse(ArithmeticUtils.isPowerOfTwo(value - 1), () -> Long.toString(value - 1));
+ }
}
}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 128245e..d782f18 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -74,7 +74,8 @@ N.B. the Performance testing module requires Java 9+.
">
<action dev="aherbert" type="add" issue="175">
"GeneralizedContinuedFraction": A continued fraction class to compute using a generator.
- Allows evaluation of continued fractions from a regular series.
+ Allows evaluation of continued fractions from a regular series where coefficients can
+ be computed iteratively from the previous coefficients.
</action>
<action dev="aherbert" type="fix" issue="173">
"ContinuedFraction": Set a minimum bound on the relative error epsilon. Prevents