mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
Add test case to BinaryPow Algorithm (#3177)
Co-authored-by: Yang Libin <contact@yanglibin.info>
This commit is contained in:
@ -21,29 +21,4 @@ public class BinaryPow {
|
|||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Function for testing binary exponentiation
|
|
||||||
*
|
|
||||||
* @param a the base
|
|
||||||
* @param p the exponent
|
|
||||||
*/
|
|
||||||
public static void test(int a, int p) {
|
|
||||||
int res = binPow(a, p);
|
|
||||||
assert res == (int) Math.pow(a, p) : "Incorrect Implementation";
|
|
||||||
System.out.println(a + "^" + p + ": " + res);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Main Function to call tests
|
|
||||||
*
|
|
||||||
* @param args System Line Arguments
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// prints 2^15: 32768
|
|
||||||
test(2, 15);
|
|
||||||
|
|
||||||
// prints 3^9: 19683
|
|
||||||
test(3, 9);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
16
src/test/java/com/thealgorithms/maths/BinaryPowTest.java
Normal file
16
src/test/java/com/thealgorithms/maths/BinaryPowTest.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class BinaryPowTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user