package ch.obermuhlner.math.big;

import static org.junit.Assert.assertEquals;

import java.math.BigDecimal;
import java.math.MathContext;

import org.junit.Test;

public class BigDecimalMathTest {

	private static final MathContext MC = MathContext.DECIMAL128;

	private static final MathContext MC_CHECK_DOUBLE = MathContext.DECIMAL32;

	@Test(expected = ArithmeticException.class)
	public void testPowIntZeroPowerNegative() {
		BigDecimalMath.pow(BigDecimal.valueOf(0), -5, MC);
	}
	
	@Test
	public void testPowIntPositive() {
		// positive exponents
		for (int x = -5; x <= 5; x++) {
			for (int y = 0; y <= 10; y++) {
				assertEquals(
						x + "^" + y,
						BigDecimal.valueOf((int) Math.pow(x, y)),
						BigDecimalMath.pow(BigDecimal.valueOf(x), y, MC));
			}
		}
	}
	
	@Test
	public void testPowIntNegative() {
		// positive exponents
		for (int x = -5; x <= 5; x++) {
			for (int y = -10; y < 0; y++) {
				if (x != 0) {
					assertEquals(
							x + "^" + y,
							BigDecimal.ONE.divide(BigDecimal.valueOf((int) Math.pow(x, -y)), MC),
							BigDecimalMath.pow(BigDecimal.valueOf(x), y, MC));
				}
			}
		}
	}
	
	@Test
	public void testLog() {
		for(double value : new double[] { 0.1, 2, 10, 33.3333 }) {
			assertEquals("log(" + value + ")", toCheck(Math.log(value)), BigDecimalMath.log(BigDecimal.valueOf(value), MC).round(MC_CHECK_DOUBLE));
		}
	}

	private static BigDecimal toCheck(double value) {
		return toCheck(BigDecimal.valueOf(value));
	}

	private static BigDecimal toCheck(BigDecimal value) {
		return value.round(MC_CHECK_DOUBLE);
	}
}

@@ -579,33 +579,67 @@
 %09for
- 
 (int x 
-= -5; x %3C= 5; x++
+: new int%5B%5D %7B -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 %7D
 ) %7B%0D
@@ -649,33 +649,47 @@
 %09for
- 
 (int y 
-= 0; y %3C= 10; y++
+: new int%5B%5D %7B 0, 1, 2, 3, 4, 5 %7D
 ) %7B%0D
@@ -932,44 +932,87 @@
 %09for
- 
 (int x 
-= -5; x %3C= 5; x++) %7B
+: new int%5B%5D %7B -5, -4, -3, -2, -1, 1, 2, 3, 4, 5 %7D) %7B // no x=0 !
 %0D%0A%09%09%09for
  (in
@@ -1011,52 +1011,49 @@
 %09for
- 
 (int y 
-= -10; y %3C 0; y++) %7B%0D%0A%09%09%09%09if (x != 0
+: new int%5B%5D %7B -5, -4, -3, -2, -1 %7D
 ) %7B%0D
@@ -1057,17 +1057,16 @@
  %7B%0D%0A%09%09%09%09
-%09
 assertEq
@@ -1074,25 +1074,24 @@
 als(%0D%0A%09%09%09%09%09%09
-%09
 x + %22%5E%22 + y,
@@ -1088,25 +1088,24 @@
 + %22%5E%22 + y,%0D%0A
-%09
 %09%09%09%09%09%09BigDec
@@ -1165,25 +1165,24 @@
 -y)), MC),%0D%0A
-%09
 %09%09%09%09%09%09BigDec
@@ -1231,15 +1231,8 @@
 );%0D%0A
-%09%09%09%09%7D%0D%0A
 %09%09%09%7D

test with explicit values