From fae3497c7ec5bb158676c11570efdea4a6f6937f Mon Sep 17 00:00:00 2001 From: audhee Date: Fri, 20 Jun 2025 13:31:03 +0530 Subject: [PATCH 1/2] Added more test cases as per the issue #6282 --- .../thealgorithms/maths/BinaryPowTest.java | 128 +++++++++++++++++- 1 file changed, 124 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/BinaryPowTest.java b/src/test/java/com/thealgorithms/maths/BinaryPowTest.java index f9b019e8fad4..0a92e1eb7004 100644 --- a/src/test/java/com/thealgorithms/maths/BinaryPowTest.java +++ b/src/test/java/com/thealgorithms/maths/BinaryPowTest.java @@ -1,16 +1,136 @@ package com.thealgorithms.maths; -import static org.junit.jupiter.api.Assertions.assertEquals; - +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; public class BinaryPowTest { + // --- Existing Tests (from your provided code) --- @Test + @DisplayName("Original tests for common cases") void testBinPow() { - assertEquals(4, BinaryPow.binPow(2, 2)); + assertEquals(4, BinaryPow.binPow(2, 2)); // No 'L' needed for int literals assertEquals(256, BinaryPow.binPow(4, 4)); assertEquals(729, BinaryPow.binPow(9, 3)); assertEquals(262144, BinaryPow.binPow(8, 6)); } -} + + // --- New Comprehensive Tests (integrated and adjusted for binPow(int, int)) --- + + @Test + @DisplayName("binPow(2, 3) should return 8") + void testBinPow_basicCase1() { + assertEquals(8, BinaryPow.binPow(2, 3)); + } + + @Test + @DisplayName("binPow(5, 2) should return 25") + void testBinPow_basicCase2() { + assertEquals(25, BinaryPow.binPow(5, 2)); + } + + @Test + @DisplayName("binPow(10, 4) should return 10000") + void testBinPow_basicCase3() { + assertEquals(10000, BinaryPow.binPow(10, 4)); + } + + // --- Edge Cases and Special Values --- + + @Test + @DisplayName("binPow(base, 0) should return 1 for non-zero base") + void testBinPow_exponentZero() { + assertEquals(1, BinaryPow.binPow(5, 0)); + assertEquals(1, BinaryPow.binPow(1, 0)); + assertEquals(1, BinaryPow.binPow(-10, 0)); + // Removed Long.MAX_VALUE as it exceeds int range + } + + @Test + @DisplayName("binPow(0, 0) should return 1 (as per common convention for this algorithm)") + void testBinPow_zeroToThePowerOfZero() { + assertEquals(1, BinaryPow.binPow(0, 0)); + } + + @Test + @DisplayName("binPow(base, 1) should return base") + void testBinPow_exponentOne() { + assertEquals(7, BinaryPow.binPow(7, 1)); + assertEquals(-3, BinaryPow.binPow(-3, 1)); + assertEquals(1, BinaryPow.binPow(1, 1)); + } + + @Test + @DisplayName("binPow(0, positive_exponent) should return 0") + void testBinPow_zeroBasePositiveExponent() { + assertEquals(0, BinaryPow.binPow(0, 5)); + assertEquals(0, BinaryPow.binPow(0, 100)); + } + + @Test + @DisplayName("binPow(1, any_exponent) should return 1") + void testBinPow_baseOne() { + assertEquals(1, BinaryPow.binPow(1, 0)); + assertEquals(1, BinaryPow.binPow(1, 5)); + assertEquals(1, BinaryPow.binPow(1, 100)); + } + + @Test + @DisplayName("binPow(-1, even_exponent) should return 1") + void testBinPow_negativeOneEvenExponent() { + assertEquals(1, BinaryPow.binPow(-1, 0)); + assertEquals(1, BinaryPow.binPow(-1, 2)); + assertEquals(1, BinaryPow.binPow(-1, 100)); + } + + @Test + @DisplayName("binPow(-1, odd_exponent) should return -1") + void testBinPow_negativeOneOddExponent() { + assertEquals(-1, BinaryPow.binPow(-1, 1)); + assertEquals(-1, BinaryPow.binPow(-1, 3)); + assertEquals(-1, BinaryPow.binPow(-1, 99)); + } + + @Test + @DisplayName("binPow(negative_base, even_exponent) should return positive result") + void testBinPow_negativeBaseEvenExponent() { + assertEquals(16, BinaryPow.binPow(-2, 4)); + assertEquals(81, BinaryPow.binPow(-3, 4)); + } + + @Test + @DisplayName("binPow(negative_base, odd_exponent) should return negative result") + void testBinPow_negativeBaseOddExponent() { + assertEquals(-8, BinaryPow.binPow(-2, 3)); + assertEquals(-243, BinaryPow.binPow(-3, 5)); + } + + // --- Exception Handling for Negative Exponent --- + + @Test + @DisplayName("Should throw IllegalArgumentException for negative exponent") + void testBinPow_negativeExponentThrowsException() { + assertThrows(IllegalArgumentException.class, () -> BinaryPow.binPow(2, -1)); + assertThrows(IllegalArgumentException.class, () -> BinaryPow.binPow(5, -10)); + assertThrows(IllegalArgumentException.class, () -> BinaryPow.binPow(0, -5)); + assertThrows(IllegalArgumentException.class, () -> BinaryPow.binPow(1, -2)); + } + + // --- Large Number Tests (within int range, careful with potential overflow) --- + + @Test + @DisplayName("binPow(2, 30) should return 1073741824 (fits in int)") + void testBinPow_largeExponentFitsInInt() { + // 2^30 = 1,073,741,824, which fits within Integer.MAX_VALUE (2,147,483,647) + assertEquals(1073741824, BinaryPow.binPow(2, 30)); + } + + @Test + @DisplayName("binPow(7, 10) should return 282475249 (fits in int)") + void testBinPow_anotherLargeExponentFitsInInt() { + // 7^10 = 282,475,249, which fits within Integer.MAX_VALUE + assertEquals(282475249, BinaryPow.binPow(7, 10)); + } +} \ No newline at end of file From ef9bb1970e11bf2112918383cb04748d7ffe7d4f Mon Sep 17 00:00:00 2001 From: audhee Date: Fri, 20 Jun 2025 14:01:33 +0530 Subject: [PATCH 2/2] Formatted the code manually --- .../thealgorithms/maths/BinaryPowTest.java | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/BinaryPowTest.java b/src/test/java/com/thealgorithms/maths/BinaryPowTest.java index 0a92e1eb7004..8d0890630c4c 100644 --- a/src/test/java/com/thealgorithms/maths/BinaryPowTest.java +++ b/src/test/java/com/thealgorithms/maths/BinaryPowTest.java @@ -7,18 +7,15 @@ public class BinaryPowTest { - // --- Existing Tests (from your provided code) --- @Test @DisplayName("Original tests for common cases") void testBinPow() { - assertEquals(4, BinaryPow.binPow(2, 2)); // No 'L' needed for int literals + assertEquals(4, BinaryPow.binPow(2, 2)); assertEquals(256, BinaryPow.binPow(4, 4)); assertEquals(729, BinaryPow.binPow(9, 3)); assertEquals(262144, BinaryPow.binPow(8, 6)); } - // --- New Comprehensive Tests (integrated and adjusted for binPow(int, int)) --- - @Test @DisplayName("binPow(2, 3) should return 8") void testBinPow_basicCase1() { @@ -37,19 +34,16 @@ void testBinPow_basicCase3() { assertEquals(10000, BinaryPow.binPow(10, 4)); } - // --- Edge Cases and Special Values --- - @Test @DisplayName("binPow(base, 0) should return 1 for non-zero base") void testBinPow_exponentZero() { assertEquals(1, BinaryPow.binPow(5, 0)); assertEquals(1, BinaryPow.binPow(1, 0)); assertEquals(1, BinaryPow.binPow(-10, 0)); - // Removed Long.MAX_VALUE as it exceeds int range } @Test - @DisplayName("binPow(0, 0) should return 1 (as per common convention for this algorithm)") + @DisplayName("binPow(0, 0) should return 1 (as per convention)") void testBinPow_zeroToThePowerOfZero() { assertEquals(1, BinaryPow.binPow(0, 0)); } @@ -107,8 +101,6 @@ void testBinPow_negativeBaseOddExponent() { assertEquals(-243, BinaryPow.binPow(-3, 5)); } - // --- Exception Handling for Negative Exponent --- - @Test @DisplayName("Should throw IllegalArgumentException for negative exponent") void testBinPow_negativeExponentThrowsException() { @@ -118,19 +110,15 @@ void testBinPow_negativeExponentThrowsException() { assertThrows(IllegalArgumentException.class, () -> BinaryPow.binPow(1, -2)); } - // --- Large Number Tests (within int range, careful with potential overflow) --- - @Test - @DisplayName("binPow(2, 30) should return 1073741824 (fits in int)") + @DisplayName("binPow(2, 30) should return 1073741824") void testBinPow_largeExponentFitsInInt() { - // 2^30 = 1,073,741,824, which fits within Integer.MAX_VALUE (2,147,483,647) assertEquals(1073741824, BinaryPow.binPow(2, 30)); } @Test - @DisplayName("binPow(7, 10) should return 282475249 (fits in int)") + @DisplayName("binPow(7, 10) should return 282475249") void testBinPow_anotherLargeExponentFitsInInt() { - // 7^10 = 282,475,249, which fits within Integer.MAX_VALUE assertEquals(282475249, BinaryPow.binPow(7, 10)); } -} \ No newline at end of file +}