Skip to content

Commit 60724e1

Browse files
fforjanAVEVAfforjan
authored andcommitted
✅enable to run unit tests in .NET 472
1 parent 704b155 commit 60724e1

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

src/Extensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static unsafe string GetString(this Encoding encoding, Span<byte> bytes)
2020
{
2121
fixed (byte* bytesPtr = bytes)
2222
{
23+
if(bytesPtr == null) return string.Empty;
2324
return encoding.GetString(bytesPtr, bytes.Length);
2425
}
2526
}
@@ -29,6 +30,7 @@ public static unsafe string GetString(this Encoding encoding, ReadOnlySpan<byte>
2930
{
3031
fixed (byte* bytesPtr = bytes)
3132
{
33+
if(bytesPtr == null) return string.Empty;
3234
return encoding.GetString(bytesPtr, bytes.Length);
3335
}
3436
}
@@ -39,6 +41,7 @@ public static unsafe int GetBytes(this Encoding encoding, Span<char> chars, Span
3941
fixed (char* charsPtr = chars)
4042
fixed (byte* bytesPtr = bytes)
4143
{
44+
if(bytesPtr == null) return 0;
4245
return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
4346
}
4447
}
@@ -49,6 +52,7 @@ public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan<char> cha
4952
fixed (char* charsPtr = chars)
5053
fixed (byte* bytesPtr = bytes)
5154
{
55+
if(bytesPtr == null) return 0;
5256
return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
5357
}
5458
}
@@ -59,6 +63,7 @@ public static unsafe int GetBytes(this Encoding encoding, string chars, Span<byt
5963
fixed (char* charsPtr = chars)
6064
fixed (byte* bytesPtr = bytes)
6165
{
66+
if(bytesPtr == null) return 0;
6267
return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
6368
}
6469
}

tests/StoreDataTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using System.Diagnostics.Metrics;
32
using System.Linq;
43
using FluentAssertions;
5-
using Newtonsoft.Json.Linq;
64
using Xunit;
75

86
namespace Wasmtime.Tests

tests/WasiTests.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,54 @@
77

88
namespace Wasmtime.Tests
99
{
10+
#if NETFRAMEWORK // the following code is a polyfill as it already exists in .Net
11+
using System.Runtime.CompilerServices;
12+
using System.Collections;
13+
14+
sealed class ReferenceEqualityComparer : IEqualityComparer<object?>, IEqualityComparer
15+
{
16+
private ReferenceEqualityComparer() { }
17+
18+
/// <summary>
19+
/// Gets the singleton <see cref="ReferenceEqualityComparer"/> instance.
20+
/// </summary>
21+
public static ReferenceEqualityComparer Instance { get; } = new ReferenceEqualityComparer();
22+
23+
/// <summary>
24+
/// Determines whether two object references refer to the same object instance.
25+
/// </summary>
26+
/// <param name="x">The first object to compare.</param>
27+
/// <param name="y">The second object to compare.</param>
28+
/// <returns>
29+
/// <see langword="true"/> if both <paramref name="x"/> and <paramref name="y"/> refer to the same object instance
30+
/// or if both are <see langword="null"/>; otherwise, <see langword="false"/>.
31+
/// </returns>
32+
/// <remarks>
33+
/// This API is a wrapper around <see cref="object.ReferenceEquals(object?, object?)"/>.
34+
/// It is not necessarily equivalent to calling <see cref="object.Equals(object?, object?)"/>.
35+
/// </remarks>
36+
public new bool Equals(object? x, object? y) => ReferenceEquals(x, y);
37+
38+
/// <summary>
39+
/// Returns a hash code for the specified object. The returned hash code is based on the object
40+
/// identity, not on the contents of the object.
41+
/// </summary>
42+
/// <param name="obj">The object for which to retrieve the hash code.</param>
43+
/// <returns>A hash code for the identity of <paramref name="obj"/>.</returns>
44+
/// <remarks>
45+
/// This API is a wrapper around <see cref="RuntimeHelpers.GetHashCode(object)"/>.
46+
/// It is not necessarily equivalent to calling <see cref="object.GetHashCode()"/>.
47+
/// </remarks>
48+
public int GetHashCode(object? obj)
49+
{
50+
// Depending on target framework, RuntimeHelpers.GetHashCode might not be annotated
51+
// with the proper nullability attribute. We'll suppress any warning that might
52+
// result.
53+
return RuntimeHelpers.GetHashCode(obj!);
54+
}
55+
}
56+
#endif
57+
1058
public class WasiTests
1159
{
1260
[Theory]
@@ -70,7 +118,7 @@ public void ItHasSpecifiedEnvironment(string path)
70118

71119
for (int i = 0; i < env.Count; ++i)
72120
{
73-
var kvp = memory.ReadNullTerminatedString(memory.ReadInt32(i * 4)).Split("=");
121+
var kvp = memory.ReadNullTerminatedString(memory.ReadInt32(i * 4)).Split('=');
74122
Assert.Equal(env[kvp[0]], kvp[1]);
75123
}
76124
}

tests/Wasmtime.Tests.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
4+
<TargetFrameworks>net8.0;net472</TargetFrameworks>
5+
</PropertyGroup>
6+
<PropertyGroup Condition="!($([MSBuild]::IsOSPlatform('Windows')))">
7+
<TargetFrameworks>net8.0</TargetFrameworks>
8+
</PropertyGroup>
29

310
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
11+
<LangVersion>Latest</LangVersion>
512
<IsPackable>false</IsPackable>
613
</PropertyGroup>
714

0 commit comments

Comments
 (0)