修复 BitTool uint 版 bit31 int 移位溢出;新建 xUnit 测试项目并入解决方案

This commit is contained in:
tfarcraw 2026-10-21 20:42:25 +08:00
parent 6a9862bbbc
commit b93493a15f
5 changed files with 227 additions and 2 deletions

View File

@ -10,20 +10,53 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "解决方案项", "解决
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{0AB3BF05-4346-4AA6-1389-037BE0695223}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PLCSharp.Tests", "Tests\PLCSharp.Tests\PLCSharp.Tests.csproj", "{F151D949-979B-4193-BFB1-6A8B93A1946D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PLCSharpWpf", "PLCSharpWpf", "{E591F1A8-23FF-EC95-040F-C75762A9CC6E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Debug|x64.ActiveCfg = Debug|Any CPU
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Debug|x64.Build.0 = Debug|Any CPU
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Debug|x86.ActiveCfg = Debug|Any CPU
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Debug|x86.Build.0 = Debug|Any CPU
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Release|Any CPU.Build.0 = Release|Any CPU
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Release|x64.ActiveCfg = Release|Any CPU
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Release|x64.Build.0 = Release|Any CPU
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Release|x86.ActiveCfg = Release|Any CPU
{E621B227-1D8A-4CE9-81AE-22EDB7730FBA}.Release|x86.Build.0 = Release|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Debug|x64.ActiveCfg = Debug|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Debug|x64.Build.0 = Debug|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Debug|x86.ActiveCfg = Debug|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Debug|x86.Build.0 = Debug|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Release|Any CPU.Build.0 = Release|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Release|x64.ActiveCfg = Release|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Release|x64.Build.0 = Release|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Release|x86.ActiveCfg = Release|Any CPU
{F151D949-979B-4193-BFB1-6A8B93A1946D}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F151D949-979B-4193-BFB1-6A8B93A1946D} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1D07D9B6-65FC-441F-B18F-6692BC8E8BDD}
EndGlobalSection

View File

@ -17,7 +17,7 @@
bool[] bitArray = new bool[32];
for (var i = 0; i <= 31; i++)
{
var val = 1 << i;
var val = 1u << i;
bitArray[i] = (intValue & val) == val;
}
return bitArray;
@ -33,7 +33,7 @@
{
if (index > 31 || index < 0) throw new Exception("索引越界");
var val = 1 << index;
var val = 1u << index;
return (intValue & val) == val;
}

View File

@ -0,0 +1,48 @@
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));
}
}

View File

@ -0,0 +1,116 @@
#nullable enable
using PLCSharp.Core.Tools;
namespace PLCSharp.Tests;
/// <summary>
/// BitTool uint 区回归测试:修过的 ToBitArray/GetBit + 还没修的 SetBit/ResetBit。
/// </summary>
public class BitToolUintTests
{
[Fact]
public void GetBit_uint第31位_应为true()
{
Assert.True(((uint)uint.MaxValue).GetBit(31));
}
[Fact]
public void ToBitArray_uint全1_32位应全为true()
{
Assert.All(((uint)uint.MaxValue).ToBitArray(), b => Assert.True(b));
}
[Fact]
public void SetBit_uint第31位_应置位()
{
// 回归点:修复前 `1 << 31` int 溢出成负数,(uint)负数 = 0x80000000,行为碰巧对;
// 这里连同 GetBit 组合验证,防止改动后语义漂移
uint v = 0;
v.SetBit(31);
Assert.Equal(0x80000000u, v);
v.SetBit(0);
Assert.Equal(0x80000001u, v);
}
[Fact]
public void ResetBit_uint第31位_应清零()
{
// 回归点:修复前 `1 << 31` int 溢出为负,~(uint)负数 = 0x7FFFFFFF → 一个没碰的位被误清;
// 修复后语义正确:只清 bit31,其余保留
uint v = uint.MaxValue;
v.ResetBit(31);
Assert.Equal(0x7FFFFFFFu, v);
v.ResetBit(0);
Assert.Equal(0x7FFFFFFEu, v); // 0x7FFFFFFF - 1(u 后缀必须紧贴数字)
}
[Fact]
public void SetBit然后GetBit_uint全位往返一致()
{
uint v = 0;
for (var i = 0; i < 32; i++) v.SetBit(i);
Assert.Equal(uint.MaxValue, v);
for (var i = 0; i < 32; i++) Assert.True(v.GetBit(i), $"bit{i}");
}
[Fact]
public void SetBit然后ResetBit_uint全位往返一致()
{
uint v = uint.MaxValue;
for (var i = 0; i < 32; i++) v.ResetBit(i);
Assert.Equal(0u, v);
}
}
/// <summary>
/// BitTool int / byte 区行为快照(int 版 bit31 是符号位,语义待拍板,先锁现状)。
/// </summary>
public class BitToolIntByteTests
{
[Fact]
public void GetBit_int正常位()
{
Assert.True(((int)0b1010).GetBit(3));
Assert.False(((int)0b1010).GetBit(2));
}
[Fact]
public void ToBitArray_int全1_32位应全为true()
{
// int.MaxValue 只有 bit0-30;-1 才是全 1。修复前 int 版对 -1 的 bit31 返回 false
Assert.All((-1).ToBitArray(), b => Assert.True(b));
}
[Fact]
public void GetBit_byte全区往返()
{
byte v = 0b10000001;
Assert.True(v.GetBit(0));
Assert.True(v.GetBit(7));
Assert.False(v.GetBit(6));
}
[Fact]
public void Bit方法byte_按tf置位清零()
{
byte v = 0;
v.Bit(3, true);
Assert.Equal((byte)8, v);
v.Bit(3, false);
Assert.Equal((byte)0, v);
}
[Fact]
public void ResteBit_byte清零()
{
byte v = 0xFF;
v.ResteBit(7);
Assert.Equal((byte)0x7F, v);
}
[Fact]
public void ToBitArray_byte全1_8位应全为true()
{
Assert.All(byte.MaxValue.ToBitArray(), b => Assert.True(b));
}
}

View File

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<RestorePackagesPath>..\..\packages</RestorePackagesPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\PLCSharpWpf\PLCSharp.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>