bigdecimal_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // bigdecimal_test.cpp
  3. // bigdecimal
  4. //
  5. // Created by Sam Jaffe on 5/18/18.
  6. //
  7. #include <gmock/gmock.h>
  8. #include "bigdecimal.h"
  9. TEST(BigDecimalTest, ConstructIntegerAsDecimal) {
  10. EXPECT_THAT(math::bigdecimal(1000, 2).to_string(), "1000.00");
  11. }
  12. TEST(BigDecimalTest, ConstructDecimal) {
  13. EXPECT_THAT(math::bigdecimal("1000.10").to_string(), "1000.10");
  14. EXPECT_THAT(math::bigdecimal("1000.01").to_string(), "1000.01");
  15. }
  16. TEST(BigDecimalTest, RescalePositiveAddsDecimalPlaces) {
  17. math::bigdecimal dec(100);
  18. EXPECT_THAT(dec.scale(), 0);
  19. dec.rescale(2);
  20. EXPECT_THAT(dec.to_string(), "100.00");
  21. }
  22. TEST(BigDecimalTest, RescaleNegativeDropsLowerOrderElements) {
  23. math::bigdecimal dec(111);
  24. EXPECT_THAT(dec.scale(), 0);
  25. dec.rescale(-2);
  26. EXPECT_THAT(dec.to_string(), "100");
  27. }
  28. TEST(BigDecimalTest, RescaleDownPermanantlyDropsLowOrderElements) {
  29. math::bigdecimal dec("100.10");
  30. EXPECT_THAT(dec.scale(), 2);
  31. dec.rescale(0);
  32. dec.rescale(2);
  33. EXPECT_THAT(dec.to_string(), "100.00");
  34. }
  35. TEST(BigDecimalTest, ScaleCtorCanBeLossy) {
  36. math::bigdecimal dec("123", -2);
  37. EXPECT_THAT(dec.to_string(), "100");
  38. }
  39. TEST(BigDecimalTest, NonLossyScaleCtorEqualsNoScaleCtor) {
  40. math::bigdecimal scl(100, -2);
  41. math::bigdecimal nc(100);
  42. EXPECT_THAT(scl, nc);
  43. }
  44. TEST(BigDecimalTest, ScaleBelowNegSegDoesntLoseAnything) {
  45. math::bigdecimal dec("1000000000", -9);
  46. EXPECT_THAT(dec.to_string(), "1000000000");
  47. }
  48. TEST(BigDecimalTest, AddingEqualScalesDoesNotAddTrailingDigitsOrLoseData) {
  49. math::bigdecimal a("1000.10");
  50. math::bigdecimal b("1000.01");
  51. EXPECT_THAT(a.scale(), b.scale());
  52. EXPECT_THAT((a+b).to_string(), "2000.11");
  53. }
  54. TEST(BigDecimalTest, AdditionNormalizeScaleToHighest) {
  55. math::bigdecimal a("1000.10");
  56. math::bigdecimal b("1000");
  57. EXPECT_THAT(a.scale(), testing::Gt(b.scale()));
  58. EXPECT_THAT((a+b).scale(), a.scale());
  59. }
  60. TEST(BigDecimalTest, InverseOfNumberCarriesSameScale) {
  61. math::bigdecimal a("1.001");
  62. EXPECT_THAT(a.scale(), (-a).scale());
  63. }
  64. TEST(BigDecimalTest, SubtractionNormalizeScaleToHighest) {
  65. math::bigdecimal a("900.10");
  66. math::bigdecimal b("1000");
  67. EXPECT_THAT(a.scale(), testing::Gt(b.scale()));
  68. EXPECT_THAT((a-b).scale(), a.scale());
  69. EXPECT_THAT((b-a).scale(), a.scale());
  70. }
  71. struct ArithTuple { math::bigdecimal lhs, rhs; std::string expected; };
  72. void PrintTo(ArithTuple const & tup, std::ostream * out) {
  73. (*out) << "{ lhs = " << tup.lhs.to_string() << "(" << tup.lhs.scale() << "), rhs = " << tup.rhs.to_string() << "(" << tup.rhs.scale() << ") }";
  74. }
  75. class MultiplicationScaleTest : public testing::TestWithParam<ArithTuple> {};
  76. TEST_P(MultiplicationScaleTest, ScaleIsSumOfOperAndMultipScales) {
  77. auto tup = GetParam();
  78. auto out = tup.lhs * tup.rhs;
  79. EXPECT_THAT(out.scale(), tup.lhs.scale() + tup.rhs.scale());
  80. EXPECT_THAT(out.to_string(), tup.expected);
  81. }
  82. class DivisionScaleTest : public testing::TestWithParam<ArithTuple> {};
  83. TEST_P(DivisionScaleTest, ScaleIsDifferenceOfNumAndDenomScales) {
  84. auto tup = GetParam();
  85. auto out = tup.lhs / tup.rhs;
  86. EXPECT_THAT(out.scale(), tup.lhs.scale() - tup.rhs.scale());
  87. EXPECT_THAT(out.to_string(), tup.expected);
  88. }
  89. #pragma clang diagnostic push
  90. #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
  91. INSTANTIATE_TEST_CASE_P(BigDecimal, MultiplicationScaleTest,
  92. testing::Values(ArithTuple{{1, 1}, { 1, 0}, "1.0"},
  93. ArithTuple{{1, 0}, { 1, 1}, "1.0"},
  94. ArithTuple{{1, 0}, {10, 0}, "10" },
  95. ArithTuple{{1, 1}, {10, 0}, "10.0"},
  96. ArithTuple{{1, 1}, {10, -1}, "10", },
  97. ArithTuple{{"1.1"}, {"1.1"}, "1.21"},
  98. ArithTuple{{"0.01"}, {100, -2}, "1"},
  99. ArithTuple{{1, 5}, {1, 5}, "1.0000000000"},
  100. ArithTuple{{1, 5}, {1000000000, -9}, "1000000000"},
  101. ArithTuple{{100, -2}, {1000000000, -9}, "100000000000"},
  102. ArithTuple{{10000, -4}, {100000, -5}, "1000000000"}));
  103. INSTANTIATE_TEST_CASE_P(BigDecimal, DivisionScaleTest,
  104. testing::Values(ArithTuple{{1, 1}, { 1, 0}, "1.0" },
  105. ArithTuple{{1, 0}, { 1, 1}, "0" },
  106. ArithTuple{{1, 0}, {10, 0}, "0" },
  107. ArithTuple{{1, 1}, {10, 0}, "0.1" },
  108. ArithTuple{{1, 1}, {10, -1}, "0.10"},
  109. ArithTuple{{"1.1"}, {"1.1"}, "1"},
  110. ArithTuple{{"0.01"}, {100, -2}, "0.0001"},
  111. ArithTuple{{1, 5}, {1, 5}, "1"},
  112. ArithTuple{{1, 5}, {1000000000, -9}, "0.00000000100000"},
  113. ArithTuple{{100, -2}, {1000000000, -9}, "0.0000001"},
  114. ArithTuple{{10000, -4}, {100000, -5}, "0.1"}));
  115. #pragma clang diagnostic pop