mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-30 21:05:56 -05:00
tests: Added properly working custom unit tests
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(algorithms_tests)
|
||||
project(algorithms_test)
|
||||
|
||||
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)
|
||||
# Add new tests here #
|
||||
set(AVAILABLE_TESTS
|
||||
TestSucceeding
|
||||
TestFailing
|
||||
)
|
||||
|
||||
set_target_properties(algorithms_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
|
||||
|
||||
include(CTest)
|
||||
include(Catch)
|
||||
catch_discover_tests(algorithms_tests TEST_PREFIX "Algorithms/")
|
||||
add_executable(algorithms_test source/hash.cpp source/main.cpp include/tests.hpp)
|
||||
target_include_directories(algorithms_test PRIVATE include)
|
||||
target_link_libraries(algorithms_test libimhex)
|
||||
|
||||
set_target_properties(algorithms_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
|
||||
|
||||
foreach (test IN LISTS AVAILABLE_TESTS)
|
||||
add_test(NAME "Algorithms/${test}" COMMAND algorithms_test "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
|
||||
endforeach ()
|
||||
75
tests/algorithms/include/tests.hpp
Normal file
75
tests/algorithms/include/tests.hpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include <hex.hpp>
|
||||
#include <utility>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
|
||||
#define TEST_SEQUENCE(...) static auto ANONYMOUS_VARIABLE(TEST_SEQUENCE) = ::hex::test::TestSequenceExecutor(__VA_ARGS__) + []() -> int
|
||||
#define TEST_FAIL() return EXIT_FAILURE;
|
||||
#define TEST_SUCCESS() return EXIT_SUCCESS;
|
||||
#define FAILING true
|
||||
|
||||
namespace hex::test {
|
||||
|
||||
struct Test {
|
||||
std::function<int()> function;
|
||||
bool shouldFail;
|
||||
};
|
||||
|
||||
class Tests {
|
||||
public:
|
||||
static auto addTest(const std::string &name, const std::function<int()> &func, bool shouldFail) noexcept {
|
||||
s_tests.insert({ name, { func, shouldFail } });
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static auto& get()noexcept {
|
||||
return s_tests;
|
||||
}
|
||||
private:
|
||||
static inline std::map<std::string, Test> s_tests;
|
||||
};
|
||||
|
||||
template<class F>
|
||||
class TestSequence {
|
||||
public:
|
||||
TestSequence(const std::string& name, F func, bool shouldFail) noexcept {
|
||||
Tests::addTest(name, func, shouldFail);
|
||||
}
|
||||
|
||||
TestSequence& operator=(TestSequence &&) = delete;
|
||||
};
|
||||
|
||||
struct TestSequenceExecutor {
|
||||
explicit TestSequenceExecutor(std::string name, bool shouldFail = false) noexcept : m_name(std::move(name)), m_shouldFail(shouldFail) {
|
||||
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
const auto& getName() const noexcept {
|
||||
return this->m_name;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool shouldFail() const noexcept {
|
||||
return this->m_shouldFail;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
bool m_shouldFail;
|
||||
};
|
||||
|
||||
|
||||
template <typename F>
|
||||
TestSequence<F> operator+(TestSequenceExecutor executor, F&& f) noexcept {
|
||||
return TestSequence<F>(executor.getName(), std::forward<F>(f), executor.shouldFail());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#include <hex/helpers/crypto.hpp>
|
||||
#include "test_provider.hpp"
|
||||
#include "tests.hpp"
|
||||
|
||||
TEST_SEQUENCE("TestSucceeding") {
|
||||
TEST_SUCCESS();
|
||||
};
|
||||
|
||||
TEST_SEQUENCE("TestFailing", FAILING) {
|
||||
TEST_FAIL();
|
||||
};
|
||||
52
tests/algorithms/source/main.cpp
Normal file
52
tests/algorithms/source/main.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <hex.hpp>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/logger.hpp>
|
||||
#include "tests.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
int test(int argc, char **argv) {
|
||||
// Check if a test to run has been provided
|
||||
if (argc != 2) {
|
||||
hex::log::fatal("Invalid number of arguments specified! {}", argc);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Check if that test exists
|
||||
std::string testName = argv[1];
|
||||
if (!hex::test::Tests::get().contains(testName)) {
|
||||
hex::log::fatal("No test with name {} found!", testName);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
auto test = hex::test::Tests::get()[testName];
|
||||
|
||||
auto result = test.function();
|
||||
|
||||
if (test.shouldFail) {
|
||||
switch (result) {
|
||||
case EXIT_SUCCESS: return EXIT_FAILURE;
|
||||
case EXIT_FAILURE: return EXIT_SUCCESS;
|
||||
default: return result;
|
||||
}
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int result = EXIT_SUCCESS;
|
||||
|
||||
for (u32 i = 0; i < 16; i++) {
|
||||
result = test(argc, argv);
|
||||
if (result != EXIT_SUCCESS)
|
||||
break;
|
||||
}
|
||||
|
||||
if (result == EXIT_SUCCESS)
|
||||
hex::log::info("Success!");
|
||||
else
|
||||
hex::log::info("Failed!");
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user