MathUtils { public static double logarithm(double base, double argument) { if (base <= 0 || argument <= 0) { throw new ArithmeticException("Base and argument of logarithms must be greater than 0"); } double answer = Math.log(argument) / Math.log(base); if (Double.isInfinite(answer)) { throw new ArithmeticException("Base of logarithm must be positive and not equal to 1"); } return answer; }  static double logarithm(double base, double argument);  }
@Test public void testLogarithmBase2() { double result = MathUtils.logarithm(2, 4); assertEquals("Log base 2 of 4 should be 2.0", 2.0, result, 0.0); assertEquals("Log base 2 of 5 should be 2.322", 2.322, MathUtils.logarithm(2, 5), 0.0001); }
@Test(expected = ArithmeticException.class) public void testArithmeticExceptionIsThrownIfTheBaseOfALogarithmIsZero() { MathUtils.logarithm(0, 2); }
@Test(expected = ArithmeticException.class) public void testArithmeticExceptionIsThrownIfTheArgumentOfALogarithmIsZero() { MathUtils.logarithm(2, 0); }
@Test(expected = ArithmeticException.class) public void testArithmeticExceptionIsThrownIfTheBaseOfALogarithmIsLessThanZero() { MathUtils.logarithm(-3, 2); }
@Test(expected = ArithmeticException.class) public void testArithmeticExceptionIsThrownIfTheArgumentOfALogarithmIsLessThanZero() { MathUtils.logarithm(3, -3); }
@Test(expected = ArithmeticException.class) public void testArithmeticExceptionIsThrownIfTheBaseOfALogarithmIsEqualTo1() { MathUtils.logarithm(1, 2); }