plcsharp/Tests/PLCSharp.Tests/StringAndLinqTests.cs

112 lines
3.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#nullable enable
using PLCSharp.Core.Tools;
namespace PLCSharp.Tests;
/// <summary>
/// StringExtension.IsIP 行为快照(正则首段不允许 0.x 开头,先锁现状)。
/// </summary>
public class StringExtensionTests
{
[Theory]
[InlineData("192.168.1.1", true)]
[InlineData("10.0.0.1", true)]
[InlineData("255.255.255.255", true)] // 修复:四段统一 0-255(修复前首段上限223,此值被拒)
[InlineData("1.1.1.1", true)]
[InlineData("0.0.0.0", true)] // 修复:四段统一 0-255(修复前首段不允许0开头)
[InlineData("256.1.1.1", false)]
[InlineData("1.1.1.256", false)]
[InlineData("192.168.1", false)]
[InlineData("abc", false)]
[InlineData("", false)]
public void IsIP(string ip, bool expected)
{
Assert.Equal(expected, ip.IsIP());
}
[Fact]
public void IsIP_首段224现在应通过_修复后四段对称()
{
// 修复前首段上限223;修复后四段统一0-255
Assert.True("224.1.1.1".IsIP());
Assert.True("1.1.1.224".IsIP());
}
}
/// <summary>
/// LinqExtension.DistinctBy 语义测试。
/// </summary>
public class LinqExtensionTests
{
[Fact]
public void DistinctBy_按key去重保留首个()
{
var items = new[] { ("a", 1), ("a", 2), ("b", 3) };
// 显式调用项目版(.NET 6+ 自带 Linq.DistinctBy,裸调用会二义)
var result = LinqExtension.DistinctBy(items, x => x.Item1).ToList();
Assert.Equal(2, result.Count);
Assert.Equal(("a", 1), result[0]);
Assert.Equal(("b", 3), result[1]);
}
[Fact]
public void DistinctBy_空集合返回空()
{
Assert.Empty(LinqExtension.DistinctBy(Array.Empty<int>(), x => x));
}
}
/// <summary>
/// XPoint 测试:坐标 6 位舍入、旋转偏移计算、相等性。
/// </summary>
public class XPointTests
{
[Fact]
public void 坐标setter_应舍入到6位小数()
{
var p = new XPoint { X = 1.1234567891 };
Assert.Equal(1.123457, p.X); // 四舍五入到第6位
}
[Fact]
public void GetOffsPoint_旋转90度_基准点应绕原点转()
{
// 原点(0,0),基准点(10,0),绕原点转90° → (0,-10)(公式 y' = -x·sin+y·cos)
var origin = new XPoint { X = 0, Y = 0 };
var before = new XPoint { X = 10, Y = 0, Z = 5 };
var result = XPoint.GetOffsPoint(origin, 90, before);
Assert.Equal(0, result.X, 6);
Assert.Equal(-10, result.Y, 6);
Assert.Equal(5, result.Z); // Z 直接继承基准点
// 实测行为:坐标旋转正确,但 Angle = atan2(orign-before) + angle = 180+90 = 270,
// 与输入角度 90 不一致——atan2 量的是"原点→基准点"方向(此例 180°)。语义存疑,锁现状
Assert.Equal(270, result.Angle, 6);
}
[Fact]
public void GetOffsPoint_旋转0度_应返回基准点本身()
{
var origin = new XPoint { X = 1, Y = 2 };
var before = new XPoint { X = 4, Y = 6, Z = 3, Angle = 45 };
var result = XPoint.GetOffsPoint(origin, 0, before);
Assert.Equal(before.X, result.X, 6);
Assert.Equal(before.Y, result.Y, 6);
Assert.Equal(before.Z, result.Z);
}
[Fact]
public void 相等性_容差内相等_容差外不等()
{
var a = new XPoint { X = 1, Y = 2, Z = 3, Angle = 4 };
var b = new XPoint { X = 1.0000000001, Y = 2, Z = 3, Angle = 4 }; // 差1e-10 < 1e-9
var c = new XPoint { X = 1.001, Y = 2, Z = 3, Angle = 4 };
Assert.True(a == b);
Assert.False(a == c);
Assert.True(a.Equals(b));
Assert.Equal(a.GetHashCode(), b.GetHashCode()); // 相等的对象哈希必须相等
}
}