Skip to content

Commit

Permalink
Implement ServerRecord equality and hashing.
Browse files Browse the repository at this point in the history
  • Loading branch information
yaakov-h authored and leonard-thieu committed Nov 29, 2017
1 parent c0fc173 commit ea626f9
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
52 changes: 51 additions & 1 deletion SteamKit2/SteamKit2/Steam/Discovery/ServerRecord.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
 using System;
using System;
using System.Net;

namespace SteamKit2.Discovery
Expand Down Expand Up @@ -122,5 +122,55 @@ public static ServerRecord CreateWebSocketServer(string address)

return new ServerRecord(endPoint, ProtocolTypes.WebSocket);
}

#region Equality and Hashing

/// <summary>
/// Determines whether two objects are equal.
/// </summary>
/// <param name="left">The object on the left-hand side of the equality operator.</param>
/// <param name="right">The object on the right-hand side of the equality operator.</param>
/// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
public static bool operator ==(ServerRecord left, ServerRecord right)
{
if (ReferenceEquals(left, right))
{
return true;
}

return !ReferenceEquals(left, null) && left.Equals(right);
}

/// <summary>
/// Determines whether two objects are not equal.
/// </summary>
/// <param name="left">The object on the left-hand side of the inequality operator.</param>
/// <param name="right">The object on the right-hand side of the inequality operator.</param>
/// <returns>true if the specified object is not equal to the current object; otherwise, false.</returns>
public static bool operator !=(ServerRecord left, ServerRecord right)
{
return !(left == right);
}

/// <summary>
/// Determines whether the specified object is equal to the current object.
/// </summary>
/// <param name="obj"></param>
/// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
public override bool Equals(object obj)
=> obj is ServerRecord other &&
EndPoint.Equals(other.EndPoint) &&
ProtocolTypes == other.ProtocolTypes;

/// <summary>
/// Hash function
/// </summary>
/// <returns>A hash code for the current object.</returns>
public override int GetHashCode()
{
return EndPoint.GetHashCode() ^ ProtocolTypes.GetHashCode();
}

#endregion
}
}
88 changes: 88 additions & 0 deletions SteamKit2/Tests/ServerRecordFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using SteamKit2;
using SteamKit2.Discovery;
using Xunit;

namespace Tests
{
public class ServerRecordFacts
{
[Fact]
public void NullsAreEqualOperator()
{
ServerRecord l = null;
ServerRecord r = null;

Assert.True(l == r);
}

[Fact]
public void NullIsNotEqual()
{
var s = ServerRecord.CreateWebSocketServer("host:1");

Assert.True(s != null);
Assert.True(null != s);
Assert.False(s.Equals(null));
Assert.False(s == null);
Assert.False(null == s);
}

[Fact]
public void DifferentProtocolsAreNotEqual()
{
var l = ServerRecord.CreateServer("host", 1, ProtocolTypes.Tcp);
var r = ServerRecord.CreateServer("host", 1, ProtocolTypes.WebSocket);

Assert.True(l != r);
Assert.True(r != l);
Assert.False(l == r);
Assert.False(r == l);
Assert.False(l.Equals(r));
Assert.False(r.Equals(l));
}

[Fact]
public void DifferentEndPointsAreNotEqual()
{
var l = ServerRecord.CreateServer("host", 1, ProtocolTypes.Tcp);
var r = ServerRecord.CreateServer("host", 2, ProtocolTypes.Tcp);

Assert.True(l != r);
Assert.True(r != l);
Assert.False(l == r);
Assert.False(r == l);
Assert.False(l.Equals(r));
Assert.False(r.Equals(l));
}

[Fact]
public void DifferentEndPointsAndProtocolsAreNotEqual()
{
var l = ServerRecord.CreateServer("host", 1, ProtocolTypes.Tcp);
var r = ServerRecord.CreateServer("host", 2, ProtocolTypes.WebSocket);

Assert.True(l != r);
Assert.True(r != l);
Assert.False(l == r);
Assert.False(r == l);
Assert.False(l.Equals(r));
Assert.False(r.Equals(l));
}

[Fact]
public void SameEndPointsAndProtocolsAreEqual()
{
var l = ServerRecord.CreateServer("host", 1, ProtocolTypes.Tcp);
var r = ServerRecord.CreateServer("host", 1, ProtocolTypes.Tcp);

Assert.False(l != r);
Assert.False(r != l);
Assert.True(l == r);
Assert.True(r == l);
Assert.True(l.Equals(r));
Assert.True(r.Equals(l));

Assert.Equal(l.GetHashCode(), r.GetHashCode());
}
}
}

0 comments on commit ea626f9

Please sign in to comment.