From c4da444bc9f8596a420b13a2c19cc6401b004f30 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Fri, 26 Jul 2024 08:49:36 +0100 Subject: [PATCH 1/3] Add a test for Crawl order --- test/lighting_test.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/test/lighting_test.cpp b/test/lighting_test.cpp index e3995be6221..c6c7ca7dcfc 100644 --- a/test/lighting_test.cpp +++ b/test/lighting_test.cpp @@ -1,11 +1,14 @@ #include +#include #include #include "control.h" #include "lighting.h" -using namespace devilution; +namespace devilution { +namespace { +using ::testing::ElementsAre; TEST(Lighting, CrawlTables) { @@ -33,3 +36,26 @@ TEST(Lighting, CrawlTables) } } } + +TEST(Lighting, CrawlTablesVisitationOrder) +{ + std::vector order; + Crawl(0, 2, [&](Displacement displacement) { + order.push_back(displacement); + return false; + }); + EXPECT_THAT( + order, + ElementsAre( + Displacement(0, 0), + Displacement(0, 1), Displacement(0, -1), + Displacement(-1, 0), Displacement(1, 0), + Displacement(0, 2), Displacement(0, -2), + Displacement(-1, 2), Displacement(1, 2), Displacement(-1, -2), Displacement(1, -2), + Displacement(-1, 1), Displacement(1, 1), Displacement(-1, -1), Displacement(1, -1), + Displacement(-2, 0), Displacement(2, 0), Displacement(-2, 1), + Displacement(2, 1), Displacement(-2, -1), Displacement(2, -1))); +} + +} // namespace +} // namespace devilution From b3863c73afac0d765c89507e4e0534b98440fad7 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Fri, 26 Jul 2024 09:19:05 +0100 Subject: [PATCH 2/3] Add google-benchmark --- .devcontainer/Dockerfile | 2 +- .editorconfig | 3 +++ .github/workflows/Linux_x86_64_test.yml | 6 ++++-- .github/workflows/src_dist_release.yml | 4 ++-- 3rdParty/benchmark/CMakeLists.txt | 16 ++++++++++++++++ Brewfile | 1 + CMake/Dependencies.cmake | 7 +++++++ docs/building.md | 4 ++-- tools/make_src_dist.py | 4 ++-- vcpkg.json | 2 +- 10 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 3rdParty/benchmark/CMakeLists.txt diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 7478ad9e7f2..22616bc4533 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -7,7 +7,7 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ && apt-get -y install tar curl zip unzip bash-completion build-essential ripgrep htop \ ninja-build ccache g++ mold gdb clang-format clang-tidy \ rpm pkg-config cmake git smpq gettext libsdl2-dev libsdl2-image-dev libsodium-dev \ - libpng-dev libbz2-dev libfmt-dev libgtest-dev libgmock-dev libsimpleini-dev zsh \ + libpng-dev libbz2-dev libfmt-dev libgtest-dev libgmock-dev libbenchmark-dev libsimpleini-dev zsh \ qtbase5-dev qt6-base-dev ristretto \ && apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* diff --git a/.editorconfig b/.editorconfig index 6bbc235ffb5..6baba88588a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -80,3 +80,6 @@ end_of_line = lf [devilutionx.spec] end_of_line = lf + +[Dockerfile] +end_of_line = lf diff --git a/.github/workflows/Linux_x86_64_test.yml b/.github/workflows/Linux_x86_64_test.yml index 7218c010558..3165868806b 100644 --- a/.github/workflows/Linux_x86_64_test.yml +++ b/.github/workflows/Linux_x86_64_test.yml @@ -30,7 +30,7 @@ jobs: - name: Install dependencies run: | sudo apt-get update -y - sudo apt-get install -y cmake curl g++ git lcov libgtest-dev libgmock-dev libfmt-dev libsdl2-dev libsodium-dev libpng-dev libbz2-dev wget + sudo apt-get install -y cmake curl g++ git lcov libgtest-dev libgmock-dev libbenchmark-dev libfmt-dev libsdl2-dev libsodium-dev libpng-dev libbz2-dev wget - name: Cache CMake build folder uses: actions/cache@v4 with: @@ -38,9 +38,11 @@ jobs: key: ${{ github.workflow }}-v1-${{ github.sha }} restore-keys: ${{ github.workflow }}-v1- + # We specify `-DDEVILUTIONX_SYSTEM_BENCHMARK=OFF` to work around the following error: + # lto1: fatal error: bytecode stream in file ‘/usr/lib/x86_64-linux-gnu/libbenchmark_main.a’ generated with LTO version 11.2 instead of the expected 11.3 - name: Build tests run: | - cmake -S. -Bbuild -DENABLE_CODECOVERAGE=ON + cmake -S. -Bbuild -DENABLE_CODECOVERAGE=ON -DDEVILUTIONX_SYSTEM_BENCHMARK=OFF wget -nc https://github.com/diasurgical/devilutionx-assets/releases/download/v2/spawn.mpq -P build cmake --build build -j $(nproc) diff --git a/.github/workflows/src_dist_release.yml b/.github/workflows/src_dist_release.yml index 5ecad2343ce..274ce10ad85 100644 --- a/.github/workflows/src_dist_release.yml +++ b/.github/workflows/src_dist_release.yml @@ -24,7 +24,7 @@ jobs: - name: Create Build Environment run: > sudo apt-get update && - sudo apt-get install -y cmake curl libsdl2-dev libsdl2-image-dev libfmt-dev libsodium-dev libbz2-dev libgtest-dev libgmock-dev git smpq gettext python-is-python3 + sudo apt-get install -y cmake curl libsdl2-dev libsdl2-image-dev libfmt-dev libsodium-dev libbz2-dev libgtest-dev libgmock-dev libbenchmark-dev git smpq gettext python-is-python3 - name: Build working-directory: ${{github.workspace}} @@ -55,7 +55,7 @@ jobs: - name: Create Build Environment run: > sudo apt-get update && - sudo apt-get install -y cmake curl libsdl2-dev libsdl2-image-dev libfmt-dev libsodium-dev libbz2-dev libgtest-dev libgmock-dev git smpq gettext python-is-python3 + sudo apt-get install -y cmake curl libsdl2-dev libsdl2-image-dev libfmt-dev libsodium-dev libbz2-dev libgtest-dev libgmock-dev libbenchmark-dev git smpq gettext python-is-python3 - name: Build working-directory: ${{github.workspace}} diff --git a/3rdParty/benchmark/CMakeLists.txt b/3rdParty/benchmark/CMakeLists.txt new file mode 100644 index 00000000000..558d571aece --- /dev/null +++ b/3rdParty/benchmark/CMakeLists.txt @@ -0,0 +1,16 @@ +include(functions/FetchContent_MakeAvailableExcludeFromAll) + +FetchContent_Declare( + benchmark + URL https://github.com/google/benchmark/archive/refs/tags/v1.8.5.tar.gz + URL_HASH MD5=708d91ce255e8af4c1d7dfec50dff178 +) + +set(INSTALL_GTEST OFF) + +set(BENCHMARK_ENABLE_TESTING OFF) +set(BENCHMARK_ENABLE_EXCEPTIONS OFF) +set(BENCHMARK_ENABLE_WERROR OFF) +set(BENCHMARK_ENABLE_INSTALL OFF) + +FetchContent_MakeAvailable(benchmark) diff --git a/Brewfile b/Brewfile index a5d2c71b1d0..e83c1a1f3c6 100644 --- a/Brewfile +++ b/Brewfile @@ -4,3 +4,4 @@ brew "sdl2" brew "libsodium" brew "pkg-config" brew "googletest" +brew "google-benchmark" diff --git a/CMake/Dependencies.cmake b/CMake/Dependencies.cmake index 99af8005d2c..979faf62f41 100644 --- a/CMake/Dependencies.cmake +++ b/CMake/Dependencies.cmake @@ -255,6 +255,13 @@ if(BUILD_TESTING) else() add_subdirectory(3rdParty/googletest) endif() + + dependency_options("benchmark" DEVILUTIONX_SYSTEM_BENCHMARK ON DEVILUTIONX_STATIC_BENCHMARK) + if(DEVILUTIONX_SYSTEM_BENCHMARK) + find_package(benchmark REQUIRED) + else() + add_subdirectory(3rdParty/benchmark) + endif() endif() if(GPERF) diff --git a/docs/building.md b/docs/building.md index 5800f1ec25d..34a65aff83f 100644 --- a/docs/building.md +++ b/docs/building.md @@ -12,7 +12,7 @@ Note that ```pkg-config``` is an optional dependency for finding libsodium, alth ### Installing dependencies on Debian and Ubuntu ``` -sudo apt-get install cmake g++ libsdl2-dev libsodium-dev libpng-dev libbz2-dev libgtest-dev libgmock-dev libsdl2-image-dev libfmt-dev +sudo apt-get install cmake g++ libsdl2-dev libsodium-dev libpng-dev libbz2-dev libgtest-dev libgmock-dev libbenchmark-dev libsdl2-image-dev libfmt-dev ``` ### If you want to build the translations (optional) @@ -30,7 +30,7 @@ sudo apt-get install smpq ### Installing dependencies on Fedora ``` -sudo dnf install cmake gcc-c++ glibc-devel libstdc++-static SDL2-devel SDL2_image-devel libsodium-devel libpng-devel bzip2-devel gmock-devel gtest-devel libasan libubsan fmt-devel +sudo dnf install cmake gcc-c++ glibc-devel libstdc++-static SDL2-devel SDL2_image-devel libsodium-devel libpng-devel bzip2-devel gmock-devel gtest-devel google-benchmark-devel libasan libubsan fmt-devel ``` ### Compiling diff --git a/tools/make_src_dist.py b/tools/make_src_dist.py index 48206f213b8..8335b5dd1fe 100755 --- a/tools/make_src_dist.py +++ b/tools/make_src_dist.py @@ -41,7 +41,7 @@ # These dependencies are not vendored by default. # Run with `--fully_vendored` to include them. -_DEPS_NOT_VENDORED_BY_DEFAULT = ['googletest', 'sdl2', 'sdl_image', +_DEPS_NOT_VENDORED_BY_DEFAULT = ['googletest', 'benchmark', 'sdl2', 'sdl_image', 'libpng', 'libfmt', 'bzip2', 'libsodium'] _ROOT_DIR = pathlib.Path(__file__).resolve().parent.parent @@ -101,7 +101,7 @@ def main(): src = src_bytes.decode() dst_path = paths.archive_top_level_dir.joinpath(src) dst_path.parent.mkdir(parents=True, exist_ok=True) - if re.search('(^|/)\.gitkeep$', src): + if re.search(r'(^|/)\.gitkeep$', src): continue shutil.copy2(_ROOT_DIR.joinpath(src), dst_path, follow_symlinks=False) diff --git a/vcpkg.json b/vcpkg.json index ba88ccab676..a8511b1b8e9 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -32,7 +32,7 @@ }, "tests": { "description": "Build tests", - "dependencies": [ "gtest" ] + "dependencies": [ "gtest", "benchmark" ] } } } From 077b51a9ead76ff471342a4215d41b37603feb69 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Fri, 26 Jul 2024 09:25:01 +0100 Subject: [PATCH 3/3] Extract Crawl to a library Also adds a test and a benchmark --- Source/CMakeLists.txt | 8 +++ Source/crawl.cpp | 74 ++++++++++++++++++++++ Source/crawl.hpp | 58 +++++++++++++++++ Source/engine/path.cpp | 1 + Source/lighting.cpp | 60 ------------------ Source/lighting.h | 49 -------------- Source/lua/modules/dev/monsters.cpp | 1 + Source/missiles.cpp | 1 + Source/monster.cpp | 1 + test/CMakeLists.txt | 18 +++++- test/crawl_benchmark.cpp | 25 ++++++++ test/{lighting_test.cpp => crawl_test.cpp} | 9 +-- 12 files changed, 189 insertions(+), 116 deletions(-) create mode 100644 Source/crawl.cpp create mode 100644 Source/crawl.hpp create mode 100644 test/crawl_benchmark.cpp rename test/{lighting_test.cpp => crawl_test.cpp} (88%) diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 058d30e9368..8371d0073bf 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -343,6 +343,13 @@ target_link_libraries(libdevilutionx_codec PRIVATE libdevilutionx_log ) +add_devilutionx_object_library(libdevilutionx_crawl + crawl.cpp +) +target_link_libraries(libdevilutionx_crawl PUBLIC + tl +) + add_devilutionx_object_library(libdevilutionx_file_util utils/file_util.cpp ) @@ -397,6 +404,7 @@ target_link_libraries(libdevilutionx PUBLIC simpleini::simpleini tl libdevilutionx_codec + libdevilutionx_crawl libdevilutionx_format_int libdevilutionx_file_util libdevilutionx_parse_int diff --git a/Source/crawl.cpp b/Source/crawl.cpp new file mode 100644 index 00000000000..fe561a05b22 --- /dev/null +++ b/Source/crawl.cpp @@ -0,0 +1,74 @@ +#include "crawl.hpp" + +#include + +#include + +#include "engine/displacement.hpp" + +namespace devilution { +namespace { + +bool CrawlFlipsX(Displacement mirrored, tl::function_ref function) +{ + for (const Displacement displacement : { mirrored.flipX(), mirrored }) { + if (!function(displacement)) + return false; + } + return true; +} + +bool CrawlFlipsY(Displacement mirrored, tl::function_ref function) +{ + for (const Displacement displacement : { mirrored, mirrored.flipY() }) { + if (!function(displacement)) + return false; + } + return true; +} + +bool CrawlFlipsXY(Displacement mirrored, tl::function_ref function) +{ + for (const Displacement displacement : { mirrored.flipX(), mirrored, mirrored.flipXY(), mirrored.flipY() }) { + if (!function(displacement)) + return false; + } + return true; +} + +} // namespace + +bool DoCrawl(unsigned radius, tl::function_ref function) +{ + if (radius == 0) + return function(Displacement { 0, 0 }); + + if (!CrawlFlipsY({ 0, static_cast(radius) }, function)) + return false; + for (unsigned i = 1; i < radius; i++) { + if (!CrawlFlipsXY({ static_cast(i), static_cast(radius) }, function)) + return false; + } + if (radius > 1) { + if (!CrawlFlipsXY({ static_cast(radius) - 1, static_cast(radius) - 1 }, function)) + return false; + } + if (!CrawlFlipsX({ static_cast(radius), 0 }, function)) + return false; + for (unsigned i = 1; i < radius; i++) { + if (!CrawlFlipsXY({ static_cast(radius), static_cast(i) }, function)) + return false; + } + return true; +} + +bool DoCrawl(unsigned minRadius, unsigned maxRadius, tl::function_ref function) +{ + for (unsigned i = minRadius; i <= maxRadius; i++) { + if (!DoCrawl(i, function)) + return false; + } + return true; +} + +} // namespace devilution diff --git a/Source/crawl.hpp b/Source/crawl.hpp new file mode 100644 index 00000000000..f0146fe9014 --- /dev/null +++ b/Source/crawl.hpp @@ -0,0 +1,58 @@ +#include + +#include + +#include "engine/displacement.hpp" + +namespace devilution { + +/** + * CrawlTable specifies X- and Y-coordinate deltas from a missile target coordinate. + * + * n=4 + * + * y + * ^ + * | 1 + * | 3#4 + * | 2 + * +-----> x + * + * n=16 + * + * y + * ^ + * | 314 + * | B7 8C + * | F # G + * | D9 AE + * | 526 + * +-------> x + */ + +bool DoCrawl(unsigned radius, tl::function_ref function); +bool DoCrawl(unsigned minRadius, unsigned maxRadius, tl::function_ref function); + +template +auto Crawl(unsigned radius, F function) -> std::invoke_result_t +{ + std::invoke_result_t result; + DoCrawl(radius, [&result, &function](Displacement displacement) -> bool { + result = function(displacement); + return !result; + }); + return result; +} + +template +auto Crawl(unsigned minRadius, unsigned maxRadius, F function) -> std::invoke_result_t +{ + std::invoke_result_t result; + DoCrawl(minRadius, maxRadius, [&result, &function](Displacement displacement) -> bool { + result = function(displacement); + return !result; + }); + return result; +} + +} // namespace devilution diff --git a/Source/engine/path.cpp b/Source/engine/path.cpp index a6f586e8f31..ed6f87944f7 100644 --- a/Source/engine/path.cpp +++ b/Source/engine/path.cpp @@ -10,6 +10,7 @@ #include +#include "crawl.hpp" #include "levels/gendung.h" #include "lighting.h" #include "objects.h" diff --git a/Source/lighting.cpp b/Source/lighting.cpp index 70076359e0e..2345c88dde4 100644 --- a/Source/lighting.cpp +++ b/Source/lighting.cpp @@ -113,33 +113,6 @@ DVL_ALWAYS_INLINE uint8_t GetLight(Point position) return dLight[position.x][position.y]; } -bool CrawlFlipsX(Displacement mirrored, tl::function_ref function) -{ - for (const Displacement displacement : { mirrored.flipX(), mirrored }) { - if (!function(displacement)) - return false; - } - return true; -} - -bool CrawlFlipsY(Displacement mirrored, tl::function_ref function) -{ - for (const Displacement displacement : { mirrored, mirrored.flipY() }) { - if (!function(displacement)) - return false; - } - return true; -} - -bool CrawlFlipsXY(Displacement mirrored, tl::function_ref function) -{ - for (const Displacement displacement : { mirrored.flipX(), mirrored, mirrored.flipXY(), mirrored.flipY() }) { - if (!function(displacement)) - return false; - } - return true; -} - bool TileAllowsLight(Point position) { if (!InDungeonBounds(position)) @@ -161,39 +134,6 @@ void DoVisionFlags(Point position, MapExplorationType doAutomap, bool visible) } // namespace -bool DoCrawl(unsigned radius, tl::function_ref function) -{ - if (radius == 0) - return function(Displacement { 0, 0 }); - - if (!CrawlFlipsY({ 0, static_cast(radius) }, function)) - return false; - for (unsigned i = 1; i < radius; i++) { - if (!CrawlFlipsXY({ static_cast(i), static_cast(radius) }, function)) - return false; - } - if (radius > 1) { - if (!CrawlFlipsXY({ static_cast(radius) - 1, static_cast(radius) - 1 }, function)) - return false; - } - if (!CrawlFlipsX({ static_cast(radius), 0 }, function)) - return false; - for (unsigned i = 1; i < radius; i++) { - if (!CrawlFlipsXY({ static_cast(radius), static_cast(i) }, function)) - return false; - } - return true; -} - -bool DoCrawl(unsigned minRadius, unsigned maxRadius, tl::function_ref function) -{ - for (unsigned i = minRadius; i <= maxRadius; i++) { - if (!DoCrawl(i, function)) - return false; - } - return true; -} - void DoUnLight(Point position, uint8_t radius) { radius++; diff --git a/Source/lighting.h b/Source/lighting.h index dd1fc72a428..f8888fbf6e5 100644 --- a/Source/lighting.h +++ b/Source/lighting.h @@ -86,53 +86,4 @@ void lighting_color_cycling(); constexpr int MaxCrawlRadius = 18; -/** - * CrawlTable specifies X- and Y-coordinate deltas from a missile target coordinate. - * - * n=4 - * - * y - * ^ - * | 1 - * | 3#4 - * | 2 - * +-----> x - * - * n=16 - * - * y - * ^ - * | 314 - * | B7 8C - * | F # G - * | D9 AE - * | 526 - * +-------> x - */ - -bool DoCrawl(unsigned radius, tl::function_ref function); -bool DoCrawl(unsigned minRadius, unsigned maxRadius, tl::function_ref function); - -template -auto Crawl(unsigned radius, F function) -> std::invoke_result_t -{ - std::invoke_result_t result; - DoCrawl(radius, [&result, &function](Displacement displacement) -> bool { - result = function(displacement); - return !result; - }); - return result; -} - -template -auto Crawl(unsigned minRadius, unsigned maxRadius, F function) -> std::invoke_result_t -{ - std::invoke_result_t result; - DoCrawl(minRadius, maxRadius, [&result, &function](Displacement displacement) -> bool { - result = function(displacement); - return !result; - }); - return result; -} - } // namespace devilution diff --git a/Source/lua/modules/dev/monsters.cpp b/Source/lua/modules/dev/monsters.cpp index 42b0ccd8488..00855ebd2c9 100644 --- a/Source/lua/modules/dev/monsters.cpp +++ b/Source/lua/modules/dev/monsters.cpp @@ -6,6 +6,7 @@ #include +#include "crawl.hpp" #include "levels/gendung.h" #include "lighting.h" #include "lua/metadoc.hpp" diff --git a/Source/missiles.cpp b/Source/missiles.cpp index 8f1d2cf9a96..aa611ec116b 100644 --- a/Source/missiles.cpp +++ b/Source/missiles.cpp @@ -11,6 +11,7 @@ #include "control.h" #include "controls/plrctrls.h" +#include "crawl.hpp" #include "cursor.h" #include "dead.h" #ifdef _DEBUG diff --git a/Source/monster.cpp b/Source/monster.cpp index c2bde27e185..d93a96c46fe 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -18,6 +18,7 @@ #include #include "control.h" +#include "crawl.hpp" #include "cursor.h" #include "dead.h" #include "engine/load_cl2.hpp" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 38f1a4bafa0..987a7dd5b4e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,7 +24,6 @@ set(tests drlg_l4_test effects_test inv_test - lighting_test math_test missiles_test pack_test @@ -40,24 +39,30 @@ set(tests ) set(standalone_tests codec_test + crawl_test file_util_test format_int_test parse_int_test str_cat_test utf8_test ) +set(benchmarks + crawl_benchmark) include(Fixtures.cmake) -foreach(test_target ${tests} ${standalone_tests}) +foreach(test_target ${tests} ${standalone_tests} ${benchmarks}) add_executable(${test_target} "${test_target}.cpp") - gtest_discover_tests(${test_target}) set_target_properties(${test_target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) if(GPERF) target_link_libraries(${test_target} PUBLIC ${GPERFTOOLS_LIBRARIES}) endif() endforeach() +foreach(test_target ${tests} ${standalone_tests}) + gtest_discover_tests(${test_target}) +endforeach() + foreach(test_target ${tests}) target_link_libraries(${test_target} PRIVATE test_main) endforeach() @@ -67,6 +72,11 @@ foreach(test_target ${standalone_tests}) target_include_directories(${test_target} PRIVATE "${PROJECT_SOURCE_DIR}/Source") endforeach() +foreach(target ${benchmarks}) + target_link_libraries(${target} PRIVATE benchmark::benchmark benchmark::benchmark_main) + target_include_directories(${target} PRIVATE "${PROJECT_SOURCE_DIR}/Source") +endforeach() + add_library(app_fatal_for_testing OBJECT app_fatal_for_testing.cpp) target_sources(app_fatal_for_testing INTERFACE $) @@ -74,6 +84,8 @@ add_library(language_for_testing OBJECT language_for_testing.cpp) target_sources(language_for_testing INTERFACE $) target_link_libraries(codec_test PRIVATE libdevilutionx_codec app_fatal_for_testing) +target_link_libraries(crawl_test PRIVATE libdevilutionx_crawl) +target_link_libraries(crawl_benchmark PRIVATE libdevilutionx_crawl) target_link_libraries(file_util_test PRIVATE libdevilutionx_file_util app_fatal_for_testing) target_link_libraries(format_int_test PRIVATE libdevilutionx_format_int language_for_testing) target_link_libraries(parse_int_test PRIVATE libdevilutionx_parse_int) diff --git a/test/crawl_benchmark.cpp b/test/crawl_benchmark.cpp new file mode 100644 index 00000000000..8c47136ad83 --- /dev/null +++ b/test/crawl_benchmark.cpp @@ -0,0 +1,25 @@ +#include + +#include "crawl.hpp" +#include "engine/displacement.hpp" + +namespace devilution { +namespace { + +void BM_Crawl(benchmark::State &state) +{ + const int radius = state.range(0); + for (auto _ : state) { + int sum; + Crawl(0, radius, [&sum](Displacement d) { + sum += d.deltaX + d.deltaY; + return false; + }); + benchmark::DoNotOptimize(sum); + } +} + +BENCHMARK(BM_Crawl)->RangeMultiplier(4)->Range(1, 20); + +} // namespace +} // namespace devilution diff --git a/test/lighting_test.cpp b/test/crawl_test.cpp similarity index 88% rename from test/lighting_test.cpp rename to test/crawl_test.cpp index c6c7ca7dcfc..a788d165ce0 100644 --- a/test/lighting_test.cpp +++ b/test/crawl_test.cpp @@ -3,14 +3,14 @@ #include #include -#include "control.h" -#include "lighting.h" +#include "crawl.hpp" +#include "engine/displacement.hpp" namespace devilution { namespace { using ::testing::ElementsAre; -TEST(Lighting, CrawlTables) +TEST(CrawlTest, BasicTest) { bool added[40][40]; memset(added, 0, sizeof(added)); @@ -18,6 +18,7 @@ TEST(Lighting, CrawlTables) int x = 20; int y = 20; + constexpr int MaxCrawlRadius = 18; Crawl(0, MaxCrawlRadius, [&](Displacement displacement) { int dx = x + displacement.deltaX; int dy = y + displacement.deltaY; @@ -37,7 +38,7 @@ TEST(Lighting, CrawlTables) } } -TEST(Lighting, CrawlTablesVisitationOrder) +TEST(CrawlTest, VisitationOrderTest) { std::vector order; Crawl(0, 2, [&](Displacement displacement) {