backup
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Misaki.HighPerformance.Test.UnitTest.Buffer;
|
||||
|
||||
@@ -10,28 +9,28 @@ public unsafe class TestFreeList
|
||||
public void SingleThreadedAllocFreeTest()
|
||||
{
|
||||
using var freeList = new FreeList(8, 1024);
|
||||
|
||||
|
||||
// Allocate various sizes
|
||||
void* p1 = freeList.Allocate(16, 8);
|
||||
void* p2 = freeList.Allocate(32, 8);
|
||||
void* p3 = freeList.Allocate(64, 8);
|
||||
|
||||
var p1 = freeList.Allocate(16, 8);
|
||||
var p2 = freeList.Allocate(32, 8);
|
||||
var p3 = freeList.Allocate(64, 8);
|
||||
|
||||
Assert.IsTrue(p1 != null);
|
||||
Assert.IsTrue(p2 != null);
|
||||
Assert.IsTrue(p3 != null);
|
||||
|
||||
|
||||
// Free them
|
||||
freeList.Free(p1);
|
||||
freeList.Free(p2);
|
||||
freeList.Free(p3);
|
||||
|
||||
|
||||
// Allocate again - should reuse from buckets (or at least succeed)
|
||||
void* p4 = freeList.Allocate(16, 8);
|
||||
void* p5 = freeList.Allocate(32, 8);
|
||||
|
||||
var p4 = freeList.Allocate(16, 8);
|
||||
var p5 = freeList.Allocate(32, 8);
|
||||
|
||||
Assert.IsTrue(p4 != null);
|
||||
Assert.IsTrue(p5 != null);
|
||||
|
||||
|
||||
freeList.Free(p4);
|
||||
freeList.Free(p5);
|
||||
}
|
||||
@@ -42,23 +41,25 @@ public unsafe class TestFreeList
|
||||
const int threadCount = 8;
|
||||
const int iterations = 1000;
|
||||
using var freeList = new FreeList(8, 64 * 1024, threadCount);
|
||||
|
||||
|
||||
var threads = new Thread[threadCount];
|
||||
for (int i = 0; i < threadCount; i++)
|
||||
for (var i = 0; i < threadCount; i++)
|
||||
{
|
||||
threads[i] = new Thread(() =>
|
||||
{
|
||||
for (int j = 0; j < iterations; j++)
|
||||
for (var j = 0; j < iterations; j++)
|
||||
{
|
||||
void* ptr = freeList.Allocate(16, 8);
|
||||
var ptr = freeList.Allocate(16, 8);
|
||||
Assert.IsTrue(ptr != null);
|
||||
freeList.Free(ptr);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var t in threads) t.Start();
|
||||
foreach (var t in threads) t.Join();
|
||||
|
||||
foreach (var t in threads)
|
||||
t.Start();
|
||||
foreach (var t in threads)
|
||||
t.Join();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -68,27 +69,27 @@ public unsafe class TestFreeList
|
||||
const int consumerCount = 4;
|
||||
const int iterations = 5000;
|
||||
using var freeList = new FreeList(8, 64 * 1024, producerCount + consumerCount);
|
||||
|
||||
|
||||
var queue = new System.Collections.Concurrent.ConcurrentQueue<IntPtr>();
|
||||
var producers = new Thread[producerCount];
|
||||
var consumers = new Thread[consumerCount];
|
||||
|
||||
bool producing = true;
|
||||
|
||||
for (int i = 0; i < producerCount; i++)
|
||||
var producing = true;
|
||||
|
||||
for (var i = 0; i < producerCount; i++)
|
||||
{
|
||||
producers[i] = new Thread(() =>
|
||||
{
|
||||
for (int j = 0; j < iterations; j++)
|
||||
for (var j = 0; j < iterations; j++)
|
||||
{
|
||||
void* ptr = freeList.Allocate(32, 8);
|
||||
var ptr = freeList.Allocate(32, 8);
|
||||
Assert.IsTrue(ptr != null);
|
||||
queue.Enqueue((IntPtr)ptr);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (int i = 0; i < consumerCount; i++)
|
||||
for (var i = 0; i < consumerCount; i++)
|
||||
{
|
||||
consumers[i] = new Thread(() =>
|
||||
{
|
||||
@@ -106,12 +107,16 @@ public unsafe class TestFreeList
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var t in producers) t.Start();
|
||||
foreach (var t in consumers) t.Start();
|
||||
|
||||
foreach (var t in producers) t.Join();
|
||||
foreach (var t in producers)
|
||||
t.Start();
|
||||
foreach (var t in consumers)
|
||||
t.Start();
|
||||
|
||||
foreach (var t in producers)
|
||||
t.Join();
|
||||
Volatile.Write(ref producing, false);
|
||||
foreach (var t in consumers) t.Join();
|
||||
foreach (var t in consumers)
|
||||
t.Join();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -120,32 +125,34 @@ public unsafe class TestFreeList
|
||||
// Set maxConcurrencyLevel to 1, but use more threads
|
||||
const int threadCount = 5;
|
||||
using var freeList = new FreeList(8, 1024, 1);
|
||||
|
||||
|
||||
var threads = new Thread[threadCount];
|
||||
for (int i = 0; i < threadCount; i++)
|
||||
for (var i = 0; i < threadCount; i++)
|
||||
{
|
||||
threads[i] = new Thread(() =>
|
||||
{
|
||||
void* ptr = freeList.Allocate(16, 8);
|
||||
var ptr = freeList.Allocate(16, 8);
|
||||
Assert.IsTrue(ptr != null);
|
||||
freeList.Free(ptr);
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var t in threads) t.Start();
|
||||
foreach (var t in threads) t.Join();
|
||||
|
||||
foreach (var t in threads)
|
||||
t.Start();
|
||||
foreach (var t in threads)
|
||||
t.Join();
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void LargeAllocationTest()
|
||||
{
|
||||
using var freeList = new FreeList(8, 1024);
|
||||
|
||||
|
||||
// Allocate larger than default chunk size
|
||||
nuint largeSize = 2048;
|
||||
void* ptr = freeList.Allocate(largeSize, 8);
|
||||
var ptr = freeList.Allocate(largeSize, 8);
|
||||
Assert.IsTrue(ptr != null);
|
||||
|
||||
|
||||
freeList.Free(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public class TestConcurrentSlotMap
|
||||
{
|
||||
var slotIndex = _slotMap.Add(200, out var generation);
|
||||
Assert.IsTrue(_slotMap.Contains(slotIndex, generation));
|
||||
var removed = _slotMap.Remove(slotIndex, generation + 1); // Wrong generation
|
||||
var removed = _slotMap.Remove(slotIndex, generation + 1); // Wrong Generation
|
||||
Assert.IsFalse(removed);
|
||||
Assert.IsTrue(_slotMap.Contains(slotIndex, generation));
|
||||
}
|
||||
@@ -71,11 +71,11 @@ public class TestConcurrentSlotMap
|
||||
const int itemsPerThread = 1000;
|
||||
var tasks = new List<Task>();
|
||||
|
||||
for (int t = 0; t < threadCount; t++)
|
||||
for (var t = 0; t < threadCount; t++)
|
||||
{
|
||||
tasks.Add(Task.Run(() =>
|
||||
{
|
||||
for (int i = 0; i < itemsPerThread; i++)
|
||||
for (var i = 0; i < itemsPerThread; i++)
|
||||
{
|
||||
_slotMap.Add(i, out _);
|
||||
}
|
||||
@@ -98,11 +98,11 @@ public class TestConcurrentSlotMap
|
||||
|
||||
var count = 0;
|
||||
|
||||
for (int t = 0; t < threadCount; t++)
|
||||
for (var t = 0; t < threadCount; t++)
|
||||
{
|
||||
tasks.Add(Task.Run(() =>
|
||||
{
|
||||
for (int i = 0; i < operationsPerThread; i++)
|
||||
for (var i = 0; i < operationsPerThread; i++)
|
||||
{
|
||||
if (rand.NextDouble() < 0.5)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Misaki.HighPerformance.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Misaki.HighPerformance.Test.UnitTest.Collections;
|
||||
|
||||
@@ -43,7 +42,7 @@ public class TestSlotMap
|
||||
{
|
||||
var slotIndex = _slotMap.Add(200, out var generation);
|
||||
Assert.IsTrue(_slotMap.Contains(slotIndex, generation));
|
||||
var removed = _slotMap.Remove(slotIndex, generation + 1); // Wrong generation
|
||||
var removed = _slotMap.Remove(slotIndex, generation + 1); // Wrong Generation
|
||||
Assert.IsFalse(removed);
|
||||
Assert.IsTrue(_slotMap.Contains(slotIndex, generation));
|
||||
}
|
||||
|
||||
@@ -29,12 +29,12 @@ public class TestUnsafeArray
|
||||
[TestMethod]
|
||||
public void TestIndexAccess()
|
||||
{
|
||||
for (int i = 0; i < _arr.Count; i++)
|
||||
for (var i = 0; i < _arr.Count; i++)
|
||||
{
|
||||
_arr[i] = i * 10;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _arr.Count; i++)
|
||||
for (var i = 0; i < _arr.Count; i++)
|
||||
{
|
||||
Assert.AreEqual(i * 10, _arr[i]);
|
||||
}
|
||||
@@ -45,7 +45,7 @@ public class TestUnsafeArray
|
||||
{
|
||||
_arr.Clear();
|
||||
|
||||
int expectedValue = 0;
|
||||
var expectedValue = 0;
|
||||
foreach (var item in _arr)
|
||||
{
|
||||
Assert.AreEqual(expectedValue, item);
|
||||
|
||||
@@ -23,14 +23,14 @@ public class TestUnsafeStack
|
||||
[TestMethod]
|
||||
public void TestPushPop()
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
_stack.Push(i);
|
||||
}
|
||||
Assert.AreEqual(10, _stack.Count);
|
||||
for (int i = 9; i >= 0; i--)
|
||||
for (var i = 9; i >= 0; i--)
|
||||
{
|
||||
int value = _stack.Pop();
|
||||
var value = _stack.Pop();
|
||||
Assert.AreEqual(i, value);
|
||||
}
|
||||
Assert.AreEqual(0, _stack.Count);
|
||||
@@ -40,7 +40,7 @@ public class TestUnsafeStack
|
||||
public void TestPeek()
|
||||
{
|
||||
_stack.Push(42);
|
||||
int value = _stack.Peek();
|
||||
var value = _stack.Peek();
|
||||
Assert.AreEqual(42, value);
|
||||
Assert.AreEqual(1, _stack.Count);
|
||||
}
|
||||
@@ -48,12 +48,12 @@ public class TestUnsafeStack
|
||||
[TestMethod]
|
||||
public void TestEnumeration()
|
||||
{
|
||||
for (int i = 0; i < 5; i++)
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
_stack.Push(i);
|
||||
}
|
||||
|
||||
int expected = 4;
|
||||
var expected = 4;
|
||||
foreach (var item in _stack)
|
||||
{
|
||||
Assert.AreEqual(expected, item);
|
||||
|
||||
@@ -36,7 +36,7 @@ public static class CompressStoreTest
|
||||
);
|
||||
}
|
||||
|
||||
private unsafe static void TestPattern_Double(double[] input, bool[] keepPattern)
|
||||
private static unsafe void TestPattern_Double(double[] input, bool[] keepPattern)
|
||||
{
|
||||
// 1. Setup Input Vector
|
||||
// Handle case where Vector<TLane> is smaller than 8 (e.g. 2 or 4)
|
||||
|
||||
@@ -34,7 +34,7 @@ internal unsafe struct Vector2LerpJob : IJobSPMD<float>
|
||||
{
|
||||
var a = MathV.LoadVector2<TLane, float>(ref arrayA[baseIndex].x);
|
||||
var b = MathV.LoadVector2<TLane, float>(ref arrayB[baseIndex].x);
|
||||
|
||||
|
||||
var t = TLane.Create(0.5f);
|
||||
var lerped = MathV.Lerp(a, b, t);
|
||||
var len = TLane.Sqrt(MathV.LengthSquared(lerped));
|
||||
@@ -213,7 +213,7 @@ public class SPMDTest
|
||||
// Verify all normalized vectors have length ~1
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var length = math.sqrt(output[i].x * output[i].x + output[i].y * output[i].y +
|
||||
var length = math.sqrt(output[i].x * output[i].x + output[i].y * output[i].y +
|
||||
output[i].z * output[i].z + output[i].w * output[i].w);
|
||||
Assert.AreEqual(1.0f, length, 0.001f, $"Vector at index {i} is not normalized");
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
using Misaki.HighPerformance.Mathematics.SPMD;
|
||||
using Misaki.HighPerformance.Test.Jobs;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Test.UnitTest.Jobs;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user