summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Common/BitUtilsTest.cpp
diff options
context:
space:
mode:
authorPierre Bourdon <delroth@gmail.com>2017-01-15 17:23:30 +0100
committerGitHub <noreply@github.com>2017-01-15 17:23:30 +0100
commit28f0d8e8a73af1323d3d88b91459911469f2ac1c (patch)
treee2f2b02c921ff7019de10a70c9fcd02503850a8c /Source/UnitTests/Common/BitUtilsTest.cpp
parent5297309dfab71b9a82de65d3b241bfab26f927bd (diff)
parent0a6f0dfb74d2a5ebacd20c13452f3eb3af994417 (diff)
Merge pull request #4658 from lioncash/bits
Common: Add bit utility header
Diffstat (limited to 'Source/UnitTests/Common/BitUtilsTest.cpp')
-rw-r--r--Source/UnitTests/Common/BitUtilsTest.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/Source/UnitTests/Common/BitUtilsTest.cpp b/Source/UnitTests/Common/BitUtilsTest.cpp
new file mode 100644
index 0000000000..f607f0da2f
--- /dev/null
+++ b/Source/UnitTests/Common/BitUtilsTest.cpp
@@ -0,0 +1,59 @@
+// Copyright 2017 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#include <gtest/gtest.h>
+
+#include "Common/BitUtils.h"
+#include "Common/CommonTypes.h"
+
+TEST(BitUtils, BitSize)
+{
+ EXPECT_EQ(Common::BitSize<s8>(), 8);
+ EXPECT_EQ(Common::BitSize<s16>(), 16);
+ EXPECT_EQ(Common::BitSize<s32>(), 32);
+ EXPECT_EQ(Common::BitSize<s64>(), 64);
+
+ EXPECT_EQ(Common::BitSize<u8>(), 8);
+ EXPECT_EQ(Common::BitSize<u16>(), 16);
+ EXPECT_EQ(Common::BitSize<u32>(), 32);
+ EXPECT_EQ(Common::BitSize<u64>(), 64);
+}
+
+TEST(BitUtils, ExtractBit)
+{
+ constexpr s32 zero = 0;
+ EXPECT_EQ(Common::ExtractBit<0>(zero), 0);
+
+ constexpr s32 one = 1;
+ EXPECT_EQ(Common::ExtractBit<0>(one), 1);
+
+ constexpr s32 negative_one = -1;
+ EXPECT_EQ(Common::ExtractBit<31>(negative_one), 1);
+
+ constexpr s32 one_hundred_twenty_eight = 0b10000000;
+ EXPECT_EQ(Common::ExtractBit<7>(one_hundred_twenty_eight), 1);
+}
+
+TEST(BitUtils, ExtractBits)
+{
+ // Note: Parenthesizing is necessary to prevent the macros from
+ // mangling the template function usages.
+
+ constexpr s32 two_hundred_four_signed = 0b0011001100;
+ EXPECT_EQ((Common::ExtractBits<2, 3>(two_hundred_four_signed)), 3);
+ EXPECT_EQ((Common::ExtractBits<2, 7>(two_hundred_four_signed)), 51);
+ EXPECT_EQ((Common::ExtractBits<3, 6>(two_hundred_four_signed)), 9);
+
+ constexpr u32 two_hundred_four_unsigned = 0b0011001100;
+ EXPECT_EQ((Common::ExtractBits<2, 3>(two_hundred_four_unsigned)), 3);
+ EXPECT_EQ((Common::ExtractBits<2, 7>(two_hundred_four_unsigned)), 51);
+ EXPECT_EQ((Common::ExtractBits<3, 6>(two_hundred_four_unsigned)), 9);
+
+ // Ensure bit extraction remains sign-independent even when signed types are used.
+ constexpr s32 negative_one = -1;
+ EXPECT_EQ((Common::ExtractBits<0, 31>(negative_one)), 0xFFFFFFFFU);
+
+ // Ensure bit extraction with type overriding works as expected
+ EXPECT_EQ((Common::ExtractBits<0, 31, s32, s32>(negative_one)), -1);
+}