| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- //
- // bigdecimal.t.h
- // bigdecimal
- //
- // Created by Sam Jaffe on 6/30/17.
- //
- #pragma once
- #include "bigdecimal.h"
- #include <cxxtest/TestSuite.h>
- class bigdecimal_integer_TestSuite : public CxxTest::TestSuite {
- public:
- void testConstructFromStringIsSameValueAsFromInt() {
- using bi = math::bigdecimal;
- TS_ASSERT_EQUALS(bi("1000000"), bi(1000000));
- }
-
- void testAddPastBounds() {
- math::bigdecimal bi{999999999ULL};
- TS_ASSERT_EQUALS((bi+1).to_string(), "1000000000");
- }
-
- void testAddReciprocalIsZero() {
- math::bigdecimal bi{1000};
- TS_ASSERT_EQUALS((bi+(-bi)).to_string(), "0");
- }
- void testAddNegativeLargerGivesNegative() {
- math::bigdecimal bi{1000};
- TS_ASSERT_EQUALS((bi+(-1001)).to_string(), "-1");
- }
- void testAddNegativeSmallerGivesPositive() {
- math::bigdecimal bi{1000};
- TS_ASSERT_EQUALS((bi+(-999)).to_string(), "1");
- }
-
- void testSubSelfIsZero() {
- math::bigdecimal bi{1000};
- TS_ASSERT_EQUALS((bi-bi).to_string(), "0");
- }
-
- void testNegativeMinusNegativeIncreateAbs() {
- math::bigdecimal bi{-1000};
- TS_ASSERT_EQUALS((bi-100).to_string(), "-1100");
- }
-
- void testSubLargerGivesNegative() {
- math::bigdecimal bi{1000};
- TS_ASSERT_EQUALS((bi-1001).to_string(), "-1");
- }
-
- void testSubSmallerGivesPositive() {
- math::bigdecimal bi{1000};
- TS_ASSERT_EQUALS((bi-999).to_string(), "1");
- }
- void testSubUnderflowBorrows() {
- math::bigdecimal bi{1000000000ULL};
- TS_ASSERT_EQUALS((bi-1).to_string(), "999999999");
- }
- void testMultiplyZeroReturnsZero() {
- auto &ZERO = math::bigdecimal::ZERO;
- math::bigdecimal bi{999999999ULL};
- TS_ASSERT_EQUALS(bi*ZERO, ZERO);
- TS_ASSERT_EQUALS(ZERO*bi, ZERO);
- }
-
- void testMultiplyOneReturnsValue() {
- auto &ONE = math::bigdecimal::ONE;
- math::bigdecimal bi{999999999ULL};
- TS_ASSERT_EQUALS((bi*ONE).to_string(), "999999999");
- TS_ASSERT_EQUALS((ONE*bi).to_string(), "999999999");
- }
- void testMultiplyNegativeOneReturnsInverse() {
- auto &NEGATIVE_ONE = math::bigdecimal::NEGATIVE_ONE;
- math::bigdecimal bi{999999999ULL};
- TS_ASSERT_EQUALS((bi*NEGATIVE_ONE).to_string(), "-999999999");
- TS_ASSERT_EQUALS((NEGATIVE_ONE*bi).to_string(), "-999999999");
- }
-
- void testMultiplyOverflowsIntoNextCell() {
- math::bigdecimal bi{999999999ULL};
- TS_ASSERT_EQUALS((bi*bi).to_string(), "999999998000000001");
- }
-
- void testMultiplyCarryIntoNextCell() {
- math::bigdecimal bi{999999999ULL};
- math::bigdecimal big{bi*bi};
- TS_ASSERT_EQUALS((big*big).to_string(),
- "999999996000000005999999996000000001");
- }
-
- void testMultiplyNoOverflow() {
- math::bigdecimal bi{1000};
- TS_ASSERT_EQUALS((bi*bi).to_string(), "1000000");
- }
-
- void testDivideByZeroThrows() {
- auto &ZERO = math::bigdecimal::ZERO;
- math::bigdecimal bi{1000};
- TS_ASSERT_THROWS(bi/ZERO, std::domain_error);
- }
-
- void testDivideByOneReturnsValue() {
- auto &ONE = math::bigdecimal::ONE;
- math::bigdecimal bi{1000};
- TS_ASSERT_EQUALS(bi/ONE, bi);
- }
- void testDivideByNegativeOneReturnsInverse() {
- auto &NEGATIVE_ONE = math::bigdecimal::NEGATIVE_ONE;
- math::bigdecimal bi{1000};
- TS_ASSERT_EQUALS((bi/NEGATIVE_ONE).to_string(), "-1000");
- }
- void testDivisionWithMultipleMultSubSteps() {
- math::bigdecimal bi{1112};
- TS_ASSERT_EQUALS((bi/2).to_string(), "556");
- }
-
- void testDivisionDroppingNumberOfCells() {
- math::bigdecimal bi{1000000000ULL};
- TS_ASSERT_EQUALS((bi/2).to_string(), "500000000");
- }
-
- void testDivisionByBiggerNumberIsZero() {
- math::bigdecimal bi{1000ULL};
- TS_ASSERT_EQUALS((bi/1001).to_string(), "0");
- }
-
- void testDivisionWithLargeNumbers() {
- math::bigdecimal big{"999999998000000001"};
- TS_ASSERT_EQUALS(big.to_string(), "999999998000000001");
- TS_ASSERT_EQUALS((big/999999999ULL).to_string(),
- "999999999");
- }
- //
- // void testModuloZeroThrows() {
- // math::bigdecimal bi{1000};
- // TS_ASSERT_THROWS(bi%0, std::domain_error);
- // }
- //
- // void testModuloBiggerIsSameValue() {
- // math::bigdecimal bi{1000};
- // TS_ASSERT_EQUALS(bi%2000, bi);
- // }
- //
- // void testModuloSameNumberIsZero() {
- // math::bigdecimal bi{1000};
- // TS_ASSERT_EQUALS(bi%1000, 0);
- // }
- //
- // void testModuloDivisorIsZero() {
- // math::bigdecimal bi{1000};
- // TS_ASSERT_EQUALS(bi%100, 0);
- // }
- //
- // void testModuloDiffSignIsInverseElement() {
- // math::bigdecimal bi{1000};
- // math::bigdecimal mod{13};
- // TS_ASSERT_EQUALS((bi%mod)+((-bi)%mod), mod);
- // }
- //
- // void testModuloNegativesIsNegative() {
- // math::bigdecimal bi{1000};
- // math::bigdecimal mod{13};
- // TS_ASSERT_EQUALS((bi%mod), -((-bi)%(-mod)));
- // }
- };
|