From a0de8ceb11a7830f73483c7c02c2a539007391a0 Mon Sep 17 00:00:00 2001
From: Timathy <53098956+rubiksmaster02@users.noreply.github.com>
Date: Sat, 18 Apr 2020 14:50:26 -0700
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20logical=20operations=20in=20B?=
=?UTF-8?q?ooleanExtensions?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Methods include:
* And
* Or
* Not
* XOr
* NAnd
* NOr
* XNOr
---
.gitignore | 1 +
X10D.Tests/src/BooleanTests.cs | 158 +++++++++++++++++++++++++++++++++
X10D/src/BooleanExtensions.cs | 86 ++++++++++++++++++
3 files changed, 245 insertions(+)
diff --git a/.gitignore b/.gitignore
index 846c8c0..16fe8be 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
+*.idea
*.rsuser
*.suo
*.user
diff --git a/X10D.Tests/src/BooleanTests.cs b/X10D.Tests/src/BooleanTests.cs
index d9c8c26..3027000 100644
--- a/X10D.Tests/src/BooleanTests.cs
+++ b/X10D.Tests/src/BooleanTests.cs
@@ -22,5 +22,163 @@ namespace X10D.Tests
Assert.AreEqual(1, a.ToInt32());
Assert.AreEqual(0, b.ToInt32());
}
+
+ ///
+ /// Tests for .
+ ///
+ [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());
+ }
+
+ ///
+ /// Tests for .
+ ///
+ [TestMethod]
+ public void Not()
+ {
+ bool a = true;
+ bool b = false;
+
+ Assert.IsTrue(a);
+ Assert.IsFalse(b);
+ Assert.IsFalse(a.Not());
+ Assert.IsTrue(b.Not());
+ }
+
+ ///
+ /// Tests for .
+ ///
+ [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));
+ }
+
+ ///
+ /// Tests for .
+ ///
+ [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));
+ }
+
+ ///
+ /// Tests for .
+ ///
+ [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));
+ }
+
+ ///
+ /// Tests for .
+ ///
+ [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));
+ }
+
+ ///
+ /// Tests for .
+ ///
+ [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));
+ }
+
+ ///
+ /// Tests for .
+ ///
+ [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));
+ }
}
}
diff --git a/X10D/src/BooleanExtensions.cs b/X10D/src/BooleanExtensions.cs
index 256806f..dcd5844 100644
--- a/X10D/src/BooleanExtensions.cs
+++ b/X10D/src/BooleanExtensions.cs
@@ -14,5 +14,91 @@
{
return value ? 1 : 0;
}
+
+ ///
+ /// Gets a byte value that represents this boolean.
+ ///
+ /// The boolean.
+ /// Returns 00000001 if is , otherwise 0000000.
+ public static byte ToByte(this bool value)
+ {
+ return value ? (byte)1 : (byte)0;
+ }
+
+ ///
+ /// Toggles this booleans current state.
+ ///
+ /// The boolean.
+ /// Returns the opposite state of this boolean.
+ public static bool Not(this ref bool value)
+ {
+ return value = !value;
+ }
+
+ ///
+ /// Gets the value of this boolean and another boolean.
+ ///
+ /// The boolean.
+ /// The boolean comparator.
+ /// Returns when and are Otherwise, the output is .
+ public static bool And(this bool value, bool comparison)
+ {
+ return value && comparison;
+ }
+
+ ///
+ /// Gets the value of this boolean or another boolean.
+ ///
+ /// The boolean.
+ /// The boolean comparator.
+ /// Returns if or is .
+ public static bool Or(this bool value, bool comparison)
+ {
+ return value || comparison;
+ }
+
+ ///
+ /// Gets the value of this boolean exclusively or another boolean.
+ ///
+ /// The boolean.
+ /// The boolean comparator.
+ /// Returns if and are or if and are .
+ public static bool XOr(this bool value, bool comparison)
+ {
+ return value ^ comparison;
+ }
+
+ ///
+ /// Gets the value of this boolean and another boolean.
+ ///
+ /// The boolean.
+ /// The boolean comparator.
+ /// Returns if and are . Otherwise, .
+ public static bool NAnd(this bool value, bool comparison)
+ {
+ return !(value && comparison);
+ }
+
+ ///
+ /// Gets the value of this boolean and another boolean.
+ ///
+ /// The boolean.
+ /// The boolean comparator.
+ /// Returns if and are . Otherwise, .
+ public static bool NOr(this bool value, bool comparison)
+ {
+ return !(value || comparison);
+ }
+
+ ///
+ /// Gets the value of this boolean and another boolean.
+ ///
+ /// The boolean.
+ /// The boolean comparator.
+ /// Returns if and are the same, Otherwise .
+ public static bool XNOr(this bool value, bool comparison)
+ {
+ return !(value ^ comparison);
+ }
}
}