|
3 | 3 | #include <assert.h> |
4 | 4 | #include <stdio.h> |
5 | 5 | #include <stdlib.h> |
| 6 | +#include <iostream> |
6 | 7 |
|
7 | 8 | #include "stlastar.h" |
8 | 9 |
|
9 | | -using namespace std; |
| 10 | +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN |
| 11 | +#include "doctest.h" |
| 12 | + |
| 13 | +using std::cout; |
| 14 | +using std::hash; |
10 | 15 |
|
11 | 16 | const int MAP_WIDTH = 20; |
12 | 17 | const int MAP_HEIGHT = 20; |
@@ -159,7 +164,7 @@ size_t MapSearchNode::Hash() { |
159 | 164 | return h1 ^ (h2 << 1); |
160 | 165 | } |
161 | 166 |
|
162 | | -int main(int argc, char* argv[]) { |
| 167 | +TEST_CASE("Map Search") { |
163 | 168 | AStarSearch<MapSearchNode> astarsearch; |
164 | 169 |
|
165 | 170 | unsigned int SearchCount = 0; |
@@ -188,82 +193,27 @@ int main(int argc, char* argv[]) { |
188 | 193 | SearchState = astarsearch.SearchStep(); |
189 | 194 |
|
190 | 195 | 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 | | - |
225 | 196 | } while (SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING); |
226 | 197 |
|
227 | 198 | if (SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED) { |
228 | | - cout << "Search found goal state\n"; |
229 | | - |
230 | 199 | MapSearchNode* node = astarsearch.GetSolutionStart(); |
231 | 200 |
|
232 | | -#if DISPLAY_SOLUTION |
233 | | - cout << "Displaying solution\n"; |
234 | | -#endif |
235 | | - int steps = 0; |
236 | | - |
237 | | - node->PrintNodeInfo(); |
238 | 201 | for (;;) { |
239 | 202 | node = astarsearch.GetSolutionNext(); |
240 | 203 |
|
241 | 204 | if (!node) { |
242 | 205 | break; |
243 | 206 | } |
244 | | - |
245 | | - node->PrintNodeInfo(); |
246 | | - steps++; |
247 | 207 | }; |
248 | 208 |
|
249 | | - cout << "Solution steps " << steps << endl; |
250 | | - |
251 | 209 | // Once you're done with the solution you can free the nodes up |
252 | 210 | astarsearch.FreeSolutionNodes(); |
253 | 211 |
|
254 | | - } else if (SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_FAILED) { |
255 | | - cout << "Search terminated. Did not find goal state\n"; |
256 | 212 | } |
257 | 213 |
|
258 | | - // Display the number of loops the search went through |
259 | | - cout << "SearchSteps : " << SearchSteps << "\n"; |
260 | | - |
261 | 214 | SearchCount++; |
262 | 215 |
|
263 | 216 | astarsearch.EnsureMemoryFreed(); |
| 217 | + CHECK(SearchSteps == 227); |
264 | 218 | } |
265 | | - |
266 | | - assert(true && "failed to be true"); |
267 | | - |
268 | | - printf("Tests succeeded\n"); |
269 | 219 | } |
0 commit comments