Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 110 additions & 2 deletions src/test/java/com/thealgorithms/maths/BinaryPowTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,124 @@
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 {

@Test
@DisplayName("Original tests for common cases")
void testBinPow() {
assertEquals(4, BinaryPow.binPow(2, 2));
assertEquals(256, BinaryPow.binPow(4, 4));
assertEquals(729, BinaryPow.binPow(9, 3));
assertEquals(262144, BinaryPow.binPow(8, 6));
}

@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));
}

@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));
}

@Test
@DisplayName("binPow(0, 0) should return 1 (as per convention)")
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));
}

@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));
}

@Test
@DisplayName("binPow(2, 30) should return 1073741824")
void testBinPow_largeExponentFitsInInt() {
assertEquals(1073741824, BinaryPow.binPow(2, 30));
}

@Test
@DisplayName("binPow(7, 10) should return 282475249")
void testBinPow_anotherLargeExponentFitsInInt() {
assertEquals(282475249, BinaryPow.binPow(7, 10));
}
}
Loading