abacus: bignum: fix comparison of negative numbers

This commit is contained in:
Bruno BELANYI 2022-02-17 10:03:21 +01:00
parent 3e5133da1a
commit a382b299b0
2 changed files with 33 additions and 1 deletions

View file

@ -363,7 +363,11 @@ bool BigNum::less_than(BigNum const& rhs) const {
return sign_ < rhs.sign_; return sign_ < rhs.sign_;
} }
if (is_positive()) {
return do_less_than(digits_, rhs.digits_); return do_less_than(digits_, rhs.digits_);
} else {
return do_less_than(rhs.digits_, digits_);
}
} }
void BigNum::canonicalize() { void BigNum::canonicalize() {

View file

@ -68,6 +68,34 @@ TEST(BigNum, comparisons_digits) {
EXPECT_GT(ten, nine); EXPECT_GT(ten, nine);
} }
TEST(BigNum, comparisons_negative) {
auto const zero = BigNum(0);
auto const minus_one = BigNum(-1);
auto const minus_two = BigNum(-2);
EXPECT_LT(minus_one, zero);
EXPECT_LE(minus_one, zero);
EXPECT_LT(minus_two, minus_one);
EXPECT_LE(minus_two, minus_one);
EXPECT_LE(minus_one, minus_one);
EXPECT_GE(minus_two, minus_two);
EXPECT_GT(zero, minus_one);
EXPECT_GE(zero, minus_one);
EXPECT_GT(minus_one, minus_two);
EXPECT_GE(minus_one, minus_two);
EXPECT_GE(minus_one, minus_one);
EXPECT_GE(minus_two, minus_two);
}
TEST(BigNum, comparisons_digits_negative) {
auto const minus_nine = BigNum(-9);
auto const minus_ten = BigNum(-10);
EXPECT_LT(minus_ten, minus_nine);
EXPECT_GT(minus_nine, minus_ten);
}
TEST(BigNum, unary) { TEST(BigNum, unary) {
auto const zero = BigNum(0); auto const zero = BigNum(0);
auto const one = BigNum(1); auto const one = BigNum(1);