Skip to content

Commit 72d8b0a

Browse files
authored
Merge pull request #38 from justinhj/doctest
Add the Doctest test frame work for the test suite
2 parents ff55a1b + fbb3abe commit 72d8b0a

File tree

2 files changed

+25
-63
lines changed

2 files changed

+25
-63
lines changed

CMakeLists.txt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
cmake_minimum_required(VERSION 3.10)
1+
cmake_minimum_required(VERSION 3.20)
22
project(astar-algorithm-cpp)
33

44
set(CMAKE_CXX_STANDARD 11)
55
set(CMAKE_CXX_STANDARD_REQUIRED True)
66

7-
# Header-only library interface (optional but good practice)
7+
include(FetchContent)
8+
FetchContent_Declare(
9+
doctest
10+
URL https://raw.githubusercontent.com/doctest/doctest/v2.4.11/doctest/doctest.h
11+
DOWNLOAD_NO_EXTRACT TRUE
12+
)
13+
FetchContent_MakeAvailable(doctest)
14+
15+
add_library(doctest INTERFACE)
16+
target_include_directories(doctest INTERFACE ${doctest_SOURCE_DIR})
17+
18+
# Header-only library interface
819
add_library(stlastar INTERFACE)
920
target_include_directories(stlastar INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
1021

11-
# Executables
1222
add_executable(8puzzle 8puzzle.cpp)
1323
target_link_libraries(8puzzle stlastar)
1424

@@ -18,8 +28,10 @@ target_link_libraries(findpath stlastar)
1828
add_executable(minpathbucharest min_path_to_Bucharest.cpp)
1929
target_link_libraries(minpathbucharest stlastar)
2030

21-
# Tests
2231
enable_testing()
2332
add_executable(tests tests.cpp)
24-
target_link_libraries(tests stlastar)
33+
34+
target_link_libraries(tests stlastar doctest)
35+
36+
# Register with CTest so you can run 'ctest' in the build folder
2537
add_test(NAME tests COMMAND tests)

tests.cpp

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
#include <assert.h>
44
#include <stdio.h>
55
#include <stdlib.h>
6+
#include <iostream>
67

78
#include "stlastar.h"
89

9-
using namespace std;
10+
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
11+
#include "doctest.h"
12+
13+
using std::cout;
14+
using std::hash;
1015

1116
const int MAP_WIDTH = 20;
1217
const int MAP_HEIGHT = 20;
@@ -159,7 +164,7 @@ size_t MapSearchNode::Hash() {
159164
return h1 ^ (h2 << 1);
160165
}
161166

162-
int main(int argc, char* argv[]) {
167+
TEST_CASE("Map Search") {
163168
AStarSearch<MapSearchNode> astarsearch;
164169

165170
unsigned int SearchCount = 0;
@@ -188,82 +193,27 @@ int main(int argc, char* argv[]) {
188193
SearchState = astarsearch.SearchStep();
189194

190195
SearchSteps++;
191-
192-
#if DEBUG_LISTS
193-
194-
cout << "Steps:" << SearchSteps << "\n";
195-
196-
int len = 0;
197-
198-
cout << "Open:\n";
199-
MapSearchNode* p = astarsearch.GetOpenListStart();
200-
while (p) {
201-
len++;
202-
#if !DEBUG_LIST_LENGTHS_ONLY
203-
((MapSearchNode*)p)->PrintNodeInfo();
204-
#endif
205-
p = astarsearch.GetOpenListNext();
206-
}
207-
208-
cout << "Open list has " << len << " nodes\n";
209-
210-
len = 0;
211-
212-
cout << "Closed:\n";
213-
p = astarsearch.GetClosedListStart();
214-
while (p) {
215-
len++;
216-
#if !DEBUG_LIST_LENGTHS_ONLY
217-
p->PrintNodeInfo();
218-
#endif
219-
p = astarsearch.GetClosedListNext();
220-
}
221-
222-
cout << "Closed list has " << len << " nodes\n";
223-
#endif
224-
225196
} while (SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING);
226197

227198
if (SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED) {
228-
cout << "Search found goal state\n";
229-
230199
MapSearchNode* node = astarsearch.GetSolutionStart();
231200

232-
#if DISPLAY_SOLUTION
233-
cout << "Displaying solution\n";
234-
#endif
235-
int steps = 0;
236-
237-
node->PrintNodeInfo();
238201
for (;;) {
239202
node = astarsearch.GetSolutionNext();
240203

241204
if (!node) {
242205
break;
243206
}
244-
245-
node->PrintNodeInfo();
246-
steps++;
247207
};
248208

249-
cout << "Solution steps " << steps << endl;
250-
251209
// Once you're done with the solution you can free the nodes up
252210
astarsearch.FreeSolutionNodes();
253211

254-
} else if (SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_FAILED) {
255-
cout << "Search terminated. Did not find goal state\n";
256212
}
257213

258-
// Display the number of loops the search went through
259-
cout << "SearchSteps : " << SearchSteps << "\n";
260-
261214
SearchCount++;
262215

263216
astarsearch.EnsureMemoryFreed();
217+
CHECK(SearchSteps == 227);
264218
}
265-
266-
assert(true && "failed to be true");
267-
268-
printf("Tests succeeded\n");
269219
}

0 commit comments

Comments
 (0)