aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils/tests
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-01-14 16:24:28 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2023-01-14 16:24:28 -0500
commit52b8e30437e235817ed534dec860e781bb0468c0 (patch)
treeb279418c23daec838fab6bf8a0b29c3091d2e300 /lib/Utils/tests
parent0dcecff0f51bdb51b070c05255fbcd9338ab11f2 (diff)
MemoryUtil native integer size update + tests
Diffstat (limited to 'lib/Utils/tests')
-rw-r--r--lib/Utils/tests/Memory/MemoryHandleTest.cs25
-rw-r--r--lib/Utils/tests/Memory/MemoryTests.cs244
-rw-r--r--lib/Utils/tests/Memory/MemoryUtilTests.cs333
-rw-r--r--lib/Utils/tests/Memory/VnTableTests.cs51
-rw-r--r--lib/Utils/tests/VnEncodingTests.cs6
5 files changed, 364 insertions, 295 deletions
diff --git a/lib/Utils/tests/Memory/MemoryHandleTest.cs b/lib/Utils/tests/Memory/MemoryHandleTest.cs
index 02ef1f1..34dbb60 100644
--- a/lib/Utils/tests/Memory/MemoryHandleTest.cs
+++ b/lib/Utils/tests/Memory/MemoryHandleTest.cs
@@ -25,10 +25,9 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
-using VNLib.Utils;
using VNLib.Utils.Extensions;
-using static VNLib.Utils.Memory.Memory;
+using static VNLib.Utils.Memory.MemoryUtil;
namespace VNLib.Utils.Memory.Tests
{
@@ -43,7 +42,7 @@ namespace VNLib.Utils.Memory.Tests
Assert.ThrowsException<ArgumentOutOfRangeException>(() => Shared.Alloc<byte>(-1));
//Make sure over-alloc throws
- Assert.ThrowsException<NativeMemoryOutOfMemoryException>(() => Shared.Alloc<byte>(ulong.MaxValue, false));
+ Assert.ThrowsException<NativeMemoryOutOfMemoryException>(() => Shared.Alloc<byte>(nuint.MaxValue, false));
}
#if TARGET_64_BIT
[TestMethod]
@@ -54,9 +53,9 @@ namespace VNLib.Utils.Memory.Tests
using MemoryHandle<byte> handle = Shared.Alloc<byte>(bigHandleSize);
//verify size
- Assert.AreEqual(handle.ByteLength, (ulong)bigHandleSize);
+ Assert.IsTrue(handle.ByteLength, (ulong)bigHandleSize);
//Since handle is byte, should also match
- Assert.AreEqual(handle.Length, (ulong)bigHandleSize);
+ Assert.IsTrue(handle.Length, (ulong)bigHandleSize);
//Should throw overflow
Assert.ThrowsException<OverflowException>(() => _ = handle.Span);
@@ -68,8 +67,6 @@ namespace VNLib.Utils.Memory.Tests
Assert.ThrowsException<ArgumentOutOfRangeException>(() => _ = handle.GetOffsetSpan((long)int.MaxValue + 1, 1024));
}
-#else
-
#endif
[TestMethod]
@@ -77,15 +74,15 @@ namespace VNLib.Utils.Memory.Tests
{
using MemoryHandle<byte> handle = Shared.Alloc<byte>(128, true);
- Assert.AreEqual(handle.IntLength, 128);
+ Assert.IsTrue(handle.Length == 128);
- Assert.AreEqual(handle.Length, (ulong)128);
+ Assert.IsTrue(handle.Length == 128);
//Check span against base pointer deref
handle.Span[120] = 10;
- Assert.AreEqual(*handle.GetOffset(120), 10);
+ Assert.IsTrue(*handle.GetOffset(120) == 10);
}
@@ -153,14 +150,14 @@ namespace VNLib.Utils.Memory.Tests
{
using MemoryHandle<byte> handle = Shared.Alloc<byte>(1024);
- Assert.AreEqual(handle.IntLength, 1024);
+ Assert.IsTrue(handle.Length == 1024);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => handle.Resize(-1));
//Resize the handle
handle.Resize(2048);
- Assert.AreEqual(handle.IntLength, 2048);
+ Assert.IsTrue(handle.Length == 2048);
Assert.IsTrue(handle.AsSpan(2048).IsEmpty);
@@ -173,11 +170,11 @@ namespace VNLib.Utils.Memory.Tests
//test resize
handle.ResizeIfSmaller(100);
//Handle should be unmodified
- Assert.AreEqual(handle.IntLength, 2048);
+ Assert.IsTrue(handle.Length == 2048);
//test working
handle.ResizeIfSmaller(4096);
- Assert.AreEqual(handle.IntLength, 4096);
+ Assert.IsTrue(handle.Length == 4096);
}
}
}
diff --git a/lib/Utils/tests/Memory/MemoryTests.cs b/lib/Utils/tests/Memory/MemoryTests.cs
deleted file mode 100644
index 5b68cf5..0000000
--- a/lib/Utils/tests/Memory/MemoryTests.cs
+++ /dev/null
@@ -1,244 +0,0 @@
-/*
-* Copyright (c) 2022 Vaughn Nugent
-*
-* Library: VNLib
-* Package: VNLib.UtilsTests
-* File: MemoryTests.cs
-*
-* MemoryTests.cs is part of VNLib.UtilsTests which is part of the larger
-* VNLib collection of libraries and utilities.
-*
-* VNLib.UtilsTests is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published
-* by the Free Software Foundation, either version 2 of the License,
-* or (at your option) any later version.
-*
-* VNLib.UtilsTests is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-* General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with VNLib.UtilsTests. If not, see http://www.gnu.org/licenses/.
-*/
-
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-using System.Runtime.InteropServices;
-
-using VNLib.Utils.Extensions;
-
-namespace VNLib.Utils.Memory.Tests
-{
- [TestClass()]
- public class MemoryTests
- {
- [TestMethod]
- public void MemorySharedHeapLoadedTest()
- {
- Assert.IsNotNull(Memory.Shared);
- }
-
- [TestMethod()]
- public void UnsafeAllocTest()
- {
- //test against negative number
- Assert.ThrowsException<ArgumentException>(() => Memory.UnsafeAlloc<byte>(-1));
-
- //Alloc large block test (100mb)
- const int largTestSize = 100000 * 1024;
- //Alloc super small block
- const int smallTestSize = 5;
-
- using (UnsafeMemoryHandle<byte> buffer = Memory.UnsafeAlloc<byte>(largTestSize, false))
- {
- Assert.AreEqual(largTestSize, buffer.IntLength);
- Assert.AreEqual(largTestSize, buffer.Span.Length);
-
- buffer.Span[0] = 254;
- Assert.AreEqual(buffer.Span[0], 254);
- }
-
- using (UnsafeMemoryHandle<byte> buffer = Memory.UnsafeAlloc<byte>(smallTestSize, false))
- {
- Assert.AreEqual(smallTestSize, buffer.IntLength);
- Assert.AreEqual(smallTestSize, buffer.Span.Length);
-
- buffer.Span[0] = 254;
- Assert.AreEqual(buffer.Span[0], 254);
- }
-
- //Different data type
-
- using(UnsafeMemoryHandle<long> buffer = Memory.UnsafeAlloc<long>(largTestSize, false))
- {
- Assert.AreEqual(largTestSize, buffer.IntLength);
- Assert.AreEqual(largTestSize, buffer.Span.Length);
-
- buffer.Span[0] = long.MaxValue;
- Assert.AreEqual(buffer.Span[0], long.MaxValue);
- }
-
- using (UnsafeMemoryHandle<long> buffer = Memory.UnsafeAlloc<long>(smallTestSize, false))
- {
- Assert.AreEqual(smallTestSize, buffer.IntLength);
- Assert.AreEqual(smallTestSize, buffer.Span.Length);
-
- buffer.Span[0] = long.MaxValue;
- Assert.AreEqual(buffer.Span[0], long.MaxValue);
- }
- }
-
- [TestMethod()]
- public void UnsafeZeroMemoryAsSpanTest()
- {
- //Alloc test buffer
- Span<byte> test = new byte[1024];
- test.Fill(0);
- //test other empty span
- Span<byte> verify = new byte[1024];
- verify.Fill(0);
-
- //Fill test buffer with random values
- Random.Shared.NextBytes(test);
-
- //make sure buffers are not equal
- Assert.IsFalse(test.SequenceEqual(verify));
-
- //Zero buffer
- Memory.UnsafeZeroMemory<byte>(test);
-
- //Make sure buffers are equal
- Assert.IsTrue(test.SequenceEqual(verify));
- }
-
- [TestMethod()]
- public void UnsafeZeroMemoryAsMemoryTest()
- {
- //Alloc test buffer
- Memory<byte> test = new byte[1024];
- test.Span.Fill(0);
- //test other empty span
- Memory<byte> verify = new byte[1024];
- verify.Span.Fill(0);
-
- //Fill test buffer with random values
- Random.Shared.NextBytes(test.Span);
-
- //make sure buffers are not equal
- Assert.IsFalse(test.Span.SequenceEqual(verify.Span));
-
- //Zero buffer
- Memory.UnsafeZeroMemory<byte>(test);
-
- //Make sure buffers are equal
- Assert.IsTrue(test.Span.SequenceEqual(verify.Span));
- }
-
- [TestMethod()]
- public void InitializeBlockAsSpanTest()
- {
- //Alloc test buffer
- Span<byte> test = new byte[1024];
- test.Fill(0);
- //test other empty span
- Span<byte> verify = new byte[1024];
- verify.Fill(0);
-
- //Fill test buffer with random values
- Random.Shared.NextBytes(test);
-
- //make sure buffers are not equal
- Assert.IsFalse(test.SequenceEqual(verify));
-
- //Zero buffer
- Memory.InitializeBlock(test);
-
- //Make sure buffers are equal
- Assert.IsTrue(test.SequenceEqual(verify));
- }
-
- [TestMethod()]
- public void InitializeBlockMemoryTest()
- {
- //Alloc test buffer
- Memory<byte> test = new byte[1024];
- test.Span.Fill(0);
- //test other empty span
- Memory<byte> verify = new byte[1024];
- verify.Span.Fill(0);
-
- //Fill test buffer with random values
- Random.Shared.NextBytes(test.Span);
-
- //make sure buffers are not equal
- Assert.IsFalse(test.Span.SequenceEqual(verify.Span));
-
- //Zero buffer
- Memory.InitializeBlock(test);
-
- //Make sure buffers are equal
- Assert.IsTrue(test.Span.SequenceEqual(verify.Span));
- }
-
- #region structmemory tests
-
- [StructLayout(LayoutKind.Sequential)]
- struct TestStruct
- {
- public int X;
- public int Y;
- }
-
- [TestMethod()]
- public unsafe void ZeroStructAsPointerTest()
- {
- TestStruct* s = Memory.Shared.StructAlloc<TestStruct>();
- s->X = 10;
- s->Y = 20;
- Assert.AreEqual(10, s->X);
- Assert.AreEqual(20, s->Y);
- //zero struct
- Memory.ZeroStruct(s);
- //Verify data was zeroed
- Assert.AreEqual(0, s->X);
- Assert.AreEqual(0, s->Y);
- //Free struct
- Memory.Shared.StructFree(s);
- }
-
- [TestMethod()]
- public unsafe void ZeroStructAsVoidPointerTest()
- {
- TestStruct* s = Memory.Shared.StructAlloc<TestStruct>();
- s->X = 10;
- s->Y = 20;
- Assert.AreEqual(10, s->X);
- Assert.AreEqual(20, s->Y);
- //zero struct
- Memory.ZeroStruct<TestStruct>((void*)s);
- //Verify data was zeroed
- Assert.AreEqual(0, s->X);
- Assert.AreEqual(0, s->Y);
- //Free struct
- Memory.Shared.StructFree(s);
- }
-
- [TestMethod()]
- public unsafe void ZeroStructAsIntPtrTest()
- {
- TestStruct* s = Memory.Shared.StructAlloc<TestStruct>();
- s->X = 10;
- s->Y = 20;
- Assert.AreEqual(10, s->X);
- Assert.AreEqual(20, s->Y);
- //zero struct
- Memory.ZeroStruct<TestStruct>((IntPtr)s);
- //Verify data was zeroed
- Assert.AreEqual(0, s->X);
- Assert.AreEqual(0, s->Y);
- //Free struct
- Memory.Shared.StructFree(s);
- }
- #endregion
- }
-} \ No newline at end of file
diff --git a/lib/Utils/tests/Memory/MemoryUtilTests.cs b/lib/Utils/tests/Memory/MemoryUtilTests.cs
new file mode 100644
index 0000000..fb3700e
--- /dev/null
+++ b/lib/Utils/tests/Memory/MemoryUtilTests.cs
@@ -0,0 +1,333 @@
+using System.Buffers;
+using System.Runtime.InteropServices;
+using System.Security.Cryptography;
+
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+using VNLib.Utils.Extensions;
+
+namespace VNLib.Utils.Memory.Tests
+{
+ [TestClass()]
+ public class MemoryUtilTests
+ {
+ const int ZERO_TEST_LOOP_ITERATIONS = 1000000;
+ const int ZERO_TEST_MAX_BUFFER_SIZE = 10 * 1024;
+
+ [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));
+ }
+ }
+
+ [TestMethod()]
+ public void UnsafeZeroMemoryTest()
+ {
+ //Get random data buffer as a readonly span
+ ReadOnlyMemory<byte> buffer = RandomNumberGenerator.GetBytes(1024);
+
+ //confirm buffer is not all zero
+ Assert.IsFalse(AllZero(buffer.Span));
+
+ //Zero readonly memory
+ MemoryUtil.UnsafeZeroMemory(buffer);
+
+ //Confirm all zero
+ Assert.IsTrue(AllZero(buffer.Span));
+ }
+
+ private static bool AllZero(ReadOnlySpan<byte> span)
+ {
+ for (int i = 0; i < span.Length; i++)
+ {
+ if (span[i] != 0)
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ [TestMethod()]
+ public void UnsafeZeroMemoryTest1()
+ {
+ //Get random data buffer as a readonly span
+ ReadOnlySpan<byte> buffer = RandomNumberGenerator.GetBytes(1024);
+
+ //confirm buffer is not all zero
+ Assert.IsFalse(AllZero(buffer));
+
+ //Zero readonly span
+ MemoryUtil.UnsafeZeroMemory(buffer);
+
+ //Confirm all zero
+ Assert.IsTrue(AllZero(buffer));
+ }
+
+
+ [TestMethod()]
+ public void InitializeBlockAsSpanTest()
+ {
+ //Get random data buffer as a readonly span
+ Span<byte> buffer = RandomNumberGenerator.GetBytes(1024);
+
+ //confirm buffer is not all zero
+ Assert.IsFalse(AllZero(buffer));
+
+ //Zero readonly span
+ MemoryUtil.InitializeBlock(buffer);
+
+ //Confirm all zero
+ Assert.IsTrue(AllZero(buffer));
+ }
+
+ [TestMethod()]
+ public void InitializeBlockMemoryTest()
+ {
+ //Get random data buffer as a readonly span
+ Memory<byte> buffer = RandomNumberGenerator.GetBytes(1024);
+
+ //confirm buffer is not all zero
+ Assert.IsFalse(AllZero(buffer.Span));
+
+ //Zero readonly span
+ MemoryUtil.InitializeBlock(buffer);
+
+ //Confirm all zero
+ Assert.IsTrue(AllZero(buffer.Span));
+ }
+
+
+ [TestMethod()]
+ public unsafe void UnsafeAllocTest()
+ {
+ //No fail
+ using (UnsafeMemoryHandle<byte> handle = MemoryUtil.UnsafeAlloc<byte>(1024))
+ {
+ _ = handle.Span;
+ _ = handle.Length;
+ _ = handle.IntLength;
+
+ //Test span pointer against pinned handle
+ using (MemoryHandle pinned = handle.Pin(0))
+ {
+ fixed (void* ptr = &MemoryMarshal.GetReference(handle.Span))
+ {
+ Assert.IsTrue(ptr == pinned.Pointer);
+ }
+ }
+
+ //Test negative pin
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() => _ = handle.Pin(-1));
+
+ //Test pinned outsie handle size
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() => _ = handle.Pin(1024));
+ }
+
+ //test against negative number
+ Assert.ThrowsException<ArgumentException>(() => MemoryUtil.UnsafeAlloc<byte>(-1));
+
+ //Alloc large block test (100mb)
+ const int largTestSize = 100000 * 1024;
+ //Alloc super small block
+ const int smallTestSize = 5;
+
+ using (UnsafeMemoryHandle<byte> buffer = MemoryUtil.UnsafeAlloc<byte>(largTestSize, false))
+ {
+ Assert.IsTrue(largTestSize == buffer.IntLength);
+ Assert.IsTrue(largTestSize == buffer.Span.Length);
+
+ buffer.Span[0] = 254;
+ Assert.IsTrue(buffer.Span[0] == 254);
+ }
+
+ using (UnsafeMemoryHandle<byte> buffer = MemoryUtil.UnsafeAlloc<byte>(smallTestSize, false))
+ {
+ Assert.IsTrue(smallTestSize == buffer.IntLength);
+ Assert.IsTrue(smallTestSize == buffer.Span.Length);
+
+ buffer.Span[0] = 254;
+ Assert.IsTrue(buffer.Span[0] == 254);
+ }
+
+ //Different data type
+ using (UnsafeMemoryHandle<long> buffer = MemoryUtil.UnsafeAlloc<long>(largTestSize, false))
+ {
+ Assert.IsTrue(largTestSize == buffer.IntLength);
+ Assert.IsTrue(largTestSize == buffer.Span.Length);
+
+ buffer.Span[0] = long.MaxValue;
+ Assert.IsTrue(buffer.Span[0] == long.MaxValue);
+ }
+
+ using (UnsafeMemoryHandle<long> buffer = MemoryUtil.UnsafeAlloc<long>(smallTestSize, false))
+ {
+ Assert.IsTrue(smallTestSize == buffer.IntLength);
+ Assert.IsTrue(smallTestSize == buffer.Span.Length);
+
+ buffer.Span[0] = long.MaxValue;
+ Assert.IsTrue(buffer.Span[0] == long.MaxValue);
+ }
+
+ //Test empty handle
+ using (UnsafeMemoryHandle<byte> empty = new())
+ {
+ Assert.IsTrue(0 == empty.Length);
+ Assert.IsTrue(0 == empty.IntLength);
+
+ //Test pinning while empty
+ Assert.ThrowsException<InvalidOperationException>(() => _ = empty.Pin(0));
+ }
+
+ //Negative value
+ Assert.ThrowsException<ArgumentException>(() => _ = MemoryUtil.UnsafeAlloc<byte>(-1));
+
+
+ /*
+ * Alloc random sized blocks in a loop, confirm they are empty
+ * then fill the block with random data before freeing it back to
+ * the pool. This confirms that if blocks are allocated from a shared
+ * pool are properly zeroed when requestd
+ */
+
+ for (int i = 0; i < ZERO_TEST_LOOP_ITERATIONS; i++)
+ {
+ int randBufferSize = Random.Shared.Next(1024, ZERO_TEST_MAX_BUFFER_SIZE);
+
+ //Alloc block, check if all zero, then free
+ using UnsafeMemoryHandle<byte> handle = MemoryUtil.UnsafeAlloc<byte>(randBufferSize, true);
+
+ //Confirm all zero
+ Assert.IsTrue(AllZero(handle.Span));
+
+ //Fill with random data
+ Random.Shared.NextBytes(handle.Span);
+ }
+ }
+
+ [TestMethod()]
+ public unsafe void SafeAllocTest()
+ {
+ //No fail
+ using (IMemoryHandle<byte> handle = MemoryUtil.SafeAlloc<byte>(1024))
+ {
+ _ = handle.Span;
+ _ = handle.Length;
+ _ = handle.GetIntLength();
+
+ //Test span pointer against pinned handle
+ using (MemoryHandle pinned = handle.Pin(0))
+ {
+ fixed (void* ptr = &MemoryMarshal.GetReference(handle.Span))
+ {
+ Assert.IsTrue(ptr == pinned.Pointer);
+ }
+ }
+
+ //Test negative pin
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() => _ = handle.Pin(-1));
+
+ //Test pinned outsie handle size
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() => _ = handle.Pin(1024));
+ }
+
+
+ //Negative value
+ Assert.ThrowsException<ArgumentException>(() => _ = MemoryUtil.SafeAlloc<byte>(-1));
+
+
+ /*
+ * Alloc random sized blocks in a loop, confirm they are empty
+ * then fill the block with random data before freeing it back to
+ * the pool. This confirms that if blocks are allocated from a shared
+ * pool are properly zeroed when requestd
+ */
+
+ for (int i = 0; i < ZERO_TEST_LOOP_ITERATIONS; i++)
+ {
+ int randBufferSize = Random.Shared.Next(1024, ZERO_TEST_MAX_BUFFER_SIZE);
+
+ //Alloc block, check if all zero, then free
+ using IMemoryHandle<byte> handle = MemoryUtil.SafeAlloc<byte>(randBufferSize, true);
+
+ //Confirm all zero
+ Assert.IsTrue(AllZero(handle.Span));
+
+ //Fill with random data
+ Random.Shared.NextBytes(handle.Span);
+ }
+ }
+
+
+ [StructLayout(LayoutKind.Sequential)]
+ struct TestStruct
+ {
+ public int X;
+ public int Y;
+ }
+
+ [TestMethod()]
+ public unsafe void ZeroStructAsPointerTest()
+ {
+ TestStruct* s = MemoryUtil.Shared.StructAlloc<TestStruct>();
+ s->X = 10;
+ s->Y = 20;
+ Assert.IsTrue(10 == s->X);
+ Assert.IsTrue(20 == s->Y);
+ //zero struct
+ MemoryUtil.ZeroStruct(s);
+ //Verify data was zeroed
+ Assert.IsTrue(0 == s->X);
+ Assert.IsTrue(0 == s->Y);
+ //Free struct
+ MemoryUtil.Shared.StructFree(s);
+ }
+
+ [TestMethod()]
+ public unsafe void ZeroStructAsVoidPointerTest()
+ {
+ TestStruct* s = MemoryUtil.Shared.StructAlloc<TestStruct>();
+ s->X = 10;
+ s->Y = 20;
+ Assert.IsTrue(10 == s->X);
+ Assert.IsTrue(20 == s->Y);
+ //zero struct
+ MemoryUtil.ZeroStruct<TestStruct>((void*)s);
+ //Verify data was zeroed
+ Assert.IsTrue(0 == s->X);
+ Assert.IsTrue(0 == s->Y);
+ //Free struct
+ MemoryUtil.Shared.StructFree(s);
+ }
+
+ [TestMethod()]
+ public unsafe void ZeroStructAsIntPtrTest()
+ {
+ TestStruct* s = MemoryUtil.Shared.StructAlloc<TestStruct>();
+ s->X = 10;
+ s->Y = 20;
+ Assert.IsTrue(10 == s->X);
+ Assert.IsTrue(20 == s->Y);
+ //zero struct
+ MemoryUtil.ZeroStruct<TestStruct>((IntPtr)s);
+ //Verify data was zeroed
+ Assert.IsTrue(0 == s->X);
+ Assert.IsTrue(0 == s->Y);
+ //Free struct
+ MemoryUtil.Shared.StructFree(s);
+ }
+ }
+} \ No newline at end of file
diff --git a/lib/Utils/tests/Memory/VnTableTests.cs b/lib/Utils/tests/Memory/VnTableTests.cs
index 11350d4..c9f99ea 100644
--- a/lib/Utils/tests/Memory/VnTableTests.cs
+++ b/lib/Utils/tests/Memory/VnTableTests.cs
@@ -33,26 +33,13 @@ namespace VNLib.Utils.Memory.Tests
[TestMethod()]
public void VnTableTest()
{
- Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
- {
- using VnTable<int> table = new(-1, 0);
- });
- Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
- {
- using VnTable<int> table = new(0, -1);
- });
- Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
- {
- using VnTable<int> table = new(-1, -1);
- });
-
//Empty table
using (VnTable<int> empty = new(0, 0))
{
Assert.IsTrue(empty.Empty);
//Test 0 rows/cols
- Assert.AreEqual(0, empty.Rows);
- Assert.AreEqual(0, empty.Cols);
+ Assert.IsTrue(0 == empty.Rows);
+ Assert.IsTrue(0 == empty.Cols);
}
using (VnTable<int> table = new(40000, 10000))
@@ -60,8 +47,8 @@ namespace VNLib.Utils.Memory.Tests
Assert.IsFalse(table.Empty);
//Test table size
- Assert.AreEqual(40000, table.Rows);
- Assert.AreEqual(10000, table.Cols);
+ Assert.IsTrue(40000 == table.Rows);
+ Assert.IsTrue(10000 == table.Cols);
}
@@ -89,41 +76,41 @@ namespace VNLib.Utils.Memory.Tests
[TestMethod()]
public void GetSetTest()
{
- static void TestIndexAt(VnTable<int> table, int row, int col, int value)
+ static void TestIndexAt(VnTable<int> table, uint row, uint col, int value)
{
table[row, col] = value;
- Assert.AreEqual(value, table[row, col]);
- Assert.AreEqual(value, table.Get(row, col));
+ Assert.IsTrue(value == table[row, col]);
+ Assert.IsTrue(value == table.Get(row, col));
}
- static void TestSetAt(VnTable<int> table, int row, int col, int value)
+ static void TestSetAt(VnTable<int> table, uint row, uint col, int value)
{
table.Set(row, col, value);
- Assert.AreEqual(value, table[row, col]);
- Assert.AreEqual(value, table.Get(row, col));
+ Assert.IsTrue(value == table[row, col]);
+ Assert.IsTrue(value == table.Get(row, col));
}
- static void TestSetDirectAccess(VnTable<int> table, int row, int col, int value)
+ static void TestSetDirectAccess(VnTable<int> table, uint row, uint col, int value)
{
- int address = row * table.Cols + col;
- table[(uint)address] = value;
+ uint address = row * table.Cols + col;
+ table[address] = value;
//Get value using indexer
- Assert.AreEqual(value, table[row, col]);
+ Assert.IsTrue(value == table[row, col]);
}
- static void TestGetDirectAccess(VnTable<int> table, int row, int col, int value)
+ static void TestGetDirectAccess(VnTable<int> table, uint row, uint col, int value)
{
table[row, col] = value;
- int address = row * table.Cols + col;
+ uint address = row * table.Cols + col;
//Test direct access
- Assert.AreEqual(value, table[(uint)address]);
+ Assert.IsTrue(value == table[address]);
//Get value using indexer
- Assert.AreEqual(value, table[row, col]);
- Assert.AreEqual(value, table.Get(row, col));
+ Assert.IsTrue(value == table[row, col]);
+ Assert.IsTrue(value == table.Get(row, col));
}
diff --git a/lib/Utils/tests/VnEncodingTests.cs b/lib/Utils/tests/VnEncodingTests.cs
index a4e52f0..373b834 100644
--- a/lib/Utils/tests/VnEncodingTests.cs
+++ b/lib/Utils/tests/VnEncodingTests.cs
@@ -23,16 +23,12 @@
*/
using System;
+using System.Text;
using System.Buffers;
using System.Buffers.Text;
-using System.Collections.Generic;
-using System.Linq;
using System.Security.Cryptography;
-using System.Text;
-using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
-using VNLib.Utils;
namespace VNLib.Utils.Tests
{