-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijikstra_test.cpp
More file actions
183 lines (148 loc) · 5.33 KB
/
dijikstra_test.cpp
File metadata and controls
183 lines (148 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* dijikstra_test.cpp
*
* Created on: Mar 11, 2022 21:17
* Description:
*
* Copyright (c) 2022 Pin Loon Lee (pllee4)
*/
#include "algorithm/robotics/dijikstra/dijikstra.hpp"
#include "gtest/gtest.h"
using namespace pllee4::graph;
TEST(Dijikstra, InvalidSetOccupiedGrid) {
Dijikstra dijikstra{MotionConstraintType::CARDINAL_MOTION};
EXPECT_FALSE(dijikstra.SetOccupiedGrid({{1, 6}}));
dijikstra.SetMapStorageSize(2, 6);
EXPECT_FALSE(dijikstra.SetOccupiedGrid({{1, 6}}));
}
TEST(Dijikstra, ValidSetOccupanciedGrid) {
Dijikstra dijikstra{MotionConstraintType::CARDINAL_MOTION};
dijikstra.SetMapStorageSize(2, 7);
EXPECT_TRUE(dijikstra.SetOccupiedGrid({{1, 6}}));
}
TEST(Dijikstra, InvalidSetStartAndDestination) {
Dijikstra dijikstra{MotionConstraintType::CARDINAL_MOTION};
EXPECT_FALSE(dijikstra.SetStartAndDestination({0, 0}, {4, 4}));
}
TEST(Dijikstra, FailedToFindPath) {
Dijikstra dijikstra{MotionConstraintType::CARDINAL_MOTION};
/**
* s = start, e = end, x = occupied
* |s | x | e |
* | | x | |
* | | | |
*/
dijikstra.SetMapStorageSize(3, 3);
dijikstra.SetOccupiedGrid({{1, 2}, {1, 1}});
dijikstra.SetStartAndDestination({0, 0}, {4, 4});
EXPECT_FALSE(dijikstra.FindPath());
EXPECT_FALSE(dijikstra.GetPath().has_value());
dijikstra.SetStartAndDestination({4, 4}, {2, 2});
EXPECT_FALSE(dijikstra.FindPath());
EXPECT_FALSE(dijikstra.GetPath().has_value());
}
TEST(Dijikstra, FindPath) {
Dijikstra dijikstra{MotionConstraintType::CARDINAL_MOTION};
/**
* s = start, e = end, x = occupied
* |s | x | e |
* | | x | |
* | | | |
*/
dijikstra.SetMapStorageSize(3, 3);
dijikstra.SetOccupiedGrid({{1, 2}, {1, 1}});
dijikstra.SetStartAndDestination({0, 2}, {2, 2});
EXPECT_TRUE(dijikstra.FindPath());
}
TEST(Dijikstra, GetPath) {
Dijikstra dijikstra{MotionConstraintType::CARDINAL_MOTION};
/**
* s = start, e = end, x = occupied
* |s | x | e |
* | | x | |
* | | | |
*/
dijikstra.SetMapStorageSize(3, 3);
dijikstra.SetOccupiedGrid({{1, 2}, {1, 1}});
dijikstra.SetStartAndDestination({0, 2}, {2, 2});
EXPECT_TRUE(dijikstra.FindPath());
EXPECT_TRUE(dijikstra.GetPath().has_value());
const auto path = dijikstra.GetPath().value();
std::vector<Coordinate> expected = {{0, 2}, {0, 1}, {0, 0}, {1, 0},
{2, 0}, {2, 1}, {2, 2}};
EXPECT_TRUE(std::equal(std::begin(path), std::end(path), std::begin(expected),
std::end(expected)));
}
TEST(Dijikstra, GetPathWithSettingOfSameStartAndDestination) {
Dijikstra dijikstra{MotionConstraintType::CARDINAL_MOTION};
/**
* s = start, e = end, x = occupied
* |s | x | e |
* | | x | |
* | | | |
*/
dijikstra.SetMapStorageSize(3, 3);
dijikstra.SetOccupiedGrid({{1, 2}, {1, 1}});
dijikstra.SetStartAndDestination({0, 2}, {2, 2});
// no clearing of occupied grid
dijikstra.SetStartAndDestination({0, 2}, {2, 2});
dijikstra.FindPath();
EXPECT_TRUE(dijikstra.GetPath().has_value());
const auto path = dijikstra.GetPath().value();
std::vector<Coordinate> expected = {{0, 2}, {0, 1}, {0, 0}, {1, 0},
{2, 0}, {2, 1}, {2, 2}};
EXPECT_TRUE(std::equal(std::begin(path), std::end(path), std::begin(expected),
std::end(expected)));
dijikstra.SetStartAndDestination({0, 2}, {2, 2});
// same start and destination, can get back previous path without finding
EXPECT_TRUE(dijikstra.GetPath().has_value());
dijikstra.SetStartAndDestination({0, 1}, {2, 2});
// different start and destination, can't get path without finding
EXPECT_FALSE(dijikstra.GetPath().has_value());
dijikstra.FindPath();
EXPECT_TRUE(dijikstra.GetPath().has_value());
}
TEST(Dijikstra, GetPathAfterReset) {
Dijikstra dijikstra{MotionConstraintType::CARDINAL_MOTION};
/**
* s = start, e = end, x = occupied
* |s | x | e |
* | | x | |
* | | | |
*/
dijikstra.SetMapStorageSize(3, 3);
dijikstra.SetOccupiedGrid({{1, 2}, {1, 1}});
dijikstra.SetStartAndDestination({0, 2}, {2, 2});
// internally clear path and occupied
dijikstra.Reset();
dijikstra.FindPath();
EXPECT_FALSE(dijikstra.GetPath().has_value());
dijikstra.SetStartAndDestination({0, 2}, {2, 2});
EXPECT_FALSE(dijikstra.GetPath().has_value());
dijikstra.FindPath();
EXPECT_TRUE(dijikstra.GetPath().has_value());
const auto path = dijikstra.GetPath().value();
// found path without occupied_grid
std::vector<Coordinate> expected = {{0, 2}, {1, 2}, {2, 2}};
EXPECT_TRUE(std::equal(std::begin(path), std::end(path), std::begin(expected),
std::end(expected)));
}
TEST(Dijikstra, GetPathWithRevisit) {
Dijikstra dijikstra{MotionConstraintType::CARDINAL_MOTION};
/**
* s = start, e = end, x = occupied
* | | | |
* | s | x | |
* | | | |
* | | | e |
*/
dijikstra.SetMapStorageSize(3, 4);
dijikstra.SetOccupiedGrid({{1, 2}});
dijikstra.SetStartAndDestination({0, 2}, {2, 0});
dijikstra.FindPath();
EXPECT_TRUE(dijikstra.GetPath().has_value());
const auto path = dijikstra.GetPath().value();
std::vector<Coordinate> expected = {{0, 2}, {0, 1}, {1, 1}, {1, 0}, {2, 0}};
EXPECT_TRUE(std::equal(std::begin(path), std::end(path), std::begin(expected),
std::end(expected)));
}