diff --git a/dist/Brewfile b/dist/Brewfile index a2bc5b94e..e70b5f7c1 100644 --- a/dist/Brewfile +++ b/dist/Brewfile @@ -8,6 +8,7 @@ brew "python3" brew "freetype2" brew "libmagic" brew "pkg-config" +brew "catch2" # TODO: Remove this when XCode version of clang will support the same level as LLVM 10 brew "llvm" diff --git a/dist/Dockerfile b/dist/Dockerfile index 6e6a9d6e2..659fccce9 100644 --- a/dist/Dockerfile +++ b/dist/Dockerfile @@ -16,7 +16,8 @@ RUN pacman -S --needed --noconfirm \ capstone \ python3 \ freetype2 \ - gtk3 + gtk3 \ + catch2 # Clone ImHex RUN git clone https://github.com/WerWolv/ImHex --recurse-submodules /root/ImHex diff --git a/dist/ImHex-9999.ebuild b/dist/ImHex-9999.ebuild index 64e7edfc7..1723f018b 100644 --- a/dist/ImHex-9999.ebuild +++ b/dist/ImHex-9999.ebuild @@ -29,5 +29,6 @@ RDEPEND="${DEPEND} dev-libs/capstone dev-cpp/nlohmann_json x11-libs/gtk+ + dev-cpp/catch " BDEPEND="${DEPEND}" diff --git a/dist/get_deps_archlinux.sh b/dist/get_deps_archlinux.sh index a0c172eac..263152708 100755 --- a/dist/get_deps_archlinux.sh +++ b/dist/get_deps_archlinux.sh @@ -9,4 +9,5 @@ pacman -S --needed \ capstone \ python3 \ freetype2 \ - gtk3 + gtk3 \ + catch2 diff --git a/dist/get_deps_debian.sh b/dist/get_deps_debian.sh index 6c1c504fd..ec1651eb2 100755 --- a/dist/get_deps_debian.sh +++ b/dist/get_deps_debian.sh @@ -26,6 +26,7 @@ apt install -y \ python3-dev \ libfreetype-dev \ libgtk-3-dev \ + catch echo "Please consider this before running cmake (useful on e.g. Ubuntu 20.04):" echo "export CXX=g++-10" diff --git a/dist/get_deps_fedora.sh b/dist/get_deps_fedora.sh index 4616c710b..6efbe5fa0 100755 --- a/dist/get_deps_fedora.sh +++ b/dist/get_deps_fedora.sh @@ -10,4 +10,5 @@ dnf install \ mbedtls-devel \ python-devel \ freetype-devel \ - gtk3 + gtk3 \ + catch-devel diff --git a/dist/get_deps_msys2.sh b/dist/get_deps_msys2.sh index 70b286f75..1dce18558 100755 --- a/dist/get_deps_msys2.sh +++ b/dist/get_deps_msys2.sh @@ -11,4 +11,5 @@ pacman -S --needed --noconfirm \ mingw-w64-x86_64-mbedtls \ mingw-w64-x86_64-python \ mingw-w64-x86_64-freetype \ - mingw-w64-x86_64-dlfcn + mingw-w64-x86_64-dlfcn \ + mingw-w64-x86_64-catch diff --git a/dist/msys2/PKGBUILD b/dist/msys2/PKGBUILD index ce7688aca..4c280a3ea 100644 --- a/dist/msys2/PKGBUILD +++ b/dist/msys2/PKGBUILD @@ -18,7 +18,8 @@ makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-mbedtls" "${MINGW_PACKAGE_PREFIX}-polly" "${MINGW_PACKAGE_PREFIX}-python" - "${MINGW_PACKAGE_PREFIX}-freetype") + "${MINGW_PACKAGE_PREFIX}-freetype" + "${MINGW_PACKAGE_PREFIX}-catch") source=() sha256sums=() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 378edf2f5..ffd877521 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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 () \ No newline at end of file +add_subdirectory(pattern_language) +add_subdirectory(algorithms) \ No newline at end of file diff --git a/tests/algorithms/CMakeLists.txt b/tests/algorithms/CMakeLists.txt new file mode 100644 index 000000000..01cdf8eb4 --- /dev/null +++ b/tests/algorithms/CMakeLists.txt @@ -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/") \ No newline at end of file diff --git a/tests/algorithms/include/test_provider.hpp b/tests/algorithms/include/test_provider.hpp new file mode 100644 index 000000000..960816bcf --- /dev/null +++ b/tests/algorithms/include/test_provider.hpp @@ -0,0 +1,60 @@ +#include + +#include +#include +#include + +namespace hex::test { + using namespace hex::prv; + + class TestProvider : public prv::Provider { + public: + TestProvider(std::vector 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 &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> 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 m_data; + }; + +} \ No newline at end of file diff --git a/tests/algorithms/source/hash.cpp b/tests/algorithms/source/hash.cpp new file mode 100644 index 000000000..83d886732 --- /dev/null +++ b/tests/algorithms/source/hash.cpp @@ -0,0 +1,5 @@ +#define CATCH_CONFIG_MAIN +#include + +#include +#include "test_provider.hpp" diff --git a/tests/pattern_language/CMakeLists.txt b/tests/pattern_language/CMakeLists.txt new file mode 100644 index 000000000..92591aca5 --- /dev/null +++ b/tests/pattern_language/CMakeLists.txt @@ -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 () \ No newline at end of file diff --git a/tests/include/test_patterns/test_pattern.hpp b/tests/pattern_language/include/test_patterns/test_pattern.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern.hpp rename to tests/pattern_language/include/test_patterns/test_pattern.hpp diff --git a/tests/include/test_patterns/test_pattern_bitfields.hpp b/tests/pattern_language/include/test_patterns/test_pattern_bitfields.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_bitfields.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_bitfields.hpp diff --git a/tests/include/test_patterns/test_pattern_enums.hpp b/tests/pattern_language/include/test_patterns/test_pattern_enums.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_enums.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_enums.hpp diff --git a/tests/include/test_patterns/test_pattern_example.hpp b/tests/pattern_language/include/test_patterns/test_pattern_example.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_example.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_example.hpp diff --git a/tests/include/test_patterns/test_pattern_extra_semicolon.hpp b/tests/pattern_language/include/test_patterns/test_pattern_extra_semicolon.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_extra_semicolon.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_extra_semicolon.hpp diff --git a/tests/include/test_patterns/test_pattern_failing_assert.hpp b/tests/pattern_language/include/test_patterns/test_pattern_failing_assert.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_failing_assert.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_failing_assert.hpp diff --git a/tests/include/test_patterns/test_pattern_literals.hpp b/tests/pattern_language/include/test_patterns/test_pattern_literals.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_literals.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_literals.hpp diff --git a/tests/include/test_patterns/test_pattern_math.hpp b/tests/pattern_language/include/test_patterns/test_pattern_math.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_math.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_math.hpp diff --git a/tests/include/test_patterns/test_pattern_namespaces.hpp b/tests/pattern_language/include/test_patterns/test_pattern_namespaces.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_namespaces.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_namespaces.hpp diff --git a/tests/include/test_patterns/test_pattern_padding.hpp b/tests/pattern_language/include/test_patterns/test_pattern_padding.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_padding.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_padding.hpp diff --git a/tests/include/test_patterns/test_pattern_placement.hpp b/tests/pattern_language/include/test_patterns/test_pattern_placement.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_placement.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_placement.hpp diff --git a/tests/include/test_patterns/test_pattern_pointers.hpp b/tests/pattern_language/include/test_patterns/test_pattern_pointers.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_pointers.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_pointers.hpp diff --git a/tests/include/test_patterns/test_pattern_rvalues.hpp b/tests/pattern_language/include/test_patterns/test_pattern_rvalues.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_rvalues.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_rvalues.hpp diff --git a/tests/include/test_patterns/test_pattern_structs.hpp b/tests/pattern_language/include/test_patterns/test_pattern_structs.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_structs.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_structs.hpp diff --git a/tests/include/test_patterns/test_pattern_succeeding_assert.hpp b/tests/pattern_language/include/test_patterns/test_pattern_succeeding_assert.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_succeeding_assert.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_succeeding_assert.hpp diff --git a/tests/include/test_patterns/test_pattern_unions.hpp b/tests/pattern_language/include/test_patterns/test_pattern_unions.hpp similarity index 100% rename from tests/include/test_patterns/test_pattern_unions.hpp rename to tests/pattern_language/include/test_patterns/test_pattern_unions.hpp diff --git a/tests/include/test_provider.hpp b/tests/pattern_language/include/test_provider.hpp similarity index 100% rename from tests/include/test_provider.hpp rename to tests/pattern_language/include/test_provider.hpp diff --git a/tests/source/main.cpp b/tests/pattern_language/source/main.cpp similarity index 100% rename from tests/source/main.cpp rename to tests/pattern_language/source/main.cpp diff --git a/tests/source/tests.cpp b/tests/pattern_language/source/tests.cpp similarity index 100% rename from tests/source/tests.cpp rename to tests/pattern_language/source/tests.cpp diff --git a/tests/test_data b/tests/pattern_language/test_data similarity index 100% rename from tests/test_data rename to tests/pattern_language/test_data