abacus: bignum: fix comparison of negative numbers
This commit is contained in:
parent
3e5133da1a
commit
a382b299b0
|
@ -363,7 +363,11 @@ bool BigNum::less_than(BigNum const& rhs) const {
|
|||
return sign_ < rhs.sign_;
|
||||
}
|
||||
|
||||
return do_less_than(digits_, rhs.digits_);
|
||||
if (is_positive()) {
|
||||
return do_less_than(digits_, rhs.digits_);
|
||||
} else {
|
||||
return do_less_than(rhs.digits_, digits_);
|
||||
}
|
||||
}
|
||||
|
||||
void BigNum::canonicalize() {
|
||||
|
|
|
@ -68,6 +68,34 @@ TEST(BigNum, comparisons_digits) {
|
|||
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) {
|
||||
auto const zero = BigNum(0);
|
||||
auto const one = BigNum(1);
|
||||
|
|
Loading…
Reference in a new issue