diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-11-10 16:20:07 +0000 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-11-11 17:50:53 +0330 |
commit | cf046dbfdb766d0ceb6df530171af92cd407f1e7 (patch) | |
tree | 5573c0df5f7d3ab38e60704ca90cc74715b9369d /Tests/AK/TestDistinctNumeric.cpp | |
parent | c33eae24f928bb4042aa027675cd98f5a6d6149a (diff) | |
download | serenity-cf046dbfdb766d0ceb6df530171af92cd407f1e7.zip |
AK: Add optional explicit cast to underlying type to DistinctNumeric
Diffstat (limited to 'Tests/AK/TestDistinctNumeric.cpp')
-rw-r--r-- | Tests/AK/TestDistinctNumeric.cpp | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/Tests/AK/TestDistinctNumeric.cpp b/Tests/AK/TestDistinctNumeric.cpp index a00c286b70..e8b398b740 100644 --- a/Tests/AK/TestDistinctNumeric.cpp +++ b/Tests/AK/TestDistinctNumeric.cpp @@ -41,7 +41,8 @@ AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, BoolNumeric, CastToBool); AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, FlagsNumeric, Flags); AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, ShiftNumeric, Shift); AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, ArithNumeric, Arithmetic); -AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, GeneralNumeric, Arithmetic, CastToBool, Comparison, Flags, Increment, Shift); +AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, UnderlyingNumeric, CastToUnderlying); +AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, GeneralNumeric, Arithmetic, CastToBool, CastToUnderlying, Comparison, Flags, Increment, Shift); TEST_CASE(address_identity) { @@ -105,6 +106,14 @@ TEST_CASE(operator_bool) EXPECT_EQ(!c, false); } +TEST_CASE(operator_underlying) +{ + UnderlyingNumeric a = 0; + UnderlyingNumeric b = 42; + EXPECT_EQ(static_cast<int>(a), 0); + EXPECT_EQ(static_cast<int>(b), 42); +} + TEST_CASE(operator_flags) { FlagsNumeric a = 0; @@ -216,6 +225,9 @@ TEST_CASE(composability) EXPECT_EQ(-b, GeneralNumeric(-1)); EXPECT_EQ(a + b, b); EXPECT_EQ(b * GeneralNumeric(42), GeneralNumeric(42)); + // Underlying + EXPECT_EQ(static_cast<int>(a), 0); + EXPECT_EQ(static_cast<int>(b), 1); } /* @@ -268,6 +280,13 @@ TEST_CASE(negative_arith) // error: static assertion failed: 'a+b' is only available for DistinctNumeric types with 'Arithmetic'. } +TEST_CASE(negative_underlying) +{ + BareNumeric a = 12; + [[maybe_unused]] int res = static_cast<int>(a); + // error: static assertion failed: Cast to underlying type is only available for DistinctNumeric types with 'CastToUnderlying'. +} + TEST_CASE(negative_incompatible) { GeneralNumeric a = 12; |