// // bigdecimal.t.h // bigdecimal // // Created by Sam Jaffe on 7/5/17. // #pragma once #include "bigdecimal.h" #include class bigdecimal_TestSuite : public CxxTest::TestSuite { public: // void testConstructFromStringIsSameValueAsFromInt() { // using bi = math::bigdecimal; // TS_ASSERT_EQUALS(bi("1000000"), bi(1000000)); // } // void testConstructIntegerAsDecimal() { using bd = math::bigdecimal; TS_ASSERT_EQUALS(bd("1000.00").to_string(), "1000.00"); } void testConstructDecimal() { using bd = math::bigdecimal; TS_ASSERT_EQUALS(bd("1000.10").to_string(), "1000.10"); TS_ASSERT_EQUALS(bd("1000.01").to_string(), "1000.01"); } void testRescaleHigherAddsDigits() { math::bigdecimal dec(100); TS_ASSERT_EQUALS(dec.scale(), 0); dec.rescale(2); TS_ASSERT_EQUALS(dec.to_string(), "100.00"); } void testRescaleLowerCanBeLossy() { math::bigdecimal dec("100.10"); TS_ASSERT_EQUALS(dec.scale(), 2); dec.rescale(0); dec.rescale(2); TS_ASSERT_EQUALS(dec.to_string(), "100.00"); } void testNegativeScaleLosesLowerDigits() { math::bigdecimal dec("123", -2); TS_ASSERT_EQUALS(dec.to_string(), "100"); } void testConstructWithScaleEqualsWithout() { math::bigdecimal scl(100, -2); TS_ASSERT_EQUALS(scl.to_string(), "100"); } void testScaleBelowNegSegDoesntLoseAnything() { TS_ASSERT_EQUALS(math::bigdecimal("1000000000", -9).to_string(), "1000000000"); } void testAddTwoDecimalsSameScale() { using bd = math::bigdecimal; bd a("1000.10"); bd b("1000.01"); TS_ASSERT_EQUALS(a.scale(), b.scale()); TS_ASSERT_EQUALS((a+b).to_string(), "2000.11"); } void testAddTwoDecimalsDifferentScalesUsesHigherScale() { using bd = math::bigdecimal; bd a("1000.10"); bd b("1000"); TS_ASSERT(a.scale() > b.scale()); TS_ASSERT_EQUALS((a+b).to_string(), "2000.10"); } void testAddNumberWithInversePreservesScale() { math::bigdecimal a("1.001"); TS_ASSERT_EQUALS((a+(-a)).to_string(), "0.000"); } void testSubtractTwoDecimalsDifferentScalesUsesHigherScale() { using bd = math::bigdecimal; bd a("900.10"); bd b("1000"); TS_ASSERT(a.scale() > b.scale()); TS_ASSERT_EQUALS((a-b).to_string(), "-99.90"); TS_ASSERT_EQUALS((b-a).to_string(), "99.90"); } void testMultiplicationIncreasesScale() { math::bigdecimal bd("1.1"); auto out = bd * bd; TS_ASSERT_EQUALS(out.scale(), bd.scale() + bd.scale()); TS_ASSERT_EQUALS(out.to_string(), "1.21"); } void testMultiplicationNegativeScaleCountersPositiveScale() { math::bigdecimal a("0.01"); math::bigdecimal b("100", -2); TS_ASSERT_EQUALS((a*b).to_string(), "1"); } void testDivideHigherScaleByLowerScale() { math::bigdecimal a("1", 2); math::bigdecimal b("1", 1); TS_ASSERT_EQUALS((a/b).to_string(), "1.0"); } void testDivideLowerScaleByHigherScale() { math::bigdecimal a("10", 1); math::bigdecimal b("1", 2); TS_ASSERT_EQUALS((a/b).to_string(), "10"); } };