1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:15:40 +00:00

Add logical operations in BooleanExtensions

Methods include:
* And
* Or
* Not
* XOr
* NAnd
* NOr
* XNOr
This commit is contained in:
Timathy 2020-04-18 14:50:26 -07:00 committed by GitHub
parent 7e0de45b64
commit a0de8ceb11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 245 additions and 0 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.idea
*.rsuser
*.suo
*.user

View File

@ -22,5 +22,163 @@ namespace X10D.Tests
Assert.AreEqual(1, a.ToInt32());
Assert.AreEqual(0, b.ToInt32());
}
/// <summary>
/// Tests for <see cref="BooleanExtensions.ToByte"/>.
/// </summary>
[TestMethod]
public void ToByte()
{
const bool a = true;
const bool b = false;
const byte c = 1;
const byte d = 0;
Assert.IsTrue(a);
Assert.IsFalse(b);
Assert.AreEqual(c, a.ToByte());
Assert.AreEqual(d, b.ToByte());
}
/// <summary>
/// Tests for <see cref="BooleanExtensions.Not"/>.
/// </summary>
[TestMethod]
public void Not()
{
bool a = true;
bool b = false;
Assert.IsTrue(a);
Assert.IsFalse(b);
Assert.IsFalse(a.Not());
Assert.IsTrue(b.Not());
}
/// <summary>
/// Tests for <see cref="BooleanExtensions.And"/>.
/// </summary>
[TestMethod]
public void And()
{
const bool a = true;
const bool b = true;
const bool c = false;
const bool d = false;
Assert.IsTrue(a);
Assert.IsTrue(b);
Assert.IsFalse(c);
Assert.IsFalse(d);
Assert.IsTrue(a.And(b));
Assert.IsFalse(b.And(c));
Assert.IsFalse(c.And(d));
}
/// <summary>
/// Tests for <see cref="BooleanExtensions.Or"/>.
/// </summary>
[TestMethod]
public void Or()
{
const bool a = true;
const bool b = true;
const bool c = false;
const bool d = false;
Assert.IsTrue(a);
Assert.IsTrue(b);
Assert.IsFalse(c);
Assert.IsFalse(d);
Assert.IsTrue(a.Or(b));
Assert.IsTrue(b.Or(c));
Assert.IsFalse(c.Or(d));
}
/// <summary>
/// Tests for <see cref="BooleanExtensions.Xor"/>.
/// </summary>
[TestMethod]
public void Xor()
{
const bool a = true;
const bool b = true;
const bool c = false;
const bool d = false;
Assert.IsTrue(a);
Assert.IsTrue(b);
Assert.IsFalse(c);
Assert.IsFalse(d);
Assert.IsFalse(a.XOr(b));
Assert.IsTrue(b.XOr(c));
Assert.IsFalse(c.XOr(d));
}
/// <summary>
/// Tests for <see cref="BooleanExtensions.NAnd"/>.
/// </summary>
[TestMethod]
public void NAnd()
{
const bool a = true;
const bool b = true;
const bool c = false;
const bool d = false;
Assert.IsTrue(a);
Assert.IsTrue(b);
Assert.IsFalse(c);
Assert.IsFalse(d);
Assert.IsFalse(a.NAnd(b));
Assert.IsTrue(b.NAnd(c));
Assert.IsTrue(c.NAnd(d));
}
/// <summary>
/// Tests for <see cref="BooleanExtensions.NOr"/>.
/// </summary>
[TestMethod]
public void NOr()
{
const bool a = true;
const bool b = true;
const bool c = false;
const bool d = false;
Assert.IsTrue(a);
Assert.IsTrue(b);
Assert.IsFalse(c);
Assert.IsFalse(d);
Assert.IsFalse(a.NOr(b));
Assert.IsFalse(b.NOr(c));
Assert.IsTrue(c.NOr(d));
}
/// <summary>
/// Tests for <see cref="BooleanExtensions.NOr"/>.
/// </summary>
[TestMethod]
public void XNOr()
{
const bool a = true;
const bool b = true;
const bool c = false;
const bool d = false;
Assert.IsTrue(a);
Assert.IsTrue(b);
Assert.IsFalse(c);
Assert.IsFalse(d);
Assert.IsTrue(a.XNOr(b));
Assert.IsFalse(b.XNOr(c));
Assert.IsTrue(c.XNOr(d));
}
}
}

View File

@ -14,5 +14,91 @@
{
return value ? 1 : 0;
}
/// <summary>
/// Gets a byte value that represents this boolean.
/// </summary>
/// <param name="value">The boolean.</param>
/// <returns>Returns 00000001 if <paramref name="value"/> is <see langword="true"/>, otherwise 0000000.</returns>
public static byte ToByte(this bool value)
{
return value ? (byte)1 : (byte)0;
}
/// <summary>
/// Toggles this booleans current state.
/// </summary>
/// <param name="value">The boolean.</param>
/// <returns>Returns the opposite state of this boolean.</returns>
public static bool Not(this ref bool value)
{
return value = !value;
}
/// <summary>
/// Gets the value of this boolean and another boolean.
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="true"/> when <paramref name="value"/> and <paramref name="comparison"/> are <see langword="true"/> Otherwise, the output is <see langword="false"/>.</returns>
public static bool And(this bool value, bool comparison)
{
return value && comparison;
}
/// <summary>
/// Gets the value of this boolean or another boolean.
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> or <paramref name="comparison"/> is <see langword="true"/>.</returns>
public static bool Or(this bool value, bool comparison)
{
return value || comparison;
}
/// <summary>
/// Gets the value of this boolean exclusively or another boolean.
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> and <paramref name="comparison"/> are <see langword="false"/> or if <paramref name="value"/> and <paramref name="comparison"/> are <see langword="true"/>.</returns>
public static bool XOr(this bool value, bool comparison)
{
return value ^ comparison;
}
/// <summary>
/// Gets the value of this boolean and another boolean.
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> and <paramref name="comparison"/> are <see langword="true"/>. Otherwise, <see langword="true"/>.</returns>
public static bool NAnd(this bool value, bool comparison)
{
return !(value && comparison);
}
/// <summary>
/// Gets the value of this boolean and another boolean.
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> and <paramref name="comparison"/> are <see langword="false"/>. Otherwise, <see langword="false"/>.</returns>
public static bool NOr(this bool value, bool comparison)
{
return !(value || comparison);
}
/// <summary>
/// Gets the value of this boolean and another boolean.
/// </summary>
/// <param name="value">The boolean.</param>
/// <param name="comparison">The boolean comparator.</param>
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> and <paramref name="comparison"/> are the same, Otherwise <see langword="false"/>.</returns>
public static bool XNOr(this bool value, bool comparison)
{
return !(value ^ comparison);
}
}
}