package com.wesleyhome.math.equation;
import static com.wesleyhome.math.equation.SmartNumber.eq;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
import com.wesleyhome.math.equation.SmartNumber;
import com.wesleyhome.math.equation.StringEquation;
public class SmartNumberTest {
@Test
public void testPlus() {
assertThat(eq(10).plus(10).intValue(), is(equalTo(20)));
assertThat(eq(10).plus(eq(10)).intValue(), is(equalTo(20)));
}
@Test
public void testMinus() {
assertThat(eq(20).minus(10).intValue(), is(equalTo(10)));
assertThat(eq(20).minus(eq(10)).intValue(), is(equalTo(10)));
}
@Test
public void testTimes() {
assertThat(eq(10).times(10).intValue(), is(equalTo(100)));
assertThat(eq(10).times(eq(10)).intValue(), is(equalTo(100)));
}
@Test
public void testDividedBy() {
assertThat(eq(20).dividedBy(10).intValue(), is(equalTo(2)));
assertThat(eq(20).dividedBy(eq(10)).intValue(), is(equalTo(2)));
}
@Test
public void testToThePowerOf() {
assertThat(eq(10).toThePowerOf(2).intValue(), is(equalTo(100)));
assertThat(eq(10).toThePowerOf(eq(2)).intValue(), is(equalTo(100)));
assertThat(eq(3).toThePowerOf(.5).doubleValue(), is(equalTo(Math.sqrt(3))));
assertThat(eq(3).toThePowerOf(eq(.5)).doubleValue(), is(equalTo(Math.sqrt(3))));
}
@Test
public void testNegate() {
assertThat(eq(10).negate().intValue(), is(equalTo(-10)));
}
@Test
public void testDoubleValue() {
assertThat(eq(10.25).doubleValue(), is(equalTo(10.25)));
}
@Test
public void testIntValue() {
assertThat(eq(10.25).intValue(), is(equalTo(10)));
}
@Test
public void testLongValue() {
assertThat(eq(10.25).longValue(), is(equalTo((long) 10.25)));
}
@Test
public void testFloatValue() {
assertThat(eq(10.25).floatValue(), is(equalTo(10.25F)));
}
@Test
public void testComparisonToFloatPointError() throws Exception {
Double targetPremium = Double.valueOf(787.5);
Double rate = Double.valueOf(10.0);
Double premium = Double.valueOf(1586.6);
double tempCommission = targetPremium.doubleValue() * rate.doubleValue() / 100d;
double netToCompany = targetPremium.doubleValue() - tempCommission;
double dCommission = premium.doubleValue() - netToCompany;
Number actual = StringEquation.evaluate("rnd(d - (a - (a * b / c)),2)".toUpperCase(), targetPremium, rate, 100d, premium);
assertThat(SmartNumber.eq(dCommission), is(not(equalTo(actual))));
}
@Test
public void testEquals() throws Exception {
SmartNumber v = eq(10);
assertThat(v, is(equalTo(v)));
assertThat(v, is(not(equalTo(eq(11)))));
assertThat(v, is(not(equalTo(null))));
assertThat(v.equals(""), is(false));
}
@Test
public void testHashCode() throws Exception {
SmartNumber v1 = eq(10);
SmartNumber v2 = eq(10);
assertThat(v1.hashCode(), is(equalTo(v2.hashCode())));
}
}