Skip to content

Commit

Permalink
[release/v1.0.0-rc1-hotfix2]: Manually apply open PR #1964 @ bc96dd2:…
Browse files Browse the repository at this point in the history
… C# endianness fixes
  • Loading branch information
bfops committed Nov 11, 2024
1 parent 3a10733 commit 5db26e2
Show file tree
Hide file tree
Showing 4 changed files with 406 additions and 9 deletions.
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>
162 changes: 162 additions & 0 deletions crates/bindings-csharp/BSATN.Runtime.Tests/Tests.cs
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));
}
}
}
Loading

0 comments on commit 5db26e2

Please sign in to comment.