forked from Alyxion/Udacity_IntroToSelfDrivingCarsNd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.cpp
executable file
·284 lines (224 loc) · 5.86 KB
/
tests.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <iostream>
#include "simulate.cpp"
bool test_normalize() {
vector < vector <float> > unnormalized, normalized, result;
unnormalized = zeros(2, 2);
normalized = zeros(2,2);
int i,j;
for (i=0; i<2; i++) {
for(j=0; j<2; j++) {
unnormalized[i][j] = 1.0;
normalized[i][j] = 0.25;
}
}
result = normalize(unnormalized);
bool correct;
correct = close_enough(normalized, result);
if (correct) {
cout << "! - normalize function worked correctly!\n";
}
else {
cout << "X - normalize function did not work correctly.\n";
cout << "For the following input:\n\n";
show_grid(unnormalized);
cout << "\nYour code returned the following:\n\n";
show_grid(result);
cout << "\nWhen it should have returned the following:\n";
show_grid(normalized);
}
return correct;
}
bool test_blur() {
vector < vector <float> > in, correct, out;
in = zeros(3, 3);
correct = zeros(3,3);
in[1][1] = 1.0;
float corner = 0.01f;
float side = 0.02f;
float center = 0.88f;
correct[0][0] = corner;
correct[0][1] = side;
correct[0][2] = corner;
correct[1][0] = side;
correct[1][1] = center;
correct[1][2] = side;
correct[2][0] = corner;
correct[2][1] = side;
correct[2][2] = corner;
out = blur(in, 0.12f);
bool right;
right = close_enough(correct, out);
if (right) {
cout << "! - blur function worked correctly!\n";
}
else {
cout << "X - blur function did not work correctly.\n";
cout << "For the following input:\n\n";
show_grid(in);
cout << "\nYour code returned the following:\n\n";
show_grid(out);
cout << "\nWhen it should have returned the following:\n";
show_grid(correct);
}
return right;
}
bool test_helpers() {
bool correct = true;
bool question_correct;
question_correct = test_normalize();
if (!question_correct) {
correct = false;
}
cout << endl;
question_correct = test_blur();
if (!question_correct) {
correct = false;
}
return correct;
}
bool test_initialize() {
vector < vector <char> > map;
map = read_map("maps/m1.txt");
vector < vector <float> > beliefs, correct;
int h, w, A;
float belief;
h = map.size();
if (h < 1) {
cout << "failed to load map. Make sure there is a maps/ directory in the same directory as this file!\n";
return false;
}
beliefs = initialize_beliefs(map);
w = map[0].size();
A = h * w;
belief = 1.f / A;
vector <float> row;
for (size_t i=0; i<map.size(); i++) {
row.clear();
for (size_t j=0; j<map[0].size(); j++) {
row.push_back(belief);
}
correct.push_back(row);
}
bool right = close_enough(correct, beliefs);
if (right) {
cout << "! - initialize_beliefs function worked correctly!\n";
}
else {
cout << "X - initialize_beliefs function did not work correctly.\n";
cout << "For the following input:\n\n";
show_grid(map);
cout << "\nYour code returned the following:\n\n";
show_grid(beliefs);
cout << "\nWhen it should have returned the following:\n";
show_grid(correct);
}
return right;
}
bool test_move() {
vector < vector <float> > in, out, correct;
in = zeros(3,3);
in[2][2] = 1.0;
int dx, dy;
dx = 1;
dy = 1;
float blurring = 0.0;
correct = zeros(3,3);
correct[0][0] = 1.0;
out = move(dy, dx, in, blurring);
bool right = close_enough(correct, out);
if (right) {
cout << "! - move function worked correctly with zero blurring\n";
}
else {
cout << "X - move function did not work correctly.\n";
cout << "When dx=1, dy=1, blurring=0.0 and with\nthe following beliefs:\n\n";
show_grid(in);
cout << "\nYour code returned the following:\n\n";
show_grid(out);
cout << "\nWhen it should have returned the following:\n";
show_grid(correct);
}
return right;
}
bool test_sense() {
vector < vector <float> > in, out, correct;
in = zeros(4,2);
// in[2][2] = 1.0; // Note: Obsolete and reported to Andy already, overwritten below
for (size_t i=0; i<in.size(); i++)
{
for (size_t j=0; j<in[0].size(); j++) {
in[i][j] = 1.0/8.0;
}
}
char color = 'r';
vector < vector <char> > map;
map = read_map("maps/half_red.txt");
float p_hit, p_miss;
p_hit = 2.0;
p_miss = 1.0;
out = sense(color, map, in, p_hit, p_miss);
float total = 0.0;
for (size_t i=0; i<out.size(); i++)
{
for (size_t j=0; j<out[0].size(); j++) {
total += out[i][j];
}
}
bool right = true;
if ( (total < 0.99) || (total > 1.01) ) {
right = false;
}
if ( (out.size() != in.size()) || out[0].size() != in[0].size()) {
right = false;
cout << "X - sense function not working correctly.\n";
cout << "Your function returned a grid with incorrect dimensions.\n";
return right;
}
float r_prob, g_prob, r_exp, g_exp;
r_prob = out[0][0];
g_prob = out[0][1];
r_exp = 1.f / 6.f;
g_exp = 1.f / 12.f;
if (close_enough(r_prob, r_exp) && close_enough(g_prob, g_exp)) {
cout << "! - sense function worked correctly\n";
return false;
}
else {
cout << "X - sense function did not work correctly.\n";
cout << "When p_hit=2.0, p_miss=1.0 and with\nthe following beliefs:\n\n";
show_grid(in);
cout << "\nYour code returned the following:\n\n";
show_grid(out);
cout << "\nbut this is incorrect.\n";
}
return right;
}
bool test_localizer() {
bool correct = true;
bool question_correct;
question_correct = test_initialize();
if (!question_correct) {
return false; // Note: Can cancel here, would crash below otherwise
}
cout << endl;
question_correct = test_move();
if (!question_correct) {
correct = false;
}
cout << endl;
question_correct = test_sense();
if (!question_correct) {
correct = false;
}
return correct;
}
// bool test_simulation() {
// // todo
// }
int main() {
cout << endl;
test_helpers();
test_localizer();
cout << endl;
return 0;
}