bigdecimal.t.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 testNegativeScaleLosesLowerDigits() {
  40. math::bigdecimal dec("123", -2);
  41. TS_ASSERT_EQUALS(dec.to_string(), "100");
  42. }
  43. void testConstructWithScaleEqualsWithout() {
  44. math::bigdecimal scl(100, -2);
  45. TS_ASSERT_EQUALS(scl.to_string(), "100");
  46. }
  47. void testScaleBelowNegSegDoesntLoseAnything() {
  48. TS_ASSERT_EQUALS(math::bigdecimal("1000000000", -9).to_string(),
  49. "1000000000");
  50. }
  51. void testAddTwoDecimalsSameScale() {
  52. using bd = math::bigdecimal;
  53. bd a("1000.10");
  54. bd b("1000.01");
  55. TS_ASSERT_EQUALS(a.scale(), b.scale());
  56. TS_ASSERT_EQUALS((a+b).to_string(), "2000.11");
  57. }
  58. void testAddTwoDecimalsDifferentScalesUsesHigherScale() {
  59. using bd = math::bigdecimal;
  60. bd a("1000.10");
  61. bd b("1000");
  62. TS_ASSERT(a.scale() > b.scale());
  63. TS_ASSERT_EQUALS((a+b).to_string(), "2000.10");
  64. }
  65. void testAddNumberWithInversePreservesScale() {
  66. math::bigdecimal a("1.001");
  67. TS_ASSERT_EQUALS((a+(-a)).to_string(), "0.000");
  68. }
  69. void testSubtractTwoDecimalsDifferentScalesUsesHigherScale() {
  70. using bd = math::bigdecimal;
  71. bd a("900.10");
  72. bd b("1000");
  73. TS_ASSERT(a.scale() > b.scale());
  74. TS_ASSERT_EQUALS((a-b).to_string(), "-99.90");
  75. TS_ASSERT_EQUALS((b-a).to_string(), "99.90");
  76. }
  77. void testMultiplicationIncreasesScale() {
  78. math::bigdecimal bd("1.1");
  79. auto out = bd * bd;
  80. TS_ASSERT_EQUALS(out.scale(), bd.scale() + bd.scale());
  81. TS_ASSERT_EQUALS(out.to_string(), "1.21");
  82. }
  83. void testMultiplicationNegativeScaleCountersPositiveScale() {
  84. math::bigdecimal a("0.01");
  85. math::bigdecimal b("100", -2);
  86. TS_ASSERT_EQUALS((a*b).to_string(), "1");
  87. }
  88. void testDivideHigherScaleByLowerScale() {
  89. math::bigdecimal a("1", 2);
  90. math::bigdecimal b("1", 1);
  91. TS_ASSERT_EQUALS((a/b).to_string(), "1.0");
  92. }
  93. void testDivideLowerScaleByHigherScale() {
  94. math::bigdecimal a("10", 1);
  95. math::bigdecimal b("1", 2);
  96. TS_ASSERT_EQUALS((a/b).to_string(), "10");
  97. }
  98. };