-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRobot2DControllerTest.cs
151 lines (132 loc) · 6.87 KB
/
Robot2DControllerTest.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
// Copyright 2024 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: Rasmus Borrisholt Schmidt, Andreas Sebastian Sørensen, Thor Beregaard, Malte Z. Andreasen, Philip I. Holler and Magnus K. Jensen,
//
// Original repository: https://github.com/Molitany/MAES
using System.Collections;
using Maes;
using Maes.ExplorationAlgorithm;
using Maes.ExplorationAlgorithm.RandomBallisticWalk;
using Maes.Map.MapGen;
using Maes.Robot;
using Maes.Robot.Task;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace PlayModeTests {
[TestFixture(1.0f)]
[TestFixture(1.5f)]
[TestFixture(0.5f)]
public class Robot2DControllerTest {
private const int RandomSeed = 123;
private Simulator _maes;
private TestingAlgorithm _testAlgorithm;
private Simulation _simulation;
private MonaRobot _robot;
private float _relativeMoveSpeed;
public Robot2DControllerTest(float relativeMoveSpeed) {
_relativeMoveSpeed = relativeMoveSpeed;
}
[SetUp]
public void InitializeTestingSimulator() {
var testingScenario = new SimulationScenario(RandomSeed,
mapSpawner: StandardTestingConfiguration.EmptyCaveMapSpawner(RandomSeed),
hasFinishedSim: simulation => false,
robotConstraints: new RobotConstraints(relativeMoveSpeed: _relativeMoveSpeed),
robotSpawner: (map, spawner) => spawner.SpawnRobotsTogether( map, RandomSeed, 1,
Vector2Int.zero, (robotSeed) => {
var algorithm = new TestingAlgorithm();
_testAlgorithm = algorithm;
return algorithm;
}));
_maes = Simulator.GetInstance();
_maes.EnqueueScenario(testingScenario);
_simulation = _maes.GetSimulationManager().CurrentSimulation;
_robot = _simulation.Robots[0];
}
[TearDown]
public void ClearSimulator() {
Simulator.Destroy();
}
// Test that the robot is able to move the given distance
[UnityTest]
[TestCase(1.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(2.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(5.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(10.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(20.0f, ExpectedResult = (IEnumerator) null)]
public IEnumerator MoveTo_IsDistanceCorrectTest(float movementDistance) {
_testAlgorithm.UpdateFunction = (tick, controller) => {
if (tick == 0) controller.Move(movementDistance);
};
var controller = _robot.Controller;
// Register the starting position and calculate the expected position
var transform = _robot.transform;
var startingPosition = transform.position;
var expectedEndingPosition = startingPosition + (controller.GetForwardDirectionVector() * movementDistance);
_maes.PressPlayButton();
// Wait until the robot has started and completed the movement task
while (_testAlgorithm.Tick < 10 || _testAlgorithm.Controller.GetStatus() != RobotStatus.Idle) {
yield return null;
}
// Wait 1 second (10 ticks) for the robot to stand completely still
var movementTaskEndTick = _simulation.SimulatedLogicTicks;
const int ticksToWait = 10;
while (_simulation.SimulatedLogicTicks < movementTaskEndTick + ticksToWait) yield return null;
// Assert that the actual final position approximately matches the expected final position
var endingPosition = _robot.transform.position;
const float maximumDeviation = 0.1f;
var targetPositionDelta = (expectedEndingPosition - endingPosition).magnitude;
Debug.Log($"Actual: {endingPosition} vs expected: {expectedEndingPosition}");
Assert.LessOrEqual(targetPositionDelta, maximumDeviation);
}
[UnityTest]
[TestCase(1.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(-1.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(2.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(5.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(10.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(20.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(-20.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(180.0f, ExpectedResult = (IEnumerator) null)]
[TestCase(-180.0f, ExpectedResult = (IEnumerator) null)]
public IEnumerator Rotate_RotatesCorrectAmountOfDegrees(float degreesToRotate) {
_testAlgorithm.UpdateFunction = (tick, controller) => { if(tick == 1) controller.Rotate(degreesToRotate); };
// Register the starting position and calculate the expected position
var transform = _robot.transform;
var startingRotation = transform.rotation.eulerAngles.z;
var expectedAngle = startingRotation + degreesToRotate;
while (expectedAngle < 0) expectedAngle += 360;
expectedAngle %= 360;
_maes.PressPlayButton();
// Wait until the robot has started and completed the movement task
while (_testAlgorithm.Tick < 10 || _testAlgorithm.Controller.GetStatus() != RobotStatus.Idle)
yield return null;
// Wait 1 second (10 ticks) for the robot to stand completely still
var movementTaskEndTick = _simulation.SimulatedLogicTicks;
const int ticksToWait = 10;
while (_simulation.SimulatedLogicTicks < movementTaskEndTick + ticksToWait) yield return null;
// Assert that the actual final rotation approximately matches the expected angle
var actualAngle = _robot.transform.rotation.eulerAngles.z;
const float maximumDeviationDegrees = 0.5f;
var targetPositionDelta = Mathf.Abs(expectedAngle - actualAngle);
Debug.Log($"Actual final angle: {actualAngle} vs expected angle: {expectedAngle}");
Assert.LessOrEqual(targetPositionDelta, maximumDeviationDegrees);
}
}
}