diff --git a/AK/Checked.h b/AK/Checked.h index 17903960fe4..13090e6b1a8 100644 --- a/AK/Checked.h +++ b/AK/Checked.h @@ -255,7 +255,7 @@ public: constexpr Checked& operator+=(Checked const& other) { m_overflow |= other.m_overflow; - add(other.value()); + add(other.value_unchecked()); return *this; } @@ -268,7 +268,7 @@ public: constexpr Checked& operator-=(Checked const& other) { m_overflow |= other.m_overflow; - sub(other.value()); + sub(other.value_unchecked()); return *this; } @@ -281,7 +281,7 @@ public: constexpr Checked& operator*=(Checked const& other) { m_overflow |= other.m_overflow; - mul(other.value()); + mul(other.value_unchecked()); return *this; } @@ -294,7 +294,7 @@ public: constexpr Checked& operator/=(Checked const& other) { m_overflow |= other.m_overflow; - div(other.value()); + div(other.value_unchecked()); return *this; } @@ -307,7 +307,7 @@ public: constexpr Checked& operator%=(Checked const& other) { m_overflow |= other.m_overflow; - mod(other.value()); + mod(other.value_unchecked()); return *this; } @@ -434,7 +434,7 @@ template constexpr Checked operator+(Checked const& a, Checked const& b) { Checked c { a }; - c.add(b.value()); + c += b; return c; } @@ -442,7 +442,7 @@ template constexpr Checked operator-(Checked const& a, Checked const& b) { Checked c { a }; - c.sub(b.value()); + c -= b; return c; } @@ -450,7 +450,7 @@ template constexpr Checked operator*(Checked const& a, Checked const& b) { Checked c { a }; - c.mul(b.value()); + c *= b; return c; } @@ -458,7 +458,7 @@ template constexpr Checked operator/(Checked const& a, Checked const& b) { Checked c { a }; - c.div(b.value()); + c /= b; return c; } @@ -466,7 +466,7 @@ template constexpr Checked operator%(Checked const& a, Checked const& b) { Checked c { a }; - c.mod(b.value()); + c %= b; return c; } diff --git a/Tests/AK/TestChecked.cpp b/Tests/AK/TestChecked.cpp index 3342d8e4566..452b1eb46e9 100644 --- a/Tests/AK/TestChecked.cpp +++ b/Tests/AK/TestChecked.cpp @@ -135,6 +135,49 @@ TEST_CASE(detects_unsigned_overflow) EXPECT((Checked(0x4000000000000000) - Checked(0x4000000000000001)).has_overflow()); } +TEST_CASE(operations_with_overflowed) +{ + { + Checked overflowed(0x100000000); + EXPECT((Checked(0x100000000) + Checked(0xff)).has_overflow()); + EXPECT((Checked(0xff) + Checked(0x100000000)).has_overflow()); + EXPECT((overflowed += Checked(0xff)).has_overflow()); + EXPECT((overflowed += Checked(0x100000000)).has_overflow()); + } + + { + Checked overflowed(0x100000000); + EXPECT((Checked(0x100000000) - Checked(0xff)).has_overflow()); + EXPECT((Checked(0xff) - Checked(0x100000000)).has_overflow()); + EXPECT((overflowed -= Checked(0xff)).has_overflow()); + EXPECT((overflowed -= Checked(0x100000000)).has_overflow()); + } + + { + Checked overflowed(0x100000000); + EXPECT((Checked(0x100000000) * Checked(0xff)).has_overflow()); + EXPECT((Checked(0xff) * Checked(0x100000000)).has_overflow()); + EXPECT((overflowed *= Checked(0xff)).has_overflow()); + EXPECT((overflowed *= Checked(0x100000000)).has_overflow()); + } + + { + Checked overflowed(0x100000000); + EXPECT((Checked(0x100000000) / Checked(0xff)).has_overflow()); + EXPECT((Checked(0xff) / Checked(0x100000000)).has_overflow()); + EXPECT((overflowed /= Checked(0xff)).has_overflow()); + EXPECT((overflowed /= Checked(0x100000000)).has_overflow()); + } + + { + Checked overflowed(0x100000000); + EXPECT((Checked(0x100000000) % Checked(0xff)).has_overflow()); + EXPECT((Checked(0xff) % Checked(0x100000000)).has_overflow()); + EXPECT((overflowed %= Checked(0xff)).has_overflow()); + EXPECT((overflowed %= Checked(0x100000000)).has_overflow()); + } +} + TEST_CASE(should_constexpr_default_construct) { constexpr Checked checked_value {};