tests: Refactor to add support for other types of tests

This commit is contained in:
WerWolv
2021-10-12 21:32:33 +02:00
parent b12cd66679
commit 9b316795fc
33 changed files with 133 additions and 41 deletions

View File

@@ -1,38 +1,4 @@
cmake_minimum_required(VERSION 3.16)
project(unit_tests)
# Add new tests here #
set(AVAILABLE_TESTS
Placement
Structs
Unions
Enums
Literals
Padding
SucceedingAssert
FailingAssert
Bitfields
Math
RValues
Namespaces
ExtraSemicolon
Pointers
)
add_executable(unit_tests source/main.cpp source/tests.cpp)
target_include_directories(unit_tests PRIVATE include)
target_link_libraries(unit_tests libimhex)
set_target_properties(unit_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
add_custom_command(TARGET unit_tests
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/test_data" ${CMAKE_BINARY_DIR})
foreach (test IN LISTS AVAILABLE_TESTS)
add_test(NAME "${test}" COMMAND unit_tests "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endforeach ()
add_subdirectory(pattern_language)
add_subdirectory(algorithms)

View File

@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.16)
project(algorithms_tests)
find_package(Catch2 REQUIRED)
add_executable(algorithms_tests source/hash.cpp)
target_include_directories(algorithms_tests PRIVATE include)
target_link_libraries(algorithms_tests libimhex Catch2::Catch2)
set_target_properties(algorithms_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
include(CTest)
include(Catch)
catch_discover_tests(algorithms_tests TEST_PREFIX "Algorithms/")

View File

@@ -0,0 +1,60 @@
#include <hex/providers/provider.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/logger.hpp>
#include <stdexcept>
namespace hex::test {
using namespace hex::prv;
class TestProvider : public prv::Provider {
public:
TestProvider(std::vector<u8> data) : Provider(){
this->setData(data);
}
~TestProvider() override = default;
[[nodiscard]] bool isAvailable() const override { return true; }
[[nodiscard]] bool isReadable() const override { return true; }
[[nodiscard]] bool isWritable() const override { return false; }
[[nodiscard]] bool isResizable() const override { return false; }
[[nodiscard]] bool isSavable() const override { return false; }
void setData(const std::vector<u8> &data) {
if (data.empty()) {
hex::log::fatal("No data provided");
throw std::runtime_error("");
}
this->m_data = data;
}
[[nodiscard]] std::string getName() const override {
return "";
}
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataInformation() const override {
return { };
}
void readRaw(u64 offset, void *buffer, size_t size) override {
if (offset + size >= this->m_data.size()) return;
std::memcpy(buffer, &this->m_data[offset], size);
}
void writeRaw(u64 offset, const void *buffer, size_t size) override {
if (offset + size >= this->m_data.size()) return;
std::memcpy(&this->m_data[offset], buffer, size);
}
size_t getActualSize() const override {
return this->m_data.size();
}
private:
std::vector<u8> m_data;
};
}

View File

@@ -0,0 +1,5 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <hex/helpers/crypto.hpp>
#include "test_provider.hpp"

View File

@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.16)
project(pattern_language_tests)
# Add new tests here #
set(AVAILABLE_TESTS
Placement
Structs
Unions
Enums
Literals
Padding
SucceedingAssert
FailingAssert
Bitfields
Math
RValues
Namespaces
ExtraSemicolon
Pointers
)
add_executable(pattern_language_tests source/main.cpp source/tests.cpp)
target_include_directories(pattern_language_tests PRIVATE include)
target_link_libraries(pattern_language_tests libimhex)
set_target_properties(pattern_language_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
add_custom_command(TARGET pattern_language_tests
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/test_data" ${CMAKE_BINARY_DIR})
foreach (test IN LISTS AVAILABLE_TESTS)
add_test(NAME "PatternLanguage/${test}" COMMAND pattern_language_tests "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endforeach ()

View File

Before

Width:  |  Height:  |  Size: 164 KiB

After

Width:  |  Height:  |  Size: 164 KiB