From 7cd0664e602714300aefa36eb7bab24e8cac0a2f Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 21 Aug 2021 01:23:23 +0200 Subject: [PATCH] abacus: bignum: fix carry bug in addition --- src/bignum/bignum.cc | 4 ++++ tests/unit/bignum.cc | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc index 4c5b19f..2d99095 100644 --- a/src/bignum/bignum.cc +++ b/src/bignum/bignum.cc @@ -52,6 +52,10 @@ digits_type do_addition(digits_type const& lhs, digits_type const& rhs) { ++it; } + if (carry != 0) { + res.push_back(carry); + } + return res; } diff --git a/tests/unit/bignum.cc b/tests/unit/bignum.cc index 8797633..31cf03a 100644 --- a/tests/unit/bignum.cc +++ b/tests/unit/bignum.cc @@ -115,6 +115,15 @@ TEST(BigNum, addition_flips_sign) { EXPECT_EQ(two + minus_one, one); } +TEST(BigNum, addition_carry) { + auto const one = BigNum(1); + auto const nine = BigNum(9); + auto const ten = BigNum(10); + + EXPECT_EQ(one + nine, ten); + EXPECT_EQ(nine + one, ten); +} + TEST(BigNum, substraction_positive) { auto const zero = BigNum(0); auto const one = BigNum(1);