diff options
author | Tom <tomut@yahoo.com> | 2022-01-20 21:31:13 -0700 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-23 22:45:21 +0000 |
commit | c468a9cc2de5cf73b4724527740f7fd612728137 (patch) | |
tree | 6ccffda66ef36e05153bc32182e820e690be683a /Tests | |
parent | 06fc72ca0cf3bda925d4dfc8dbcea9e952c4a2af (diff) | |
download | serenity-c468a9cc2de5cf73b4724527740f7fd612728137.zip |
AK: Add FixedPoint cast operator for up/downcasting to other sizes
This enables casting between different size FixedPoint variables or
constructing them from other sized FixedPoint values.
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/AK/TestFixedPoint.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Tests/AK/TestFixedPoint.cpp b/Tests/AK/TestFixedPoint.cpp index 422320d1e3..8127837ab0 100644 --- a/Tests/AK/TestFixedPoint.cpp +++ b/Tests/AK/TestFixedPoint.cpp @@ -73,6 +73,38 @@ TEST_CASE(rounding) EXPECT_EQ(Type(-1.5).ltrunk(), -1); } +TEST_CASE(cast) +{ + FixedPoint<16, u32> downcast_value1(FixedPoint<32, u64>(123.4567)); + EXPECT((double)downcast_value1 >= 123.4566 && (double)downcast_value1 <= 123.4568); + static constexpr FixedPoint<32, u64> value1(321.7654); + downcast_value1 = value1; + EXPECT((double)downcast_value1 >= 321.7653 && (double)downcast_value1 <= 321.7655); + FixedPoint<6, u32> downcast_value2(FixedPoint<32, u64>(4567.123456)); + EXPECT((double)downcast_value2 >= 4567.1 && (double)downcast_value2 <= 4567.2); + downcast_value2 = FixedPoint<32, u64>(7654.654321); + EXPECT((double)downcast_value2 >= 7654.64 && (double)downcast_value2 <= 7654.66); + + EXPECT((double)downcast_value2 >= 7654.64 && (double)downcast_value2 <= 7654.66); + FixedPoint<6, u32> downcast_value3(FixedPoint<32, u64>(4567.987654)); + EXPECT((double)downcast_value3 >= 4567.9 && (double)downcast_value3 <= 4567.99); + downcast_value3 = FixedPoint<32, u64>(7654.456789); + EXPECT((double)downcast_value3 >= 7654.45 && (double)downcast_value3 <= 7654.46); + + FixedPoint<32, u64> upcast_value1(FixedPoint<16, u32>(123.4567)); + EXPECT((double)upcast_value1 >= 123.4566 && (double)upcast_value1 <= 123.4568); + upcast_value1 = FixedPoint<16, u32>(321.7654); + EXPECT((double)upcast_value1 >= 321.7653 && (double)upcast_value1 <= 321.7655); + FixedPoint<32, u64> upcast_value2(FixedPoint<6, u32>(4567.123456)); + EXPECT((double)upcast_value2 >= 4567.1 && (double)upcast_value2 <= 4567.2); + upcast_value2 = FixedPoint<6, u32>(7654.654321); + EXPECT((double)upcast_value2 >= 7654.64 && (double)upcast_value2 <= 7654.66); + FixedPoint<32, u64> upcast_value3(FixedPoint<6, u32>(4567.987654)); + EXPECT((double)upcast_value3 >= 4567.9 && (double)upcast_value3 <= 4567.99); + upcast_value3 = FixedPoint<6, u32>(7654.456789); + EXPECT((double)upcast_value3 >= 7654.45 && (double)upcast_value3 <= 7654.46); +} + TEST_CASE(formatter) { EXPECT_EQ(String::formatted("{}", FixedPoint<16>(123.456)), "123.455993"sv); |