< Summary

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

Metrics

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

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]
 09  public void Assign_Should_Assign_All_Interceptors_And_Threats() {
 10    // Arrange
 011    ThreatAssignment threatAssignment = new ThreatAssignment();
 12
 13    // Create interceptors
 014    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
 021    Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>();
 022    Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>();
 023    Threat threat3 = new GameObject("Threat 3").AddComponent<RotaryWingThreat>();
 24
 25    // Add Rigidbody components to threats to set velocities
 026    Rigidbody rb1 = threat1.gameObject.AddComponent<Rigidbody>();
 027    Rigidbody rb2 = threat2.gameObject.AddComponent<Rigidbody>();
 028    Rigidbody rb3 = threat3.gameObject.AddComponent<Rigidbody>();
 29
 30    // Set positions and velocities
 031    threat1.transform.position = Vector3.forward * -20f;
 032    threat2.transform.position = Vector3.forward * -20f;
 033    threat3.transform.position = Vector3.forward * -20f;
 34
 035    rb1.linearVelocity = Vector3.forward * 5f;
 036    rb2.linearVelocity = Vector3.forward * 10f;
 037    rb3.linearVelocity = Vector3.forward * 15f;
 38
 39    // Create threat data
 040    List<ThreatData> threats = new List<ThreatData> { new ThreatData(threat1, "Threat1ID"),
 41                                                      new ThreatData(threat2, "Threat2ID"),
 42                                                      new ThreatData(threat3, "Threat3ID") };
 43
 44    // Act
 045    IEnumerable<IAssignment.AssignmentItem> assignments =
 46        threatAssignment.Assign(interceptors, threats);
 47
 48    // Assert
 049    Assert.AreEqual(3, assignments.Count(), "All interceptors should be assigned");
 50
 051    HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
 052    HashSet<Threat> assignedThreats = new HashSet<Threat>();
 53
 054    foreach (var assignment in assignments) {
 055      Assert.IsNotNull(assignment.Interceptor, "Interceptor should not be null");
 056      Assert.IsNotNull(assignment.Threat, "Threat should not be null");
 057      assignedInterceptors.Add(assignment.Interceptor);
 058      assignedThreats.Add(assignment.Threat);
 059    }
 60
 061    Assert.AreEqual(3, assignedInterceptors.Count, "All interceptors should be unique");
 062    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)
 066    var orderedAssignments =
 67        assignments
 068            .OrderByDescending(a => a.Threat.GetVelocity().magnitude /
 69                                    Vector3.Distance(a.Threat.transform.position, Vector3.zero))
 70            .ToList();
 071    Assert.AreEqual(threat3, orderedAssignments[0].Threat,
 72                    "Highest threat should be assigned first");
 073    Assert.AreEqual(threat2, orderedAssignments[1].Threat,
 74                    "Second highest threat should be assigned second");
 075    Assert.AreEqual(threat1, orderedAssignments[2].Threat, "Lowest threat should be assigned last");
 076  }
 77
 78  [Test]
 079  public void Assign_Should_Handle_More_Interceptors_Than_Threats() {
 80    // Arrange
 081    ThreatAssignment threatAssignment = new ThreatAssignment();
 82
 83    // Create interceptors
 084    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
 091    Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>();
 092    Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>();
 93
 94    // Add Rigidbody components to threats to set velocities
 095    Rigidbody rb1 = threat1.gameObject.AddComponent<Rigidbody>();
 096    Rigidbody rb2 = threat2.gameObject.AddComponent<Rigidbody>();
 97
 98    // Set positions and velocities
 099    threat1.transform.position = Vector3.up * 10f;
 0100    threat2.transform.position = Vector3.right * 5f;
 101
 0102    rb1.linearVelocity = Vector3.forward * 10f;
 0103    rb2.linearVelocity = Vector3.forward * 15f;
 104
 105    // Create threat data
 0106    List<ThreatData> threats = new List<ThreatData> { new ThreatData(threat1, "Threat1ID"),
 107                                                      new ThreatData(threat2, "Threat2ID") };
 108
 109    // Act
 0110    IEnumerable<IAssignment.AssignmentItem> assignments =
 111        threatAssignment.Assign(interceptors, threats);
 112
 113    // Assert
 0114    Assert.AreEqual(2, assignments.Count(), "All threats should be assigned");
 115
 0116    HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
 0117    HashSet<Threat> assignedThreats = new HashSet<Threat>();
 118
 0119    foreach (var assignment in assignments) {
 0120      Assert.IsNotNull(assignment.Interceptor, "Interceptor should not be null");
 0121      Assert.IsNotNull(assignment.Threat, "Threat should not be null");
 0122      assignedInterceptors.Add(assignment.Interceptor);
 0123      assignedThreats.Add(assignment.Threat);
 0124    }
 125
 0126    Assert.AreEqual(2, assignedInterceptors.Count, "Two interceptors should be assigned");
 0127    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)
 0131    var orderedAssignments =
 132        assignments
 0133            .OrderByDescending(a => a.Threat.GetVelocity().magnitude /
 134                                    Vector3.Distance(a.Threat.transform.position, Vector3.zero))
 135            .ToList();
 0136    Assert.AreEqual(threat2, orderedAssignments[0].Threat,
 137                    "Higher threat should be assigned first");
 0138    Assert.AreEqual(threat1, orderedAssignments[1].Threat,
 139                    "Lower threat should be assigned second");
 0140  }
 141}