forked from varoudis/depthmapX
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtestagentparser.cpp
More file actions
471 lines (399 loc) · 18.4 KB
/
testagentparser.cpp
File metadata and controls
471 lines (399 loc) · 18.4 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright (C) 2017 Petros Koutsolampros
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "catch.hpp"
#include "../depthmapXcli/agentparser.h"
#include "argumentholder.h"
#include "selfcleaningfile.h"
TEST_CASE("AgentParserFail", "Parsing errors")
{
// missing arguments
SECTION("Missing argument to -am")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-am"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-am requires an argument"));
}
SECTION("Missing argument to -ats")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-ats"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-ats requires an argument"));
}
SECTION("Missing argument to -arr")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-arr"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-arr requires an argument"));
}
SECTION("Missing argument to -afov")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-afov"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-afov requires an argument"));
}
SECTION("Missing argument to -asteps")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-asteps"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-asteps requires an argument"));
}
SECTION("Missing argument to -alife")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-alife"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-alife requires an argument"));
}
SECTION("Missing argument to -alife")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-alife"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-alife requires an argument"));
}
SECTION("Missing argument to -alocseed")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-alocseed"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-alocseed requires an argument"));
}
SECTION("Missing argument to -alocfile")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-alocfile"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-alocfile requires an argument"));
}
SECTION("Missing argument to -aloc")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-aloc"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-aloc requires an argument"));
}
// rubbish input
SECTION("Non-numeric input to -ats")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-ats", "foo"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-ats must be a number >0, got foo"));
}
SECTION("Non-numeric input to -arr")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-arr", "foo"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-arr must be a number >0, got foo"));
}
SECTION("Non-numeric input to -atrails")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-atrails", "foo"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-atrails must be a number >=1 or 0 for all (max possible = 50), got foo"));
}
SECTION("Non-numeric input to -afov")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-afov", "foo"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-afov must be a number between 1 and 32, got foo"));
}
SECTION("Out of range input to -afov (0)")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-afov", "0"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-afov must be a number between 1 and 32, got 0"));
}
SECTION("Out of range input to -afov (33)")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-afov", "33"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-afov must be a number between 1 and 32, got 33"));
}
SECTION("Non-numeric input to -asteps")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-asteps", "foo"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-asteps must be a number >0, got foo"));
}
SECTION("Non-numeric input to -alife")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-alife", "foo"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-alife must be a number >0, got foo"));
}
SECTION("Rubbish input to -alocseed")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-alocseed", "foo"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Invalid starting location seed provided (foo). Should only contain digits"));
}
SECTION("Rubbish input to -aloc")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-aloc", "foo"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Invalid starting point provided (foo). Should only contain digits dots and commas"));
}
SECTION("Define graph output twice")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-ot", "graph", "-ot", "graph"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Same output type argument (graph) provided twice"));
}
SECTION("Define gatecounts output twice")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-ot", "gatecounts", "-ot", "gatecounts"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Same output type argument (gatecounts) provided twice"));
}
SECTION("Define trails output twice")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-ot", "trails", "-ot", "trails"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Same output type argument (trails) provided twice"));
}
}
TEST_CASE("AgentParserInputFail", "Bad or missing input")
{
SECTION("-ats not provided")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alife", "1000", "-alocseed", "0"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Total number of timesteps (-ats <timesteps>) is required"));
}
SECTION("-arr not provided")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-ats", "5000", "-afov","15", "-asteps", "3", "-alife", "1000", "-alocseed", "0"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Release rate (-arr <rate>) is required"));
}
SECTION("-afov not provided")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-ats", "5000", "-arr", "0.1", "-asteps", "3", "-alife", "1000", "-alocseed", "0"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Agent field-of-view (-afov <bins>) is required"));
}
SECTION("-asteps not provided")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-ats", "5000", "-arr", "0.1", "-afov","15", "-alife", "1000", "-alocseed", "0"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Agent number of steps before turn decision (-asteps <steps>) is required"));
}
SECTION("-alife not provided")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-ats", "5000", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alocseed", "0"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Agent life in timesteps (-alife <timesteps>) is required"));
}
SECTION("No random starting poins, manual points or point file provided")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-ats", "5000", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alife", "1000"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Either -aloc, -alocfile or -alocseed must be given"));
}
SECTION("Manual points and pointfile provided")
{
AgentParser parser;
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f("testpoints.csv");
f << "x\ty\n1\t2\n" << std::flush;
}
ArgumentHolder ah{"prog", "-aloc", "0.1,5.2", "-alocfile", "testpoints.csv", "-ats", "5000", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alife", "1000"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-alocfile cannot be used together with -aloc"));
}
SECTION("Pointfile and manual points provided")
{
AgentParser parser;
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f("testpoints.csv");
f << "x\ty\n1\t2\n" << std::flush;
}
ArgumentHolder ah{"prog", "-alocfile", "testpoints.csv", "-aloc", "0.1,5.2", "-ats", "5000", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alife", "1000"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-aloc cannot be used together with -alocfile"));
}
SECTION("Manual points and random points provided")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-aloc", "0.1,5.2", "-alocseed", "0", "-ats", "5000", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alife", "1000"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-alocseed cannot be used together with -aloc"));
}
SECTION("Pointfile and random points provided")
{
AgentParser parser;
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f("testpoints.csv");
f << "x\ty\n1\t2\n" << std::flush;
}
ArgumentHolder ah{"prog", "-alocfile", "testpoints.csv", "-alocseed", "0", "-ats", "5000", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alife", "1000"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-alocseed cannot be used together with -alocfile"));
}
SECTION("Random points and manual points provided")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-alocseed", "0", "-aloc", "0.1,5.2", "-ats", "5000", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alife", "1000"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-aloc cannot be used together with -alocseed"));
}
SECTION("Random points and Pointfile provided")
{
AgentParser parser;
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f("testpoints.csv");
f << "x\ty\n1\t2\n" << std::flush;
}
ArgumentHolder ah{"prog", "-alocseed", "0", "-alocfile", "testpoints.csv", "-ats", "5000", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alife", "1000"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-alocfile cannot be used together with -alocseed"));
}
SECTION("Non-existing file provided")
{
AgentParser parser;
ArgumentHolder ah{"prog", "-alocfile", "foo.csv", "-ats", "5000", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alife", "1000"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Failed to load file foo.csv, error"));
}
SECTION("Malformed pointfile")
{
AgentParser parser;
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f("testpoints.csv");
f << "x\ty\n1\n" << std::flush;
}
ArgumentHolder ah{"prog", "-alocfile", "testpoints.csv", "-ats", "5000", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alife", "1000"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Error parsing line: 1"));
}
SECTION("Malformed point arg")
{
AgentParser parser;
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f("testpoints.csv");
f << "x\ty\n1\n" << std::flush;
}
ArgumentHolder ah{"prog", "-aloc", "0.1", "-ats", "5000", "-arr", "0.1", "-afov","15", "-asteps", "3", "-alife", "1000"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Error parsing line: 0.1"));
}
}
TEST_CASE("AgentParserSuccess", "Read successfully")
{
AgentParser parser;
double x1 = 1.0;
double y1 = 2.0;
double x2 = 1.1;
double y2 = 1.2;
int totalTimeSteps = 5000;
std::stringstream ats;
ats << totalTimeSteps << std::flush;
double releaseRate = 0.1;
std::stringstream arr;
arr << releaseRate << std::flush;
int agentFOV = 15;
std::stringstream afov;
afov << agentFOV << std::flush;
int agentStepsBeforeTurnDecision = 3;
std::stringstream asteps;
asteps << agentStepsBeforeTurnDecision << std::flush;
int agentLifeTimesteps = 1000;
std::stringstream alife;
alife << agentLifeTimesteps << std::flush;
SECTION("Random starting locations (points vector should be empty, seed 0)")
{
ArgumentHolder ah{"prog", "-ats", ats.str(), "-arr", arr.str(), "-afov", afov.str(), "-asteps", asteps.str(), "-alife", alife.str(), "-alocseed", "0"};
parser.parse(ah.argc(), ah.argv());
auto points = parser.getReleasePoints();
REQUIRE(points.size() == 0);
REQUIRE(parser.randomReleaseLocationSeed() == 0);
}
SECTION("Random starting locations (points vector should be empty, seed 1)")
{
ArgumentHolder ah{"prog", "-ats", ats.str(), "-arr", arr.str(), "-afov", afov.str(), "-asteps", asteps.str(), "-alife", alife.str(), "-alocseed", "1"};
parser.parse(ah.argc(), ah.argv());
auto points = parser.getReleasePoints();
REQUIRE(points.size() == 0);
REQUIRE(parser.randomReleaseLocationSeed() == 1);
}
SECTION("Read from commandline")
{
std::stringstream p1;
p1 << x1 << "," << y1 << std::flush;
std::stringstream p2;
p2 << x2 << "," << y2 << std::flush;
ArgumentHolder ah{"prog", "-ats", ats.str(), "-arr", arr.str(), "-afov", afov.str(), "-asteps", asteps.str(), "-alife", alife.str(), "-aloc", p1.str(), "-aloc", p2.str()};
parser.parse(ah.argc(), ah.argv());
auto points = parser.getReleasePoints();
REQUIRE(points.size() == 2);
REQUIRE(points[0].x == Approx(x1));
REQUIRE(points[0].y == Approx(y1));
REQUIRE(points[1].x == Approx(x2));
REQUIRE(points[1].y == Approx(y2));
}
SECTION("Read from file")
{
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f(scf.Filename().c_str());
f << "x\ty\n" << x1 << "\t" << y1 << "\n"
<< x2 << "\t" << y2 << "\n" << std::flush;
}
ArgumentHolder ah{"prog", "-ats", ats.str(), "-arr", arr.str(), "-afov", afov.str(), "-asteps", asteps.str(), "-alife", alife.str(), "-alocfile", scf.Filename()};
parser.parse(ah.argc(), ah.argv() );
auto points = parser.getReleasePoints();
REQUIRE(points.size() == 2);
REQUIRE(points[0].x == Approx(x1));
REQUIRE(points[0].y == Approx(y1));
REQUIRE(points[1].x == Approx(x2));
REQUIRE(points[1].y == Approx(y2));
}
SECTION("Output type not set")
{
ArgumentHolder ah{"prog", "-ats", ats.str(), "-arr", arr.str(), "-afov", afov.str(), "-asteps", asteps.str(), "-alife", alife.str(), "-alocseed", "0"};
parser.parse(ah.argc(), ah.argv());
auto outputTypes = parser.outputTypes();
REQUIRE(outputTypes.size() == 0);
}
SECTION("Set output type to graph")
{
ArgumentHolder ah{"prog", "-ats", ats.str(), "-arr", arr.str(), "-afov", afov.str(), "-asteps", asteps.str(), "-alife", alife.str(), "-alocseed", "0", "-ot", "graph"};
parser.parse(ah.argc(), ah.argv());
auto outputTypes = parser.outputTypes();
REQUIRE(outputTypes.size() == 1);
REQUIRE(outputTypes[0] == AgentParser::OutputType::GRAPH);
}
SECTION("Set output type to gatecounts")
{
ArgumentHolder ah{"prog", "-ats", ats.str(), "-arr", arr.str(), "-afov", afov.str(), "-asteps", asteps.str(), "-alife", alife.str(), "-alocseed", "0", "-ot", "gatecounts"};
parser.parse(ah.argc(), ah.argv());
auto outputTypes = parser.outputTypes();
REQUIRE(outputTypes.size() == 1);
REQUIRE(outputTypes[0] == AgentParser::OutputType::GATECOUNTS);
}
SECTION("Set output type to trails")
{
ArgumentHolder ah{"prog", "-ats", ats.str(), "-arr", arr.str(), "-afov", afov.str(), "-asteps", asteps.str(), "-alife", alife.str(), "-alocseed", "0", "-atrails", "1", "-ot", "trails"};
parser.parse(ah.argc(), ah.argv());
auto noOfTrails = parser.recordTrailsForAgents();
REQUIRE(noOfTrails == 1);
auto outputTypes = parser.outputTypes();
REQUIRE(outputTypes.size() == 1);
REQUIRE(outputTypes[0] == AgentParser::OutputType::TRAILS);
}
SECTION("Set two output types")
{
ArgumentHolder ah{"prog", "-ats", ats.str(), "-arr", arr.str(), "-afov", afov.str(), "-asteps", asteps.str(), "-alife", alife.str(), "-alocseed", "0", "-ot", "graph", "-ot", "gatecounts"};
parser.parse(ah.argc(), ah.argv());
auto outputTypes = parser.outputTypes();
REQUIRE(outputTypes.size() == 2);
REQUIRE(outputTypes[0] == AgentParser::OutputType::GRAPH);
REQUIRE(outputTypes[1] == AgentParser::OutputType::GATECOUNTS);
}
REQUIRE(parser.totalSystemTimestemps() == totalTimeSteps);
REQUIRE(parser.releaseRate() == Approx(releaseRate));
REQUIRE(parser.agentFOV() == agentFOV);
REQUIRE(parser.agentStepsBeforeTurnDecision() == agentStepsBeforeTurnDecision);
REQUIRE(parser.agentLifeTimesteps() == agentLifeTimesteps);
}