diff options
Diffstat (limited to 'lib/Utils/tests')
-rw-r--r-- | lib/Utils/tests/Memory/MemoryHandleTest.cs | 8 | ||||
-rw-r--r-- | lib/Utils/tests/Memory/MemoryUtilTests.cs | 50 | ||||
-rw-r--r-- | lib/Utils/tests/Memory/NativeHeapTests.cs | 32 | ||||
-rw-r--r-- | lib/Utils/tests/Memory/VnTableTests.cs | 14 |
4 files changed, 82 insertions, 22 deletions
diff --git a/lib/Utils/tests/Memory/MemoryHandleTest.cs b/lib/Utils/tests/Memory/MemoryHandleTest.cs index f7ab8d4..f8d9b79 100644 --- a/lib/Utils/tests/Memory/MemoryHandleTest.cs +++ b/lib/Utils/tests/Memory/MemoryHandleTest.cs @@ -36,13 +36,15 @@ namespace VNLib.Utils.Memory.Tests { [TestMethod] - public void MemoryHandleAllocLongExtensionTest() + public unsafe void MemoryHandleAllocLongExtensionTest() { + Assert.IsTrue(sizeof(nuint) == 8); + //Check for negatives - Assert.ThrowsException<ArgumentOutOfRangeException>(() => Shared.Alloc<byte>(-1)); + Assert.ThrowsException<ArgumentOutOfRangeException>(() => Shared.Alloc<byte>(-1).Dispose()); //Make sure over-alloc throws - Assert.ThrowsException<OutOfMemoryException>(() => Shared.Alloc<byte>(nuint.MaxValue, false)); + Assert.ThrowsException<OverflowException>(() => Shared.Alloc<short>(nuint.MaxValue, false).Dispose()); } #if TARGET_64_BIT [TestMethod] diff --git a/lib/Utils/tests/Memory/MemoryUtilTests.cs b/lib/Utils/tests/Memory/MemoryUtilTests.cs index 10e5d31..473281f 100644 --- a/lib/Utils/tests/Memory/MemoryUtilTests.cs +++ b/lib/Utils/tests/Memory/MemoryUtilTests.cs @@ -20,20 +20,17 @@ namespace VNLib.Utils.Memory.Tests [TestMethod()] public void InitializeNewHeapForProcessTest() { - //Check if rpmalloc is loaded - if (MemoryUtil.IsRpMallocLoaded) - { - //Initialize the heap - using IUnmangedHeap heap = MemoryUtil.InitializeNewHeapForProcess(); - //Confirm that the heap is actually a rpmalloc heap - Assert.IsInstanceOfType(heap, typeof(RpMallocPrivateHeap)); - } - else - { - //Confirm that Rpmalloc will throw DLLNotFound if the lib is not loaded - Assert.ThrowsException<DllNotFoundException>(() => _ = RpMallocPrivateHeap.GlobalHeap.Alloc(1, 1, false)); - } + //Initialize the heap + using IUnmangedHeap heap = MemoryUtil.InitializeNewHeapForProcess(); + + //Test alloc + IntPtr block = heap.Alloc(1, 1, false); + + //Free block + heap.Free(ref block); + + //TODO verify the heap type by loading a dynamic heap dll } [TestMethod()] @@ -337,7 +334,7 @@ namespace VNLib.Utils.Memory.Tests public void GetSharedHeapStatsTest() { //Confirm heap diagnostics are enabled - Assert.AreEqual<string?>(Environment.GetEnvironmentVariable(MemoryUtil.SHARED_HEAP_ENABLE_DIAGNOISTICS_ENV), "1"); + Assert.AreEqual<string?>("1", Environment.GetEnvironmentVariable(MemoryUtil.SHARED_HEAP_ENABLE_DIAGNOISTICS_ENV)); //Get current stats HeapStatistics preTest = MemoryUtil.GetSharedHeapStats(); @@ -441,6 +438,29 @@ namespace VNLib.Utils.Memory.Tests const int TEST_1 = 1; //Unsafe byte test + using (UnsafeMemoryHandle<byte> byteBuffer = MemoryUtil.UnsafeAllocNearestPage(TEST_1, false)) + { + nuint byteSize = MemoryUtil.ByteSize(byteBuffer); + + //Confirm byte size is working also + Assert.IsTrue(byteSize == byteBuffer.Length); + + //Should be the same as the page size + Assert.IsTrue(byteSize == (nuint)Environment.SystemPageSize); + } + + using(IMemoryHandle<byte> safeByteBuffer = MemoryUtil.SafeAllocNearestPage(TEST_1, false)) + { + nuint byteSize = MemoryUtil.ByteSize(safeByteBuffer); + + //Confirm byte size is working also + Assert.IsTrue(byteSize == safeByteBuffer.Length); + + //Should be the same as the page size + Assert.IsTrue(byteSize == (nuint)Environment.SystemPageSize); + } + + //Unsafe byte test with generics using (UnsafeMemoryHandle<byte> byteBuffer = MemoryUtil.UnsafeAllocNearestPage<byte>(TEST_1, false)) { nuint byteSize = MemoryUtil.ByteSize(byteBuffer); @@ -452,7 +472,7 @@ namespace VNLib.Utils.Memory.Tests Assert.IsTrue(byteSize == (nuint)Environment.SystemPageSize); } - using(IMemoryHandle<byte> safeByteBuffer = MemoryUtil.SafeAllocNearestPage<byte>(TEST_1, false)) + using (IMemoryHandle<byte> safeByteBuffer = MemoryUtil.SafeAllocNearestPage<byte>(TEST_1, false)) { nuint byteSize = MemoryUtil.ByteSize(safeByteBuffer); diff --git a/lib/Utils/tests/Memory/NativeHeapTests.cs b/lib/Utils/tests/Memory/NativeHeapTests.cs new file mode 100644 index 0000000..d27d5fd --- /dev/null +++ b/lib/Utils/tests/Memory/NativeHeapTests.cs @@ -0,0 +1,32 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using System; + +namespace VNLib.Utils.Memory.Tests +{ + [TestClass()] + public class NativeHeapTests + { + [TestMethod()] + public void LoadHeapTest() + { + const string TEST_HEAP_FILENAME = @"rpmalloc.dll"; + + //Try to load the global heap + using NativeHeap heap = NativeHeap.LoadHeap(TEST_HEAP_FILENAME, System.Runtime.InteropServices.DllImportSearchPath.SafeDirectories, HeapCreation.None, 0); + + Assert.IsFalse(heap.IsInvalid); + + IntPtr block = heap.Alloc(100, sizeof(byte), false); + + Assert.IsTrue(block != IntPtr.Zero); + + //Free the block + Assert.IsTrue(heap.Free(ref block)); + + //confirm the pointer it zeroed + Assert.IsTrue(block == IntPtr.Zero); + + } + } +}
\ No newline at end of file diff --git a/lib/Utils/tests/Memory/VnTableTests.cs b/lib/Utils/tests/Memory/VnTableTests.cs index 474a201..cb8ea91 100644 --- a/lib/Utils/tests/Memory/VnTableTests.cs +++ b/lib/Utils/tests/Memory/VnTableTests.cs @@ -52,12 +52,18 @@ namespace VNLib.Utils.Memory.Tests Assert.IsTrue(10000 == table.Cols); } - - //Test oom, should be native - Assert.ThrowsException<OutOfMemoryException>(() => + try { using VnTable<int> table = new(uint.MaxValue, 20); - }); + + Assert.Fail("The table allocation did not fail as expected"); + } + catch (OutOfMemoryException) + {} + catch(Exception ex) + { + Assert.Fail("Table overflow creation test failed because another exception type was raised, {0}", ex.GetType().Name); + } } [TestMethod()] |