Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline Boolean equality #119

Merged
merged 4 commits into from
Oct 24, 2023
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
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
Loading