Skip to content

Commit

Permalink
Unittest: Switch to Boost.UT
Browse files Browse the repository at this point in the history
  • Loading branch information
cursey committed Feb 1, 2024
1 parent 6e8ed61 commit 66c369b
Show file tree
Hide file tree
Showing 8 changed files with 801 additions and 731 deletions.
24 changes: 17 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ project(safetyhook)
include(FetchContent)

if(SAFETYHOOK_BUILD_TESTS) # build-tests
message(STATUS "Fetching Catch2 (v3.3.2)...")
FetchContent_Declare(Catch2
message(STATUS "Fetching ut (v2.0.1)...")
FetchContent_Declare(ut
GIT_REPOSITORY
"https://github.com/catchorg/Catch2.git"
"https://github.com/boost-ext/ut.git"
GIT_TAG
v3.3.2
v2.0.1
GIT_SHALLOW
ON
)
FetchContent_MakeAvailable(Catch2)
FetchContent_MakeAvailable(ut)

endif()
if(SAFETYHOOK_BUILD_TESTS) # build-tests
Expand Down Expand Up @@ -330,6 +330,7 @@ if(SAFETYHOOK_BUILD_TESTS) # build-tests
"unittest/allocator.cpp"
"unittest/inline_hook.cpp"
"unittest/inline_hook.x86_64.cpp"
"unittest/main.cpp"
"unittest/mid_hook.cpp"
"unittest/vmt_hook.cpp"
cmake.toml
Expand All @@ -348,8 +349,12 @@ if(SAFETYHOOK_BUILD_TESTS) # build-tests
safetyhook::safetyhook
)

target_compile_definitions(unittest PRIVATE
BOOST_UT_DISABLE_MODULE
)

target_link_libraries(unittest PRIVATE
Catch2::Catch2WithMain
Boost::ut
safetyhook::safetyhook
xbyak::xbyak
)
Expand All @@ -367,6 +372,7 @@ if(SAFETYHOOK_BUILD_TESTS) # build-tests
"unittest/allocator.cpp"
"unittest/inline_hook.cpp"
"unittest/inline_hook.x86_64.cpp"
"unittest/main.cpp"
"unittest/mid_hook.cpp"
"unittest/vmt_hook.cpp"
"amalgamated-dist/safetyhook.cpp"
Expand All @@ -386,13 +392,17 @@ if(SAFETYHOOK_BUILD_TESTS) # build-tests
safetyhook::safetyhook
)

target_compile_definitions(unittest-amalgamated PRIVATE
BOOST_UT_DISABLE_MODULE
)

target_include_directories(unittest-amalgamated PRIVATE
"amalgamated-dist/"
)

target_link_libraries(unittest-amalgamated PRIVATE
Zydis
Catch2::Catch2WithMain
Boost::ut
xbyak::xbyak
)

Expand Down
12 changes: 7 additions & 5 deletions cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ amalgamate = "SAFETYHOOK_AMALGAMATE"
build-amalgamate-tests = "SAFETYHOOK_BUILD_TESTS AND SAFETYHOOK_AMALGAMATE"
fetch-zydis = "SAFETYHOOK_FETCH_ZYDIS"

[fetch-content.Catch2]
[fetch-content.ut]
condition = "build-tests"
git = "https://github.com/catchorg/Catch2.git"
tag = "v3.3.2"
git = "https://github.com/boost-ext/ut.git"
tag = "v2.0.1"
shallow = true

[fetch-content.xbyak]
Expand Down Expand Up @@ -130,14 +130,16 @@ sources = ["tests/test4.cpp"]
[target.unittest]
type = "test"
sources = ["unittest/*.cpp"]
link-libraries = ["Catch2::Catch2WithMain", "safetyhook::safetyhook", "xbyak::xbyak"]
link-libraries = ["Boost::ut", "safetyhook::safetyhook", "xbyak::xbyak"]
compile-definitions = ["BOOST_UT_DISABLE_MODULE"]

[target.unittest-amalgamated]
condition = "build-amalgamate-tests"
type = "test"
sources = ["unittest/*.cpp", "amalgamated-dist/safetyhook.cpp"]
include-directories = ["amalgamated-dist/"]
link-libraries = ["Zydis", "Catch2::Catch2WithMain", "xbyak::xbyak"]
link-libraries = ["Zydis", "Boost::ut", "xbyak::xbyak"]
compile-definitions = ["BOOST_UT_DISABLE_MODULE"]
cmake-after = """
add_dependencies(unittest-amalgamated Amalgamate)
"""
38 changes: 21 additions & 17 deletions unittest/allocator.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
#include <catch2/catch_test_macros.hpp>
#include <boost/ut.hpp>
#include <safetyhook.hpp>

TEST_CASE("Allocator reuses freed memory", "[allocator]") {
const auto allocator = safetyhook::Allocator::create();
auto first_allocation = allocator->allocate(128);
using namespace boost::ut;

REQUIRE(first_allocation);
static suite<"allocator"> allocator_tests = [] {
"Allocator reuses freed memory"_test = [] {
const auto allocator = safetyhook::Allocator::create();
auto first_allocation = allocator->allocate(128);

const auto first_allocation_address = first_allocation->address();
const auto second_allocation = allocator->allocate(256);
expect(first_allocation.has_value());

REQUIRE(second_allocation);
REQUIRE(second_allocation->address() != first_allocation_address);
const auto first_allocation_address = first_allocation->address();
const auto second_allocation = allocator->allocate(256);

first_allocation->free();
expect(second_allocation.has_value());
expect(neq(second_allocation->address(), first_allocation_address));

const auto third_allocation = allocator->allocate(64);
first_allocation->free();

REQUIRE(third_allocation);
REQUIRE(third_allocation->address() == first_allocation_address);
const auto third_allocation = allocator->allocate(64);

const auto fourth_allocation = allocator->allocate(64);
expect(third_allocation.has_value());
expect(eq(third_allocation->address(), first_allocation_address));

REQUIRE(fourth_allocation);
REQUIRE(fourth_allocation->address() == third_allocation->address() + 64);
}
const auto fourth_allocation = allocator->allocate(64);

expect(fourth_allocation.has_value());
expect(eq(fourth_allocation->address(), third_allocation->address() + 64));
};
};
Loading

0 comments on commit 66c369b

Please sign in to comment.