Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)
66 changes: 8 additions & 58 deletions tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

#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;
Expand Down Expand Up @@ -159,7 +164,7 @@ size_t MapSearchNode::Hash() {
return h1 ^ (h2 << 1);
}

int main(int argc, char* argv[]) {
TEST_CASE("Map Search") {
AStarSearch<MapSearchNode> astarsearch;

unsigned int SearchCount = 0;
Expand Down Expand Up @@ -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<MapSearchNode>::SEARCH_STATE_SEARCHING);

if (SearchState == AStarSearch<MapSearchNode>::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<MapSearchNode>::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");
}