Skip to content

Commit

Permalink
Format all files
Browse files Browse the repository at this point in the history
  • Loading branch information
mads256h committed Nov 11, 2024
1 parent 34e6023 commit d5f7be5
Show file tree
Hide file tree
Showing 105 changed files with 1,789 additions and 594 deletions.
22 changes: 13 additions & 9 deletions Assets/EditTests/CoverageCalculatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public void InitializeCalculatorAndMaps()
// Generates a collision map where only the edge tiles are solid
private static SimulationMap<Tile> GenerateCollisionMap()
{
SimulationMapTile<Tile>[,] tiles = new SimulationMapTile<Tile>[Width, Height];
var tiles = new SimulationMapTile<Tile>[Width, Height];
Tile.Rand = new Random(RandomSeed);
var wall = Tile.GetRandomWall();
for (int x = 0; x < Width; x++)
for (var x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
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);
Expand Down Expand Up @@ -115,9 +115,9 @@ public void AdjacentTilesAreCoveredTest(float robotX, float robotY)

// Find all cells that are immediate neighbours of tile currently occupied by the robot
var cells = new List<ExplorationCell>();
for (int x = -1; x < 1; x++)
for (var x = -1; x < 1; x++)
{
for (int y = -1; y < 1; y++)
for (var y = -1; y < 1; y++)
{
var xOffset = x * 0.5f;
var yOffset = y * 0.5f;
Expand All @@ -130,7 +130,9 @@ public void AdjacentTilesAreCoveredTest(float robotX, float robotY)

// 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, (_, _, _, _) =>
Expand All @@ -139,7 +141,9 @@ public void AdjacentTilesAreCoveredTest(float robotX, float robotY)

// Assert that the status of the tiles has now changed
foreach (var cell in cells)
{
Assert.IsTrue(cell.IsCovered);
}
}


Expand Down Expand Up @@ -174,9 +178,9 @@ 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 (int x = 0; x < Width; x++)
for (var x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
for (var y = 0; y < Height; y++)
{
Assert.IsTrue(freshExplorationMap.GetTileByLocalCoordinate(x, y).IsTrueForAll(cell => cell.CanBeCovered));
}
Expand All @@ -185,9 +189,9 @@ public void TilesAreMarkedCoverableCorrectly()
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 (int x = 0; x < Width; x++)
for (var x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
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)
Expand Down
47 changes: 31 additions & 16 deletions Assets/PlayModeTests/CommunicationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public class CommunicationTest

private Tile[,] GenerateMapWithHorizontalWallInMiddle(int wallThicknessInTiles)
{
Tile[,] bitmap = new Tile[MapWidth, MapHeight];
int firstWallRowY = MapHeight / 2;
int lastWallRowY = firstWallRowY + wallThicknessInTiles;
var bitmap = new Tile[MapWidth, MapHeight];
var firstWallRowY = MapHeight / 2;
var lastWallRowY = firstWallRowY + wallThicknessInTiles;
Tile.Rand = new Random(RandomSeed);
var wall = Tile.GetRandomWall();

for (int x = 0; x < MapWidth; x++)
for (var x = 0; x < MapWidth; x++)
{
for (int y = 0; y < MapHeight; y++)
for (var y = 0; y < MapHeight; y++)
{
var isSolid = y >= firstWallRowY && y <= lastWallRowY - 1;
bitmap[x, y] = isSolid ? wall : new Tile(TileType.Room);
Expand Down Expand Up @@ -97,7 +97,10 @@ private void InitSimulator(MapFactory mapFactory,
// The first robot will broadcast immediatealy
_robotTestAlgorithms[0].UpdateFunction = (tick, controller) =>
{
if (tick == 0) controller.Broadcast("Test Message");
if (tick == 0)
{
controller.Broadcast("Test Message");
}
};

// The second robot will continuously receive broadcasts
Expand All @@ -116,19 +119,25 @@ public IEnumerator Broadcast_TransmissionSuccessTest()
new List<Vector2Int> { new(2, 2), new(6, 6) });

string receivedMessage = null;
string sentMessage = "message sent between robots 1 and 2";
var sentMessage = "message sent between robots 1 and 2";
var algorithm1 = _robotTestAlgorithms[0];
var algorithm2 = _robotTestAlgorithms[1];

algorithm1.UpdateFunction = (tick, controller) =>
{
if (tick == 0) controller.Broadcast(sentMessage);
if (tick == 0)
{
controller.Broadcast(sentMessage);
}
};

algorithm2.UpdateFunction = (_, controller) =>
{
var results = controller.ReceiveBroadcast();
if (results.Count != 0) receivedMessage = results[0] as string;
if (results.Count != 0)
{
receivedMessage = results[0] as string;
}
};

_maes.PressPlayButton();
Expand All @@ -149,19 +158,25 @@ public IEnumerator Broadcast_TransmissionFailedTest()
new List<Vector2Int> { new(2, 2), new(6, 6) });

string receivedMessage = null;
string sentMessage = "message sent between robots 1 and 2";
var sentMessage = "message sent between robots 1 and 2";
var algorithm1 = _robotTestAlgorithms[0];
var algorithm2 = _robotTestAlgorithms[1];

algorithm1.UpdateFunction = (tick, controller) =>
{
if (tick == 0) controller.Broadcast(sentMessage);
if (tick == 0)
{
controller.Broadcast(sentMessage);
}
};

algorithm2.UpdateFunction = (_, controller) =>
{
var results = controller.ReceiveBroadcast();
if (results.Count != 0) receivedMessage = results[0] as string;
if (results.Count != 0)
{
receivedMessage = results[0] as string;
}
};

_maes.PressPlayButton();
Expand All @@ -177,7 +192,7 @@ public IEnumerator Broadcast_TransmissionFailedTest()
[Test(ExpectedResult = null)]
public IEnumerator Broadcast_NoWallsCommunicationTest()
{
float foundWallDistance = float.PositiveInfinity;
var foundWallDistance = float.PositiveInfinity;

InitSimulator(StandardTestingConfiguration.EmptyCaveMapSpawner(RandomSeed),
(_, wallDistance) =>
Expand Down Expand Up @@ -206,10 +221,10 @@ public IEnumerator Broadcast_NoWallsCommunicationTest()
[TestCase(-5, -5, ExpectedResult = null)]
public IEnumerator Broadcast_CorrectDistanceCalculation(int secondRobotX, int secondRobotY)
{
float transmissionDistance = float.PositiveInfinity;
var transmissionDistance = float.PositiveInfinity;
var firstRobotPosition = new Vector2Int(0, 0);
var secondRobotPosition = new Vector2Int(secondRobotX, secondRobotY);
float actualDistance = (firstRobotPosition - secondRobotPosition).magnitude;
var actualDistance = (firstRobotPosition - secondRobotPosition).magnitude;

InitSimulator(StandardTestingConfiguration.EmptyCaveMapSpawner(RandomSeed),
(distance, _) =>
Expand Down Expand Up @@ -238,7 +253,7 @@ public IEnumerator Broadcast_CorrectDistanceCalculation(int secondRobotX, int se
[TestCase(10, ExpectedResult = null)]
public IEnumerator Broadcast_WallDistanceIsApproximatelyCorrect(int wallThickness)
{
float foundWallDistance = float.PositiveInfinity;
var foundWallDistance = float.PositiveInfinity;
InitSimulator(
generator => generator.GenerateMap(GenerateMapWithHorizontalWallInMiddle(wallThickness), RandomSeed, borderSize: 2),
transmissionSuccessCalculatorFunc:
Expand Down
21 changes: 17 additions & 4 deletions Assets/PlayModeTests/MaterialCommunicationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ private void InitSimulator(MapFactory mapFactory,
// The first robot will broadcast immediately
_robotTestAlgorithms[0].UpdateFunction = (tick, controller) =>
{
if (tick == 0) controller.Broadcast("Test Message");
if (tick == 0)
{
controller.Broadcast("Test Message");
}
};

// The second robot will continuously receive broadcasts
Expand All @@ -148,13 +151,19 @@ public IEnumerator Broadcast_TransmissionSuccessTest()

algorithm1.UpdateFunction = (tick, controller) =>
{
if (tick == 0) controller.Broadcast(sentMessage);
if (tick == 0)
{
controller.Broadcast(sentMessage);
}
};

algorithm2.UpdateFunction = (_, controller) =>
{
var results = controller.ReceiveBroadcast();
if (results.Count != 0) receivedMessage = results[0] as string;
if (results.Count != 0)
{
receivedMessage = results[0] as string;
}
};

_maes.PressPlayButton();
Expand Down Expand Up @@ -193,14 +202,18 @@ public IEnumerator Broadcast_TransmissionFailedTest()
algorithm1.UpdateFunction = (tick, controller) =>
{
if (tick == 0)
{
controller.Broadcast(sentMessage);
}
};

algorithm2.UpdateFunction = (_, controller) =>
{
var results = controller.ReceiveBroadcast();
if (results.Count != 0)
{
receivedMessage = results[0] as string;
}
};

_maes.PressPlayButton();
Expand Down Expand Up @@ -307,7 +320,7 @@ public IEnumerator Broadcast_CorrectDistanceCalculation(int secondRobotX, int se
[TestCase(10, ExpectedResult = null)]
public IEnumerator Broadcast_WallDistanceIsApproximatelyCorrect(int wallThickness)
{
float foundWallDistance = float.PositiveInfinity;
var foundWallDistance = float.PositiveInfinity;
InitSimulator(
generator => generator.GenerateMap(GenerateMapWithHorizontalWallInMiddle(wallThickness), RandomSeed, borderSize: 2),
new List<Vector2Int> { new(0, -2), new(0, 3 + wallThickness) },
Expand Down
3 changes: 3 additions & 0 deletions Assets/PlayModeTests/MinotaurDoorwayTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ private void InitSimulator(string mapName, List<Vector2Int> robotSpawnPositions)
private IEnumerator AssertDoorsWhenFinished(int doorAmount)
{
if (_explorationSimulation.SimulatedLogicTicks > 36000)
{
yield return false;
}

while (_explorationSimulation.ExplorationTracker.ExploredProportion < 0.999f)
{
yield return null;
Expand Down
25 changes: 20 additions & 5 deletions Assets/PlayModeTests/Robot2DControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public IEnumerator MoveTo_IsDistanceCorrectTest(float movementDistance)
{
_testAlgorithm.UpdateFunction = (tick, controller) =>
{
if (tick == 0) controller.Move(movementDistance);
if (tick == 0)
{
controller.Move(movementDistance);
}
};
var controller = _robot.Controller;

Expand All @@ -114,7 +117,10 @@ public IEnumerator MoveTo_IsDistanceCorrectTest(float movementDistance)
// Wait 1 second (10 ticks) for the robot to stand completely still
var movementTaskEndTick = _simulationBase.SimulatedLogicTicks;
const int ticksToWait = 10;
while (_simulationBase.SimulatedLogicTicks < movementTaskEndTick + ticksToWait) yield return null;
while (_simulationBase.SimulatedLogicTicks < movementTaskEndTick + ticksToWait)
{
yield return null;
}

// Assert that the actual final position approximately matches the expected final position
var endingPosition = _robot.transform.position;
Expand All @@ -136,24 +142,33 @@ public IEnumerator MoveTo_IsDistanceCorrectTest(float movementDistance)
[TestCase(-180.0f, ExpectedResult = null)]
public IEnumerator Rotate_RotatesCorrectAmountOfDegrees(float degreesToRotate)
{
_testAlgorithm.UpdateFunction = (tick, controller) => { if (tick == 1) controller.Rotate(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;
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 = _simulationBase.SimulatedLogicTicks;
const int ticksToWait = 10;
while (_simulationBase.SimulatedLogicTicks < movementTaskEndTick + ticksToWait) yield return null;
while (_simulationBase.SimulatedLogicTicks < movementTaskEndTick + ticksToWait)
{
yield return null;
}

// Assert that the actual final rotation approximately matches the expected angle
var actualAngle = _robot.transform.rotation.eulerAngles.z;
Expand Down
2 changes: 1 addition & 1 deletion Assets/PlayModeTests/TestingAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void UpdateLogic()

public void SetController(Robot2DController controller)
{
this.Controller = controller;
Controller = controller;
}

public string GetDebugInfo()
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/ExperimentSimulations/AvariceExperiment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void Start()

var random = new System.Random(1234);
var randNumbers = new List<int>();
for (int i = 0; i < 100; i++)
for (var i = 0; i < 100; i++)
{
var val = random.Next(0, 1000000);
randNumbers.Add(val);
Expand All @@ -107,7 +107,7 @@ private void Start()
var buildingConfigList50 = new List<BuildingMapConfig>();
var buildingConfigList75 = new List<BuildingMapConfig>();
var buildingConfigList100 = new List<BuildingMapConfig>();
foreach (int val in randNumbers)
foreach (var val in randNumbers)
{
buildingConfigList50.Add(new BuildingMapConfig(val, widthInTiles: 50, heightInTiles: 50));
buildingConfigList75.Add(new BuildingMapConfig(val, widthInTiles: 75, heightInTiles: 75));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void Start()

var mapFromFile = PgmMapFileLoader.LoadMapFromFileIfPresent("blank_100.pgm");
var random = new System.Random(1234);
for (int i = 0; i < 10; i++)
for (var i = 0; i < 10; i++)
{
var val = random.Next(0, 1000000);
simulator.EnqueueScenario(new MySimulationScenario(val,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private void Start()
var simulator = new MySimulator();
var random = new System.Random(12345);
var randNumbers = new List<int>();
for (int i = 0; i < 100; i++)
for (var i = 0; i < 100; i++)
{
var val = random.Next(0, 1000000);
randNumbers.Add(val);
Expand All @@ -116,7 +116,7 @@ private void Start()
var robotConstraints = constraintsDict[constraintName];

var buildingConfigList100 = new List<BuildingMapConfig>();
foreach (int val in randNumbers)
foreach (var val in randNumbers)
{
buildingConfigList100.Add(new BuildingMapConfig(val, widthInTiles: 100, heightInTiles: 100));
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/ExperimentSimulations/ExampleProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void Start()
var simulator = new MySimulator();
var random = new System.Random(1234);
var randNumbers = new List<int>();
for (int i = 0; i < 100; i++)
for (var i = 0; i < 100; i++)
{
var val = random.Next(0, 1000000);
randNumbers.Add(val);
Expand All @@ -107,7 +107,7 @@ private void Start()
var buildingConfigList50 = new List<BuildingMapConfig>();
var buildingConfigList75 = new List<BuildingMapConfig>();
var buildingConfigList100 = new List<BuildingMapConfig>();
foreach (int val in randNumbers)
foreach (var val in randNumbers)
{
buildingConfigList50.Add(new BuildingMapConfig(val, widthInTiles: 50, heightInTiles: 50));
buildingConfigList75.Add(new BuildingMapConfig(val, widthInTiles: 75, heightInTiles: 75));
Expand Down
Loading

0 comments on commit d5f7be5

Please sign in to comment.