From f273b30998b023e159772ebe33c89ef75e741951 Mon Sep 17 00:00:00 2001 From: Ankush Banik <86042508+Ankush263@users.noreply.github.com> Date: Mon, 4 Jul 2022 18:23:56 +0530 Subject: [PATCH] Add test case to BinaryPow Algorithm (#3177) Co-authored-by: Yang Libin --- .../com/thealgorithms/maths/BinaryPow.java | 25 ------------------- .../thealgorithms/maths/BinaryPowTest.java | 16 ++++++++++++ 2 files changed, 16 insertions(+), 25 deletions(-) create mode 100644 src/test/java/com/thealgorithms/maths/BinaryPowTest.java diff --git a/src/main/java/com/thealgorithms/maths/BinaryPow.java b/src/main/java/com/thealgorithms/maths/BinaryPow.java index 64b4e34fd..d431d58b9 100644 --- a/src/main/java/com/thealgorithms/maths/BinaryPow.java +++ b/src/main/java/com/thealgorithms/maths/BinaryPow.java @@ -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); - } } diff --git a/src/test/java/com/thealgorithms/maths/BinaryPowTest.java b/src/test/java/com/thealgorithms/maths/BinaryPowTest.java new file mode 100644 index 000000000..ec4005d4e --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/BinaryPowTest.java @@ -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)); + } +}