< Summary

Class:MaxSpeedAssignmentTests
Assembly:bamlab.test.editmode
File(s):/github/workspace/Assets/Tests/EditMode/MaxSpeedAssignmentTests.cs
Covered lines:0
Uncovered lines:103
Coverable lines:103
Total lines:177
Line coverage:0% (0 of 103)
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
LoadStaticConfig()0%2100%
AssignNoInterceptors()0%2100%
AssignNoThreats()0%2100%
AssignShouldAssignAllInterceptorsAndThreats()0%12300%
AssignShouldHandleMoreInterceptorsThanThreats()0%12300%

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 {
 08  public static Configs.StaticConfig LoadStaticConfig() {
 09    return ConfigLoader.LoadStaticConfig("micromissile.pbtxt");
 010  }
 11
 12  [Test]
 013  public void AssignNoInterceptors() {
 14    // Define the assignment.
 015    IAssignment assignment = new MaxSpeedAssignment();
 16
 17    // Create interceptors.
 018    List<Interceptor> interceptors = new List<Interceptor>();
 19
 20    // Create threats.
 021    List<Threat> threats =
 22        new List<Threat> { new GameObject("Threat").AddComponent<RotaryWingThreat>() };
 23
 24    // Assign the interceptors to the threats.
 025    LogAssert.Expect(LogType.Warning, "No assignable interceptors found.");
 026    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 027    Assert.AreEqual(0, assignments.Count(), "There should be no assignments.");
 028  }
 29
 30  [Test]
 031  public void AssignNoThreats() {
 32    // Define the assignment.
 033    IAssignment assignment = new MaxSpeedAssignment();
 34
 35    // Create interceptors.
 036    List<Interceptor> interceptors =
 37        new List<Interceptor> { new GameObject("Interceptor").AddComponent<MissileInterceptor>() };
 38
 39    // Create threats.
 040    List<Threat> threats = new List<Threat>();
 41
 42    // Assign the interceptors to the threats.
 043    LogAssert.Expect(LogType.Warning, "No active threats found.");
 044    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 045    Assert.AreEqual(0, assignments.Count(), "There should be no assignments.");
 046  }
 47
 48  [Test]
 049  public void AssignShouldAssignAllInterceptorsAndThreats() {
 50    // Define the assignment.
 051    IAssignment assignment = new MaxSpeedAssignment();
 52
 53    // Create the interceptors.
 054    Interceptor interceptor1 = new GameObject("Interceptor 1").AddComponent<MissileInterceptor>();
 055    interceptor1.staticConfig = LoadStaticConfig();
 056    Interceptor interceptor2 = new GameObject("Interceptor 2").AddComponent<MissileInterceptor>();
 057    interceptor2.staticConfig = LoadStaticConfig();
 058    Interceptor interceptor3 = new GameObject("Interceptor 3").AddComponent<MissileInterceptor>();
 059    interceptor3.staticConfig = LoadStaticConfig();
 60
 61    // Add rigid body components to interceptors to set their velocities.
 062    Rigidbody interceptorRb1 = interceptor1.gameObject.AddComponent<Rigidbody>();
 063    Rigidbody interceptorRb2 = interceptor2.gameObject.AddComponent<Rigidbody>();
 064    Rigidbody interceptorRb3 = interceptor3.gameObject.AddComponent<Rigidbody>();
 65
 66    // Set the interceptor positions and velocities.
 067    interceptor1.transform.position = Vector3.zero;
 068    interceptor2.transform.position = Vector3.zero;
 069    interceptor3.transform.position = new Vector3(0, 100, 0);
 070    interceptorRb1.linearVelocity = new Vector3(0, 0, 5);
 071    interceptorRb2.linearVelocity = new Vector3(0, 10, 0);
 072    interceptorRb3.linearVelocity = new Vector3(0, 0, 5);
 73
 074    List<Interceptor> interceptors =
 75        new List<Interceptor> { interceptor1, interceptor2, interceptor3 };
 76
 77    // Create the threats.
 078    Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>();
 079    Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>();
 080    Threat threat3 = new GameObject("Threat 3").AddComponent<RotaryWingThreat>();
 81
 82    // Set threat positions.
 083    threat1.transform.position = new Vector3(0, -1, 0);
 084    threat2.transform.position = new Vector3(0, 1, 0);
 085    threat3.transform.position = new Vector3(0, 105, 0);
 86
 087    List<Threat> threats = new List<Threat> { threat1, threat2, threat3 };
 88
 89    // Assign the interceptors to the threats.
 090    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 091    Assert.AreEqual(3, assignments.Count(), "All interceptors should be assigned.");
 92
 093    HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
 094    HashSet<Threat> assignedThreats = new HashSet<Threat>();
 095    Dictionary<Interceptor, Threat> assignmentMap = new Dictionary<Interceptor, Threat>();
 96
 097    foreach (var assignmentItem in assignments) {
 098      Assert.IsNotNull(assignmentItem.Interceptor, "Interceptor should not be null.");
 099      Assert.IsNotNull(assignmentItem.Threat, "Threat should not be null.");
 0100      assignedInterceptors.Add(assignmentItem.Interceptor);
 0101      assignedThreats.Add(assignmentItem.Threat);
 0102      assignmentMap[assignmentItem.Interceptor] = assignmentItem.Threat;
 0103    }
 104
 0105    Assert.AreEqual(3, assignedInterceptors.Count, "All interceptors should be unique.");
 0106    Assert.AreEqual(3, assignedThreats.Count, "All threats should be assigned.");
 107
 108    // Verify that threats are assigned to maximize the intercept speed.
 0109    Assert.AreEqual(assignmentMap[interceptor1], threat1);
 0110    Assert.AreEqual(assignmentMap[interceptor2], threat2);
 0111    Assert.AreEqual(assignmentMap[interceptor3], threat3);
 0112  }
 113
 114  [Test]
 0115  public void AssignShouldHandleMoreInterceptorsThanThreats() {
 116    // Define the assignment.
 0117    IAssignment assignment = new MaxSpeedAssignment();
 118
 119    // Create the interceptors.
 0120    Interceptor interceptor1 = new GameObject("Interceptor 1").AddComponent<MissileInterceptor>();
 0121    interceptor1.staticConfig = LoadStaticConfig();
 0122    Interceptor interceptor2 = new GameObject("Interceptor 2").AddComponent<MissileInterceptor>();
 0123    interceptor2.staticConfig = LoadStaticConfig();
 0124    Interceptor interceptor3 = new GameObject("Interceptor 3").AddComponent<MissileInterceptor>();
 0125    interceptor3.staticConfig = LoadStaticConfig();
 126
 127    // Add rigid body components to interceptors to set their velocities.
 0128    Rigidbody interceptorRb1 = interceptor1.gameObject.AddComponent<Rigidbody>();
 0129    Rigidbody interceptorRb2 = interceptor2.gameObject.AddComponent<Rigidbody>();
 0130    Rigidbody interceptorRb3 = interceptor3.gameObject.AddComponent<Rigidbody>();
 131
 132    // Set the interceptor positions and velocities.
 0133    interceptor1.transform.position = Vector3.zero;
 0134    interceptor2.transform.position = Vector3.zero;
 0135    interceptor3.transform.position = new Vector3(0, 100, 0);
 0136    interceptorRb1.linearVelocity = new Vector3(0, 0, 5);
 0137    interceptorRb2.linearVelocity = new Vector3(0, 10, 0);
 0138    interceptorRb3.linearVelocity = new Vector3(0, 0, 5);
 139
 0140    List<Interceptor> interceptors =
 141        new List<Interceptor> { interceptor1, interceptor2, interceptor3 };
 142
 143    // Create the threats.
 0144    Threat threat1 = new GameObject("Threat 1").AddComponent<RotaryWingThreat>();
 0145    Threat threat2 = new GameObject("Threat 2").AddComponent<RotaryWingThreat>();
 146
 147    // Set threat positions.
 0148    threat1.transform.position = new Vector3(0, -1, 0);
 0149    threat2.transform.position = new Vector3(0, 1, 0);
 150
 0151    List<Threat> threats = new List<Threat> { threat1, threat2 };
 152
 153    // Assign the interceptors to the threats.
 0154    IEnumerable<IAssignment.AssignmentItem> assignments = assignment.Assign(interceptors, threats);
 0155    Assert.AreEqual(3, assignments.Count(), "All interceptors should be assigned.");
 156
 0157    HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
 0158    HashSet<Threat> assignedThreats = new HashSet<Threat>();
 0159    Dictionary<Interceptor, Threat> assignmentMap = new Dictionary<Interceptor, Threat>();
 160
 0161    foreach (var assignmentItem in assignments) {
 0162      Assert.IsNotNull(assignmentItem.Interceptor, "Interceptor should not be null.");
 0163      Assert.IsNotNull(assignmentItem.Threat, "Threat should not be null.");
 0164      assignedInterceptors.Add(assignmentItem.Interceptor);
 0165      assignedThreats.Add(assignmentItem.Threat);
 0166      assignmentMap[assignmentItem.Interceptor] = assignmentItem.Threat;
 0167    }
 168
 0169    Assert.AreEqual(3, assignedInterceptors.Count, "All interceptors should be assigned.");
 0170    Assert.AreEqual(2, assignedThreats.Count, "Both threats should be assigned.");
 171
 172    // Verify that threats are assigned to maximize the intercept speed.
 0173    Assert.AreEqual(assignmentMap[interceptor1], threat1);
 0174    Assert.AreEqual(assignmentMap[interceptor2], threat2);
 0175    Assert.AreEqual(assignmentMap[interceptor3], threat2);
 0176  }
 177}