summaryrefslogtreecommitdiff
path: root/Tests/AK
diff options
context:
space:
mode:
authorNick Johnson <sylvyrfysh@gmail.com>2021-12-12 16:52:57 -0600
committerAndreas Kling <kling@serenityos.org>2021-12-18 23:36:08 +0100
commit548529ace403130b88072d548277f489eaaf4d9e (patch)
tree0c9cf4d35f38c95c2beba51139799c19ba6dc2a5 /Tests/AK
parent01eefc344a63e5b64a23d9c1c71ddc78bfcb80ef (diff)
downloadserenity-548529ace403130b88072d548277f489eaaf4d9e.zip
AK: Add BuiltinWrappers.h
The goal of this file is to enable C++ overloaded functions for standard builtin functions that we use. It contains fallback implementations for systems that do not have the builtins available.
Diffstat (limited to 'Tests/AK')
-rw-r--r--Tests/AK/CMakeLists.txt1
-rw-r--r--Tests/AK/TestBuiltinWrappers.cpp54
2 files changed, 55 insertions, 0 deletions
diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txt
index f1cd7d0129..e1dc8eb076 100644
--- a/Tests/AK/CMakeLists.txt
+++ b/Tests/AK/CMakeLists.txt
@@ -10,6 +10,7 @@ set(AK_TEST_SOURCES
TestBinarySearch.cpp
TestBitCast.cpp
TestBitmap.cpp
+ TestBuiltinWrappers.cpp
TestByteBuffer.cpp
TestCharacterTypes.cpp
TestChecked.cpp
diff --git a/Tests/AK/TestBuiltinWrappers.cpp b/Tests/AK/TestBuiltinWrappers.cpp
new file mode 100644
index 0000000000..87b0769b7c
--- /dev/null
+++ b/Tests/AK/TestBuiltinWrappers.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2018-2020, Nick Johnson <sylvyrfysh@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibTest/TestCase.h>
+
+#include <AK/BuiltinWrappers.h>
+#include <AK/Types.h>
+
+TEST_CASE(wrapped_popcount)
+{
+ EXPECT_EQ(popcount(NumericLimits<u8>::max()), 8);
+ EXPECT_EQ(popcount(NumericLimits<u16>::max()), 16);
+ EXPECT_EQ(popcount(NumericLimits<u32>::max()), 32);
+ EXPECT_EQ(popcount(NumericLimits<u64>::max()), 64);
+ EXPECT_EQ(popcount(NumericLimits<size_t>::max()), static_cast<int>(8 * sizeof(size_t)));
+ EXPECT_EQ(popcount(0u), 0);
+ EXPECT_EQ(popcount(0b01010101ULL), 4);
+}
+
+TEST_CASE(wrapped_count_leading_zeroes)
+{
+ EXPECT_EQ(count_leading_zeroes(NumericLimits<u8>::max()), 0);
+ EXPECT_EQ(count_leading_zeroes(static_cast<u8>(0x20)), 2);
+ EXPECT_EQ(count_leading_zeroes_safe(static_cast<u8>(0)), 8);
+ EXPECT_EQ(count_leading_zeroes(NumericLimits<u16>::max()), 0);
+ EXPECT_EQ(count_leading_zeroes(static_cast<u16>(0x20)), 10);
+ EXPECT_EQ(count_leading_zeroes_safe(static_cast<u16>(0)), 16);
+ EXPECT_EQ(count_leading_zeroes(NumericLimits<u32>::max()), 0);
+ EXPECT_EQ(count_leading_zeroes(static_cast<u32>(0x20)), 26);
+ EXPECT_EQ(count_leading_zeroes_safe(static_cast<u32>(0)), 32);
+ EXPECT_EQ(count_leading_zeroes(NumericLimits<u64>::max()), 0);
+}
+
+TEST_CASE(wrapped_count_trailing_zeroes)
+{
+ EXPECT_EQ(count_trailing_zeroes(NumericLimits<u8>::max()), 0);
+ EXPECT_EQ(count_trailing_zeroes(static_cast<u8>(1)), 0);
+ EXPECT_EQ(count_trailing_zeroes(static_cast<u8>(2)), 1);
+ EXPECT_EQ(count_trailing_zeroes_safe(static_cast<u8>(0)), 8);
+ EXPECT_EQ(count_trailing_zeroes(NumericLimits<u16>::max()), 0);
+ EXPECT_EQ(count_trailing_zeroes(static_cast<u16>(1)), 0);
+ EXPECT_EQ(count_trailing_zeroes(static_cast<u16>(2)), 1);
+ EXPECT_EQ(count_trailing_zeroes_safe(static_cast<u16>(0)), 16);
+ EXPECT_EQ(count_trailing_zeroes(NumericLimits<u32>::max()), 0);
+ EXPECT_EQ(count_trailing_zeroes(static_cast<u32>(1)), 0);
+ EXPECT_EQ(count_trailing_zeroes(static_cast<u32>(2)), 1);
+ EXPECT_EQ(count_trailing_zeroes_safe(static_cast<u32>(0)), 32);
+ EXPECT_EQ(count_trailing_zeroes(NumericLimits<u64>::max()), 0);
+ EXPECT_EQ(count_trailing_zeroes(static_cast<u64>(1)), 0);
+ EXPECT_EQ(count_trailing_zeroes(static_cast<u64>(2)), 1);
+}