summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Common/BitFieldTest.cpp
diff options
context:
space:
mode:
authorTony Wasserka <NeoBrainX@gmail.com>2014-04-13 13:47:21 +0200
committerTony Wasserka <NeoBrainX@gmail.com>2014-04-14 20:13:23 +0200
commit774a394808d6855ccd64574f5e93c1c2a3188f58 (patch)
tree96338edbe6bfbef0ed3d63df87641bfdb2a41a90 /Source/UnitTests/Common/BitFieldTest.cpp
parentccc04944b2847e91fda3f22fae6723394644d75a (diff)
UnitTests: Add a test for BitField behavior on odd structure alignment.
Diffstat (limited to 'Source/UnitTests/Common/BitFieldTest.cpp')
-rw-r--r--Source/UnitTests/Common/BitFieldTest.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/Source/UnitTests/Common/BitFieldTest.cpp b/Source/UnitTests/Common/BitFieldTest.cpp
index 16efd29d19..800bb84360 100644
--- a/Source/UnitTests/Common/BitFieldTest.cpp
+++ b/Source/UnitTests/Common/BitFieldTest.cpp
@@ -125,3 +125,45 @@ TEST(BitField, Assignment)
EXPECT_EQ(object.regular_field_unsigned, object.at_dword_boundary);
}
}
+
+// Test class behavior on oddly aligned structures.
+TEST(BitField, Alignment)
+{
+ #pragma pack(1)
+ struct OddlyAlignedTestStruct
+ {
+ u8 padding;
+ TestUnion obj;
+ };
+ #pragma pack()
+
+ GC_ALIGNED16(OddlyAlignedTestStruct test_struct);
+ TestUnion& object = test_struct.obj;
+ static_assert(alignof(test_struct.obj.signed_1bit) == 1, "Incorrect variable alignment");
+
+ for (u64 val : table)
+ {
+ // Assignments with fixed values
+ object.full_u64 = val;
+ EXPECT_EQ(val, object.full_u64);
+
+ object.full_s64 = (s64)val;
+ EXPECT_EQ((s64)val, object.full_u64);
+
+ object.regular_field_unsigned = val;
+ EXPECT_EQ(val & 0x7, object.regular_field_unsigned);
+
+ object.at_dword_boundary = val;
+ EXPECT_EQ(((s64)(val << 60)) >> 60, object.at_dword_boundary);
+
+ object.signed_1bit = val;
+ EXPECT_EQ((val & 1) ? -1 : 0, object.signed_1bit);
+
+ object.regular_field_signed = val;
+ EXPECT_EQ(((s64)(object.hex << 61)) >> 61, object.regular_field_signed);
+
+ // Assignment from other BitField
+ object.at_dword_boundary = object.regular_field_unsigned;
+ EXPECT_EQ(object.regular_field_unsigned, object.at_dword_boundary);
+ }
+}