bigdecimal_integer.t.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // bigdecimal.t.h
  3. // bigdecimal
  4. //
  5. // Created by Sam Jaffe on 6/30/17.
  6. //
  7. #pragma once
  8. #include "bigdecimal.h"
  9. #include <cxxtest/TestSuite.h>
  10. class bigdecimal_integer_TestSuite : public CxxTest::TestSuite {
  11. public:
  12. void testConstructFromStringIsSameValueAsFromInt() {
  13. using bi = math::bigdecimal;
  14. TS_ASSERT_EQUALS(bi("1000000"), bi(1000000));
  15. }
  16. void testAddPastBounds() {
  17. math::bigdecimal bi{999999999ULL};
  18. TS_ASSERT_EQUALS((bi+1).to_string(), "1000000000");
  19. }
  20. void testAddReciprocalIsZero() {
  21. math::bigdecimal bi{1000};
  22. TS_ASSERT_EQUALS((bi+(-bi)).to_string(), "0");
  23. }
  24. void testAddNegativeLargerGivesNegative() {
  25. math::bigdecimal bi{1000};
  26. TS_ASSERT_EQUALS((bi+(-1001)).to_string(), "-1");
  27. }
  28. void testAddNegativeSmallerGivesPositive() {
  29. math::bigdecimal bi{1000};
  30. TS_ASSERT_EQUALS((bi+(-999)).to_string(), "1");
  31. }
  32. void testSubSelfIsZero() {
  33. math::bigdecimal bi{1000};
  34. TS_ASSERT_EQUALS((bi-bi).to_string(), "0");
  35. }
  36. void testNegativeMinusNegativeIncreateAbs() {
  37. math::bigdecimal bi{-1000};
  38. TS_ASSERT_EQUALS((bi-100).to_string(), "-1100");
  39. }
  40. void testSubLargerGivesNegative() {
  41. math::bigdecimal bi{1000};
  42. TS_ASSERT_EQUALS((bi-1001).to_string(), "-1");
  43. }
  44. void testSubSmallerGivesPositive() {
  45. math::bigdecimal bi{1000};
  46. TS_ASSERT_EQUALS((bi-999).to_string(), "1");
  47. }
  48. void testSubUnderflowBorrows() {
  49. math::bigdecimal bi{1000000000ULL};
  50. TS_ASSERT_EQUALS((bi-1).to_string(), "999999999");
  51. }
  52. void testMultiplyZeroReturnsZero() {
  53. auto &ZERO = math::bigdecimal::ZERO;
  54. math::bigdecimal bi{999999999ULL};
  55. TS_ASSERT_EQUALS(bi*ZERO, ZERO);
  56. TS_ASSERT_EQUALS(ZERO*bi, ZERO);
  57. }
  58. void testMultiplyOneReturnsValue() {
  59. auto &ONE = math::bigdecimal::ONE;
  60. math::bigdecimal bi{999999999ULL};
  61. TS_ASSERT_EQUALS((bi*ONE).to_string(), "999999999");
  62. TS_ASSERT_EQUALS((ONE*bi).to_string(), "999999999");
  63. }
  64. void testMultiplyNegativeOneReturnsInverse() {
  65. auto &NEGATIVE_ONE = math::bigdecimal::NEGATIVE_ONE;
  66. math::bigdecimal bi{999999999ULL};
  67. TS_ASSERT_EQUALS((bi*NEGATIVE_ONE).to_string(), "-999999999");
  68. TS_ASSERT_EQUALS((NEGATIVE_ONE*bi).to_string(), "-999999999");
  69. }
  70. void testMultiplyOverflowsIntoNextCell() {
  71. math::bigdecimal bi{999999999ULL};
  72. TS_ASSERT_EQUALS((bi*bi).to_string(), "999999998000000001");
  73. }
  74. void testMultiplyCarryIntoNextCell() {
  75. math::bigdecimal bi{999999999ULL};
  76. math::bigdecimal big{bi*bi};
  77. TS_ASSERT_EQUALS((big*big).to_string(),
  78. "999999996000000005999999996000000001");
  79. }
  80. void testMultiplyNoOverflow() {
  81. math::bigdecimal bi{1000};
  82. TS_ASSERT_EQUALS((bi*bi).to_string(), "1000000");
  83. }
  84. void testDivideByZeroThrows() {
  85. auto &ZERO = math::bigdecimal::ZERO;
  86. math::bigdecimal bi{1000};
  87. TS_ASSERT_THROWS(bi/ZERO, std::domain_error);
  88. }
  89. void testDivideByOneReturnsValue() {
  90. auto &ONE = math::bigdecimal::ONE;
  91. math::bigdecimal bi{1000};
  92. TS_ASSERT_EQUALS(bi/ONE, bi);
  93. }
  94. void testDivideByNegativeOneReturnsInverse() {
  95. auto &NEGATIVE_ONE = math::bigdecimal::NEGATIVE_ONE;
  96. math::bigdecimal bi{1000};
  97. TS_ASSERT_EQUALS((bi/NEGATIVE_ONE).to_string(), "-1000");
  98. }
  99. void testDivisionWithMultipleMultSubSteps() {
  100. math::bigdecimal bi{1112};
  101. TS_ASSERT_EQUALS((bi/2).to_string(), "556");
  102. }
  103. void testDivisionDroppingNumberOfCells() {
  104. math::bigdecimal bi{1000000000ULL};
  105. TS_ASSERT_EQUALS((bi/2).to_string(), "500000000");
  106. }
  107. void testDivisionByBiggerNumberIsZero() {
  108. math::bigdecimal bi{1000ULL};
  109. TS_ASSERT_EQUALS((bi/1001).to_string(), "0");
  110. }
  111. void testDivisionWithLargeNumbers() {
  112. math::bigdecimal big{"999999998000000001"};
  113. TS_ASSERT_EQUALS(big.to_string(), "999999998000000001");
  114. TS_ASSERT_EQUALS((big/999999999ULL).to_string(),
  115. "999999999");
  116. }
  117. //
  118. // void testModuloZeroThrows() {
  119. // math::bigdecimal bi{1000};
  120. // TS_ASSERT_THROWS(bi%0, std::domain_error);
  121. // }
  122. //
  123. // void testModuloBiggerIsSameValue() {
  124. // math::bigdecimal bi{1000};
  125. // TS_ASSERT_EQUALS(bi%2000, bi);
  126. // }
  127. //
  128. // void testModuloSameNumberIsZero() {
  129. // math::bigdecimal bi{1000};
  130. // TS_ASSERT_EQUALS(bi%1000, 0);
  131. // }
  132. //
  133. // void testModuloDivisorIsZero() {
  134. // math::bigdecimal bi{1000};
  135. // TS_ASSERT_EQUALS(bi%100, 0);
  136. // }
  137. //
  138. // void testModuloDiffSignIsInverseElement() {
  139. // math::bigdecimal bi{1000};
  140. // math::bigdecimal mod{13};
  141. // TS_ASSERT_EQUALS((bi%mod)+((-bi)%mod), mod);
  142. // }
  143. //
  144. // void testModuloNegativesIsNegative() {
  145. // math::bigdecimal bi{1000};
  146. // math::bigdecimal mod{13};
  147. // TS_ASSERT_EQUALS((bi%mod), -((-bi)%(-mod)));
  148. // }
  149. };