Repository navigation
Expand file tree
/
Copy pathPowerUsingRecursionTest.java
More file actions
40 lines (33 loc) · 1.61 KB
/
Copy pathPowerUsingRecursionTest.java
File metadata and controls
40 lines (33 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
/**
* Test cases for Power using Recursion.
*
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
*/
class PowerUsingRecursionTest {
@ParameterizedTest
@CsvSource({"2.0, 5, 32.0", "2.5, 5, 97.65625", "3.0, 4, 81.0", "5.0, 0, 1.0", "-2.0, 3, -8.0", "-2.0, 4, 16.0", "2.0, -1, 0.5", "2.0, -3, 0.125", "-2.0, -1, -0.5", "-2.0, -2, 0.25", "0.0, 0, 1.0", "0.0, 5, 0.0", "1.0, -5, 1.0", "1.0, 100000, 1.0", "-1.0, 100000, 1.0", "-1.0, 100001, -1.0"})
void testPower(double base, int exponent, double expected) {
assertEquals(expected, PowerUsingRecursion.power(base, exponent), 1e-9);
}
@Test
void testZeroBaseWithNegativeExponentThrowsException() {
assertThrows(ArithmeticException.class, () -> PowerUsingRecursion.power(0.0, -1));
assertThrows(ArithmeticException.class, () -> PowerUsingRecursion.power(0.0, -10));
}
@Test
void testIntegerMinValueExponent() {
assertEquals(1.0, PowerUsingRecursion.power(1.0, Integer.MIN_VALUE), 1e-9);
assertEquals(1.0, PowerUsingRecursion.power(-1.0, Integer.MIN_VALUE), 1e-9);
assertEquals(0.0, PowerUsingRecursion.power(2.0, Integer.MIN_VALUE));
}
@Test
void testNegativeExponentAvoidsPrematureOverflow() {
assertEquals(Math.pow(2.0, -1024), PowerUsingRecursion.power(2.0, -1024));
}
}