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