1
0
mirror of synced 2024-11-28 01:20:51 +01:00

tests: Added properly working custom unit tests

This commit is contained in:
WerWolv 2021-10-14 21:19:31 +02:00
parent 3e5d6cf88c
commit 1b6035d6c6
13 changed files with 156 additions and 26 deletions

1
dist/Brewfile vendored
View File

@ -8,7 +8,6 @@ brew "python3"
brew "freetype2" brew "freetype2"
brew "libmagic" brew "libmagic"
brew "pkg-config" brew "pkg-config"
brew "catch2"
# TODO: Remove this when XCode version of clang will support the same level as LLVM 10 # TODO: Remove this when XCode version of clang will support the same level as LLVM 10
brew "llvm" brew "llvm"

3
dist/Dockerfile vendored
View File

@ -16,8 +16,7 @@ RUN pacman -S --needed --noconfirm \
capstone \ capstone \
python3 \ python3 \
freetype2 \ freetype2 \
gtk3 \ gtk3
catch2
# Clone ImHex # Clone ImHex
RUN git clone https://github.com/WerWolv/ImHex --recurse-submodules /root/ImHex RUN git clone https://github.com/WerWolv/ImHex --recurse-submodules /root/ImHex

View File

@ -29,6 +29,5 @@ RDEPEND="${DEPEND}
dev-libs/capstone dev-libs/capstone
dev-cpp/nlohmann_json dev-cpp/nlohmann_json
x11-libs/gtk+ x11-libs/gtk+
dev-cpp/catch
" "
BDEPEND="${DEPEND}" BDEPEND="${DEPEND}"

View File

@ -9,5 +9,4 @@ pacman -S --needed \
capstone \ capstone \
python3 \ python3 \
freetype2 \ freetype2 \
gtk3 \ gtk3
catch2

View File

@ -26,7 +26,6 @@ apt install -y \
python3-dev \ python3-dev \
libfreetype-dev \ libfreetype-dev \
libgtk-3-dev \ libgtk-3-dev \
catch
echo "Please consider this before running cmake (useful on e.g. Ubuntu 20.04):" echo "Please consider this before running cmake (useful on e.g. Ubuntu 20.04):"
echo "export CXX=g++-10" echo "export CXX=g++-10"

View File

@ -10,5 +10,4 @@ dnf install \
mbedtls-devel \ mbedtls-devel \
python-devel \ python-devel \
freetype-devel \ freetype-devel \
gtk3 \ gtk3
catch-devel

View File

@ -11,5 +11,4 @@ pacman -S --needed --noconfirm \
mingw-w64-x86_64-mbedtls \ mingw-w64-x86_64-mbedtls \
mingw-w64-x86_64-python \ mingw-w64-x86_64-python \
mingw-w64-x86_64-freetype \ mingw-w64-x86_64-freetype \
mingw-w64-x86_64-dlfcn \ mingw-w64-x86_64-dlfcn
mingw-w64-x86_64-catch

3
dist/msys2/PKGBUILD vendored
View File

@ -18,8 +18,7 @@ makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
"${MINGW_PACKAGE_PREFIX}-mbedtls" "${MINGW_PACKAGE_PREFIX}-mbedtls"
"${MINGW_PACKAGE_PREFIX}-polly" "${MINGW_PACKAGE_PREFIX}-polly"
"${MINGW_PACKAGE_PREFIX}-python" "${MINGW_PACKAGE_PREFIX}-python"
"${MINGW_PACKAGE_PREFIX}-freetype" "${MINGW_PACKAGE_PREFIX}-freetype")
"${MINGW_PACKAGE_PREFIX}-catch")
source=() source=()
sha256sums=() sha256sums=()

View File

@ -1,15 +1,21 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(algorithms_tests) project(algorithms_test)
find_package(Catch2 REQUIRED)
add_executable(algorithms_tests source/hash.cpp) # Add new tests here #
target_include_directories(algorithms_tests PRIVATE include) set(AVAILABLE_TESTS
target_link_libraries(algorithms_tests libimhex Catch2::Catch2) TestSucceeding
TestFailing
)
set_target_properties(algorithms_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
include(CTest) add_executable(algorithms_test source/hash.cpp source/main.cpp include/tests.hpp)
include(Catch) target_include_directories(algorithms_test PRIVATE include)
catch_discover_tests(algorithms_tests TEST_PREFIX "Algorithms/") 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 ()

View 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());
}
}

View File

@ -1,5 +1,11 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <hex/helpers/crypto.hpp> #include <hex/helpers/crypto.hpp>
#include "test_provider.hpp" #include "test_provider.hpp"
#include "tests.hpp"
TEST_SEQUENCE("TestSucceeding") {
TEST_SUCCESS();
};
TEST_SEQUENCE("TestFailing", FAILING) {
TEST_FAIL();
};

View 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;
}

View File

@ -22,7 +22,6 @@ set(AVAILABLE_TESTS
) )
add_executable(pattern_language_tests source/main.cpp source/tests.cpp) add_executable(pattern_language_tests source/main.cpp source/tests.cpp)
target_include_directories(pattern_language_tests PRIVATE include) target_include_directories(pattern_language_tests PRIVATE include)
target_link_libraries(pattern_language_tests libimhex) target_link_libraries(pattern_language_tests libimhex)