49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using PLCSharp.Core.Tools;
|
||
|
||
namespace PLCSharp.Tests;
|
||
|
||
/// <summary>
|
||
/// BitTool 单元测试 —— 第一个冒烟测试,验证测试链路(还原→编译→运行)通。
|
||
/// </summary>
|
||
public class BitToolTests
|
||
{
|
||
[Fact]
|
||
public void GetBit_第0位_应返回true()
|
||
{
|
||
// uint 值 1 = 二进制只有第0位是1
|
||
Assert.True(((uint)1).GetBit(0));
|
||
Assert.False(((uint)1).GetBit(1));
|
||
}
|
||
|
||
[Fact]
|
||
public void GetBit_第3位_应按位返回()
|
||
{
|
||
// 8 = 二进制 1000,只有第3位是1
|
||
Assert.True(((uint)8).GetBit(3));
|
||
Assert.False(((uint)8).GetBit(2));
|
||
}
|
||
|
||
[Fact]
|
||
public void ToBitArray_全1_应32位全为true()
|
||
{
|
||
var bits = uint.MaxValue.ToBitArray();
|
||
Assert.Equal(32, bits.Length);
|
||
// 第31位是回归点:修复前 `1 << i` 在 i=31 时 int 溢出,该位永远为 false
|
||
Assert.All(bits, b => Assert.True(b));
|
||
}
|
||
|
||
[Fact]
|
||
public void GetBit_uint第31位_应返回true()
|
||
{
|
||
// 回归点:修复前 uint 版 GetBit 用 int 移位,index=31 时对 0xFFFFFFFF 返回 false
|
||
Assert.True(((uint)uint.MaxValue).GetBit(31));
|
||
}
|
||
|
||
[Fact]
|
||
public void GetBit_索引越界_应抛异常()
|
||
{
|
||
Assert.Throws<Exception>(() => ((uint)0).GetBit(32));
|
||
Assert.Throws<Exception>(() => ((uint)0).GetBit(-1));
|
||
}
|
||
}
|