2014-03-10 23:43:29 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-03-10 23:43:29 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "Common/BitField.h"
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
|
2017-01-04 11:45:40 +00:00
|
|
|
union TestUnion
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
u64 hex;
|
2014-03-10 23:43:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
BitField<0, 64, u64> full_u64; // spans whole storage
|
|
|
|
BitField<0, 64, s64> full_s64; // spans whole storage
|
2014-03-10 23:43:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
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
|
2014-03-10 23:43:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
BitField<30, 4, s64> at_dword_boundary; // goes over the boundary of two u32 values
|
2014-03-10 23:43:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
BitField<15, 1, s64> signed_1bit; // allowed values: -1 and 0
|
2014-03-10 23:43:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// table of raw numbers to test with
|
2016-06-24 08:43:46 +00:00
|
|
|
static 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
|
2019-05-05 23:48:12 +00:00
|
|
|
0x0F7B8B1ABD9B8D3Full,
|
|
|
|
0xA8B86F73FDAADD2Dull,
|
|
|
|
0x1B17A557BFEB351Dull,
|
|
|
|
0xE3354268B0C2395Bull,
|
2014-03-10 23:43:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Verify that bitfields in a union have the same underlying data
|
|
|
|
TEST(BitField, Storage)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-03-10 23:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BitField, Read)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-03-10 23:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BitField, Assignment)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
TestUnion object;
|
2014-03-10 23:43:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
for (u64 val : table)
|
|
|
|
{
|
|
|
|
// Assignments with fixed values
|
|
|
|
object.full_u64 = val;
|
|
|
|
EXPECT_EQ(val, object.full_u64);
|
2014-03-10 23:43:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
object.full_s64 = (s64)val;
|
|
|
|
EXPECT_EQ(val, object.full_u64);
|
2014-03-10 23:43:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
object.regular_field_unsigned = val;
|
|
|
|
EXPECT_EQ(val & 0x7, object.regular_field_unsigned);
|
2014-03-10 23:43:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
object.at_dword_boundary = val;
|
|
|
|
EXPECT_EQ(((s64)(val << 60)) >> 60, object.at_dword_boundary);
|
2014-03-10 23:43:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
object.signed_1bit = val;
|
|
|
|
EXPECT_EQ((val & 1) ? -1 : 0, object.signed_1bit);
|
2014-03-10 23:43:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
object.regular_field_signed = val;
|
|
|
|
EXPECT_EQ(((s64)(object.hex << 61)) >> 61, object.regular_field_signed);
|
2014-03-10 23:43:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Assignment from other BitField
|
|
|
|
object.at_dword_boundary = object.regular_field_signed;
|
|
|
|
EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary);
|
|
|
|
}
|
2014-03-10 23:43:29 +00:00
|
|
|
}
|
2014-04-13 11:47:21 +00:00
|
|
|
|
|
|
|
// Test class behavior on oddly aligned structures.
|
|
|
|
TEST(BitField, Alignment)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
#pragma pack(1)
|
|
|
|
struct OddlyAlignedTestStruct
|
|
|
|
{
|
|
|
|
u8 padding;
|
|
|
|
TestUnion obj;
|
|
|
|
};
|
|
|
|
#pragma pack()
|
|
|
|
|
|
|
|
alignas(16) OddlyAlignedTestStruct test_struct;
|
|
|
|
TestUnion& object = test_struct.obj;
|
|
|
|
static_assert(alignof(decltype(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(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_signed;
|
|
|
|
EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary);
|
|
|
|
}
|
2014-04-13 11:47:21 +00:00
|
|
|
}
|