FastPowerCalculator implements PowerCalculator { @Complexity(value = "O(log(n))", explanation = "n is the exponent. We divide the problem size by half at each recursion.") @Override public double calculate(double base, int exponent) { if (exponent < 0) { throw new IllegalArgumentException("Exponent cannot be negative."); } if (base == 0D) { return 0D; } else if (base == 1D) { return 1D; } if (exponent == 0) { return 1; } else if (exponent == 1) { return base; } double intermediate = calculate(base, exponent / 2); intermediate *= intermediate; return exponent % 2 == 1 ? base * intermediate : intermediate; }  @Complexity(value = "O(log(n))", explanation = "n is the exponent. We divide the problem size by half at each recursion.") @Override double calculate(double base, int exponent);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testThatItDoesNotSupportNegativeNumber() throws Exception { calculate(100, -1); }
@Test(dataProvider = "numberProvider") public void testCalculation(double base, int exponent) throws Exception { assertThat(calculate(base, exponent), is(Math.pow(base, exponent))); }