From bed5361879ca05834efa4d9d59545e739c79dc64 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 12 Sep 2021 20:28:32 +0200 Subject: [PATCH] tests: Added bitfield test --- tests/CMakeLists.txt | 1 + tests/include/test_patterns/test_pattern.hpp | 4 +- .../test_patterns/test_pattern_bitfields.hpp | 44 +++++++++++++++++++ .../test_patterns/test_pattern_enums.hpp | 2 +- .../test_patterns/test_pattern_padding.hpp | 10 ++--- .../test_patterns/test_pattern_placement.hpp | 6 +-- .../test_patterns/test_pattern_structs.hpp | 8 ++-- .../test_patterns/test_pattern_unions.hpp | 8 ++-- tests/include/test_provider.hpp | 5 +-- tests/source/tests.cpp | 4 +- 10 files changed, 69 insertions(+), 23 deletions(-) create mode 100644 tests/include/test_patterns/test_pattern_bitfields.hpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fc8db44fe..55067d1f4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -13,6 +13,7 @@ set(AVAILABLE_TESTS Padding SucceedingAssert FailingAssert + Bitfields ) diff --git a/tests/include/test_patterns/test_pattern.hpp b/tests/include/test_patterns/test_pattern.hpp index e8b767acc..84e803431 100644 --- a/tests/include/test_patterns/test_pattern.hpp +++ b/tests/include/test_patterns/test_pattern.hpp @@ -28,8 +28,8 @@ namespace hex::test { } template - static T* create(u64 offset, size_t size, const std::string &typeName, const std::string &varName) { - auto pattern = new T(offset, size); + static T* create(const std::string &typeName, const std::string &varName, auto ... args) { + auto pattern = new T(args...); pattern->setTypeName(typeName); pattern->setVariableName(varName); diff --git a/tests/include/test_patterns/test_pattern_bitfields.hpp b/tests/include/test_patterns/test_pattern_bitfields.hpp new file mode 100644 index 000000000..dc47316ec --- /dev/null +++ b/tests/include/test_patterns/test_pattern_bitfields.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include "test_pattern.hpp" + +namespace hex::test { + + class TestPatternBitfields : public TestPattern { + public: + TestPatternBitfields() : TestPattern("Bitfields") { + auto testBitfield = create("TestBitfield", "testBitfield", 0x12, (4 * 4) / 8); + testBitfield->setEndian(std::endian::big); + testBitfield->setFields({ + create("", "a", 0x12, 0, 4), + create("", "b", 0x12, 4, 4), + create("", "c", 0x12, 8, 4), + create("", "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"); + )"; + } + + }; + +} \ No newline at end of file diff --git a/tests/include/test_patterns/test_pattern_enums.hpp b/tests/include/test_patterns/test_pattern_enums.hpp index 2a8ff4b8e..fdc79b75b 100644 --- a/tests/include/test_patterns/test_pattern_enums.hpp +++ b/tests/include/test_patterns/test_pattern_enums.hpp @@ -7,7 +7,7 @@ namespace hex::test { class TestPatternEnums : public TestPattern { public: TestPatternEnums() : TestPattern("Enums"){ - auto testEnum = create(0x120, sizeof(u32), "TestEnum", "testEnum"); + auto testEnum = create("TestEnum", "testEnum", 0x120, sizeof(u32)); testEnum->setEnumValues({ { s32(0x0000), "A" }, { s32(0x1234), "B" }, diff --git a/tests/include/test_patterns/test_pattern_padding.hpp b/tests/include/test_patterns/test_pattern_padding.hpp index d64003c18..6f3b81681 100644 --- a/tests/include/test_patterns/test_pattern_padding.hpp +++ b/tests/include/test_patterns/test_pattern_padding.hpp @@ -7,12 +7,12 @@ namespace hex::test { class TestPatternPadding : public TestPattern { public: TestPatternPadding() : TestPattern("Padding") { - auto testStruct = create(0x100, sizeof(s32) + 20 + sizeof(u8[0x10]), "TestStruct", "testStruct"); + auto testStruct = create("TestStruct", "testStruct", 0x100, sizeof(s32) + 20 + sizeof(u8[0x10])); - auto variable = create(0x100, sizeof(s32), "s32", "variable"); - auto padding = create(0x100 + sizeof(s32), 20, "", ""); - auto array = create(0x100 + sizeof(s32) + 20, sizeof(u8[0x10]), "u8", "array"); - array->setEntries(create(0x100 + sizeof(s32) + 20, sizeof(u8), "u8", ""), 0x10); + auto variable = create("s32", "variable", 0x100, sizeof(s32)); + auto padding = create("", "", 0x100 + sizeof(s32), 20); + auto array = create("u8", "array", 0x100 + sizeof(s32) + 20, sizeof(u8[0x10])); + array->setEntries(create("u8", "", 0x100 + sizeof(s32) + 20, sizeof(u8)), 0x10); testStruct->setMembers({ variable, padding, array }); diff --git a/tests/include/test_patterns/test_pattern_placement.hpp b/tests/include/test_patterns/test_pattern_placement.hpp index d9bdddf4d..d8a5576b9 100644 --- a/tests/include/test_patterns/test_pattern_placement.hpp +++ b/tests/include/test_patterns/test_pattern_placement.hpp @@ -9,13 +9,13 @@ namespace hex::test { TestPatternPlacement() : TestPattern("Placement") { // placementVar { - addPattern(create(0x00, sizeof(u32), "u32", "placementVar")); + addPattern(create("u32", "placementVar", 0x00, sizeof(u32))); } // placementArray { - auto placementArray = create(0x10, sizeof(u8) * 10, "u8", "placementArray"); - placementArray->setEntries(create(0x10, sizeof(u8), "u8", ""), 10); + auto placementArray = create("u8", "placementArray", 0x10, sizeof(u8) * 10); + placementArray->setEntries(create("u8", "", 0x10, sizeof(u8)), 10); addPattern(placementArray); } diff --git a/tests/include/test_patterns/test_pattern_structs.hpp b/tests/include/test_patterns/test_pattern_structs.hpp index 221a7353e..963e3dfe2 100644 --- a/tests/include/test_patterns/test_pattern_structs.hpp +++ b/tests/include/test_patterns/test_pattern_structs.hpp @@ -7,11 +7,11 @@ namespace hex::test { class TestPatternStructs : public TestPattern { public: TestPatternStructs() : TestPattern("Structs") { - auto testStruct = create(0x100, sizeof(s32) + sizeof(u8[0x10]), "TestStruct", "testStruct"); + auto testStruct = create("TestStruct", "testStruct", 0x100, sizeof(s32) + sizeof(u8[0x10])); - auto variable = create(0x100, sizeof(s32), "s32", "variable"); - auto array = create(0x100 + sizeof(s32), sizeof(u8[0x10]), "u8", "array"); - array->setEntries(create(0x100 + sizeof(s32), sizeof(u8), "u8", ""), 0x10); + auto variable = create("s32", "variable", 0x100, sizeof(s32)); + auto array = create("u8", "array", 0x100 + sizeof(s32), sizeof(u8[0x10])); + array->setEntries(create("u8", "", 0x100 + sizeof(s32), sizeof(u8)), 0x10); testStruct->setMembers({ variable, array }); diff --git a/tests/include/test_patterns/test_pattern_unions.hpp b/tests/include/test_patterns/test_pattern_unions.hpp index 342eb6c99..907fea477 100644 --- a/tests/include/test_patterns/test_pattern_unions.hpp +++ b/tests/include/test_patterns/test_pattern_unions.hpp @@ -7,11 +7,11 @@ namespace hex::test { class TestPatternUnions : public TestPattern { public: TestPatternUnions() : TestPattern("Unions") { - auto testUnion = create(0x200, sizeof(u128), "TestUnion", "testUnion"); + auto testUnion = create("TestUnion", "testUnion", 0x200, sizeof(u128)); - auto array = create(0x200, sizeof(s32[2]), "s32", "array"); - array->setEntries(create(0x200, sizeof(s32), "s32", ""), 2); - auto variable = create(0x200, sizeof(u128), "u128", "variable"); + auto array = create("s32", "array", 0x200, sizeof(s32[2])); + array->setEntries(create("s32", "", 0x200, sizeof(s32)), 2); + auto variable = create("u128", "variable", 0x200, sizeof(u128)); testUnion->setMembers({ array, variable }); diff --git a/tests/include/test_provider.hpp b/tests/include/test_provider.hpp index a88c22710..e2e7e5802 100644 --- a/tests/include/test_provider.hpp +++ b/tests/include/test_provider.hpp @@ -9,9 +9,8 @@ namespace hex::test { class TestProvider : public prv::Provider { public: - TestProvider() : Provider() { - this->m_testFile = File("test_data", File::Mode::Read); - if (!this->m_testFile.isValid()) { + TestProvider() : Provider(), m_testFile(File("test_data", File::Mode::Read)) { + if (!this->m_testFile.isValid() || this->m_testFile.getSize() == 0) { hex::log::fatal("Failed to open test data!"); throw std::runtime_error(""); } diff --git a/tests/source/tests.cpp b/tests/source/tests.cpp index cf2cf5f64..28427f962 100644 --- a/tests/source/tests.cpp +++ b/tests/source/tests.cpp @@ -8,6 +8,7 @@ #include "test_patterns/test_pattern_padding.hpp" #include "test_patterns/test_pattern_succeeding_assert.hpp" #include "test_patterns/test_pattern_failing_assert.hpp" +#include "test_patterns/test_pattern_bitfields.hpp" std::array Tests = { TEST(Placement), @@ -17,5 +18,6 @@ std::array Tests = { TEST(Literals), TEST(Padding), TEST(SucceedingAssert), - TEST(FailingAssert) + TEST(FailingAssert), + TEST(Bitfields) }; \ No newline at end of file