bigdecimal.t.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // bigdecimal.t.h
  3. // bigdecimal
  4. //
  5. // Created by Sam Jaffe on 7/5/17.
  6. //
  7. #pragma once
  8. #include "bigdecimal.h"
  9. #include <cxxtest/TestSuite.h>
  10. class bigdecimal_TestSuite : public CxxTest::TestSuite {
  11. public:
  12. // void testConstructFromStringIsSameValueAsFromInt() {
  13. // using bi = math::bigdecimal;
  14. // TS_ASSERT_EQUALS(bi("1000000"), bi(1000000));
  15. // }
  16. //
  17. void testConstructIntegerAsDecimal() {
  18. using bd = math::bigdecimal;
  19. TS_ASSERT_EQUALS(bd("1000.00").to_string(), "1000.00");
  20. }
  21. void testConstructDecimal() {
  22. using bd = math::bigdecimal;
  23. TS_ASSERT_EQUALS(bd("1000.10").to_string(), "1000.10");
  24. TS_ASSERT_EQUALS(bd("1000.01").to_string(), "1000.01");
  25. }
  26. void testRescaleHigherAddsDigits() {
  27. math::bigdecimal dec(100);
  28. TS_ASSERT_EQUALS(dec.scale(), 0);
  29. dec.rescale(2);
  30. TS_ASSERT_EQUALS(dec.to_string(), "100.00");
  31. }
  32. void testRescaleLowerCanBeLossy() {
  33. math::bigdecimal dec("100.10");
  34. TS_ASSERT_EQUALS(dec.scale(), 2);
  35. dec.rescale(0);
  36. dec.rescale(2);
  37. TS_ASSERT_EQUALS(dec.to_string(), "100.00");
  38. }
  39. void testConstructWithScaleEqualsWithout() {
  40. math::bigdecimal scl(100, -2);
  41. TS_ASSERT_EQUALS(scl.to_string(), "100");
  42. }
  43. void testAddTwoDecimalsSameScale() {
  44. using bd = math::bigdecimal;
  45. bd a("1000.10");
  46. bd b("1000.01");
  47. TS_ASSERT_EQUALS(a.scale(), b.scale());
  48. TS_ASSERT_EQUALS((a+b).to_string(), "2000.11");
  49. }
  50. void testAddTwoDecimalsDifferentScalesUsesHigherScale() {
  51. using bd = math::bigdecimal;
  52. bd a("1000.10");
  53. bd b("1000");
  54. TS_ASSERT(a.scale() > b.scale());
  55. TS_ASSERT_EQUALS((a+b).to_string(), "2000.10");
  56. }
  57. void testAddNumberWithInversePreservesScale() {
  58. math::bigdecimal a("1.001");
  59. TS_ASSERT_EQUALS((a+(-a)).to_string(), "0.000");
  60. }
  61. void testSubtractTwoDecimalsDifferentScalesUsesHigherScale() {
  62. using bd = math::bigdecimal;
  63. bd a("900.10");
  64. bd b("1000");
  65. TS_ASSERT(a.scale() > b.scale());
  66. TS_ASSERT_EQUALS((a-b).to_string(), "-99.90");
  67. TS_ASSERT_EQUALS((b-a).to_string(), "99.90");
  68. }
  69. // void testAddPastBounds() {
  70. // math::bigdecimal bi{999999999ULL};
  71. // TS_ASSERT_EQUALS((bi+1).to_string(), "1000000000");
  72. // }
  73. //
  74. // void testAddReciprocalIsZero() {
  75. // math::bigdecimal bi{1000};
  76. // TS_ASSERT_EQUALS((bi+(-bi)).to_string(), "0");
  77. // }
  78. //
  79. // void testAddNegativeLargerGivesNegative() {
  80. // math::bigdecimal bi{1000};
  81. // TS_ASSERT_EQUALS((bi+(-1001)).to_string(), "-1");
  82. // }
  83. //
  84. // void testAddNegativeSmallerGivesPositive() {
  85. // math::bigdecimal bi{1000};
  86. // TS_ASSERT_EQUALS((bi+(-999)).to_string(), "1");
  87. // }
  88. //
  89. // void testSubSelfIsZero() {
  90. // math::bigdecimal bi{1000};
  91. // TS_ASSERT_EQUALS((bi-bi).to_string(), "0");
  92. // }
  93. //
  94. // void testNegativeMinusNegativeIncreateAbs() {
  95. // math::bigdecimal bi{-1000};
  96. // TS_ASSERT_EQUALS((bi-100).to_string(), "-1100");
  97. // }
  98. //
  99. // void testSubLargerGivesNegative() {
  100. // math::bigdecimal bi{1000};
  101. // TS_ASSERT_EQUALS((bi-1001).to_string(), "-1");
  102. // }
  103. //
  104. // void testSubSmallerGivesPositive() {
  105. // math::bigdecimal bi{1000};
  106. // TS_ASSERT_EQUALS((bi-999).to_string(), "1");
  107. // }
  108. //
  109. // void testSubUnderflowBorrows() {
  110. // math::bigdecimal bi{1000000000ULL};
  111. // TS_ASSERT_EQUALS((bi-1).to_string(), "999999999");
  112. // }
  113. //
  114. // void testMultiplyZeroReturnsZero() {
  115. // auto &ZERO = math::bigdecimal::ZERO;
  116. // math::bigdecimal bi{999999999ULL};
  117. // TS_ASSERT_EQUALS(bi*ZERO, ZERO);
  118. // TS_ASSERT_EQUALS(ZERO*bi, ZERO);
  119. // }
  120. //
  121. // void testMultiplyOneReturnsValue() {
  122. // auto &ONE = math::bigdecimal::ONE;
  123. // math::bigdecimal bi{999999999ULL};
  124. // TS_ASSERT_EQUALS((bi*ONE).to_string(), "999999999");
  125. // TS_ASSERT_EQUALS((ONE*bi).to_string(), "999999999");
  126. // }
  127. //
  128. // void testMultiplyNegativeOneReturnsInverse() {
  129. // auto &NEGATIVE_ONE = math::bigdecimal::NEGATIVE_ONE;
  130. // math::bigdecimal bi{999999999ULL};
  131. // TS_ASSERT_EQUALS((bi*NEGATIVE_ONE).to_string(), "-999999999");
  132. // TS_ASSERT_EQUALS((NEGATIVE_ONE*bi).to_string(), "-999999999");
  133. // }
  134. //
  135. // void testMultiplyOverflowsIntoNextCell() {
  136. // math::bigdecimal bi{999999999ULL};
  137. // TS_ASSERT_EQUALS((bi*bi).to_string(), "999999998000000001");
  138. // }
  139. //
  140. // void testMultiplyCarryIntoNextCell() {
  141. // math::bigdecimal bi{999999999ULL};
  142. // math::bigdecimal big{bi*bi};
  143. // TS_ASSERT_EQUALS((big*big).to_string(),
  144. // "999999996000000005999999996000000001");
  145. // }
  146. //
  147. // void testMultiplyNoOverflow() {
  148. // math::bigdecimal bi{1000};
  149. // TS_ASSERT_EQUALS((bi*bi).to_string(), "1000000");
  150. // }
  151. //
  152. // void testDivideByZeroThrows() {
  153. // auto &ZERO = math::bigdecimal::ZERO;
  154. // math::bigdecimal bi{1000};
  155. // TS_ASSERT_THROWS(bi/ZERO, std::domain_error);
  156. // }
  157. //
  158. // void testDivideByOneReturnsValue() {
  159. // auto &ONE = math::bigdecimal::ONE;
  160. // math::bigdecimal bi{1000};
  161. // TS_ASSERT_EQUALS(bi/ONE, bi);
  162. // }
  163. //
  164. // void testDivideByNegativeOneReturnsInverse() {
  165. // auto &NEGATIVE_ONE = math::bigdecimal::NEGATIVE_ONE;
  166. // math::bigdecimal bi{1000};
  167. // TS_ASSERT_EQUALS((bi/NEGATIVE_ONE).to_string(), "-1000");
  168. // }
  169. //
  170. // void testDivisionWithMultipleMultSubSteps() {
  171. // math::bigdecimal bi{1112};
  172. // TS_ASSERT_EQUALS((bi/2).to_string(), "556");
  173. // }
  174. //
  175. // void testDivisionDroppingNumberOfCells() {
  176. // math::bigdecimal bi{1000000000ULL};
  177. // TS_ASSERT_EQUALS((bi/2).to_string(), "500000000");
  178. // }
  179. //
  180. // void testDivisionByBiggerNumberIsZero() {
  181. // math::bigdecimal bi{1000ULL};
  182. // TS_ASSERT_EQUALS((bi/1001).to_string(), "0");
  183. // }
  184. //
  185. // void testDivisionWithLargeNumbers() {
  186. // math::bigdecimal big{"999999998000000001"};
  187. // TS_ASSERT_EQUALS(big.to_string(), "999999998000000001");
  188. // TS_ASSERT_EQUALS((big/999999999ULL).to_string(),
  189. // "999999999");
  190. // }
  191. //
  192. // void testModuloZeroThrows() {
  193. // math::bigdecimal bi{1000};
  194. // TS_ASSERT_THROWS(bi%0, std::domain_error);
  195. // }
  196. //
  197. // void testModuloBiggerIsSameValue() {
  198. // math::bigdecimal bi{1000};
  199. // TS_ASSERT_EQUALS(bi%2000, bi);
  200. // }
  201. //
  202. // void testModuloSameNumberIsZero() {
  203. // math::bigdecimal bi{1000};
  204. // TS_ASSERT_EQUALS(bi%1000, 0);
  205. // }
  206. //
  207. // void testModuloDivisorIsZero() {
  208. // math::bigdecimal bi{1000};
  209. // TS_ASSERT_EQUALS(bi%100, 0);
  210. // }
  211. //
  212. // void testModuloDiffSignIsInverseElement() {
  213. // math::bigdecimal bi{1000};
  214. // math::bigdecimal mod{13};
  215. // TS_ASSERT_EQUALS((bi%mod)+((-bi)%mod), mod);
  216. // }
  217. //
  218. // void testModuloNegativesIsNegative() {
  219. // math::bigdecimal bi{1000};
  220. // math::bigdecimal mod{13};
  221. // TS_ASSERT_EQUALS((bi%mod), -((-bi)%(-mod)));
  222. // }
  223. };