tests: Added bitfield test

This commit is contained in:
WerWolv 2021-09-12 20:28:32 +02:00
parent 82cc528c49
commit bed5361879
10 changed files with 69 additions and 23 deletions

View File

@ -13,6 +13,7 @@ set(AVAILABLE_TESTS
Padding Padding
SucceedingAssert SucceedingAssert
FailingAssert FailingAssert
Bitfields
) )

View File

@ -28,8 +28,8 @@ namespace hex::test {
} }
template<typename T> template<typename T>
static T* create(u64 offset, size_t size, const std::string &typeName, const std::string &varName) { static T* create(const std::string &typeName, const std::string &varName, auto ... args) {
auto pattern = new T(offset, size); auto pattern = new T(args...);
pattern->setTypeName(typeName); pattern->setTypeName(typeName);
pattern->setVariableName(varName); pattern->setVariableName(varName);

View File

@ -0,0 +1,44 @@
#pragma once
#include "test_pattern.hpp"
namespace hex::test {
class TestPatternBitfields : public TestPattern {
public:
TestPatternBitfields() : TestPattern("Bitfields") {
auto testBitfield = create<PatternDataBitfield>("TestBitfield", "testBitfield", 0x12, (4 * 4) / 8);
testBitfield->setEndian(std::endian::big);
testBitfield->setFields({
create<PatternDataBitfieldField>("", "a", 0x12, 0, 4),
create<PatternDataBitfieldField>("", "b", 0x12, 4, 4),
create<PatternDataBitfieldField>("", "c", 0x12, 8, 4),
create<PatternDataBitfieldField>("", "d", 0x12, 12, 4)
});
addPattern(testBitfield);
}
~TestPatternBitfields() override = default;
[[nodiscard]]
std::string getSourceCode() const override {
return R"(
bitfield TestBitfield {
a : 4;
b : 4;
c : 4;
d : 4;
};
be TestBitfield testBitfield @ 0x12;
std::assert(testBitfield.a == 0x0A, "Field A invalid");
std::assert(testBitfield.b == 0x00, "Field B invalid");
std::assert(testBitfield.c == 0x04, "Field C invalid");
std::assert(testBitfield.d == 0x03, "Field D invalid");
)";
}
};
}

View File

@ -7,7 +7,7 @@ namespace hex::test {
class TestPatternEnums : public TestPattern { class TestPatternEnums : public TestPattern {
public: public:
TestPatternEnums() : TestPattern("Enums"){ TestPatternEnums() : TestPattern("Enums"){
auto testEnum = create<PatternDataEnum>(0x120, sizeof(u32), "TestEnum", "testEnum"); auto testEnum = create<PatternDataEnum>("TestEnum", "testEnum", 0x120, sizeof(u32));
testEnum->setEnumValues({ testEnum->setEnumValues({
{ s32(0x0000), "A" }, { s32(0x0000), "A" },
{ s32(0x1234), "B" }, { s32(0x1234), "B" },

View File

@ -7,12 +7,12 @@ namespace hex::test {
class TestPatternPadding : public TestPattern { class TestPatternPadding : public TestPattern {
public: public:
TestPatternPadding() : TestPattern("Padding") { TestPatternPadding() : TestPattern("Padding") {
auto testStruct = create<PatternDataStruct>(0x100, sizeof(s32) + 20 + sizeof(u8[0x10]), "TestStruct", "testStruct"); auto testStruct = create<PatternDataStruct>("TestStruct", "testStruct", 0x100, sizeof(s32) + 20 + sizeof(u8[0x10]));
auto variable = create<PatternDataSigned>(0x100, sizeof(s32), "s32", "variable"); auto variable = create<PatternDataSigned>("s32", "variable", 0x100, sizeof(s32));
auto padding = create<PatternDataPadding>(0x100 + sizeof(s32), 20, "", ""); auto padding = create<PatternDataPadding>("", "", 0x100 + sizeof(s32), 20);
auto array = create<PatternDataStaticArray>(0x100 + sizeof(s32) + 20, sizeof(u8[0x10]), "u8", "array"); auto array = create<PatternDataStaticArray>("u8", "array", 0x100 + sizeof(s32) + 20, sizeof(u8[0x10]));
array->setEntries(create<PatternDataUnsigned>(0x100 + sizeof(s32) + 20, sizeof(u8), "u8", ""), 0x10); array->setEntries(create<PatternDataUnsigned>("u8", "", 0x100 + sizeof(s32) + 20, sizeof(u8)), 0x10);
testStruct->setMembers({ variable, padding, array }); testStruct->setMembers({ variable, padding, array });

View File

@ -9,13 +9,13 @@ namespace hex::test {
TestPatternPlacement() : TestPattern("Placement") { TestPatternPlacement() : TestPattern("Placement") {
// placementVar // placementVar
{ {
addPattern(create<PatternDataUnsigned>(0x00, sizeof(u32), "u32", "placementVar")); addPattern(create<PatternDataUnsigned>("u32", "placementVar", 0x00, sizeof(u32)));
} }
// placementArray // placementArray
{ {
auto placementArray = create<PatternDataStaticArray>(0x10, sizeof(u8) * 10, "u8", "placementArray"); auto placementArray = create<PatternDataStaticArray>("u8", "placementArray", 0x10, sizeof(u8) * 10);
placementArray->setEntries(create<PatternDataUnsigned>(0x10, sizeof(u8), "u8", ""), 10); placementArray->setEntries(create<PatternDataUnsigned>("u8", "", 0x10, sizeof(u8)), 10);
addPattern(placementArray); addPattern(placementArray);
} }

View File

@ -7,11 +7,11 @@ namespace hex::test {
class TestPatternStructs : public TestPattern { class TestPatternStructs : public TestPattern {
public: public:
TestPatternStructs() : TestPattern("Structs") { TestPatternStructs() : TestPattern("Structs") {
auto testStruct = create<PatternDataStruct>(0x100, sizeof(s32) + sizeof(u8[0x10]), "TestStruct", "testStruct"); auto testStruct = create<PatternDataStruct>("TestStruct", "testStruct", 0x100, sizeof(s32) + sizeof(u8[0x10]));
auto variable = create<PatternDataSigned>(0x100, sizeof(s32), "s32", "variable"); auto variable = create<PatternDataSigned>("s32", "variable", 0x100, sizeof(s32));
auto array = create<PatternDataStaticArray>(0x100 + sizeof(s32), sizeof(u8[0x10]), "u8", "array"); auto array = create<PatternDataStaticArray>("u8", "array", 0x100 + sizeof(s32), sizeof(u8[0x10]));
array->setEntries(create<PatternDataUnsigned>(0x100 + sizeof(s32), sizeof(u8), "u8", ""), 0x10); array->setEntries(create<PatternDataUnsigned>("u8", "", 0x100 + sizeof(s32), sizeof(u8)), 0x10);
testStruct->setMembers({ variable, array }); testStruct->setMembers({ variable, array });

View File

@ -7,11 +7,11 @@ namespace hex::test {
class TestPatternUnions : public TestPattern { class TestPatternUnions : public TestPattern {
public: public:
TestPatternUnions() : TestPattern("Unions") { TestPatternUnions() : TestPattern("Unions") {
auto testUnion = create<PatternDataUnion>(0x200, sizeof(u128), "TestUnion", "testUnion"); auto testUnion = create<PatternDataUnion>("TestUnion", "testUnion", 0x200, sizeof(u128));
auto array = create<PatternDataStaticArray>(0x200, sizeof(s32[2]), "s32", "array"); auto array = create<PatternDataStaticArray>("s32", "array", 0x200, sizeof(s32[2]));
array->setEntries(create<PatternDataSigned>(0x200, sizeof(s32), "s32", ""), 2); array->setEntries(create<PatternDataSigned>("s32", "", 0x200, sizeof(s32)), 2);
auto variable = create<PatternDataUnsigned>(0x200, sizeof(u128), "u128", "variable"); auto variable = create<PatternDataUnsigned>("u128", "variable", 0x200, sizeof(u128));
testUnion->setMembers({ array, variable }); testUnion->setMembers({ array, variable });

View File

@ -9,9 +9,8 @@ namespace hex::test {
class TestProvider : public prv::Provider { class TestProvider : public prv::Provider {
public: public:
TestProvider() : Provider() { TestProvider() : Provider(), m_testFile(File("test_data", File::Mode::Read)) {
this->m_testFile = File("test_data", File::Mode::Read); if (!this->m_testFile.isValid() || this->m_testFile.getSize() == 0) {
if (!this->m_testFile.isValid()) {
hex::log::fatal("Failed to open test data!"); hex::log::fatal("Failed to open test data!");
throw std::runtime_error(""); throw std::runtime_error("");
} }

View File

@ -8,6 +8,7 @@
#include "test_patterns/test_pattern_padding.hpp" #include "test_patterns/test_pattern_padding.hpp"
#include "test_patterns/test_pattern_succeeding_assert.hpp" #include "test_patterns/test_pattern_succeeding_assert.hpp"
#include "test_patterns/test_pattern_failing_assert.hpp" #include "test_patterns/test_pattern_failing_assert.hpp"
#include "test_patterns/test_pattern_bitfields.hpp"
std::array Tests = { std::array Tests = {
TEST(Placement), TEST(Placement),
@ -17,5 +18,6 @@ std::array Tests = {
TEST(Literals), TEST(Literals),
TEST(Padding), TEST(Padding),
TEST(SucceedingAssert), TEST(SucceedingAssert),
TEST(FailingAssert) TEST(FailingAssert),
TEST(Bitfields)
}; };