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..4f30142 100644 --- a/tests.cpp +++ b/tests.cpp @@ -3,10 +3,15 @@ #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; @@ -159,7 +164,7 @@ size_t MapSearchNode::Hash() { return h1 ^ (h2 << 1); } -int main(int argc, char* argv[]) { +TEST_CASE("Map Search") { AStarSearch astarsearch; unsigned int SearchCount = 0; @@ -188,82 +193,27 @@ int main(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"); }