From 0244f6deff3a08b6b3d0f58f6872f9aa0dc0fe46 Mon Sep 17 00:00:00 2001 From: Justin Heyes-Jones Date: Mon, 8 Dec 2025 21:13:57 -0800 Subject: [PATCH 1/2] Add the header and start tests --- CMakeLists.txt | 22 +++++++++++++++++----- tests.cpp | 10 +++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b3ebad1..12c8180 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,24 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.20) project(astar-algorithm-cpp) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) -# Header-only library interface (optional but good practice) +include(FetchContent) +FetchContent_Declare( + doctest + URL https://raw.githubusercontent.com/doctest/doctest/v2.4.11/doctest/doctest.h + DOWNLOAD_NO_EXTRACT TRUE +) +FetchContent_MakeAvailable(doctest) + +add_library(doctest INTERFACE) +target_include_directories(doctest INTERFACE ${doctest_SOURCE_DIR}) + +# Header-only library interface add_library(stlastar INTERFACE) target_include_directories(stlastar INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) -# Executables add_executable(8puzzle 8puzzle.cpp) target_link_libraries(8puzzle stlastar) @@ -18,8 +28,10 @@ target_link_libraries(findpath stlastar) add_executable(minpathbucharest min_path_to_Bucharest.cpp) target_link_libraries(minpathbucharest stlastar) -# Tests enable_testing() add_executable(tests tests.cpp) -target_link_libraries(tests stlastar) + +target_link_libraries(tests stlastar doctest) + +# Register with CTest so you can run 'ctest' in the build folder add_test(NAME tests COMMAND tests) diff --git a/tests.cpp b/tests.cpp index 16f5739..55c75f9 100644 --- a/tests.cpp +++ b/tests.cpp @@ -1,5 +1,13 @@ // A unit test suite +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "stlastar.h" // Assuming this is your header + +TEST_CASE("Test A* Basic Functionality") { + // Your test logic here + CHECK(1 + 1 == 2); +} #include #include #include @@ -159,7 +167,7 @@ size_t MapSearchNode::Hash() { return h1 ^ (h2 << 1); } -int main(int argc, char* argv[]) { +int main2(int argc, char* argv[]) { AStarSearch astarsearch; unsigned int SearchCount = 0; From fbb3abe8031709c8504edbbba18b653d2a8f76a0 Mon Sep 17 00:00:00 2001 From: Justin Heyes-Jones Date: Mon, 8 Dec 2025 21:24:08 -0800 Subject: [PATCH 2/2] A real test --- tests.cpp | 74 ++++++------------------------------------------------- 1 file changed, 8 insertions(+), 66 deletions(-) diff --git a/tests.cpp b/tests.cpp index 55c75f9..4f30142 100644 --- a/tests.cpp +++ b/tests.cpp @@ -1,20 +1,17 @@ // A unit test suite -#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN -#include "doctest.h" -#include "stlastar.h" // Assuming this is your header - -TEST_CASE("Test A* Basic Functionality") { - // Your test logic here - CHECK(1 + 1 == 2); -} #include #include #include +#include #include "stlastar.h" -using namespace std; +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" + +using std::cout; +using std::hash; const int MAP_WIDTH = 20; const int MAP_HEIGHT = 20; @@ -167,7 +164,7 @@ size_t MapSearchNode::Hash() { return h1 ^ (h2 << 1); } -int main2(int argc, char* argv[]) { +TEST_CASE("Map Search") { AStarSearch astarsearch; unsigned int SearchCount = 0; @@ -196,82 +193,27 @@ int main2(int argc, char* argv[]) { SearchState = astarsearch.SearchStep(); SearchSteps++; - -#if DEBUG_LISTS - - cout << "Steps:" << SearchSteps << "\n"; - - int len = 0; - - cout << "Open:\n"; - MapSearchNode* p = astarsearch.GetOpenListStart(); - while (p) { - len++; -#if !DEBUG_LIST_LENGTHS_ONLY - ((MapSearchNode*)p)->PrintNodeInfo(); -#endif - p = astarsearch.GetOpenListNext(); - } - - cout << "Open list has " << len << " nodes\n"; - - len = 0; - - cout << "Closed:\n"; - p = astarsearch.GetClosedListStart(); - while (p) { - len++; -#if !DEBUG_LIST_LENGTHS_ONLY - p->PrintNodeInfo(); -#endif - p = astarsearch.GetClosedListNext(); - } - - cout << "Closed list has " << len << " nodes\n"; -#endif - } while (SearchState == AStarSearch::SEARCH_STATE_SEARCHING); if (SearchState == AStarSearch::SEARCH_STATE_SUCCEEDED) { - cout << "Search found goal state\n"; - MapSearchNode* node = astarsearch.GetSolutionStart(); -#if DISPLAY_SOLUTION - cout << "Displaying solution\n"; -#endif - int steps = 0; - - node->PrintNodeInfo(); for (;;) { node = astarsearch.GetSolutionNext(); if (!node) { break; } - - node->PrintNodeInfo(); - steps++; }; - cout << "Solution steps " << steps << endl; - // Once you're done with the solution you can free the nodes up astarsearch.FreeSolutionNodes(); - } else if (SearchState == AStarSearch::SEARCH_STATE_FAILED) { - cout << "Search terminated. Did not find goal state\n"; } - // Display the number of loops the search went through - cout << "SearchSteps : " << SearchSteps << "\n"; - SearchCount++; astarsearch.EnsureMemoryFreed(); + CHECK(SearchSteps == 227); } - - assert(true && "failed to be true"); - - printf("Tests succeeded\n"); }