Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions crates/bindings-csharp/BSATN.Runtime.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,51 @@ public static void TimestampConversionChecks()
Assert.Equal(-1, stamp.CompareTo(laterStamp));
Assert.Equal(+1, laterStamp.CompareTo(stamp));
}

[Fact]
public static void ConnectionIdComparableChecks()
{
var str = "00112233445566778899AABBCCDDEEFF";
var strHigh = "00001111222233334444555566667777";
var strLow = "FFFFEEEEDDDDCCCCBBBBAAAA99998888";

var connIdA = ConnectionId.FromHexString(str);
var connIdB = ConnectionId.FromHexString(str);
var connIdHigh = ConnectionId.FromHexString(strHigh);
var connIdLow = ConnectionId.FromHexString(strLow);

Assert.NotNull(connIdA);
Assert.NotNull(connIdB);
Assert.NotNull(connIdHigh);
Assert.NotNull(connIdLow);

Assert.Equal(0, connIdA.Value.CompareTo(connIdB.Value));
Assert.Equal(+1, connIdA.Value.CompareTo(connIdHigh.Value));
Assert.Equal(-1, connIdA.Value.CompareTo(connIdLow.Value));

var notAConnId = new uint();

Assert.ThrowsAny<Exception>(() => connIdA.Value.CompareTo(notAConnId));
}

[Fact]
public static void IdentityComparableChecks()
{
var str = "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF";
var strHigh = "0000111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF";
var strLow = "FFFFEEEEDDDDCCCCBBBBAAAA9999888877776666555544443333222211110000";

var identityA = Identity.FromHexString(str);
var identityB = Identity.FromHexString(str);
var identityHigh = Identity.FromHexString(strHigh);
var identityLow = Identity.FromHexString(strLow);

Assert.Equal(0, identityA.CompareTo(identityB));
Assert.Equal(+1, identityA.CompareTo(identityHigh));
Assert.Equal(-1, identityA.CompareTo(identityLow));

var notAnIdentity = new uint();

Assert.ThrowsAny<Exception>(() => identityA.CompareTo(notAnIdentity));
}
}
45 changes: 44 additions & 1 deletion crates/bindings-csharp/BSATN.Runtime/Builtins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>

[StructLayout(LayoutKind.Sequential)]
public readonly record struct ConnectionId
: IEquatable<ConnectionId>,
IComparable,
IComparable<ConnectionId>
{
private readonly U128 value;

Expand Down Expand Up @@ -198,10 +201,30 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
}

public override string ToString() => Util.ToHexBigEndian(value);

/// <inheritdoc cref="IComparable.CompareTo(object)" />
public int CompareTo(object? value)
{
if (value is ConnectionId other)
{
return CompareTo(other);
}
else if (value is null)
{
return 1;
}
else
{
throw new ArgumentException("Argument must be a ConnectionId", nameof(value));
}
}

/// <inheritdoc cref="IComparable{T}.CompareTo(T)" />
public int CompareTo(ConnectionId connectionId) => this.value.CompareTo(connectionId.value);
}

[StructLayout(LayoutKind.Sequential)]
public readonly record struct Identity
public readonly record struct Identity : IEquatable<Identity>, IComparable, IComparable<Identity>
{
private readonly U256 value;

Expand Down Expand Up @@ -271,6 +294,26 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>

// This must be explicitly implemented, otherwise record will generate a new implementation.
public override string ToString() => Util.ToHexBigEndian(value);

/// <inheritdoc cref="IComparable.CompareTo(object)" />
public int CompareTo(object? value)
{
if (value is Identity other)
{
return CompareTo(other);
}
else if (value is null)
{
return 1;
}
else
{
throw new ArgumentException("Argument must be a Identity", nameof(value));
}
}

/// <inheritdoc cref="IComparable{T}.CompareTo(T)" />
public int CompareTo(Identity identity) => this.value.CompareTo(identity.value);
}

/// <summary>
Expand Down
Loading