-
-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix SilkMarshal.PtrToStringArray ignores the encoding parameter (#2376)
* Add Silk.NET.Core.Tests project * Add test cases for testing string encoding * Fix issue where PtrToStringArray was ignoring the encoding parameter
- Loading branch information
Showing
4 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0</TargetFrameworks> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<LangVersion>preview</LangVersion> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Silk.NET.Core\Silk.NET.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.6.6" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Collections.Generic; | ||
using Silk.NET.Core.Native; | ||
using Xunit; | ||
|
||
namespace Silk.NET.Core.Tests; | ||
|
||
public class TestSilkMarshal | ||
{ | ||
private readonly List<NativeStringEncoding> encodings = new() | ||
{ | ||
NativeStringEncoding.BStr, | ||
NativeStringEncoding.LPStr, | ||
NativeStringEncoding.LPTStr, | ||
NativeStringEncoding.LPUTF8Str, | ||
NativeStringEncoding.LPWStr, | ||
}; | ||
|
||
[Fact] | ||
public void TestEncodingString() | ||
{ | ||
var input = "Hello world"; | ||
foreach (var encoding in encodings) | ||
{ | ||
var pointer = SilkMarshal.StringToPtr(input, encoding); | ||
var roundTrip = SilkMarshal.PtrToString(pointer, encoding); | ||
Assert.Equal(input, roundTrip); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void TestEncodingStringArray() | ||
{ | ||
var inputs = new List<string>() | ||
{ | ||
"Hello world", | ||
"Foo", | ||
"Bar", | ||
"123", | ||
}; | ||
|
||
foreach (var encoding in encodings) | ||
{ | ||
var pointer = SilkMarshal.StringArrayToPtr(inputs, encoding); | ||
var roundTrip = SilkMarshal.PtrToStringArray(pointer, inputs.Count, encoding); | ||
for (var i = 0; i < roundTrip.Length; i++) | ||
{ | ||
Assert.Equal(inputs[i], roundTrip[i]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters