diff options
author | Lenny Maiorani <lenny@colorado.edu> | 2020-10-19 12:30:30 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-20 16:31:24 +0200 |
commit | a40abd6ce3a032b1baf52c3573ff02ad658c6e91 (patch) | |
tree | d632ac43153598b3fadb515ff9724c4574d03da5 /AK/Tests | |
parent | bd99083436f313d8105cf7ba489f594e49e9e625 (diff) | |
download | serenity-a40abd6ce3a032b1baf52c3573ff02ad658c6e91.zip |
Checked: constexpr support
Problem:
- `Checked` is not `constexpr`-aware.
Solution:
- Decorate member functions with `constexpr` keyword.
- Add tests to ensure the functionality where possible.
Diffstat (limited to 'AK/Tests')
-rw-r--r-- | AK/Tests/TestChecked.cpp | 269 |
1 files changed, 269 insertions, 0 deletions
diff --git a/AK/Tests/TestChecked.cpp b/AK/Tests/TestChecked.cpp index e1a7a234a3..20ea0032dc 100644 --- a/AK/Tests/TestChecked.cpp +++ b/AK/Tests/TestChecked.cpp @@ -27,6 +27,7 @@ #include <AK/TestSuite.h> #include <AK/Checked.h> +#include <AK/NumericLimits.h> // These tests only check whether the usual operator semantics work. // TODO: Add tests about the actual `Check`ing itself! @@ -94,4 +95,272 @@ TEST_CASE(operator_arith) EXPECT_EQ(b / a, 28); } +TEST_CASE(should_constexpr_default_construct) +{ + constexpr Checked<int> checked_value {}; + static_assert(!checked_value.has_overflow()); + static_assert(checked_value == int {}); +} + +TEST_CASE(should_constexpr_value_construct) +{ + constexpr Checked<int> checked_value { 42 }; + static_assert(!checked_value.has_overflow()); + static_assert(checked_value == 42); +} + +TEST_CASE(should_constexpr_convert_construct) +{ + constexpr Checked<int> checked_value { 42u }; + static_assert(!checked_value.has_overflow()); + static_assert(checked_value == 42); +} + +TEST_CASE(should_constexpr_copy_construct) +{ + constexpr auto checked_value = [] { + const Checked<int> old_value { 42 }; + Checked<int> value(old_value); + return value; + }(); + static_assert(!checked_value.has_overflow()); + static_assert(checked_value == 42); +} + +TEST_CASE(should_constexpr_move_construct) +{ + constexpr auto checked_value = [] { + Checked<int> value(Checked<int> { 42 }); + return value; + }(); + static_assert(!checked_value.has_overflow()); + static_assert(checked_value == 42); +} + +TEST_CASE(should_constexpr_copy_assign) +{ + constexpr auto checked_value = [] { + const Checked<int> old_value { 42 }; + Checked<int> value {}; + value = old_value; + return value; + }(); + static_assert(!checked_value.has_overflow()); + static_assert(checked_value == 42); +} + +TEST_CASE(should_constexpr_move_assign) +{ + constexpr auto checked_value = [] { + Checked<int> value {}; + value = Checked<int> { 42 }; + return value; + }(); + static_assert(!checked_value.has_overflow()); + static_assert(checked_value == 42); +} + +TEST_CASE(should_constexpr_convert_and_assign) +{ + constexpr auto checked_value = [] { + Checked<int> value {}; + value = 42; + return value; + }(); + static_assert(!checked_value.has_overflow()); + static_assert(checked_value == 42); +} + +TEST_CASE(should_constexpr_not_operator) +{ + constexpr Checked<int> value {}; + static_assert(!value); +} + +TEST_CASE(should_constexpr_value_accessor) +{ + constexpr Checked<int> value { 42 }; + static_assert(value.value() == 42); +} + +TEST_CASE(should_constexpr_add) +{ + constexpr auto checked_value = [] { + Checked<int> value { 42 }; + value.add(3); + return value; + }(); + static_assert(checked_value == 45); +} + +TEST_CASE(should_constexpr_sub) +{ + constexpr auto checked_value = [] { + Checked<int> value { 42 }; + value.sub(3); + return value; + }(); + static_assert(checked_value == 39); +} + +TEST_CASE(should_constexpr_mul) +{ + constexpr auto checked_value = [] { + Checked<int> value { 42 }; + value.mul(2); + return value; + }(); + static_assert(checked_value == 84); +} + +TEST_CASE(should_constexpr_div) +{ + constexpr auto checked_value = [] { + Checked<int> value { 42 }; + value.div(3); + return value; + }(); + static_assert(checked_value == 14); +} + +TEST_CASE(should_constexpr_assignment_by_sum) +{ + constexpr auto checked_value = [] { + Checked<int> value { 42 }; + value += 3; + return value; + }(); + static_assert(checked_value == 45); +} + +TEST_CASE(should_constexpr_assignment_by_diff) +{ + constexpr auto checked_value = [] { + Checked<int> value { 42 }; + value -= 3; + return value; + }(); + static_assert(checked_value == 39); +} + +TEST_CASE(should_constexpr_assignment_by_product) +{ + constexpr auto checked_value = [] { + Checked<int> value { 42 }; + value *= 2; + return value; + }(); + static_assert(checked_value == 84); +} + +TEST_CASE(should_constexpr_assignment_by_quotient) +{ + constexpr auto checked_value = [] { + Checked<int> value { 42 }; + value /= 3; + return value; + }(); + static_assert(checked_value == 14); +} + +TEST_CASE(should_constexpr_prefix_increment) +{ + constexpr auto checked_value = [] { + Checked<int> value { 42 }; + ++value; + return value; + }(); + static_assert(checked_value == 43); +} + +TEST_CASE(should_constexpr_postfix_increment) +{ + constexpr auto checked_value = [] { + Checked<int> value { 42 }; + value++; + return value; + }(); + static_assert(checked_value == 43); +} + +TEST_CASE(should_constexpr_check_for_overflow_addition) +{ + static_assert(Checked<int>::addition_would_overflow(NumericLimits<int>::max(), 1)); +} + +TEST_CASE(should_constexpr_check_for_overflow_multiplication) +{ + static_assert(Checked<int>::multiplication_would_overflow(NumericLimits<int>::max(), 2)); + static_assert(Checked<int>::multiplication_would_overflow(NumericLimits<int>::max(), 1, 2)); +} + +TEST_CASE(should_constexpr_add_checked_values) +{ + constexpr Checked<int> a { 42 }; + constexpr Checked<int> b { 17 }; + constexpr Checked<int> expected { 59 }; + static_assert(expected == (a + b).value()); +} + +TEST_CASE(should_constexpr_subtract_checked_values) +{ + constexpr Checked<int> a { 42 }; + constexpr Checked<int> b { 17 }; + constexpr Checked<int> expected { 25 }; + static_assert(expected == (a - b).value()); +} + +TEST_CASE(should_constexpr_multiply_checked_values) +{ + constexpr Checked<int> a { 3 }; + constexpr Checked<int> b { 5 }; + constexpr Checked<int> expected { 15 }; + static_assert(expected == (a * b).value()); +} + +TEST_CASE(should_constexpr_divide_checked_values) +{ + constexpr Checked<int> a { 10 }; + constexpr Checked<int> b { 2 }; + constexpr Checked<int> expected { 5 }; + static_assert(expected == (a / b).value()); +} + +TEST_CASE(should_constexpr_compare_checked_values_lhs) +{ + constexpr Checked<int> a { 10 }; + + static_assert(a > 5); + static_assert(a >= 10); + static_assert(a >= 5); + + static_assert(a < 20); + static_assert(a <= 30); + static_assert(a <= 20); + + static_assert(a == 10); + static_assert(a != 20); +} + +TEST_CASE(should_constexpr_compare_checked_values_rhs) +{ + constexpr Checked<int> a { 10 }; + + static_assert(5 < a); + static_assert(10 <= a); + static_assert(5 <= a); + + static_assert(20 > a); + static_assert(30 >= a); + static_assert(30 >= a); + + static_assert(10 == a); + static_assert(20 != a); +} + +TEST_CASE(should_constexpr_make_via_factory) +{ + [[maybe_unused]] constexpr auto value = make_checked(42); +} + TEST_MAIN(Checked) |