From bc96dd23e1275b04a1ddb1bbec72c60cef2a99b9 Mon Sep 17 00:00:00 2001 From: James Gilles Date: Fri, 8 Nov 2024 06:21:23 -0500 Subject: [PATCH] Add unit tests --- .../BSATN.Runtime.Tests.csproj | 25 +++ .../BSATN.Runtime.Tests/Tests.cs | 162 ++++++++++++++++++ crates/bindings-csharp/SpacetimeSharpSATS.sln | 6 + 3 files changed, 193 insertions(+) create mode 100644 crates/bindings-csharp/BSATN.Runtime.Tests/BSATN.Runtime.Tests.csproj create mode 100644 crates/bindings-csharp/BSATN.Runtime.Tests/Tests.cs diff --git a/crates/bindings-csharp/BSATN.Runtime.Tests/BSATN.Runtime.Tests.csproj b/crates/bindings-csharp/BSATN.Runtime.Tests/BSATN.Runtime.Tests.csproj new file mode 100644 index 00000000000..4abecdfbe7f --- /dev/null +++ b/crates/bindings-csharp/BSATN.Runtime.Tests/BSATN.Runtime.Tests.csproj @@ -0,0 +1,25 @@ + + + + false + true + + + + net8.0 + SpacetimeDB + + + + + + + + + + + + + + + diff --git a/crates/bindings-csharp/BSATN.Runtime.Tests/Tests.cs b/crates/bindings-csharp/BSATN.Runtime.Tests/Tests.cs new file mode 100644 index 00000000000..858bd78753a --- /dev/null +++ b/crates/bindings-csharp/BSATN.Runtime.Tests/Tests.cs @@ -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(() => 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(() => Identity.From(bytes)); + } + } +} diff --git a/crates/bindings-csharp/SpacetimeSharpSATS.sln b/crates/bindings-csharp/SpacetimeSharpSATS.sln index 4e49b92f77b..737bb0981c2 100644 --- a/crates/bindings-csharp/SpacetimeSharpSATS.sln +++ b/crates/bindings-csharp/SpacetimeSharpSATS.sln @@ -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 @@ -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