Compare commits

..

No commits in common. "c9460c09d5e0938d6e09330460d785f35ac7dbb8" and "3e5133da1ae0fa0eac384a81a23e02877113b5e0" have entirely different histories.

2 changed files with 7 additions and 39 deletions

View file

@ -363,11 +363,7 @@ 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() {
@ -385,15 +381,15 @@ bool BigNum::is_canonicalized() const {
return sign_ == 0; return sign_ == 0;
} }
// `back` is valid since there is at least one element auto const leading_zeros = std::find_if(digits_.rbegin(), digits_.rend(),
auto const has_leading_zero = digits_.back() == 0; [](auto v) { return v != 0; });
if (has_leading_zero) { if (leading_zeros != digits_.rbegin()) {
return false; return false;
} }
auto const has_overflow = std::any_of(digits_.begin(), digits_.end(), auto const overflow = std::find_if(digits_.begin(), digits_.end(),
[](auto v) { return v >= BASE; }); [](auto v) { return v >= BASE; });
if (has_overflow) { if (overflow != digits_.end()) {
return false; return false;
} }

View file

@ -68,34 +68,6 @@ 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);