< Summary

Class:ThreatAssignmentTests
Assembly:bamlab.test.editmode
File(s):/github/workspace/Assets/Tests/EditMode/ThreatAssignmentTests.cs
Covered lines:63
Uncovered lines:0
Coverable lines:63
Total lines:141
Line coverage:100% (63 of 63)
Covered branches:0
Total branches:0
Covered methods:2
Total methods:2
Method coverage:100% (2 of 2)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Assign_Should_Assign_All_Interceptors_And_Threats()0%440100%
Assign_Should_Handle_More_Interceptors_Than_Threats()0%440100%

File(s)

/github/workspace/Assets/Tests/EditMode/ThreatAssignmentTests.cs

#LineLine coverage
 1using NUnit.Framework;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using UnityEngine.TestTools;
 5using System.Linq;
 6
 7public class ThreatAssignmentTests {
 8  [Test]
 19  public void Assign_Should_Assign_All_Interceptors_And_Threats() {
 10    // Arrange
 111    ThreatAssignment threatAssignment = new ThreatAssignment();
 12
 13    // Create interceptors
 114    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
 121    Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>();
 122    Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>();
 123    Threat threat3 = new GameObject("Threat 3").AddComponent<RotaryWingThreat>();
 24
 25    // Add Rigidbody components to threats to set velocities
 126    Rigidbody rb1 = threat1.gameObject.AddComponent<Rigidbody>();
 127    Rigidbody rb2 = threat2.gameObject.AddComponent<Rigidbody>();
 128    Rigidbody rb3 = threat3.gameObject.AddComponent<Rigidbody>();
 29
 30    // Set positions and velocities
 131    threat1.transform.position = Vector3.forward * -20f;
 132    threat2.transform.position = Vector3.forward * -20f;
 133    threat3.transform.position = Vector3.forward * -20f;
 34
 135    rb1.linearVelocity = Vector3.forward * 5f;
 136    rb2.linearVelocity = Vector3.forward * 10f;
 137    rb3.linearVelocity = Vector3.forward * 15f;
 38
 39    // Create threat data
 140    List<ThreatData> threats = new List<ThreatData> { new ThreatData(threat1, "Threat1ID"),
 41                                                      new ThreatData(threat2, "Threat2ID"),
 42                                                      new ThreatData(threat3, "Threat3ID") };
 43
 44    // Act
 145    IEnumerable<IAssignment.AssignmentItem> assignments =
 46        threatAssignment.Assign(interceptors, threats);
 47
 48    // Assert
 149    Assert.AreEqual(3, assignments.Count(), "All interceptors should be assigned");
 50
 151    HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
 152    HashSet<Threat> assignedThreats = new HashSet<Threat>();
 53
 1254    foreach (var assignment in assignments) {
 355      Assert.IsNotNull(assignment.Interceptor, "Interceptor should not be null");
 356      Assert.IsNotNull(assignment.Threat, "Threat should not be null");
 357      assignedInterceptors.Add(assignment.Interceptor);
 358      assignedThreats.Add(assignment.Threat);
 359    }
 60
 161    Assert.AreEqual(3, assignedInterceptors.Count, "All interceptors should be unique");
 162    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)
 166    var orderedAssignments =
 67        assignments
 368            .OrderByDescending(a => a.Threat.GetVelocity().magnitude /
 69                                    Vector3.Distance(a.Threat.transform.position, Vector3.zero))
 70            .ToList();
 171    Assert.AreEqual(threat3, orderedAssignments[0].Threat,
 72                    "Highest threat should be assigned first");
 173    Assert.AreEqual(threat2, orderedAssignments[1].Threat,
 74                    "Second highest threat should be assigned second");
 175    Assert.AreEqual(threat1, orderedAssignments[2].Threat, "Lowest threat should be assigned last");
 176  }
 77
 78  [Test]
 179  public void Assign_Should_Handle_More_Interceptors_Than_Threats() {
 80    // Arrange
 181    ThreatAssignment threatAssignment = new ThreatAssignment();
 82
 83    // Create interceptors
 184    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
 191    Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>();
 192    Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>();
 93
 94    // Add Rigidbody components to threats to set velocities
 195    Rigidbody rb1 = threat1.gameObject.AddComponent<Rigidbody>();
 196    Rigidbody rb2 = threat2.gameObject.AddComponent<Rigidbody>();
 97
 98    // Set positions and velocities
 199    threat1.transform.position = Vector3.up * 10f;
 1100    threat2.transform.position = Vector3.right * 5f;
 101
 1102    rb1.linearVelocity = Vector3.forward * 10f;
 1103    rb2.linearVelocity = Vector3.forward * 15f;
 104
 105    // Create threat data
 1106    List<ThreatData> threats = new List<ThreatData> { new ThreatData(threat1, "Threat1ID"),
 107                                                      new ThreatData(threat2, "Threat2ID") };
 108
 109    // Act
 1110    IEnumerable<IAssignment.AssignmentItem> assignments =
 111        threatAssignment.Assign(interceptors, threats);
 112
 113    // Assert
 1114    Assert.AreEqual(2, assignments.Count(), "All threats should be assigned");
 115
 1116    HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
 1117    HashSet<Threat> assignedThreats = new HashSet<Threat>();
 118
 9119    foreach (var assignment in assignments) {
 2120      Assert.IsNotNull(assignment.Interceptor, "Interceptor should not be null");
 2121      Assert.IsNotNull(assignment.Threat, "Threat should not be null");
 2122      assignedInterceptors.Add(assignment.Interceptor);
 2123      assignedThreats.Add(assignment.Threat);
 2124    }
 125
 1126    Assert.AreEqual(2, assignedInterceptors.Count, "Two interceptors should be assigned");
 1127    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)
 1131    var orderedAssignments =
 132        assignments
 2133            .OrderByDescending(a => a.Threat.GetVelocity().magnitude /
 134                                    Vector3.Distance(a.Threat.transform.position, Vector3.zero))
 135            .ToList();
 1136    Assert.AreEqual(threat2, orderedAssignments[0].Threat,
 137                    "Higher threat should be assigned first");
 1138    Assert.AreEqual(threat1, orderedAssignments[1].Threat,
 139                    "Lower threat should be assigned second");
 1140  }
 141}