Fixed bug that SlotMap.Contains does not return a correct value.
Some checks failed
Publish NuGet Packages / publish (pull_request) Failing after 6s

This commit is contained in:
2025-11-11 17:38:30 +09:00
parent e97b295b05
commit bc8b2c0aaa
3 changed files with 116 additions and 2 deletions

View File

@@ -96,7 +96,9 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
_data = new UnsafeArray<SlotData>(capacity, ref handle, allocationOption); _data = new UnsafeArray<SlotData>(capacity, ref handle, allocationOption);
_freeSlots = new UnsafeQueue<int>(capacity, ref handle, allocationOption); _freeSlots = new UnsafeQueue<int>(capacity, ref handle, allocationOption);
_count = 0; _count = 0;
_capacity = capacity;
} }
/// <summary> /// <summary>
@@ -179,7 +181,7 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <param name="slotIndex">The zero-based index of the slot to check. Must be greater than or equal to 0 and less than the current capacity.</param> /// <param name="slotIndex">The zero-based index of the slot to check. Must be greater than or equal to 0 and less than the current capacity.</param>
/// <param name="generation">The generation value to compare against the slot's generation.</param> /// <param name="generation">The generation value to compare against the slot's generation.</param>
/// <returns>true if the slot at the specified index is valid and its generation matches the specified value; otherwise, false.</returns> /// <returns>true if the slot at the specified index is valid and its generation matches the specified value; otherwise, false.</returns>
public bool Contain(int slotIndex, int generation) public bool Contains(int slotIndex, int generation)
{ {
if (slotIndex < 0 || slotIndex >= Volatile.Read(ref _capacity)) if (slotIndex < 0 || slotIndex >= Volatile.Read(ref _capacity))
{ {

View File

@@ -6,7 +6,7 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Authors>Misaki</Authors> <Authors>Misaki</Authors>
<AssemblyVersion>1.1.0</AssemblyVersion> <AssemblyVersion>1.1.1</AssemblyVersion>
<Version>$(AssemblyVersion)</Version> <Version>$(AssemblyVersion)</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl> <PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>

View File

@@ -0,0 +1,112 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
namespace Misaki.HighPerformance.Test.UnitTest.Collections;
[TestClass]
public class TestUnsafeSlotMap
{
private UnsafeSlotMap<int> _slotMap;
[TestInitialize]
public void Initialize()
{
_slotMap = new UnsafeSlotMap<int>(16, Allocator.Persistent, AllocationOption.Clear);
}
[TestCleanup]
public void Cleanup()
{
_slotMap.Dispose();
}
[TestMethod]
public void Add()
{
var id = _slotMap.Add(10, out var gen);
Assert.IsTrue(_slotMap.Contains(id, gen));
}
[TestMethod]
public void Remove()
{
var id = _slotMap.Add(20, out var gen);
Assert.IsTrue(_slotMap.Contains(id, gen));
_slotMap.Remove(id, gen);
Assert.IsFalse(_slotMap.Contains(id, gen));
}
[TestMethod]
public void IndexReuse()
{
var id = _slotMap.Add(20, out var gen);
Assert.IsTrue(_slotMap.Contains(id, gen));
_slotMap.Remove(id, gen);
Assert.IsFalse(_slotMap.Contains(id, gen));
var newId = _slotMap.Add(30, out var newGen);
Assert.AreEqual(id, newId);
Assert.AreNotEqual(gen, newGen);
}
[TestMethod]
public unsafe void Resize()
{
const int count = 20;
var indices = stackalloc int[count];
var generations = stackalloc int[count];
for (var i = 0; i < count; i++)
{
indices[i] = _slotMap.Add(i, out generations[i]);
}
Assert.AreEqual(count, _slotMap.Count);
for (var i = 0; i < count; i++)
{
Assert.IsTrue(_slotMap.Contains(indices[i], generations[i]));
}
}
[TestMethod]
public void Clear()
{
var id1 = _slotMap.Add(10, out var gen1);
var id2 = _slotMap.Add(20, out var gen2);
Assert.AreEqual(2, _slotMap.Count);
_slotMap.Clear();
Assert.AreEqual(0, _slotMap.Count);
Assert.IsFalse(_slotMap.Contains(id1, gen1));
Assert.IsFalse(_slotMap.Contains(id2, gen2));
}
[TestMethod]
public unsafe void Enumerate()
{
const int count = 3;
var values = stackalloc int[count] { 10, 20, 30 };
var ids = stackalloc int[count];
var gens = stackalloc int[count];
for (var i = 0; i < count; i++)
{
ids[i] = _slotMap.Add(values[i], out gens[i]);
}
var index = 0;
foreach (var value in _slotMap)
{
Assert.AreEqual(values[index], value);
index++;
}
Assert.AreEqual(count, index);
}
}