forked from DEIS-Tools/MAES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoverageCalculatorTests.cs
205 lines (177 loc) · 7.84 KB
/
CoverageCalculatorTests.cs
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
// Copyright 2022 MAES
//
// This file is part of MAES
//
// MAES 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.
//
// MAES 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 MAES. If not, see http://www.gnu.org/licenses/.
//
// Contributors: Malte Z. Andreasen, Philip I. Holler and Magnus K. Jensen
//
// Original repository: https://github.com/MalteZA/MAES
using System.Collections.Generic;
using Maes.Map;
using Maes.Map.MapGen;
using Maes.Statistics;
using NUnit.Framework;
using UnityEngine;
using Random = System.Random;
namespace EditTests
{
public class CoverageCalculatorTest
{
private CoverageCalculator<ExplorationCell> _coverageCalculator;
private SimulationMap<Tile> _collisionMap;
private SimulationMap<ExplorationCell> _explorationMap;
private const int RandomSeed = 123;
private const int Width = 50, Height = 50;
[SetUp]
public void InitializeCalculatorAndMaps()
{
_collisionMap = GenerateCollisionMap();
_explorationMap = _collisionMap.FMap(tile => new ExplorationCell(!Tile.IsWall(tile.Type)));
_coverageCalculator = new CoverageCalculator<ExplorationCell>(_explorationMap, _collisionMap);
}
// Generates a collision map where only the edge tiles are solid
private static SimulationMap<Tile> GenerateCollisionMap()
{
var tiles = new SimulationMapTile<Tile>[Width, Height];
var random = new Random(RandomSeed);
var wall = Tile.GetRandomWall(random);
for (var x = 0; x < Width; x++)
{
for (var y = 0; y < Height; y++)
{
var tile = IsGeneratedTileSolid(new Vector2Int(x, y)) ? wall : new Tile(TileType.Room);
tiles[x, y] = new SimulationMapTile<Tile>(() => tile);
}
}
return new SimulationMap<Tile>(tiles, Vector2.zero);
}
// All edges are solid. All other tiles are non-solid
private static bool IsGeneratedTileSolid(Vector2Int tileCoordinate)
{
return (tileCoordinate.x == 0 ||
tileCoordinate.y == 0 ||
tileCoordinate.x == Width - 1 ||
tileCoordinate.y == Height - 1);
}
[Test]
public void RobotOnTopOfTileCoverageTest()
{
// The test robot is positioned in the middle of the coarse tile at coordinates (20, 20)
// (Equivalent to the slam tile at (40, 40)
var robotWorldPos = new Vector2(20.25f, 20.25f);
var ((_, cell1), (_, cell2)) = _explorationMap.GetMiniTilesByCoarseTileCoordinate(robotWorldPos);
// Assert that none of cells are covered in advance
Assert.IsFalse(cell1.IsCovered);
Assert.IsFalse(cell2.IsCovered);
// Register coverage for the testing robot
_coverageCalculator.UpdateRobotCoverage(robotWorldPos, 1, (_, _, _, _) =>
{
});
// Assert that the status of the tiles has now changed
Assert.IsTrue(cell1.IsCovered);
Assert.IsTrue(cell2.IsCovered);
}
[Test]
[TestCase(20.00f, 20.00f)]
[TestCase(20.5f, 20.5f)]
[TestCase(20.25f, 20.25f)]
[TestCase(20.75f, 20.75f)]
[TestCase(20.49f, 20.49f)]
[TestCase(20.99f, 20.99f)]
public void AdjacentTilesAreCoveredTest(float robotX, float robotY)
{
// The test robot is positioned in the middle of the coarse tile at coordinates (20, 20)
// (Equivalent to the slam tile at (40, 40)
var robotWorldPos = new Vector2(robotX, robotY);
// Find all cells that are immediate neighbours of tile currently occupied by the robot
var cells = new List<ExplorationCell>();
for (var x = -1; x < 1; x++)
{
for (var y = -1; y < 1; y++)
{
var xOffset = x * 0.5f;
var yOffset = y * 0.5f;
var ((_, cell1), (__, cell2)) = _explorationMap
.GetMiniTilesByCoarseTileCoordinate(robotWorldPos + new Vector2(xOffset, yOffset));
cells.Add(cell1);
cells.Add(cell2);
}
}
// Assert that none of cells are covered in advance
foreach (var cell in cells)
{
Assert.IsFalse(cell.IsCovered);
}
// Register coverage for the testing robot
_coverageCalculator.UpdateRobotCoverage(robotWorldPos, 1, (_, _, _, _) =>
{
});
// Assert that the status of the tiles has now changed
foreach (var cell in cells)
{
Assert.IsTrue(cell.IsCovered);
}
}
[Test]
public void CoverageTimeUpdateTest()
{
// The test robot is positioned in the middle of the coarse tile at coordinates (20, 20)
// (Equivalent to the slam tile at (40, 40)
var robotWorldPos = new Vector2(20.25f, 20.25f);
var ((_, cell1), (_, cell2)) = _explorationMap.GetMiniTilesByCoarseTileCoordinate(robotWorldPos);
const int coverageTick = 123456;
// Register coverage for the testing robot
_coverageCalculator.UpdateRobotCoverage(robotWorldPos, coverageTick, (_, _, _, _) =>
{
});
// Assert the the coverage time is updated
Assert.AreEqual(cell1.LastCoverageTimeInTicks, coverageTick);
Assert.AreEqual(cell2.LastCoverageTimeInTicks, coverageTick);
// Cover again at one tick later
_coverageCalculator.UpdateRobotCoverage(robotWorldPos, coverageTick + 1, (_, _, _, _) =>
{
});
Assert.AreEqual(cell1.LastCoverageTimeInTicks, coverageTick + 1);
Assert.AreEqual(cell2.LastCoverageTimeInTicks, coverageTick + 1);
}
[Test]
public void TilesAreMarkedCoverableCorrectly()
{
// Copy the existing exploration map and to get a new map where all CanBeCovered flags are true
var freshExplorationMap = _explorationMap.FMap((cell) => new ExplorationCell(cell.IsExplorable));
for (var x = 0; x < Width; x++)
{
for (var y = 0; y < Height; y++)
{
Assert.IsTrue(freshExplorationMap.GetTileByLocalCoordinate(x, y).IsTrueForAll(cell => cell.CanBeCovered));
}
}
new CoverageCalculator<ExplorationCell>(freshExplorationMap, _collisionMap);
// Pass the new exploration map to the coverage calculator which should cause the solid tiles to be
// marked as non-coverable
for (var x = 0; x < Width; x++)
{
for (var y = 0; y < Height; y++)
{
// We now expect the tiles have a 'CanBeCovered' status that is opposite to the solid status of tile
// (ie. solid tiles cannot be covered and non-solid ones can be covered)
var isSolid = IsGeneratedTileSolid(new Vector2Int(x, y));
Assert.IsTrue(freshExplorationMap.GetTileByLocalCoordinate(x, y)
.IsTrueForAll(cell => cell.CanBeCovered != isSolid));
}
}
}
}
}