瀏覽代碼

Fix bug where comparisons for operator==, etc were inverted (e.g. < was >, but == was still ==)

Sam Jaffe 7 年之前
父節點
當前提交
df9da20f73
共有 2 個文件被更改,包括 28 次插入4 次删除
  1. 2 2
      src/bigdecimal.cpp
  2. 26 2
      test/bigdecimal_test.cpp

+ 2 - 2
src/bigdecimal.cpp

@@ -35,8 +35,8 @@ static int compare(bigdecimal::data_type const & ldata, bigdecimal::data_type co
 
 static int compare(bigdecimal::data_type const & ldata, int32_t lsteps, bigdecimal::data_type const & rdata, int32_t rsteps) {
   return lsteps < rsteps ?
-  compare(rdata, ldata, size_t(rsteps - lsteps))
-  : -compare(ldata, rdata, size_t(lsteps - rsteps));
+  -compare(rdata, ldata, size_t(rsteps - lsteps))
+  : compare(ldata, rdata, size_t(lsteps - rsteps));
 }
 
 static bool is_one(bigdecimal::data_type const & data, int32_t scale) {

+ 26 - 2
test/bigdecimal_test.cpp

@@ -89,8 +89,19 @@ TEST(BigDecimalTest, SubtractionNormalizeScaleToHighest) {
 
 struct ArithTuple { math::bigdecimal lhs, rhs; std::string expected; };
 struct BigDecPair { math::bigdecimal lhs, rhs; };
+void PrintTo(math::bigdecimal const & dec, std::ostream * out) {
+  (*out) << dec.to_string() << "(" << dec.scale() << ")";
+}
+std::ostream & operator<<(std::ostream & out, math::bigdecimal const & dec) {
+  PrintTo(dec, &out);
+  return out;
+}
 void PrintTo(BigDecPair const & tup, std::ostream * out) {
-  (*out) << "{ lhs = " << tup.lhs.to_string() << "(" << tup.lhs.scale() << "), rhs = " << tup.rhs.to_string() << "(" << tup.rhs.scale() << ") }";
+  (*out) << "{ lhs = ";
+  PrintTo(tup.lhs, out);
+  (*out) << ", rhs = ";
+  PrintTo(tup.rhs, out);
+  (*out) << " }";
 }
 
 class MultiplicationScaleTest : public testing::TestWithParam<ArithTuple> {};
@@ -114,7 +125,15 @@ TEST_P(DivisionScaleTest, ScaleIsDifferenceOfNumAndDenomScales) {
 class EqTest : public testing::TestWithParam<BigDecPair> {};
 
 TEST_P(EqTest, DecimalsAreEqualEvenIfScalesAreNot) {
-  EXPECT_THAT(GetParam().lhs, GetParam().rhs);
+  auto pair = GetParam();
+  EXPECT_THAT(pair.lhs, pair.rhs);
+}
+
+class DecimalLtTest : public testing::TestWithParam<BigDecPair> {};
+
+TEST_P(DecimalLtTest, IsLessThanEvenWithDifferentScales) {
+  auto pair = GetParam();
+  EXPECT_THAT(pair.lhs, testing::Lt(pair.rhs));
 }
 
 #pragma clang diagnostic push
@@ -151,4 +170,9 @@ INSTANTIATE_TEST_CASE_P(BigDecimal, EqTest,
                                         BigDecPair{{1000000000, -4}, {1000000000, -9}},
                                         BigDecPair{{"0.1", 1}, {"0.10", 2}}));
 
+INSTANTIATE_TEST_CASE_P(BigDecimal, DecimalLtTest,
+                        testing::Values(BigDecPair{{ 0, 0}, { 1, 4}} ,
+                                        BigDecPair{{ 0, 4}, { 1, 0}},
+                                        BigDecPair{{ 0, 4}, { 1, 4}}));
+
 #pragma clang diagnostic pop