diff options
author | John Wiegley <johnw@newartisans.com> | 2009-01-31 04:58:16 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-01-31 04:58:16 -0400 |
commit | cce65b8dd2f9cb22a20acb891a8a83cdfce90c7b (patch) | |
tree | 2cd8e428843848f9b7a9e780c053def5ce245d3c | |
parent | 5a4478481795ac0460c7d9d2e8f90986bee3aece (diff) | |
download | fork-ledger-cce65b8dd2f9cb22a20acb891a8a83cdfce90c7b.tar.gz fork-ledger-cce65b8dd2f9cb22a20acb891a8a83cdfce90c7b.tar.bz2 fork-ledger-cce65b8dd2f9cb22a20acb891a8a83cdfce90c7b.zip |
Added a more efficient amount_t::operator==, which changed semantics slightly.
-rw-r--r-- | src/amount.cc | 12 | ||||
-rw-r--r-- | src/amount.h | 6 | ||||
-rw-r--r-- | test/unit/t_amount.cc | 7 |
3 files changed, 15 insertions, 10 deletions
diff --git a/src/amount.cc b/src/amount.cc index 653272ed..bc8b0b6f 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -259,6 +259,18 @@ int amount_t::compare(const amount_t& amt) const return mpq_cmp(MP(quantity), MP(amt.quantity)); } +bool amount_t::operator==(const amount_t& amt) const +{ + if ((quantity && ! amt.quantity) || (! quantity && amt.quantity)) + return false; + else if (! quantity && ! amt.quantity) + return true; + else if (commodity() != amt.commodity()) + return false; + + return mpq_equal(MP(quantity), MP(amt.quantity)); +} + amount_t& amount_t::operator+=(const amount_t& amt) { diff --git a/src/amount.h b/src/amount.h index ef9fc4b0..d5c759db 100644 --- a/src/amount.h +++ b/src/amount.h @@ -781,12 +781,6 @@ inline std::istream& operator>>(std::istream& in, amount_t& amt) { namespace ledger { -inline bool amount_t::operator==(const amount_t& amt) const { - if (commodity() != amt.commodity()) - return false; - return compare(amt) == 0; -} - inline commodity_t& amount_t::commodity() const { return has_commodity() ? *commodity_ : *current_pool->null_commodity; } diff --git a/test/unit/t_amount.cc b/test/unit/t_amount.cc index 577a4aaf..d0a49119 100644 --- a/test/unit/t_amount.cc +++ b/test/unit/t_amount.cc @@ -140,10 +140,9 @@ void AmountTestCase::testConstructors() amount_t x10(x6); amount_t x11(x8); - assertThrow(amount_t(0L) == x0, amount_error); - assertThrow(amount_t() == x0, amount_error); - assertThrow(amount_t("0") == x0, amount_error); - assertThrow(amount_t("0.0") == x0, amount_error); + assertEqual(amount_t(), x0); + assertNotEqual(amount_t("0"), x0); + assertNotEqual(amount_t("0.0"), x0); assertEqual(x2, x1); assertEqual(x5, x1); assertEqual(x7, x1); |