Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 18570dec1c | |||
| 9244d49d75 |
@@ -4,6 +4,7 @@ import com.example.util.AddFunction;
|
||||
import com.example.util.CalcFunction;
|
||||
import com.example.util.DivideFunction;
|
||||
import com.example.util.MultiplyFunction;
|
||||
import com.example.util.PowerFunction;
|
||||
import com.example.util.SubtractFunction;
|
||||
|
||||
public class LoopTestApplication {
|
||||
@@ -18,5 +19,7 @@ public class LoopTestApplication {
|
||||
System.out.println("MULTIPLICATION: " + multiply.calc(2, 5, 6));
|
||||
CalcFunction divide = new DivideFunction();
|
||||
System.out.println("DIVISION: " + divide.calc(2, 5, 6));
|
||||
CalcFunction power = new PowerFunction();
|
||||
System.out.println("POWER: " + power.calc(2, 5, 6));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.util;
|
||||
|
||||
public class PowerFunction implements CalcFunction {
|
||||
|
||||
public double calc(double... input) {
|
||||
if (input.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
double result = input[0];
|
||||
for (int i = 1; i < input.length; i++) {
|
||||
result = Math.pow(result, input[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.example.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class PowerFunctionTest {
|
||||
|
||||
private final PowerFunction powerFunction = new PowerFunction();
|
||||
|
||||
@Test
|
||||
void raisesMultipleValuesLeftToRight() {
|
||||
assertEquals(1073741824.0, powerFunction.calc(2.0, 5.0, 6.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsSingleValue() {
|
||||
assertEquals(5.0, powerFunction.calc(5.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsZeroForNoInput() {
|
||||
assertEquals(0.0, powerFunction.calc());
|
||||
}
|
||||
|
||||
@Test
|
||||
void raisesToNegativeExponent() {
|
||||
assertEquals(0.25, powerFunction.calc(2.0, -2.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void raisesToZeroExponent() {
|
||||
assertEquals(1.0, powerFunction.calc(7.0, 0.0));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user