bigdecimal.t.h 2.9 KB

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