Skip to content

Commit

Permalink
Fixed Dictionary equlaity spec
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Oct 19, 2023
1 parent 04dbc8f commit 829cca5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 44 deletions.
5 changes: 0 additions & 5 deletions Bencodex.Tests/Types/DictionaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Bencodex.Misc;
using Bencodex.Types;
using SharpYaml.Tokens;
using Xunit;
using static Bencodex.Misc.ImmutableByteArrayExtensions;
using static Bencodex.Tests.TestUtils;
using IEquatableDict = System.IEquatable<System.Collections.Immutable.IImmutableDictionary<
Bencodex.Types.IKey,
Bencodex.Types.IValue>>;

namespace Bencodex.Tests.Types
{
Expand Down
54 changes: 15 additions & 39 deletions Bencodex/Types/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace Bencodex.Types
public sealed class Dictionary :
IValue,
IEquatable<Dictionary>,
IEquatable<IImmutableDictionary<IKey, IValue>>,
IImmutableDictionary<IKey, IValue>
{
/// <summary>
Expand Down Expand Up @@ -1697,54 +1696,31 @@ public T GetValue<T>(byte[] name)
return (T)this[name];
}

/// <inheritdoc cref="object.Equals(object)"/>
public override bool Equals(object obj) =>
obj switch
{
null => false,
Dictionary d => Equals(d),
_ => false,
};
public override bool Equals(object obj) => obj is Dictionary d && Equals(d);

/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
public bool Equals(Dictionary other) =>
Fingerprint.Equals(other.Fingerprint);
public bool Equals(IValue other) => other is Dictionary d && Equals(d);

/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
bool IEquatable<IImmutableDictionary<IKey, IValue>>.Equals(
IImmutableDictionary<IKey, IValue> other
)
public bool Equals(Dictionary other)
{
if (_dict.Count != other.Count)
{
return false;
}
else if (other is Dictionary od)
if (Count == other.Count)
{
return od.Fingerprint.Equals(Fingerprint);
}

foreach (KeyValuePair<IKey, IValue> kv in _dict)
{
if (!other.TryGetValue(kv.Key, out IValue v))
foreach (KeyValuePair<IKey, IValue> kv in _dict)
{
return false;
if (!other.TryGetValue(kv.Key, out IValue v) ||
!kv.Value.Equals(v))
{
return false;
}
}

if (!kv.Value.Equals(v))
{
return false;
}
return true;
}
else
{
return false;
}

return true;
}

/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
bool IEquatable<IValue>.Equals(IValue other) =>
other is Dictionary o &&
((IEquatable<IImmutableDictionary<IKey, IValue>>)this).Equals(o);

/// <inheritdoc cref="object.GetHashCode()"/>
public override int GetHashCode()
=> unchecked(_dict.Aggregate(GetType().GetHashCode(), (accum, next)
Expand Down

0 comments on commit 829cca5

Please sign in to comment.