From 322bbe00f77772ba6b0e25759de95dd517b6014c Mon Sep 17 00:00:00 2001 From: vnugent Date: Sat, 14 Sep 2024 15:43:45 -0400 Subject: build: Testing updates and easier dev-testing --- lib/Utils/src/ERRNO.cs | 25 ++++++++++++++----------- lib/Utils/src/Memory/MemoryUtil.cs | 3 ++- lib/Utils/tests/.runsettings | 3 +++ lib/Utils/tests/IO/VnMemoryStreamTests.cs | 15 ++++++--------- lib/Utils/tests/Memory/NativeHeapTests.cs | 10 ++++++---- 5 files changed, 31 insertions(+), 25 deletions(-) (limited to 'lib/Utils') diff --git a/lib/Utils/src/ERRNO.cs b/lib/Utils/src/ERRNO.cs index 684a3c7..0a95780 100644 --- a/lib/Utils/src/ERRNO.cs +++ b/lib/Utils/src/ERRNO.cs @@ -30,8 +30,12 @@ namespace VNLib.Utils /// /// Implements a C style integer error code type. Size is platform dependent /// + /// + /// Creates a new from the specified error value + /// + /// The value of the error to represent [StructLayout(LayoutKind.Sequential)] - public readonly struct ERRNO : IEquatable, ISpanFormattable, IFormattable + public readonly struct ERRNO(nint errno) : IEquatable, ISpanFormattable, IFormattable { /// /// Represents a successfull error code (true) @@ -43,13 +47,7 @@ namespace VNLib.Utils /// public static readonly ERRNO E_FAIL = false; - private readonly nint ErrorCode; - - /// - /// Creates a new from the specified error value - /// - /// The value of the error to represent - public ERRNO(nint errno) => ErrorCode = errno; + private readonly nint ErrorCode = errno; /// /// Creates a new from an error code. null = 0 = false @@ -130,13 +128,14 @@ namespace VNLib.Utils } return false; } +#pragma warning disable CA1305 // Specify IFormatProvider /// /// The integer error value of the current instance in radix 10 /// /// The radix 10 formatted error code - public readonly override string ToString() => ErrorCode.ToString(); + public override readonly string ToString() => ErrorCode.ToString(); /// /// Formats the internal nint error code as a string in specified format /// @@ -144,11 +143,15 @@ namespace VNLib.Utils /// The formatted error code public readonly string ToString(string format) => ErrorCode.ToString(format); +#pragma warning restore CA1305 // Specify IFormatProvider + /// - public readonly bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) => ErrorCode.TryFormat(destination, out charsWritten, format, provider); + public readonly bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + => ErrorCode.TryFormat(destination, out charsWritten, format, provider); /// - public readonly string ToString(string? format, IFormatProvider? formatProvider) => ErrorCode.ToString(format, formatProvider); + public readonly string ToString(string? format, IFormatProvider? formatProvider) + => ErrorCode.ToString(format, formatProvider); public static ERRNO operator +(ERRNO err, int add) => new(err.ErrorCode + add); public static ERRNO operator +(ERRNO err, nint add) => new(err.ErrorCode + add); diff --git a/lib/Utils/src/Memory/MemoryUtil.cs b/lib/Utils/src/Memory/MemoryUtil.cs index 2ef9c24..cbaaa2f 100644 --- a/lib/Utils/src/Memory/MemoryUtil.cs +++ b/lib/Utils/src/Memory/MemoryUtil.cs @@ -604,7 +604,8 @@ namespace VNLib.Utils.Memory /// A reference to the first byte of the memory location to copy the struct data to /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void CopyStruct(void* source, ref byte target) where T : unmanaged => CopyStruct((T*)source, ref target); + public static void CopyStruct(void* source, ref byte target) where T : unmanaged + => CopyStruct((T*)source, ref target); /// /// Copies the memory of the structure pointed to by the source pointer to the target diff --git a/lib/Utils/tests/.runsettings b/lib/Utils/tests/.runsettings index 0e7a703..b13ddb7 100644 --- a/lib/Utils/tests/.runsettings +++ b/lib/Utils/tests/.runsettings @@ -3,6 +3,9 @@ 1 + ../../../../../Utils.Memory/vnlib_rpmalloc/build/Debug/vnlib_rpmalloc.dll + ../../../../../Utils.Memory/vnlib_mimalloc/build/Debug/vnlib_mimalloc.dll + \ No newline at end of file diff --git a/lib/Utils/tests/IO/VnMemoryStreamTests.cs b/lib/Utils/tests/IO/VnMemoryStreamTests.cs index 9bcb823..6bbf328 100644 --- a/lib/Utils/tests/IO/VnMemoryStreamTests.cs +++ b/lib/Utils/tests/IO/VnMemoryStreamTests.cs @@ -47,8 +47,11 @@ namespace VNLib.Utils.IO.Tests Assert.IsTrue(vms.CanWrite == true); } + //Handle should throw since the stream owns the handle and it gets dispoed + Assert.ThrowsException(handle.ThrowIfClosed); + //From existing data - ReadOnlySpan testSpan = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; + ReadOnlySpan testSpan = [1, 2, 3, 4, 5, 6, 7, 8]; using (VnMemoryStream vms = new (privateHeap, testSpan)) { Assert.IsTrue(vms.Length == testSpan.Length); @@ -125,19 +128,13 @@ namespace VNLib.Utils.IO.Tests ReadOnlyMemory memory = vms.AsMemory(); Assert.AreEqual(memory.Length, testData.Length); - for (int i = 0; i < memory.Length; i++) - { - Assert.AreEqual(memory.Span[i], testData[i]); - } + Assert.IsTrue(memory.Span.SequenceEqual(testData)); //Get the data as a byte array byte[] array = vms.ToArray(); Assert.AreEqual(array.Length, testData.Length); - for (int i = 0; i < array.Length; i++) - { - Assert.AreEqual(array[i], testData[i]); - } + Assert.IsTrue(array.AsSpan().SequenceEqual(testData)); } } } \ No newline at end of file diff --git a/lib/Utils/tests/Memory/NativeHeapTests.cs b/lib/Utils/tests/Memory/NativeHeapTests.cs index 8653bd0..a7072ed 100644 --- a/lib/Utils/tests/Memory/NativeHeapTests.cs +++ b/lib/Utils/tests/Memory/NativeHeapTests.cs @@ -1,20 +1,22 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; +using System.Runtime.InteropServices; namespace VNLib.Utils.Memory.Tests { [TestClass()] public class NativeHeapTests { - const string RpMallocLibPath = "../../../../../Utils.Memory/vnlib_rpmalloc/build/Debug/vnlib_rpmalloc.dll"; - const string MimallocLibPath = "../../../../../Utils.Memory/vnlib_mimalloc/build/Debug/vnlib_mimalloc.dll"; + private static string? RpMallocLibPath => Environment.GetEnvironmentVariable("TEST_RPMALLOC_LIB_PATH"); + + private static string? MimallocLibPath => Environment.GetEnvironmentVariable("TEST_MIMALLOC_LIB_PATH"); [TestMethod()] public void LoadInTreeRpmallocTest() { //Try to load the shared heap - using NativeHeap heap = NativeHeap.LoadHeap(RpMallocLibPath, System.Runtime.InteropServices.DllImportSearchPath.SafeDirectories, HeapCreation.Shared, 0); + using NativeHeap heap = NativeHeap.LoadHeap(RpMallocLibPath, DllImportSearchPath.SafeDirectories, HeapCreation.Shared, flags: 0); Assert.IsFalse(heap.IsInvalid); @@ -36,7 +38,7 @@ namespace VNLib.Utils.Memory.Tests public void LoadInTreeMimallocTest() { //Try to load the shared heap - using NativeHeap heap = NativeHeap.LoadHeap(MimallocLibPath, System.Runtime.InteropServices.DllImportSearchPath.SafeDirectories, HeapCreation.Shared, 0); + using NativeHeap heap = NativeHeap.LoadHeap(MimallocLibPath, DllImportSearchPath.SafeDirectories, HeapCreation.Shared, flags: 0); Assert.IsFalse(heap.IsInvalid); -- cgit