| | | 1 | | using NUnit.Framework; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using UnityEngine; |
| | | 5 | | using UnityEngine.TestTools; |
| | | 6 | | |
| | | 7 | | public class ThreatAssignmentTests { |
| | | 8 | | [Test] |
| | 1 | 9 | | public void AssignNoInterceptors() { |
| | | 10 | | // Define the threat assignment. |
| | 1 | 11 | | IAssignment threatAssignment = new ThreatAssignment(); |
| | | 12 | | |
| | | 13 | | // Create interceptors. |
| | 1 | 14 | | List<Interceptor> interceptors = new List<Interceptor>(); |
| | | 15 | | |
| | | 16 | | // Create threats. |
| | 1 | 17 | | List<Threat> threats = |
| | | 18 | | new List<Threat> { new GameObject("Threat").AddComponent<RotaryWingThreat>() }; |
| | | 19 | | |
| | | 20 | | // Assign the interceptors to the threats. |
| | 1 | 21 | | LogAssert.Expect(LogType.Warning, "No assignable interceptors found."); |
| | 1 | 22 | | IEnumerable<IAssignment.AssignmentItem> assignments = |
| | | 23 | | threatAssignment.Assign(interceptors, threats); |
| | 1 | 24 | | Assert.AreEqual(0, assignments.Count(), "There should be no assignments."); |
| | 1 | 25 | | } |
| | | 26 | | |
| | | 27 | | [Test] |
| | 1 | 28 | | public void AssignNoThreats() { |
| | | 29 | | // Define the threat assignment. |
| | 1 | 30 | | IAssignment threatAssignment = new ThreatAssignment(); |
| | | 31 | | |
| | | 32 | | // Create interceptors. |
| | 1 | 33 | | List<Interceptor> interceptors = |
| | | 34 | | new List<Interceptor> { new GameObject("Interceptor").AddComponent<MissileInterceptor>() }; |
| | | 35 | | |
| | | 36 | | // Create threats. |
| | 1 | 37 | | List<Threat> threats = new List<Threat>(); |
| | | 38 | | |
| | | 39 | | // Assign the interceptors to the threats. |
| | 1 | 40 | | LogAssert.Expect(LogType.Warning, "No active threats found."); |
| | 1 | 41 | | IEnumerable<IAssignment.AssignmentItem> assignments = |
| | | 42 | | threatAssignment.Assign(interceptors, threats); |
| | 1 | 43 | | Assert.AreEqual(0, assignments.Count(), "There should be no assignments."); |
| | 1 | 44 | | } |
| | | 45 | | |
| | | 46 | | [Test] |
| | 1 | 47 | | public void AssignShouldAssignAllInterceptorsAndThreats() { |
| | | 48 | | // Define the threat assignment. |
| | 1 | 49 | | IAssignment threatAssignment = new ThreatAssignment(); |
| | | 50 | | |
| | | 51 | | // Create interceptors. |
| | 1 | 52 | | List<Interceptor> interceptors = new List<Interceptor> { |
| | | 53 | | new GameObject("Interceptor 1").AddComponent<MissileInterceptor>(), |
| | | 54 | | new GameObject("Interceptor 2").AddComponent<MissileInterceptor>(), |
| | | 55 | | new GameObject("Interceptor 3").AddComponent<MissileInterceptor>() |
| | | 56 | | }; |
| | | 57 | | |
| | | 58 | | // Create threats. |
| | 1 | 59 | | Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>(); |
| | 1 | 60 | | Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>(); |
| | 1 | 61 | | Threat threat3 = new GameObject("Threat 3").AddComponent<RotaryWingThreat>(); |
| | | 62 | | |
| | | 63 | | // Add rigid body components to threats to set velocities. |
| | 1 | 64 | | Rigidbody rb1 = threat1.gameObject.AddComponent<Rigidbody>(); |
| | 1 | 65 | | Rigidbody rb2 = threat2.gameObject.AddComponent<Rigidbody>(); |
| | 1 | 66 | | Rigidbody rb3 = threat3.gameObject.AddComponent<Rigidbody>(); |
| | | 67 | | |
| | | 68 | | // Set positions and velocities. |
| | 1 | 69 | | threat1.transform.position = Vector3.forward * -20f; |
| | 1 | 70 | | threat2.transform.position = Vector3.forward * -20f; |
| | 1 | 71 | | threat3.transform.position = Vector3.forward * -20f; |
| | | 72 | | |
| | 1 | 73 | | rb1.linearVelocity = Vector3.forward * 5f; |
| | 1 | 74 | | rb2.linearVelocity = Vector3.forward * 10f; |
| | 1 | 75 | | rb3.linearVelocity = Vector3.forward * 15f; |
| | | 76 | | |
| | | 77 | | // Create threats. |
| | 1 | 78 | | List<Threat> threats = new List<Threat> { threat1, threat2, threat3 }; |
| | | 79 | | |
| | | 80 | | // Assign the interceptors to the threats. |
| | 1 | 81 | | IEnumerable<IAssignment.AssignmentItem> assignments = |
| | | 82 | | threatAssignment.Assign(interceptors, threats); |
| | | 83 | | |
| | 1 | 84 | | Assert.AreEqual(3, assignments.Count(), "All interceptors should be assigned."); |
| | | 85 | | |
| | 1 | 86 | | HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>(); |
| | 1 | 87 | | HashSet<Threat> assignedThreats = new HashSet<Threat>(); |
| | | 88 | | |
| | 12 | 89 | | foreach (var assignment in assignments) { |
| | 3 | 90 | | Assert.IsNotNull(assignment.Interceptor, "Interceptor should not be null."); |
| | 3 | 91 | | Assert.IsNotNull(assignment.Threat, "Threat should not be null."); |
| | 3 | 92 | | assignedInterceptors.Add(assignment.Interceptor); |
| | 3 | 93 | | assignedThreats.Add(assignment.Threat); |
| | 3 | 94 | | } |
| | | 95 | | |
| | 1 | 96 | | Assert.AreEqual(3, assignedInterceptors.Count, "All interceptors should be unique."); |
| | 1 | 97 | | Assert.AreEqual(3, assignedThreats.Count, "All threats should be assigned."); |
| | | 98 | | |
| | | 99 | | // Verify that threats are assigned in order of their threat level (based on velocity and |
| | | 100 | | // distance). |
| | 1 | 101 | | var orderedAssignments = |
| | | 102 | | assignments |
| | 3 | 103 | | .OrderByDescending(a => a.Threat.GetVelocity().magnitude / |
| | | 104 | | Vector3.Distance(a.Threat.transform.position, Vector3.zero)) |
| | | 105 | | .ToList(); |
| | 1 | 106 | | Assert.AreEqual(threat3, orderedAssignments[0].Threat, |
| | | 107 | | "Highest threat should be assigned first."); |
| | 1 | 108 | | Assert.AreEqual(threat2, orderedAssignments[1].Threat, |
| | | 109 | | "Second highest threat should be assigned second."); |
| | 1 | 110 | | Assert.AreEqual(threat1, orderedAssignments[2].Threat, |
| | | 111 | | "Lowest threat should be assigned last."); |
| | 1 | 112 | | } |
| | | 113 | | |
| | | 114 | | [Test] |
| | 1 | 115 | | public void AssignShouldHandleMoreInterceptorsThanThreats() { |
| | | 116 | | // Define the threat assignment. |
| | 1 | 117 | | IAssignment threatAssignment = new ThreatAssignment(); |
| | | 118 | | |
| | | 119 | | // Create interceptors. |
| | 1 | 120 | | List<Interceptor> interceptors = new List<Interceptor> { |
| | | 121 | | new GameObject("Interceptor 1").AddComponent<MissileInterceptor>(), |
| | | 122 | | new GameObject("Interceptor 2").AddComponent<MissileInterceptor>(), |
| | | 123 | | new GameObject("Interceptor 3").AddComponent<MissileInterceptor>() |
| | | 124 | | }; |
| | | 125 | | |
| | | 126 | | // Create threats. |
| | 1 | 127 | | Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>(); |
| | 1 | 128 | | Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>(); |
| | | 129 | | |
| | | 130 | | // Add rigid body components to threats to set velocities. |
| | 1 | 131 | | Rigidbody rb1 = threat1.gameObject.AddComponent<Rigidbody>(); |
| | 1 | 132 | | Rigidbody rb2 = threat2.gameObject.AddComponent<Rigidbody>(); |
| | | 133 | | |
| | | 134 | | // Set positions and velocities. |
| | 1 | 135 | | threat1.transform.position = Vector3.up * 10f; |
| | 1 | 136 | | threat2.transform.position = Vector3.right * 5f; |
| | | 137 | | |
| | 1 | 138 | | rb1.linearVelocity = Vector3.forward * 10f; |
| | 1 | 139 | | rb2.linearVelocity = Vector3.forward * 15f; |
| | | 140 | | |
| | | 141 | | // Create threats. |
| | 1 | 142 | | List<Threat> threats = new List<Threat> { threat1, threat2 }; |
| | | 143 | | |
| | | 144 | | // Assign the interceptors to the threats. |
| | 1 | 145 | | IEnumerable<IAssignment.AssignmentItem> assignments = |
| | | 146 | | threatAssignment.Assign(interceptors, threats); |
| | | 147 | | |
| | 1 | 148 | | Assert.AreEqual(3, assignments.Count(), "All interceptors should be assigned."); |
| | | 149 | | |
| | 1 | 150 | | HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>(); |
| | 1 | 151 | | HashSet<Threat> assignedThreats = new HashSet<Threat>(); |
| | | 152 | | |
| | 12 | 153 | | foreach (var assignment in assignments) { |
| | 3 | 154 | | Assert.IsNotNull(assignment.Interceptor, "Interceptor should not be null."); |
| | 3 | 155 | | Assert.IsNotNull(assignment.Threat, "Threat should not be null."); |
| | 3 | 156 | | assignedInterceptors.Add(assignment.Interceptor); |
| | 3 | 157 | | assignedThreats.Add(assignment.Threat); |
| | 3 | 158 | | } |
| | | 159 | | |
| | 1 | 160 | | Assert.AreEqual(3, assignedInterceptors.Count, "All interceptors should be assigned."); |
| | 1 | 161 | | Assert.AreEqual(2, assignedThreats.Count, "Both threats should be assigned."); |
| | | 162 | | |
| | | 163 | | // Verify that threats are assigned in order of their threat level (based on velocity and |
| | | 164 | | // distance). |
| | 1 | 165 | | var orderedAssignments = |
| | | 166 | | assignments |
| | 3 | 167 | | .OrderByDescending(a => a.Threat.GetVelocity().magnitude / |
| | | 168 | | Vector3.Distance(a.Threat.transform.position, Vector3.zero)) |
| | | 169 | | .ToList(); |
| | 1 | 170 | | Assert.AreEqual(threat2, orderedAssignments[0].Threat, |
| | | 171 | | "Higher threat should be assigned first."); |
| | 1 | 172 | | Assert.AreEqual(threat2, orderedAssignments[1].Threat, |
| | | 173 | | "Higher threat should be assigned twice."); |
| | 1 | 174 | | Assert.AreEqual(threat1, orderedAssignments[2].Threat, "Lower threat should be assigned last."); |
| | 1 | 175 | | } |
| | | 176 | | } |