< Summary

Class:MaxSpeedAssignmentTest
Assembly:bamlab.test.editmode
File(s):/github/workspace/Assets/Tests/EditMode/MaxSpeedAssignmentTest.cs
Covered lines:0
Uncovered lines:107
Coverable lines:107
Total lines:181
Line coverage:0% (0 of 107)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:5
Method coverage:0% (0 of 5)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CreateStaticAgentConfig()0%2100%
AssignNoInterceptors()0%2100%
AssignNoThreats()0%2100%
AssignShouldAssignAllInterceptorsAndThreats()0%12300%
AssignShouldHandleMoreInterceptorsThanThreats()0%12300%

File(s)

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

#LineLine coverage
 1using NUnit.Framework;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5using UnityEngine.TestTools;
 6
 7public class MaxSpeedAssignmentTest {
 08  public static StaticAgentConfig CreateStaticAgentConfig() {
 09    StaticAgentConfig config = new StaticAgentConfig();
 010    config.accelerationConfig = new StaticAgentConfig.AccelerationConfig();
 011    config.bodyConfig = new StaticAgentConfig.BodyConfig();
 012    config.liftDragConfig = new StaticAgentConfig.LiftDragConfig();
 013    return config;
 014  }
 15
 16  [Test]
 017  public void AssignNoInterceptors() {
 18    // Define the assignment.
 019    IAssignment assignment = new MaxSpeedAssignment();
 20
 21    // Create interceptors.
 022    List<Interceptor> interceptors = new List<Interceptor>();
 23
 24    // Create threats.
 025    List<Threat> threats =
 26        new List<Threat> { new GameObject("Threat").AddComponent<RotaryWingThreat>() };
 27
 28    // Assign the interceptors to the threats.
 029    LogAssert.Expect(LogType.Warning, "No assignable interceptors found.");
 030    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 031    Assert.AreEqual(0, assignments.Count(), "There should be no assignments.");
 032  }
 33
 34  [Test]
 035  public void AssignNoThreats() {
 36    // Define the assignment.
 037    IAssignment assignment = new MaxSpeedAssignment();
 38
 39    // Create interceptors.
 040    List<Interceptor> interceptors =
 41        new List<Interceptor> { new GameObject("Interceptor").AddComponent<MissileInterceptor>() };
 42
 43    // Create threats.
 044    List<Threat> threats = new List<Threat>();
 45
 46    // Assign the interceptors to the threats.
 047    LogAssert.Expect(LogType.Warning, "No active threats found.");
 048    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 049    Assert.AreEqual(0, assignments.Count(), "There should be no assignments.");
 050  }
 51
 52  [Test]
 053  public void AssignShouldAssignAllInterceptorsAndThreats() {
 54    // Define the assignment.
 055    IAssignment assignment = new MaxSpeedAssignment();
 56
 57    // Create the interceptors.
 058    Interceptor interceptor1 = new GameObject("Interceptor 1").AddComponent<MissileInterceptor>();
 059    interceptor1.staticAgentConfig = CreateStaticAgentConfig();
 060    Interceptor interceptor2 = new GameObject("Interceptor 2").AddComponent<MissileInterceptor>();
 061    interceptor2.staticAgentConfig = CreateStaticAgentConfig();
 062    Interceptor interceptor3 = new GameObject("Interceptor 3").AddComponent<MissileInterceptor>();
 063    interceptor3.staticAgentConfig = CreateStaticAgentConfig();
 64
 65    // Add rigid body components to interceptors to set their velocities.
 066    Rigidbody interceptorRb1 = interceptor1.gameObject.AddComponent<Rigidbody>();
 067    Rigidbody interceptorRb2 = interceptor2.gameObject.AddComponent<Rigidbody>();
 068    Rigidbody interceptorRb3 = interceptor3.gameObject.AddComponent<Rigidbody>();
 69
 70    // Set the interceptor positions and velocities.
 071    interceptor1.transform.position = Vector3.zero;
 072    interceptor2.transform.position = Vector3.zero;
 073    interceptor3.transform.position = new Vector3(0, 100, 0);
 074    interceptorRb1.linearVelocity = new Vector3(0, 0, 5);
 075    interceptorRb2.linearVelocity = new Vector3(0, 10, 0);
 076    interceptorRb3.linearVelocity = new Vector3(0, 0, 5);
 77
 078    List<Interceptor> interceptors =
 79        new List<Interceptor> { interceptor1, interceptor2, interceptor3 };
 80
 81    // Create the threats.
 082    Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>();
 083    Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>();
 084    Threat threat3 = new GameObject("Threat 3").AddComponent<RotaryWingThreat>();
 85
 86    // Set threat positions.
 087    threat1.transform.position = new Vector3(0, -1, 0);
 088    threat2.transform.position = new Vector3(0, 1, 0);
 089    threat3.transform.position = new Vector3(0, 105, 0);
 90
 091    List<Threat> threats = new List<Threat> { threat1, threat2, threat3 };
 92
 93    // Assign the interceptors to the threats.
 094    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 095    Assert.AreEqual(3, assignments.Count(), "All interceptors should be assigned.");
 96
 097    HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
 098    HashSet<Threat> assignedThreats = new HashSet<Threat>();
 099    Dictionary<Interceptor, Threat> assignmentMap = new Dictionary<Interceptor, Threat>();
 100
 0101    foreach (var assignmentItem in assignments) {
 0102      Assert.IsNotNull(assignmentItem.Interceptor, "Interceptor should not be null.");
 0103      Assert.IsNotNull(assignmentItem.Threat, "Threat should not be null.");
 0104      assignedInterceptors.Add(assignmentItem.Interceptor);
 0105      assignedThreats.Add(assignmentItem.Threat);
 0106      assignmentMap[assignmentItem.Interceptor] = assignmentItem.Threat;
 0107    }
 108
 0109    Assert.AreEqual(3, assignedInterceptors.Count, "All interceptors should be unique.");
 0110    Assert.AreEqual(3, assignedThreats.Count, "All threats should be assigned.");
 111
 112    // Verify that threats are assigned to maximize the intercept speed.
 0113    Assert.AreEqual(assignmentMap[interceptor1], threat1);
 0114    Assert.AreEqual(assignmentMap[interceptor2], threat2);
 0115    Assert.AreEqual(assignmentMap[interceptor3], threat3);
 0116  }
 117
 118  [Test]
 0119  public void AssignShouldHandleMoreInterceptorsThanThreats() {
 120    // Define the assignment.
 0121    IAssignment assignment = new MaxSpeedAssignment();
 122
 123    // Create the interceptors.
 0124    Interceptor interceptor1 = new GameObject("Interceptor 1").AddComponent<MissileInterceptor>();
 0125    interceptor1.staticAgentConfig = CreateStaticAgentConfig();
 0126    Interceptor interceptor2 = new GameObject("Interceptor 2").AddComponent<MissileInterceptor>();
 0127    interceptor2.staticAgentConfig = CreateStaticAgentConfig();
 0128    Interceptor interceptor3 = new GameObject("Interceptor 3").AddComponent<MissileInterceptor>();
 0129    interceptor3.staticAgentConfig = CreateStaticAgentConfig();
 130
 131    // Add rigid body components to interceptors to set their velocities.
 0132    Rigidbody interceptorRb1 = interceptor1.gameObject.AddComponent<Rigidbody>();
 0133    Rigidbody interceptorRb2 = interceptor2.gameObject.AddComponent<Rigidbody>();
 0134    Rigidbody interceptorRb3 = interceptor3.gameObject.AddComponent<Rigidbody>();
 135
 136    // Set the interceptor positions and velocities.
 0137    interceptor1.transform.position = Vector3.zero;
 0138    interceptor2.transform.position = Vector3.zero;
 0139    interceptor3.transform.position = new Vector3(0, 100, 0);
 0140    interceptorRb1.linearVelocity = new Vector3(0, 0, 5);
 0141    interceptorRb2.linearVelocity = new Vector3(0, 10, 0);
 0142    interceptorRb3.linearVelocity = new Vector3(0, 0, 5);
 143
 0144    List<Interceptor> interceptors =
 145        new List<Interceptor> { interceptor1, interceptor2, interceptor3 };
 146
 147    // Create the threats.
 0148    Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>();
 0149    Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>();
 150
 151    // Set threat positions.
 0152    threat1.transform.position = new Vector3(0, -1, 0);
 0153    threat2.transform.position = new Vector3(0, 1, 0);
 154
 0155    List<Threat> threats = new List<Threat> { threat1, threat2 };
 156
 157    // Assign the interceptors to the threats.
 0158    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 0159    Assert.AreEqual(3, assignments.Count(), "All interceptors should be assigned.");
 160
 0161    HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
 0162    HashSet<Threat> assignedThreats = new HashSet<Threat>();
 0163    Dictionary<Interceptor, Threat> assignmentMap = new Dictionary<Interceptor, Threat>();
 164
 0165    foreach (var assignmentItem in assignments) {
 0166      Assert.IsNotNull(assignmentItem.Interceptor, "Interceptor should not be null.");
 0167      Assert.IsNotNull(assignmentItem.Threat, "Threat should not be null.");
 0168      assignedInterceptors.Add(assignmentItem.Interceptor);
 0169      assignedThreats.Add(assignmentItem.Threat);
 0170      assignmentMap[assignmentItem.Interceptor] = assignmentItem.Threat;
 0171    }
 172
 0173    Assert.AreEqual(3, assignedInterceptors.Count, "All interceptors should be assigned.");
 0174    Assert.AreEqual(2, assignedThreats.Count, "Both threats should be assigned.");
 175
 176    // Verify that threats are assigned to maximize the intercept speed.
 0177    Assert.AreEqual(assignmentMap[interceptor1], threat1);
 0178    Assert.AreEqual(assignmentMap[interceptor2], threat2);
 0179    Assert.AreEqual(assignmentMap[interceptor3], threat2);
 0180  }
 181}