- DroneType 枚举清理:移除 Electric/Piston,重编号 HighSpeed=2 - planner_config.json / defaults.json 同步迁移 - 238 单元测试全部通过
98 lines
3.2 KiB
C#
98 lines
3.2 KiB
C#
using System;
|
||
using System.IO;
|
||
using CounterDrone.Core.Algorithms;
|
||
using CounterDrone.Core.Models;
|
||
using Xunit;
|
||
|
||
namespace CounterDrone.Core.Tests
|
||
{
|
||
public class PlannerConfigTests : IDisposable
|
||
{
|
||
private readonly string _testDir;
|
||
|
||
public PlannerConfigTests()
|
||
{
|
||
_testDir = Path.Combine(Path.GetTempPath(), $"cd_cfg_{Guid.NewGuid():N}");
|
||
Directory.CreateDirectory(_testDir);
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
if (Directory.Exists(_testDir)) Directory.Delete(_testDir, true);
|
||
}
|
||
|
||
private void WriteConfig(string json)
|
||
=> File.WriteAllText(Path.Combine(_testDir, "planner_config.json"), json);
|
||
|
||
private static readonly string ValidJson = @"
|
||
{
|
||
""CloudOverlapRatio"": 0.2,
|
||
""CriticalProbabilityThreshold"": 0.5,
|
||
""MaxInterceptProbability"": 0.95,
|
||
""TypeCoefficient"": {
|
||
""HighSpeed"": 4.0, ""FixedWing"": 2.0,
|
||
""Rotor"": 1.0
|
||
},
|
||
""AmmoMatch"": {
|
||
""Electric"": ""InertGas"", ""Piston"": ""InertGas"", ""Jet"": ""ActiveMaterial""
|
||
},
|
||
""DefaultDetectionAccuracy"": 50.0
|
||
}";
|
||
|
||
[Fact]
|
||
public void Load_ValidJson_ReturnsAllFields()
|
||
{
|
||
WriteConfig(ValidJson);
|
||
var cfg = PlannerConfig.Load(_testDir);
|
||
Assert.Equal(0.2f, cfg.CloudOverlapRatio);
|
||
Assert.Equal(0.5f, cfg.CriticalProbabilityThreshold);
|
||
Assert.Equal(0.95f, cfg.MaxInterceptProbability);
|
||
Assert.Equal(4.0f, cfg.TypeCoefficient[DroneType.HighSpeed]);
|
||
Assert.Equal(2.0f, cfg.TypeCoefficient[DroneType.FixedWing]);
|
||
Assert.Equal(AerosolType.InertGas, cfg.AmmoMatch[PowerType.Piston]);
|
||
Assert.Equal(AerosolType.ActiveMaterial, cfg.AmmoMatch[PowerType.Jet]);
|
||
}
|
||
|
||
[Fact]
|
||
public void Load_MissingFile_Throws()
|
||
{
|
||
// 不写文件
|
||
Assert.Throws<FileNotFoundException>(() => PlannerConfig.Load(_testDir));
|
||
}
|
||
|
||
[Fact]
|
||
public void Load_MissingField_Throws()
|
||
{
|
||
// 缺 CriticalProbabilityThreshold
|
||
WriteConfig(@"{ ""CloudOverlapRatio"": 0.2, ""MaxInterceptProbability"": 0.95,
|
||
""TypeCoefficient"": {""FixedWing"": 2.0}, ""AmmoMatch"": {""Piston"": ""InertGas""} }");
|
||
Assert.Throws<InvalidDataException>(() => PlannerConfig.Load(_testDir));
|
||
}
|
||
|
||
[Fact]
|
||
public void Load_OverlapOutOfRange_Throws()
|
||
{
|
||
// CloudOverlapRatio = 1.0 非法(必须 < 1)
|
||
var bad = ValidJson.Replace("0.2", "1.0");
|
||
WriteConfig(bad);
|
||
Assert.Throws<InvalidDataException>(() => PlannerConfig.Load(_testDir));
|
||
}
|
||
|
||
[Fact]
|
||
public void Load_EmptyDataRoot_Throws()
|
||
{
|
||
Assert.Throws<ArgumentException>(() => PlannerConfig.Load(""));
|
||
}
|
||
|
||
[Fact]
|
||
public void Load_EnumParsedFromString()
|
||
{
|
||
// 验证枚举用字符串(非数字)也能正确解析
|
||
WriteConfig(ValidJson);
|
||
var cfg = PlannerConfig.Load(_testDir);
|
||
Assert.Equal(1.0f, cfg.TypeCoefficient[DroneType.Rotor]);
|
||
Assert.Equal(AerosolType.InertGas, cfg.AmmoMatch[PowerType.Electric]);
|
||
}
|
||
}
|
||
}
|