Add test case to BinaryPow Algorithm (#3177)

Co-authored-by: Yang Libin <contact@yanglibin.info>
This commit is contained in:
Ankush Banik
2022-07-04 18:23:56 +05:30
committed by GitHub
parent 8b8e98e89a
commit f273b30998
2 changed files with 16 additions and 25 deletions

View File

@ -21,29 +21,4 @@ public class BinaryPow {
}
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);
}
}

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