298 lines
12 KiB
C#
298 lines
12 KiB
C#
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||
using ThreatSource.Guidance;
|
||
using ThreatSource.Jamming;
|
||
using ThreatSource.Simulation;
|
||
using ThreatSource.Tests.Simulation;
|
||
using ThreatSource.Utils;
|
||
using ThreatSource.Target;
|
||
|
||
namespace ThreatSource.Tests.Jamming
|
||
{
|
||
[TestClass]
|
||
public class InfraredImagingGuidanceJammingTests : IDisposable
|
||
{
|
||
private SimulationManager? _simulationManager;
|
||
private TestSimulationAdapter? _testAdapter;
|
||
private InfraredImagingGuidanceSystem? _guidanceSystem;
|
||
private Tank? _target;
|
||
|
||
[TestInitialize]
|
||
public void TestInitialize()
|
||
{
|
||
// 初始化模拟管理器和测试适配器
|
||
_simulationManager = new SimulationManager();
|
||
_testAdapter = new TestSimulationAdapter(_simulationManager);
|
||
_simulationManager.SetSimulationAdapter(_testAdapter);
|
||
|
||
// 创建红外成像引导系统配置
|
||
var config = new InfraredImagingGuidanceConfig
|
||
{
|
||
ImageWidth = 640,
|
||
ImageHeight = 480,
|
||
SearchFieldOfView = Math.PI / 6, // 30度
|
||
TrackFieldOfView = Math.PI / 12, // 15度
|
||
BackgroundIntensity = 0.1,
|
||
MaxDetectionRange = 5000,
|
||
SearchRecognitionProbability = 0.4,
|
||
TrackRecognitionProbability = 0.8,
|
||
LockConfirmationTime = 1.5,
|
||
TargetLostTolerance = 0.5,
|
||
JammingResistanceThreshold = 1e-4 // 修改为1e-4W
|
||
};
|
||
|
||
// 创建并注册目标实体
|
||
var tankInitialMotion = new InitialMotionParameters
|
||
{
|
||
Position = new Vector3D(100, 0, 0), // 修改为100米,与激光指示器一致
|
||
Orientation = new Orientation(0, 0, 0),
|
||
InitialSpeed = 0
|
||
};
|
||
|
||
_target = new Tank("target1", tankInitialMotion, _simulationManager);
|
||
_simulationManager.RegisterEntity("target1", _target);
|
||
_testAdapter.AddTestEntity("target1", _target);
|
||
|
||
// 创建红外引导系统
|
||
_guidanceSystem = new InfraredImagingGuidanceSystem(
|
||
"infraredGuidance1",
|
||
config,
|
||
TargetType.Tank,
|
||
100, // 最大加速度
|
||
3.0, // 比例导引系数
|
||
_simulationManager
|
||
);
|
||
|
||
// 注册引导系统
|
||
_simulationManager.RegisterEntity("infraredGuidance1", _guidanceSystem);
|
||
_testAdapter.AddTestEntity("infraredGuidance1", _guidanceSystem);
|
||
|
||
// 激活引导系统
|
||
_guidanceSystem.Activate();
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
_guidanceSystem?.Deactivate();
|
||
}
|
||
|
||
[TestMethod]
|
||
public void InfraredJamming_TargetsGuidance_GuidanceIsJammed()
|
||
{
|
||
// 确保组件不为空
|
||
Assert.IsNotNull(_simulationManager);
|
||
Assert.IsNotNull(_guidanceSystem);
|
||
|
||
// Arrange
|
||
var initialPosition = new Vector3D(500, 0, 0);
|
||
var initialVelocity = new Vector3D(100, 0, 0);
|
||
|
||
System.Diagnostics.Debug.WriteLine($"测试开始 - 初始位置: {initialPosition}, 初始速度: {initialVelocity}");
|
||
|
||
// 更新一次系统,使其跟踪目标
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// 创建干扰事件
|
||
var jammingEvent = new InfraredJammingEvent
|
||
{
|
||
JammingPower = 2000, // 修改为2000瓦特,与激光指示器一致
|
||
JammingSourcePosition = new Vector3D(50, 0, 0), // 修改为50米,与激光指示器一致
|
||
JammingDirection = new Vector3D(1, 0, 0), // 修改为(1,0,0),指向设备的方向
|
||
JammingAngleRange = Math.PI / 4,
|
||
JammingMode = JammingMode.Noise,
|
||
Duration = null
|
||
};
|
||
|
||
System.Diagnostics.Debug.WriteLine($"创建干扰事件 - 功率: {jammingEvent.JammingPower}W, 位置: {jammingEvent.JammingSourcePosition}, 方向: {jammingEvent.JammingDirection}, 角度范围: {jammingEvent.JammingAngleRange * 180 / Math.PI}度");
|
||
|
||
// Act
|
||
_simulationManager.PublishEvent(jammingEvent);
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// Assert
|
||
System.Diagnostics.Debug.WriteLine($"测试结果 - IsJammed: {_guidanceSystem.IsJammed}, HasGuidance: {_guidanceSystem.HasGuidance}");
|
||
Assert.IsTrue(_guidanceSystem.IsJammed, "红外引导系统应该处于被干扰状态");
|
||
Assert.IsFalse(_guidanceSystem.HasGuidance, "被干扰时不应该有制导信号");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void InfraredJamming_OutsideAngleRange_GuidanceNotJammed()
|
||
{
|
||
// 确保组件不为空
|
||
Assert.IsNotNull(_simulationManager);
|
||
Assert.IsNotNull(_guidanceSystem);
|
||
|
||
// Arrange
|
||
var initialPosition = new Vector3D(500, 0, 0);
|
||
var initialVelocity = new Vector3D(100, 0, 0);
|
||
|
||
// 更新一次系统,使其跟踪目标
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// 创建角度范围外的干扰事件
|
||
var jammingEvent = new InfraredJammingEvent
|
||
{
|
||
JammingPower = 500, // 保持不变
|
||
JammingSourcePosition = new Vector3D(0, 50, 0),
|
||
JammingDirection = new Vector3D(1, 0, 0),
|
||
JammingAngleRange = 0.1,
|
||
JammingMode = JammingMode.Noise
|
||
};
|
||
|
||
// Act
|
||
_simulationManager.PublishEvent(jammingEvent);
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// Assert
|
||
Assert.IsFalse(_guidanceSystem.IsJammed, "引导系统不应该被干扰,因为干扰源不在角度范围内");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void InfraredJamming_LowPower_GuidanceNotJammed()
|
||
{
|
||
// 确保组件不为空
|
||
Assert.IsNotNull(_simulationManager);
|
||
Assert.IsNotNull(_guidanceSystem);
|
||
|
||
// Arrange
|
||
var initialPosition = new Vector3D(500, 0, 0);
|
||
var initialVelocity = new Vector3D(100, 0, 0);
|
||
|
||
// 更新一次系统,使其跟踪目标
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// 创建低功率干扰事件
|
||
var jammingEvent = new InfraredJammingEvent
|
||
{
|
||
JammingPower = 10, // 保持不变
|
||
JammingSourcePosition = new Vector3D(50, 0, 0),
|
||
JammingDirection = new Vector3D(1, 0, 0),
|
||
JammingAngleRange = Math.PI / 4,
|
||
JammingMode = JammingMode.Noise
|
||
};
|
||
|
||
// Act
|
||
_simulationManager.PublishEvent(jammingEvent);
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// Assert
|
||
Assert.IsFalse(_guidanceSystem.IsJammed, "引导系统不应该被干扰,因为干扰功率低于阈值");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void InfraredJamming_ClearedAfterDuration_GuidanceNotJammed()
|
||
{
|
||
// 确保组件不为空
|
||
Assert.IsNotNull(_simulationManager);
|
||
Assert.IsNotNull(_guidanceSystem);
|
||
|
||
// Arrange
|
||
var initialPosition = new Vector3D(500, 0, 0);
|
||
var initialVelocity = new Vector3D(100, 0, 0);
|
||
|
||
// 更新一次系统,使其跟踪目标
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// 创建有限时间的干扰事件
|
||
var jammingEvent = new InfraredJammingEvent
|
||
{
|
||
JammingPower = 2000, // 修改为2000瓦特,与激光指示器一致
|
||
JammingSourcePosition = new Vector3D(50, 0, 0), // 修改为50米,与激光指示器一致
|
||
JammingDirection = new Vector3D(1, 0, 0), // 修改为(1,0,0),指向设备的方向
|
||
JammingAngleRange = Math.PI / 4,
|
||
JammingMode = JammingMode.Noise,
|
||
Duration = 0.5
|
||
};
|
||
|
||
// Act - 应用干扰
|
||
_simulationManager.PublishEvent(jammingEvent);
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// Assert - 确认已被干扰
|
||
Assert.IsTrue(_guidanceSystem.IsJammed, "红外引导系统应该处于被干扰状态");
|
||
|
||
// Act - 等待干扰过期
|
||
// 通过多次更新引导系统,让JammingHandler处理时间
|
||
for (int i = 0; i < 10; i++)
|
||
{
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity); // 总共更新1秒,超过干扰持续时间
|
||
}
|
||
|
||
// Assert - 确认干扰已清除
|
||
Assert.IsFalse(_guidanceSystem.IsJammed, "干扰持续时间结束后,引导系统应该恢复正常");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void InfraredJamming_ManualClear_GuidanceNotJammed()
|
||
{
|
||
// 确保组件不为空
|
||
Assert.IsNotNull(_simulationManager);
|
||
Assert.IsNotNull(_guidanceSystem);
|
||
|
||
// Arrange
|
||
var initialPosition = new Vector3D(500, 0, 0);
|
||
var initialVelocity = new Vector3D(100, 0, 0);
|
||
|
||
// 更新一次系统,使其跟踪目标
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// 创建持续干扰事件
|
||
var jammingEvent = new InfraredJammingEvent
|
||
{
|
||
JammingPower = 2000, // 修改为2000瓦特,与激光指示器一致
|
||
JammingSourcePosition = new Vector3D(50, 0, 0), // 修改为50米,与激光指示器一致
|
||
JammingDirection = new Vector3D(1, 0, 0), // 修改为(1,0,0),指向设备的方向
|
||
JammingAngleRange = Math.PI / 4,
|
||
JammingMode = JammingMode.Noise,
|
||
Duration = null
|
||
};
|
||
|
||
// Act - 应用干扰
|
||
_simulationManager.PublishEvent(jammingEvent);
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// Assert - 确认已被干扰
|
||
Assert.IsTrue(_guidanceSystem.IsJammed, "红外引导系统应该处于被干扰状态");
|
||
|
||
// Act - 手动清除干扰
|
||
_guidanceSystem.ClearJamming(JammingType.Infrared);
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// Assert - 确认干扰已清除
|
||
Assert.IsFalse(_guidanceSystem.IsJammed, "手动清除后,引导系统应该恢复正常");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void InfraredJamming_GuidanceSwitchesToSearchMode()
|
||
{
|
||
// 确保组件不为空
|
||
Assert.IsNotNull(_simulationManager);
|
||
Assert.IsNotNull(_guidanceSystem);
|
||
|
||
// Arrange
|
||
var initialPosition = new Vector3D(500, 0, 0);
|
||
var initialVelocity = new Vector3D(100, 0, 0);
|
||
|
||
// 更新一次系统,使其跟踪目标
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// 创建干扰事件
|
||
var jammingEvent = new InfraredJammingEvent
|
||
{
|
||
JammingPower = 2000, // 修改为2000瓦特,与激光指示器一致
|
||
JammingSourcePosition = new Vector3D(50, 0, 0), // 修改为50米,与激光指示器一致
|
||
JammingDirection = new Vector3D(1, 0, 0), // 修改为(1,0,0),指向设备的方向
|
||
JammingAngleRange = Math.PI / 4,
|
||
JammingMode = JammingMode.Noise,
|
||
Duration = null
|
||
};
|
||
|
||
// Act - 应用干扰
|
||
_simulationManager.PublishEvent(jammingEvent);
|
||
_guidanceSystem.Update(0.1, initialPosition, initialVelocity);
|
||
|
||
// Assert - 确认系统切换到搜索模式
|
||
Assert.IsTrue(_guidanceSystem.IsJammed, "红外引导系统应该处于被干扰状态");
|
||
Assert.IsFalse(_guidanceSystem.HasGuidance, "被干扰时不应该有制导信号");
|
||
}
|
||
}
|
||
} |