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