< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LoadStaticConfig()0%110100%
AssignNoInterceptors()0%110100%
AssignNoThreats()0%110100%
AssignShouldAssignAllInterceptorsAndThreats()0%330100%
AssignShouldHandleMoreInterceptorsThanThreats()0%330100%

File(s)

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

#LineLine coverage
 1using NUnit.Framework;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5using UnityEngine.TestTools;
 6
 7public class MaxSpeedAssignmentTests {
 68  public static Configs.StaticConfig LoadStaticConfig() {
 69    return ConfigLoader.LoadStaticConfig("micromissile.pbtxt");
 610  }
 11
 12  [Test]
 113  public void AssignNoInterceptors() {
 14    // Define the assignment.
 115    IAssignment assignment = new MaxSpeedAssignment();
 16
 17    // Create interceptors.
 118    List<Interceptor> interceptors = new List<Interceptor>();
 19
 20    // Create threats.
 121    List<Threat> threats =
 22        new List<Threat> { new GameObject("Threat").AddComponent<RotaryWingThreat>() };
 23
 24    // Assign the interceptors to the threats.
 125    LogAssert.Expect(LogType.Warning, "No assignable interceptors found.");
 126    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 127    Assert.AreEqual(0, assignments.Count(), "There should be no assignments.");
 128  }
 29
 30  [Test]
 131  public void AssignNoThreats() {
 32    // Define the assignment.
 133    IAssignment assignment = new MaxSpeedAssignment();
 34
 35    // Create interceptors.
 136    List<Interceptor> interceptors =
 37        new List<Interceptor> { new GameObject("Interceptor").AddComponent<MissileInterceptor>() };
 38
 39    // Create threats.
 140    List<Threat> threats = new List<Threat>();
 41
 42    // Assign the interceptors to the threats.
 143    LogAssert.Expect(LogType.Warning, "No active threats found.");
 144    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 145    Assert.AreEqual(0, assignments.Count(), "There should be no assignments.");
 146  }
 47
 48  [Test]
 149  public void AssignShouldAssignAllInterceptorsAndThreats() {
 50    // Define the assignment.
 151    IAssignment assignment = new MaxSpeedAssignment();
 52
 53    // Create the interceptors.
 154    Interceptor interceptor1 = new GameObject("Interceptor 1").AddComponent<MissileInterceptor>();
 155    interceptor1.staticConfig = LoadStaticConfig();
 156    Interceptor interceptor2 = new GameObject("Interceptor 2").AddComponent<MissileInterceptor>();
 157    interceptor2.staticConfig = LoadStaticConfig();
 158    Interceptor interceptor3 = new GameObject("Interceptor 3").AddComponent<MissileInterceptor>();
 159    interceptor3.staticConfig = LoadStaticConfig();
 60
 61    // Add rigid body components to interceptors to set their velocities.
 162    Rigidbody interceptorRb1 = interceptor1.gameObject.AddComponent<Rigidbody>();
 163    Rigidbody interceptorRb2 = interceptor2.gameObject.AddComponent<Rigidbody>();
 164    Rigidbody interceptorRb3 = interceptor3.gameObject.AddComponent<Rigidbody>();
 65
 66    // Set the interceptor positions and velocities.
 167    interceptor1.transform.position = Vector3.zero;
 168    interceptor2.transform.position = Vector3.zero;
 169    interceptor3.transform.position = new Vector3(0, 100, 0);
 170    interceptorRb1.linearVelocity = new Vector3(0, 0, 5);
 171    interceptorRb2.linearVelocity = new Vector3(0, 10, 0);
 172    interceptorRb3.linearVelocity = new Vector3(0, 0, 5);
 73
 174    List<Interceptor> interceptors =
 75        new List<Interceptor> { interceptor1, interceptor2, interceptor3 };
 76
 77    // Create the threats.
 178    Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>();
 179    Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>();
 180    Threat threat3 = new GameObject("Threat 3").AddComponent<RotaryWingThreat>();
 81
 82    // Set threat positions.
 183    threat1.transform.position = new Vector3(0, -1, 0);
 184    threat2.transform.position = new Vector3(0, 1, 0);
 185    threat3.transform.position = new Vector3(0, 105, 0);
 86
 187    List<Threat> threats = new List<Threat> { threat1, threat2, threat3 };
 88
 89    // Assign the interceptors to the threats.
 190    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 191    Assert.AreEqual(3, assignments.Count(), "All interceptors should be assigned.");
 92
 193    HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
 194    HashSet<Threat> assignedThreats = new HashSet<Threat>();
 195    Dictionary<Interceptor, Threat> assignmentMap = new Dictionary<Interceptor, Threat>();
 96
 1297    foreach (var assignmentItem in assignments) {
 398      Assert.IsNotNull(assignmentItem.Interceptor, "Interceptor should not be null.");
 399      Assert.IsNotNull(assignmentItem.Threat, "Threat should not be null.");
 3100      assignedInterceptors.Add(assignmentItem.Interceptor);
 3101      assignedThreats.Add(assignmentItem.Threat);
 3102      assignmentMap[assignmentItem.Interceptor] = assignmentItem.Threat;
 3103    }
 104
 1105    Assert.AreEqual(3, assignedInterceptors.Count, "All interceptors should be unique.");
 1106    Assert.AreEqual(3, assignedThreats.Count, "All threats should be assigned.");
 107
 108    // Verify that threats are assigned to maximize the intercept speed.
 1109    Assert.AreEqual(assignmentMap[interceptor1], threat1);
 1110    Assert.AreEqual(assignmentMap[interceptor2], threat2);
 1111    Assert.AreEqual(assignmentMap[interceptor3], threat3);
 1112  }
 113
 114  [Test]
 1115  public void AssignShouldHandleMoreInterceptorsThanThreats() {
 116    // Define the assignment.
 1117    IAssignment assignment = new MaxSpeedAssignment();
 118
 119    // Create the interceptors.
 1120    Interceptor interceptor1 = new GameObject("Interceptor 1").AddComponent<MissileInterceptor>();
 1121    interceptor1.staticConfig = LoadStaticConfig();
 1122    Interceptor interceptor2 = new GameObject("Interceptor 2").AddComponent<MissileInterceptor>();
 1123    interceptor2.staticConfig = LoadStaticConfig();
 1124    Interceptor interceptor3 = new GameObject("Interceptor 3").AddComponent<MissileInterceptor>();
 1125    interceptor3.staticConfig = LoadStaticConfig();
 126
 127    // Add rigid body components to interceptors to set their velocities.
 1128    Rigidbody interceptorRb1 = interceptor1.gameObject.AddComponent<Rigidbody>();
 1129    Rigidbody interceptorRb2 = interceptor2.gameObject.AddComponent<Rigidbody>();
 1130    Rigidbody interceptorRb3 = interceptor3.gameObject.AddComponent<Rigidbody>();
 131
 132    // Set the interceptor positions and velocities.
 1133    interceptor1.transform.position = Vector3.zero;
 1134    interceptor2.transform.position = Vector3.zero;
 1135    interceptor3.transform.position = new Vector3(0, 100, 0);
 1136    interceptorRb1.linearVelocity = new Vector3(0, 0, 5);
 1137    interceptorRb2.linearVelocity = new Vector3(0, 10, 0);
 1138    interceptorRb3.linearVelocity = new Vector3(0, 0, 5);
 139
 1140    List<Interceptor> interceptors =
 141        new List<Interceptor> { interceptor1, interceptor2, interceptor3 };
 142
 143    // Create the threats.
 1144    Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>();
 1145    Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>();
 146
 147    // Set threat positions.
 1148    threat1.transform.position = new Vector3(0, -1, 0);
 1149    threat2.transform.position = new Vector3(0, 1, 0);
 150
 1151    List<Threat> threats = new List<Threat> { threat1, threat2 };
 152
 153    // Assign the interceptors to the threats.
 1154    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 1155    Assert.AreEqual(3, assignments.Count(), "All interceptors should be assigned.");
 156
 1157    HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
 1158    HashSet<Threat> assignedThreats = new HashSet<Threat>();
 1159    Dictionary<Interceptor, Threat> assignmentMap = new Dictionary<Interceptor, Threat>();
 160
 12161    foreach (var assignmentItem in assignments) {
 3162      Assert.IsNotNull(assignmentItem.Interceptor, "Interceptor should not be null.");
 3163      Assert.IsNotNull(assignmentItem.Threat, "Threat should not be null.");
 3164      assignedInterceptors.Add(assignmentItem.Interceptor);
 3165      assignedThreats.Add(assignmentItem.Threat);
 3166      assignmentMap[assignmentItem.Interceptor] = assignmentItem.Threat;
 3167    }
 168
 1169    Assert.AreEqual(3, assignedInterceptors.Count, "All interceptors should be assigned.");
 1170    Assert.AreEqual(2, assignedThreats.Count, "Both threats should be assigned.");
 171
 172    // Verify that threats are assigned to maximize the intercept speed.
 1173    Assert.AreEqual(assignmentMap[interceptor1], threat1);
 1174    Assert.AreEqual(assignmentMap[interceptor2], threat2);
 1175    Assert.AreEqual(assignmentMap[interceptor3], threat2);
 1176  }
 177}