Skip to content

Commit

Permalink
Implements IComparable for Identity and ConnectionId (#2354)
Browse files Browse the repository at this point in the history
  • Loading branch information
rekhoff authored Mar 7, 2025
1 parent 9a2b30f commit 7947a59
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
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

2 comments on commit 7947a59

@github-actions
Copy link

@github-actions github-actions bot commented on 7947a59 Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Criterion benchmark results

Error when comparing benchmarks: Couldn't find AWS credentials in environment, credentials file, or IAM role.

Caused by:
Couldn't find AWS credentials in environment, credentials file, or IAM role.

@github-actions
Copy link

@github-actions github-actions bot commented on 7947a59 Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callgrind benchmark results Error when comparing benchmarks: Couldn't find AWS credentials in environment, credentials file, or IAM role.

Caused by:
Couldn't find AWS credentials in environment, credentials file, or IAM role.

Please sign in to comment.