From 60724e1a328ad383e576cfef6ff0f975420d48f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Forjan?= Date: Tue, 4 Jun 2024 14:42:40 -0700 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=85enable=20to=20run=20unit=20tests?= =?UTF-8?q?=20in=20.NET=20472?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Extensions.cs | 5 ++++ tests/StoreDataTests.cs | 2 -- tests/WasiTests.cs | 50 ++++++++++++++++++++++++++++++++++++- tests/Wasmtime.Tests.csproj | 11 ++++++-- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/Extensions.cs b/src/Extensions.cs index e0318e8a..55d844a7 100644 --- a/src/Extensions.cs +++ b/src/Extensions.cs @@ -20,6 +20,7 @@ public static unsafe string GetString(this Encoding encoding, Span bytes) { fixed (byte* bytesPtr = bytes) { + if(bytesPtr == null) return string.Empty; return encoding.GetString(bytesPtr, bytes.Length); } } @@ -29,6 +30,7 @@ public static unsafe string GetString(this Encoding encoding, ReadOnlySpan { fixed (byte* bytesPtr = bytes) { + if(bytesPtr == null) return string.Empty; return encoding.GetString(bytesPtr, bytes.Length); } } @@ -39,6 +41,7 @@ public static unsafe int GetBytes(this Encoding encoding, Span chars, Span fixed (char* charsPtr = chars) fixed (byte* bytesPtr = bytes) { + if(bytesPtr == null) return 0; return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length); } } @@ -49,6 +52,7 @@ public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan cha fixed (char* charsPtr = chars) fixed (byte* bytesPtr = bytes) { + if(bytesPtr == null) return 0; return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length); } } @@ -59,6 +63,7 @@ public static unsafe int GetBytes(this Encoding encoding, string chars, Span, IEqualityComparer + { + private ReferenceEqualityComparer() { } + + /// + /// Gets the singleton instance. + /// + public static ReferenceEqualityComparer Instance { get; } = new ReferenceEqualityComparer(); + + /// + /// Determines whether two object references refer to the same object instance. + /// + /// The first object to compare. + /// The second object to compare. + /// + /// if both and refer to the same object instance + /// or if both are ; otherwise, . + /// + /// + /// This API is a wrapper around . + /// It is not necessarily equivalent to calling . + /// + public new bool Equals(object? x, object? y) => ReferenceEquals(x, y); + + /// + /// Returns a hash code for the specified object. The returned hash code is based on the object + /// identity, not on the contents of the object. + /// + /// The object for which to retrieve the hash code. + /// A hash code for the identity of . + /// + /// This API is a wrapper around . + /// It is not necessarily equivalent to calling . + /// + public int GetHashCode(object? obj) + { + // Depending on target framework, RuntimeHelpers.GetHashCode might not be annotated + // with the proper nullability attribute. We'll suppress any warning that might + // result. + return RuntimeHelpers.GetHashCode(obj!); + } + } +#endif + public class WasiTests { [Theory] @@ -70,7 +118,7 @@ public void ItHasSpecifiedEnvironment(string path) for (int i = 0; i < env.Count; ++i) { - var kvp = memory.ReadNullTerminatedString(memory.ReadInt32(i * 4)).Split("="); + var kvp = memory.ReadNullTerminatedString(memory.ReadInt32(i * 4)).Split('='); Assert.Equal(env[kvp[0]], kvp[1]); } } diff --git a/tests/Wasmtime.Tests.csproj b/tests/Wasmtime.Tests.csproj index ee330e2a..a81158e4 100644 --- a/tests/Wasmtime.Tests.csproj +++ b/tests/Wasmtime.Tests.csproj @@ -1,7 +1,14 @@ - + + + + net8.0;net472 + + + net8.0 + - net8.0 + Latest false From fa80cf90b52819463084c0d75cc5b2632f6cf79e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Forjan?= Date: Mon, 1 Jul 2024 07:01:43 -0700 Subject: [PATCH 2/2] Fix NetFramework assert message --- tests/MemoryImportBindingTests.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/MemoryImportBindingTests.cs b/tests/MemoryImportBindingTests.cs index 71cbcf3c..7bbc7045 100644 --- a/tests/MemoryImportBindingTests.cs +++ b/tests/MemoryImportBindingTests.cs @@ -43,7 +43,11 @@ public void ItFailsToInstantiateWithNullStore() action .Should() .Throw() - .WithMessage("Value cannot be null. (Parameter 'store')"); +#if NETFRAMEWORK + .WithMessage("Value cannot be null.\r\nParameter name: store"); +#else + .WithMessage("Value cannot be null. (Parameter 'store')"); +#endif } [Fact] @@ -67,7 +71,11 @@ public void ItFailsToInstantiateWithNegativeMinimum() action .Should() .Throw() +#if NETFRAMEWORK + .WithMessage("Specified argument was out of the range of valid values.\r\nParameter name: minimum"); +#else .WithMessage("Specified argument was out of the range of valid values. (Parameter 'minimum')"); +#endif } [Fact] @@ -78,7 +86,11 @@ public void ItFailsToInstantiateWithNegativeMaximum() action .Should() .Throw() +#if NETFRAMEWORK + .WithMessage("Specified argument was out of the range of valid values.\r\nParameter name: maximum"); +#else .WithMessage("Specified argument was out of the range of valid values. (Parameter 'maximum')"); +#endif } [Fact] @@ -89,7 +101,12 @@ public void ItFailsToInstantiateWithMaximumLessThanMinimum() action .Should() .Throw() +#if NETFRAMEWORK + .WithMessage("The maximum cannot be less than the minimum.\r\nParameter name: maximum"); +#else .WithMessage("The maximum cannot be less than the minimum. (Parameter 'maximum')"); +#endif + } [Fact]