From 948c0a54f14085b1edb76afe39ee985424ea4474 Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Tue, 11 Mar 2014 00:43:29 +0100 Subject: UnitTests: Add tests for BitField. --- Source/UnitTests/Common/BitFieldTest.cpp | 127 +++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Source/UnitTests/Common/BitFieldTest.cpp (limited to 'Source/UnitTests/Common/BitFieldTest.cpp') diff --git a/Source/UnitTests/Common/BitFieldTest.cpp b/Source/UnitTests/Common/BitFieldTest.cpp new file mode 100644 index 0000000000..16efd29d19 --- /dev/null +++ b/Source/UnitTests/Common/BitFieldTest.cpp @@ -0,0 +1,127 @@ +// Copyright 2014 Dolphin Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include + +#include "Common/BitField.h" +#include "Common/CommonTypes.h" + +union TestUnion +{ + u64 hex; + + BitField< 0,64,u64> full_u64; // spans whole storage + BitField< 0,64,s64> full_s64; // spans whole storage + + BitField< 9, 3,u64> regular_field_unsigned; // a plain bitfield + BitField< 9, 3,u64> regular_field_unsigned2; // Just the very same bitfield again + BitField< 9, 3,s64> regular_field_signed; // Same bitfield, but different sign + + BitField<30, 4,s64> at_dword_boundary; // goes over the boundary of two u32 values + + BitField<15, 1,s64> signed_1bit; // allowed values: -1 and 0 +}; + +// table of raw numbers to test with +u64 table[] = +{ + 0x0000000000000000ull, // all zero + 0xffffffffffffffffull, // all one + 0x7fffffffffffffffull, // all one apart from the sign bit + 0x8000000000000000ull, // all zero apart from the sign bit + 0x8000000000000048ull, // regular_field = 0b1001 + + // "random" numbers + 0x0F7B8B1ABD9B8D3Full, + 0xA8B86F73FDAADD2Dull, + 0x1B17A557BFEB351Dull, + 0xE3354268B0C2395Bull, +}; + +// Verify that bitfields in a union have the same underlying data +TEST(BitField, Storage) +{ + TestUnion object; + + EXPECT_EQ((void*)&object.hex, (void*)&object.regular_field_unsigned); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.hex)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.full_u64)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.full_s64)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.regular_field_unsigned)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.regular_field_signed)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.at_dword_boundary)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.signed_1bit)); + + // Now write some values to one field and check if this reflects properly + // in the others. + for (u64 val : table) + { + object.hex = val; + EXPECT_EQ(object.hex, object.full_u64); + EXPECT_EQ(object.regular_field_unsigned, object.regular_field_unsigned2); + + object.regular_field_unsigned = val & 0x3; + EXPECT_EQ(object.hex, object.full_u64); + EXPECT_EQ(object.regular_field_unsigned, object.regular_field_unsigned2); + } +} + +TEST(BitField, Read) +{ + TestUnion object; + + for (u64 val : table) + { + object.hex = val; + + // Make sure reading/casting does not behave completely idiotic + EXPECT_EQ(object.full_u64, (u64)object.full_u64); + EXPECT_EQ(object.full_s64, (s64)object.full_s64); + EXPECT_EQ(object.regular_field_unsigned, (u64)object.regular_field_unsigned); + EXPECT_EQ(object.regular_field_unsigned2, (u64)object.regular_field_unsigned2); + EXPECT_EQ(object.regular_field_signed, (s64)object.regular_field_signed); + EXPECT_EQ(object.at_dword_boundary, (s64)object.at_dword_boundary); + EXPECT_EQ(object.signed_1bit, (s64)object.signed_1bit); + + // Now make sure the value is indeed correct + EXPECT_EQ(val, object.full_u64); + EXPECT_EQ(*(s64*)&val, object.full_s64); + EXPECT_EQ((val >> 9) & 0x7, object.regular_field_unsigned); + EXPECT_EQ((val >> 9) & 0x7, object.regular_field_unsigned2); + EXPECT_EQ(((s64)(object.hex << 52)) >> 61, object.regular_field_signed); + EXPECT_EQ(((s64)(object.hex << 30)) >> 60, object.at_dword_boundary); + EXPECT_EQ(((object.hex >> 15) & 1) ? -1 : 0, object.signed_1bit); + } +} + +TEST(BitField, Assignment) +{ + TestUnion object; + + 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); + } +} -- cgit v1.2.3 From 774a394808d6855ccd64574f5e93c1c2a3188f58 Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Sun, 13 Apr 2014 13:47:21 +0200 Subject: UnitTests: Add a test for BitField behavior on odd structure alignment. --- Source/UnitTests/Common/BitFieldTest.cpp | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'Source/UnitTests/Common/BitFieldTest.cpp') 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); + } +} -- cgit v1.2.3 From c9405b2030fec81e7edef6f5d3f3c2e2a338bf4d Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sat, 19 Apr 2014 18:31:35 +0200 Subject: BitFieldTest: fix warnings --- Source/UnitTests/Common/BitFieldTest.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'Source/UnitTests/Common/BitFieldTest.cpp') diff --git a/Source/UnitTests/Common/BitFieldTest.cpp b/Source/UnitTests/Common/BitFieldTest.cpp index 800bb84360..a97fd3bb82 100644 --- a/Source/UnitTests/Common/BitFieldTest.cpp +++ b/Source/UnitTests/Common/BitFieldTest.cpp @@ -106,7 +106,7 @@ TEST(BitField, Assignment) EXPECT_EQ(val, object.full_u64); object.full_s64 = (s64)val; - EXPECT_EQ((s64)val, object.full_u64); + EXPECT_EQ(val, object.full_u64); object.regular_field_unsigned = val; EXPECT_EQ(val & 0x7, object.regular_field_unsigned); @@ -121,8 +121,8 @@ TEST(BitField, Assignment) 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); + object.at_dword_boundary = object.regular_field_signed; + EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary); } } @@ -148,7 +148,7 @@ TEST(BitField, Alignment) EXPECT_EQ(val, object.full_u64); object.full_s64 = (s64)val; - EXPECT_EQ((s64)val, object.full_u64); + EXPECT_EQ(val, object.full_u64); object.regular_field_unsigned = val; EXPECT_EQ(val & 0x7, object.regular_field_unsigned); @@ -163,7 +163,7 @@ TEST(BitField, Alignment) 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); + object.at_dword_boundary = object.regular_field_signed; + EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary); } } -- cgit v1.2.3 From f927af20f2d62286a7d077cc6ef4e345819ecafc Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Thu, 7 Aug 2014 03:24:42 +0200 Subject: Fix more warnings from #579 --- Source/UnitTests/Common/BitFieldTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/UnitTests/Common/BitFieldTest.cpp') diff --git a/Source/UnitTests/Common/BitFieldTest.cpp b/Source/UnitTests/Common/BitFieldTest.cpp index a97fd3bb82..aa98352272 100644 --- a/Source/UnitTests/Common/BitFieldTest.cpp +++ b/Source/UnitTests/Common/BitFieldTest.cpp @@ -24,7 +24,7 @@ union TestUnion }; // table of raw numbers to test with -u64 table[] = +static u64 table[] = { 0x0000000000000000ull, // all zero 0xffffffffffffffffull, // all one -- cgit v1.2.3 From 839cace5ff15b56afa3a72c86b379c6abc4c0bf6 Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Sun, 31 Aug 2014 05:52:21 -0700 Subject: msvc: get UnitTests compiling Choose it from VS or pass /p:RunUnitTests=true to msbuild --- Source/UnitTests/Common/BitFieldTest.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Source/UnitTests/Common/BitFieldTest.cpp') diff --git a/Source/UnitTests/Common/BitFieldTest.cpp b/Source/UnitTests/Common/BitFieldTest.cpp index aa98352272..ff317649e6 100644 --- a/Source/UnitTests/Common/BitFieldTest.cpp +++ b/Source/UnitTests/Common/BitFieldTest.cpp @@ -139,7 +139,8 @@ TEST(BitField, Alignment) GC_ALIGNED16(OddlyAlignedTestStruct test_struct); TestUnion& object = test_struct.obj; - static_assert(alignof(test_struct.obj.signed_1bit) == 1, "Incorrect variable alignment"); + static_assert(alignof(decltype(test_struct.obj.signed_1bit)) == 1, + "Incorrect variable alignment"); for (u64 val : table) { -- cgit v1.2.3 From cefcb0ace9d363b3679b4e93bcc9ec05f1e5f4f8 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Mon, 18 May 2015 01:08:10 +0200 Subject: Update license headers to GPLv2+ --- Source/UnitTests/Common/BitFieldTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/UnitTests/Common/BitFieldTest.cpp') diff --git a/Source/UnitTests/Common/BitFieldTest.cpp b/Source/UnitTests/Common/BitFieldTest.cpp index ff317649e6..f3a6a4ca2a 100644 --- a/Source/UnitTests/Common/BitFieldTest.cpp +++ b/Source/UnitTests/Common/BitFieldTest.cpp @@ -1,5 +1,5 @@ // Copyright 2014 Dolphin Emulator Project -// Licensed under GPLv2 +// Licensed under GPLv2+ // Refer to the license.txt file included. #include -- cgit v1.2.3