summaryrefslogtreecommitdiff
path: root/Tests
diff options
context:
space:
mode:
authorHediadyoin1 <leon2002.la@gmail.com>2021-08-20 17:35:03 +0200
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-08-31 17:03:55 +0430
commitfdef6e5f76ee4ff25249101d7f682a1b16850c31 (patch)
treefd92534bd22559fad21d782575895fbf2b252568 /Tests
parentbeb8c48f26c882a77d7ddf323463b6f0f07e08f0 (diff)
downloadserenity-fdef6e5f76ee4ff25249101d7f682a1b16850c31.zip
AK: Add FixedPoint arithmetic helper
Co-authored-by: Hendiadyoin1 <leon2002.la@gmail.com> Co-authored-by: kleines Filmröllchen <malu.bertsch@gmail.com>
Diffstat (limited to 'Tests')
-rw-r--r--Tests/AK/CMakeLists.txt1
-rw-r--r--Tests/AK/TestFixedPoint.cpp74
2 files changed, 75 insertions, 0 deletions
diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txt
index ba5f1fb7c0..759ca6aa68 100644
--- a/Tests/AK/CMakeLists.txt
+++ b/Tests/AK/CMakeLists.txt
@@ -1,4 +1,5 @@
set(AK_TEST_SOURCES
+ TestFixedPoint.cpp
TestAllOf.cpp
TestAnyOf.cpp
TestArray.cpp
diff --git a/Tests/AK/TestFixedPoint.cpp b/Tests/AK/TestFixedPoint.cpp
new file mode 100644
index 0000000000..83d1271f67
--- /dev/null
+++ b/Tests/AK/TestFixedPoint.cpp
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2021, the SerenityOS developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibTest/TestCase.h>
+
+#include <AK/FixedPoint.h>
+
+using Type = FixedPoint<4>;
+
+TEST_CASE(arithmetic)
+{
+ EXPECT_EQ(
+ Type(0.5) + Type(0.5),
+ Type(1));
+ EXPECT_EQ(
+ Type(1) + Type(0.5),
+ Type(1.5));
+ EXPECT_EQ(
+ (float)(Type(1) + Type(0.5)),
+ 1.5f);
+ // FIXME: Test for rounded multiply
+ EXPECT_EQ(
+ Type((int)1) * Type(0.5),
+ Type(0.5));
+ EXPECT_EQ(
+ Type((int)1) / Type(0.5),
+ Type(2));
+}
+
+TEST_CASE(rounding)
+{
+ EXPECT_EQ(Type(0.5).round(), Type(0));
+ EXPECT_EQ(Type(0.5).floor(), Type(0));
+ EXPECT_EQ(Type(0.5).ceil(), Type(1));
+ EXPECT_EQ(Type(0.75).trunk(), Type(0));
+
+ EXPECT_EQ(Type(1.5).round(), Type(2));
+ EXPECT_EQ(Type(1.5).floor(), Type(1));
+ EXPECT_EQ(Type(1.5).ceil(), Type(2));
+ EXPECT_EQ(Type(1.25).trunk(), Type(1));
+
+ EXPECT_EQ(Type(-0.5).round(), Type(0));
+ EXPECT_EQ(Type(-0.5).floor(), Type(-1));
+ EXPECT_EQ(Type(-0.5).ceil(), Type(0));
+ EXPECT_EQ(Type(-0.75).trunk(), Type(0));
+
+ EXPECT_EQ(Type(-1.5).round(), Type(-2));
+ EXPECT_EQ(Type(-1.5).floor(), Type(-2));
+ EXPECT_EQ(Type(-1.5).ceil(), Type(-1));
+ EXPECT_EQ(Type(-1.25).trunk(), Type(-1));
+
+ EXPECT_EQ(Type(0.5).lround(), 0);
+ EXPECT_EQ(Type(0.5).lfloor(), 0);
+ EXPECT_EQ(Type(0.5).lceil(), 1);
+ EXPECT_EQ(Type(0.5).ltrunk(), 0);
+
+ EXPECT_EQ(Type(1.5).lround(), 2);
+ EXPECT_EQ(Type(1.5).lfloor(), 1);
+ EXPECT_EQ(Type(1.5).lceil(), 2);
+ EXPECT_EQ(Type(1.5).ltrunk(), 1);
+
+ EXPECT_EQ(Type(-0.5).lround(), 0);
+ EXPECT_EQ(Type(-0.5).lfloor(), -1);
+ EXPECT_EQ(Type(-0.5).lceil(), 0);
+ EXPECT_EQ(Type(-0.5).ltrunk(), 0);
+
+ EXPECT_EQ(Type(-1.5).lround(), -2);
+ EXPECT_EQ(Type(-1.5).lfloor(), -2);
+ EXPECT_EQ(Type(-1.5).lceil(), -1);
+ EXPECT_EQ(Type(-1.5).ltrunk(), -1);
+}