-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
… C# endianness fixes
- Loading branch information
Showing
4 changed files
with
406 additions
and
9 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
crates/bindings-csharp/BSATN.Runtime.Tests/BSATN.Runtime.Tests.csproj
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,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net8.0</TargetFrameworks> | ||
<RootNamespace>SpacetimeDB</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CSharpier.Core" Version="0.28.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" /> | ||
<PackageReference Include="xunit" Version="2.9.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../BSATN.Runtime/BSATN.Runtime.csproj" /> | ||
</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,162 @@ | ||
namespace SpacetimeDB; | ||
|
||
using Xunit; | ||
|
||
public static class BSATNRuntimeTests | ||
{ | ||
[Fact] | ||
public static void AddressRoundtrips() | ||
{ | ||
var str = "00112233445566778899AABBCCDDEEFF"; | ||
var addr = Address.FromHexString(str); | ||
|
||
Assert.NotNull(addr); | ||
Assert.Equal(addr.ToString(), str); | ||
|
||
byte[] bytes = | ||
[ | ||
0x00, | ||
0x11, | ||
0x22, | ||
0x33, | ||
0x44, | ||
0x55, | ||
0x66, | ||
0x77, | ||
0x88, | ||
0x99, | ||
0xaa, | ||
0xbb, | ||
0xcc, | ||
0xdd, | ||
0xee, | ||
0xff, | ||
]; | ||
var addr2 = Address.FromBigEndian(bytes); | ||
Assert.Equal(addr2, addr); | ||
|
||
Array.Reverse(bytes); | ||
var addr3 = Address.From(bytes); | ||
Assert.Equal(addr3, addr); | ||
|
||
var memoryStream = new MemoryStream(); | ||
var writer = new BinaryWriter(memoryStream); | ||
var bsatn = new Address.BSATN(); | ||
if (addr is { } addrNotNull) | ||
{ | ||
bsatn.Write(writer, addrNotNull); | ||
} | ||
else | ||
{ | ||
Assert.Fail("Impossible"); | ||
} | ||
writer.Flush(); | ||
|
||
var littleEndianBytes = memoryStream.ToArray(); | ||
var reader = new BinaryReader(new MemoryStream(littleEndianBytes)); | ||
var addr4 = bsatn.Read(reader); | ||
Assert.Equal(addr4, addr); | ||
|
||
// Note: From = FromLittleEndian | ||
var addr5 = Address.From(littleEndianBytes); | ||
Assert.Equal(addr5, addr); | ||
} | ||
|
||
[Fact] | ||
public static void AddressLengthCheck() | ||
{ | ||
for (var i = 0; i < 64; i++) | ||
{ | ||
if (i == 16) | ||
{ | ||
continue; | ||
} | ||
|
||
var bytes = new byte[i]; | ||
|
||
Assert.ThrowsAny<Exception>(() => Address.From(bytes)); | ||
} | ||
} | ||
|
||
[Fact] | ||
public static void IdentityRoundtrips() | ||
{ | ||
var str = "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF"; | ||
var ident = Identity.FromHexString(str); | ||
|
||
Assert.Equal(ident.ToString(), str); | ||
|
||
byte[] bytes = | ||
[ | ||
0x00, | ||
0x11, | ||
0x22, | ||
0x33, | ||
0x44, | ||
0x55, | ||
0x66, | ||
0x77, | ||
0x88, | ||
0x99, | ||
0xaa, | ||
0xbb, | ||
0xcc, | ||
0xdd, | ||
0xee, | ||
0xff, | ||
0x00, | ||
0x11, | ||
0x22, | ||
0x33, | ||
0x44, | ||
0x55, | ||
0x66, | ||
0x77, | ||
0x88, | ||
0x99, | ||
0xaa, | ||
0xbb, | ||
0xcc, | ||
0xdd, | ||
0xee, | ||
0xff, | ||
]; | ||
var ident2 = Identity.FromBigEndian(bytes); | ||
Assert.Equal(ident2, ident); | ||
|
||
Array.Reverse(bytes); | ||
var ident3 = Identity.From(bytes); | ||
Assert.Equal(ident3, ident); | ||
|
||
var memoryStream = new MemoryStream(); | ||
var writer = new BinaryWriter(memoryStream); | ||
var bsatn = new Identity.BSATN(); | ||
bsatn.Write(writer, ident); | ||
writer.Flush(); | ||
|
||
var littleEndianBytes = memoryStream.ToArray(); | ||
var reader = new BinaryReader(new MemoryStream(littleEndianBytes)); | ||
var ident4 = bsatn.Read(reader); | ||
Assert.Equal(ident4, ident); | ||
|
||
// Note: From = FromLittleEndian | ||
var ident5 = Identity.From(littleEndianBytes); | ||
Assert.Equal(ident5, ident); | ||
} | ||
|
||
[Fact] | ||
public static void IdentityLengthCheck() | ||
{ | ||
for (var i = 0; i < 64; i++) | ||
{ | ||
if (i == 32) | ||
{ | ||
continue; | ||
} | ||
|
||
var bytes = new byte[i]; | ||
|
||
Assert.ThrowsAny<Exception>(() => Identity.From(bytes)); | ||
} | ||
} | ||
} |
Oops, something went wrong.