/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 decimal4j (tools4j), Marco Terzer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.decimal4j.op.arith;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import org.decimal4j.api.Decimal;
import org.decimal4j.api.DecimalArithmetic;
import org.decimal4j.op.AbstractDecimalDecimalToDecimalTest;
import org.decimal4j.scale.ScaleMetrics;
import org.decimal4j.test.TestSettings;
import org.decimal4j.truncate.TruncationPolicy;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * Unit test for {@link Decimal#divide(Decimal, RoundingMode)}
 */
@RunWith(Parameterized.class)
public class DivideTest extends AbstractDecimalDecimalToDecimalTest {
	
	public DivideTest(ScaleMetrics scaleMetrics, TruncationPolicy truncationPolicy, DecimalArithmetic arithmetic) {
		super(arithmetic);
	}

	@Parameters(name = "{index}: {0}, {1}")
	public static Iterable<Object[]> data() {
		final List<Object[]> data = new ArrayList<Object[]>();
		for (final ScaleMetrics s : TestSettings.SCALES) {
			for (final TruncationPolicy tp : TestSettings.POLICIES) {
				final DecimalArithmetic arith = s.getArithmetic(tp);
				data.add(new Object[] {s, tp, arith});
			}
		}
		return data;
	}
	
	@Override
	public void runSpecialValueTest() {
		super.runSpecialValueTest();
	}

	@Override
	protected String operation() {
		return "/";
	}
	
	@Override
	protected BigDecimal expectedResult(BigDecimal a, BigDecimal b) {
		return a.divide(b, mathContextLong128);
	}
	
	@Override
	protected <S extends ScaleMetrics> Decimal<S> actualResult(Decimal<S> a, Decimal<S> b) {
		if (isStandardTruncationPolicy() && RND.nextBoolean()) {
			return a.divide(b);
		} else {
			if (isUnchecked() && RND.nextBoolean()) {
				return a.divide(b, getRoundingMode());
			} else {
				return a.divide(b, getTruncationPolicy());
			}
		}
	}
}

@@ -2853,36 +2853,160 @@
 %0A%09%09%7D
- else %7B%0A%09%09%09if (isUnchecked()
+%0A%09%09if (isUnchecked() && RND.nextBoolean()) %7B%0A%09%09%09return a.divide(b, getRoundingMode());%0A%09%09%7D%0A%09%09if (isUnchecked() && getRoundingMode() == RoundingMode.DOWN
  && 
@@ -3025,18 +3025,18 @@
 ean()) %7B
+ 
 %0A
-%09
 %09%09%09retur
@@ -3049,46 +3049,152 @@
 vide
-(b, getRoundingMode());%0A%09%09%09%7D else %7B
+Truncate(b);%0A%09%09%7D%0A%09%09if (!isUnchecked() && getRoundingMode() == RoundingMode.UNNECESSARY && RND.nextBoolean()) %7B %0A%09%09%09return a.divideExact(b);
 %0A%09%09
+%7D%0A
 %09%09re
@@ -3234,22 +3234,13 @@
 icy());%0A
-%09%09%09%7D%0A%09%09%7D%0A
 %09%7D%0A%7D%0A

add test coverage for divideExact and divideTruncate