Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kazimuth committed Nov 8, 2024
1 parent d08ec2d commit bc96dd2
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 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));
}
}
}
6 changes: 6 additions & 0 deletions crates/bindings-csharp/SpacetimeSharpSATS.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sdk-tests", "sdk-tests", "{
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "benchmarks-cs", "..\..\modules\benchmarks-cs\benchmarks-cs.csproj", "{50E1AAE1-C42C-4C2F-B708-5190B0362165}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BSATN.Runtime.Tests", "BSATN.Runtime.Tests\BSATN.Runtime.Tests.csproj", "{FCF18E21-FB59-4A4D-A9ED-B85D2874E536}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -62,6 +64,10 @@ Global
{FDACD960-168E-44F9-B036-2E29EA391BE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50E1AAE1-C42C-4C2F-B708-5190B0362165}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50E1AAE1-C42C-4C2F-B708-5190B0362165}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FCF18E21-FB59-4A4D-A9ED-B85D2874E536}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FCF18E21-FB59-4A4D-A9ED-B85D2874E536}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FCF18E21-FB59-4A4D-A9ED-B85D2874E536}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FCF18E21-FB59-4A4D-A9ED-B85D2874E536}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit bc96dd2

Please sign in to comment.