| | 1 | | using NUnit.Framework; |
| | 2 | |
|
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.TestTools; |
| | 5 | |
|
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Diagnostics; |
| | 8 | | using System.Collections; |
| | 9 | |
|
| | 10 | | namespace DroneGame.Tests |
| | 11 | | { |
| | 12 | | public class TestGrid |
| | 13 | | { |
| | 14 | | Grid _grid; |
| | 15 | |
|
| | 16 | | [OneTimeSetUp] |
| | 17 | | public void GlobalSetup() |
| 1 | 18 | | { |
| 1 | 19 | | var gridPrefab = Resources.Load<Grid>("Grid") ?? throw new("Resource Grid Not found"); |
| 1 | 20 | | _grid = Object.Instantiate(gridPrefab); |
| 1 | 21 | | } |
| | 22 | |
|
| | 23 | | [UnityTest] |
| | 24 | | public IEnumerator TestGridCreation() |
| 1 | 25 | | { |
| 1 | 26 | | while(_grid.parsed == null) yield return null; |
| | 27 | |
|
| 131 | 28 | | foreach (var currTile in _grid.parsed) |
| 64 | 29 | | { |
| 64 | 30 | | var currCoordinate = currTile.Key; |
| 64 | 31 | | var tileInGrid = _grid[currCoordinate]; |
| | 32 | |
|
| 64 | 33 | | Assert.True(char.IsLetter(currCoordinate[0])); |
| 64 | 34 | | Assert.True(char.IsNumber(currCoordinate[1])); |
| | 35 | |
|
| 64 | 36 | | var neighbors = tileInGrid.neighbors; |
| | 37 | |
|
| 64 | 38 | | Assert.IsNotEmpty(neighbors); |
| 64 | 39 | | Assert.False(neighbors.ContainsKey(currCoordinate)); |
| | 40 | |
|
| 640 | 41 | | foreach (var neighbor in currTile.Value) |
| 224 | 42 | | { |
| 224 | 43 | | var neighborCoordinate = neighbor.Key; |
| | 44 | |
|
| 224 | 45 | | Assert.Contains(neighborCoordinate, neighbors.Keys); |
| | 46 | |
|
| | 47 | | // no need to test for the time because the parser automatically raises an error if it were to be invalid |
| 224 | 48 | | Assert.True(char.IsLetter(neighborCoordinate[0])); |
| 224 | 49 | | Assert.True(char.IsNumber(neighborCoordinate[1])); |
| 224 | 50 | | } |
| 64 | 51 | | } |
| 1 | 52 | | } |
| | 53 | |
|
| | 54 | | [UnityTest] |
| | 55 | | public IEnumerator TestShortestPath() |
| 1 | 56 | | { |
| 1 | 57 | | while(_grid.parsed == null) yield return null; |
| | 58 | |
|
| 1 | 59 | | var A1ToA1 = new List<string>() { "A1" }; |
| 1 | 60 | | var A1ToA3 = new List<string>() { "A1", "A2", "A3" }; |
| 1 | 61 | | var A1ToA4 = new List<string>() { "A1", "A2", "A3", "A4" }; |
| | 62 | |
|
| | 63 | | // Where we going, we don't need diagonals |
| | 64 | | // At first, I tried with diagonals, but my own code reminded that there were no diagonals in the API |
| 1 | 65 | | var A1ToH8 = new List<string>(){"A1", "B1", "C1", "C2", "C3", "D3", "E3", |
| | 66 | | "F3", "F4", "F5", "F6", "G6", "G7", "H7", |
| | 67 | | "H8"}; |
| | 68 | |
|
| | 69 | | // Nothing is done, A1 needs to be returned immediately |
| 1 | 70 | | var calculatedPath = _grid.GetShortestPath("A1", "A1").path; |
| 1 | 71 | | Assert.AreEqual(A1ToA1, calculatedPath); |
| | 72 | |
|
| 1 | 73 | | var watch = Stopwatch.StartNew(); |
| | 74 | | // Now the algorithm needs to work. |
| 1 | 75 | | calculatedPath = _grid.GetShortestPath("A1", "A3").path; |
| 1 | 76 | | Assert.AreEqual(A1ToA3, calculatedPath); |
| 1 | 77 | | watch.Stop(); |
| | 78 | |
|
| 1 | 79 | | var firstRunTime = watch.ElapsedMilliseconds; |
| | 80 | |
|
| 1 | 81 | | watch = Stopwatch.StartNew(); |
| | 82 | | // This one must be executed faster than the first time because of cache |
| 1 | 83 | | calculatedPath = _grid.GetShortestPath("A1", "A3").path; |
| 1 | 84 | | Assert.AreEqual(A1ToA3, calculatedPath); |
| 1 | 85 | | watch.Stop(); |
| 1 | 86 | | Assert.Less(watch.ElapsedMilliseconds, firstRunTime); |
| | 87 | |
|
| 1 | 88 | | calculatedPath = _grid.GetShortestPath("A1", "A4").path; |
| 1 | 89 | | Assert.AreEqual(A1ToA4, calculatedPath); |
| | 90 | |
|
| 1 | 91 | | calculatedPath = _grid.GetShortestPath("A1", "H8").path; |
| 1 | 92 | | Assert.AreEqual(A1ToH8, calculatedPath); |
| | 93 | |
|
| 1 | 94 | | calculatedPath = _grid.GetShortestPath(new string[] { "A1", "H8" }).path; |
| 1 | 95 | | Assert.AreEqual(A1ToH8, calculatedPath); |
| | 96 | |
|
| 1 | 97 | | calculatedPath = _grid.GetShortestPath(new string[] { "A1", "F5", "H8" }).path; |
| 1 | 98 | | Assert.AreEqual(A1ToH8, calculatedPath); |
| | 99 | |
|
| 1 | 100 | | calculatedPath = _grid.GetShortestPath(new string[] { "A1", "F5", "G6", "H8" }).path; |
| 1 | 101 | | Assert.AreEqual(A1ToH8, calculatedPath); |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | [UnityTest] |
| | 105 | | public IEnumerator TestDroneMove() |
| 1 | 106 | | { |
| 12207 | 107 | | while(_grid.parsed == null) yield return null; |
| | 108 | |
|
| 1 | 109 | | var dronePrefab = Resources.Load<Drone>("Drone/Drone") ?? throw new("Resource Drone Not found"); |
| 1 | 110 | | var drone = Object.Instantiate(dronePrefab); |
| | 111 | |
|
| | 112 | |
|
| 1 | 113 | | var first = _grid["A1"]; |
| 1 | 114 | | var second = _grid["A2"]; |
| | 115 | |
|
| 1 | 116 | | drone.StartCoroutine(drone.FollowPath(new(){first, second})); |
| 1 | 117 | | } |
| | 118 | | } |
| | 119 | | } |