Skip to content

Commit

Permalink
Merge pull request #119 from greymistcube/refactor/boolean-equality
Browse files Browse the repository at this point in the history
Streamline `Boolean` equality
  • Loading branch information
greymistcube authored Oct 24, 2023
2 parents 9f39bed + 1596618 commit 0d46ccb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 34 deletions.
59 changes: 59 additions & 0 deletions Bencodex.Tests/Types/BooleanTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,65 @@ public class BooleanTest
private readonly Boolean _t = new Boolean(true);
private readonly Boolean _f = new Boolean(false);

[Fact]
public void Equality()
{
bool b = true;
Boolean x = new Boolean(true);
object ob = (object)b;
object ox = (object)x;

#pragma warning disable CS1718 // Comparison made to same variable
Assert.True(x == x);
Assert.True(x.Equals(x));
Assert.True(x.Equals(ox));
Assert.True(ox.Equals(x));
Assert.True(ox.Equals(ox));
#pragma warning restore CS1718

Assert.True(b == x);
Assert.True(x == b);
Assert.True(b.Equals(x));
Assert.True(x.Equals(x));

Assert.False(b.Equals(ox));
Assert.False(x.Equals(ob));
Assert.False(ob.Equals(x));
Assert.False(ox.Equals(b));
Assert.False(ob.Equals(ox));
Assert.False(ox.Equals(ob));
}

[Fact]
public void Comparison()
{
bool b = true;
Boolean x = new Boolean(true);
Boolean? n = null;
object ob = (object)b;
object ox = (object)x;
object on = null;

Assert.Equal(0, x.CompareTo(x));
Assert.Equal(0, x.CompareTo(ox));
Assert.Equal(1, x.CompareTo(n));
Assert.Equal(1, x.CompareTo(on));

Assert.Equal(0, b.CompareTo(x));
Assert.Equal(0, x.CompareTo(b));

Assert.Throws<System.ArgumentException>(() => b.CompareTo(ox));
Assert.Throws<System.ArgumentException>(() => x.CompareTo(ob));

Boolean f = new Boolean(false);
Boolean t = new Boolean(true);

Assert.Equal(0, f.CompareTo(f));
Assert.Equal(0, t.CompareTo(t));
Assert.True(f.CompareTo(t) < 0);
Assert.True(t.CompareTo(f) > 0);
}

[Fact]
public void Kind()
{
Expand Down
59 changes: 25 additions & 34 deletions Bencodex/Types/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ namespace Bencodex.Types
/// or false (i.e., <c>f</c>).</summary>
public struct Boolean :
IValue,
IEquatable<bool>,
IEquatable<Boolean>,
IComparable<bool>,
IComparable<Boolean>,
IComparable
{
Expand Down Expand Up @@ -38,51 +36,44 @@ public static implicit operator Boolean(bool b)
return new Boolean(b);
}

public int CompareTo(object obj)
{
if (obj is bool b)
{
return ((IComparable<bool>)this).CompareTo(b);
}
public static bool operator ==(Boolean a, Boolean b) => a.Equals(b);

return Value.CompareTo(obj);
}
public static bool operator !=(Boolean a, Boolean b) => !a.Equals(b);

int IComparable<bool>.CompareTo(bool other) => Value.CompareTo(other);
public static bool operator ==(Boolean a, bool b) => a.Equals(b);

int IComparable<Boolean>.CompareTo(Boolean other)
{
return CompareTo(other.Value);
}
public static bool operator !=(Boolean a, bool b) => !a.Equals(b);

bool IEquatable<bool>.Equals(bool other)
{
return Value == other;
}
public static bool operator ==(bool a, Boolean b) => a.Equals(b.Value);

public static bool operator !=(bool a, Boolean b) => !a.Equals(b.Value);

bool IEquatable<Boolean>.Equals(Boolean other)
public int CompareTo(Boolean other)
{
return Value == other.Value;
return Value.CompareTo(other.Value);
}

bool IEquatable<IValue>.Equals(IValue other) =>
other is Boolean o && ((IEquatable<Boolean>)this).Equals(o);

public override bool Equals(object obj)
public int CompareTo(object? obj)
{
switch (obj)
if (obj is null)
{
return 1;
}

if (obj is Boolean b)
{
case null:
return false;
case Boolean b:
return Value.Equals(b.Value);
case bool b:
return Value.Equals(b);
default:
return false;
return CompareTo(b);
}

throw new ArgumentException($"Object must be of type {nameof(Boolean)}");
}

public bool Equals(Boolean other) => Value == other.Value;

public bool Equals(IValue other) => other is Boolean b && Equals(b);

public override bool Equals(object obj) => obj is Boolean b && Equals(b);

public override int GetHashCode()
{
return Value.GetHashCode();
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ To be released.

- Removed `IValue.Inspection` property. [[#117]]
- Changed `IValue.Inspect(bool)` to `IValue.Inspect()`. [[#118]]
- Removed `IEquatable<bool>` and `IComparable<bool>` from `Boolean`.
[[#104], [#119]]
- Added `==` and `!=` operators for `Boolean`. [[#119]]

[#117]: https://github.com/planetarium/bencodex.net/pull/117
[#118]: https://github.com/planetarium/bencodex.net/pull/118
[#119]: https://github.com/planetarium/bencodex.net/pull/119


Version 0.15.0
Expand Down

0 comments on commit 0d46ccb

Please sign in to comment.