diff options
author | Sebastien Pouliot <sebastien@ximian.com> | 2010-08-05 21:21:05 +0400 |
---|---|---|
committer | Sebastien Pouliot <sebastien@ximian.com> | 2010-08-05 21:21:05 +0400 |
commit | e70cf886e4a05921b5cab5e7422e0eb171b52c7d (patch) | |
tree | dd87fe20260abe8abf8484155d29a89345442874 | |
parent | c866b235b3d2f39f9a76713ea4b1ff8714f3653a (diff) |
Add Count() method. Fix Clear(int) when > 32. Add more test cases to get full coverage.
-rw-r--r-- | gendarme/framework/Gendarme.Framework/Bitmask.cs | 15 | ||||
-rw-r--r-- | gendarme/framework/Test/Gendarme.Framework/BitmaskTest.cs | 51 |
2 files changed, 65 insertions, 1 deletions
diff --git a/gendarme/framework/Gendarme.Framework/Bitmask.cs b/gendarme/framework/Gendarme.Framework/Bitmask.cs index 099198d2..438953ee 100644 --- a/gendarme/framework/Gendarme.Framework/Bitmask.cs +++ b/gendarme/framework/Gendarme.Framework/Bitmask.cs @@ -66,7 +66,7 @@ namespace Gendarme.Framework { public void Clear (T bit) { unchecked { - mask &= (ulong) ~(1 << bit.ToInt32 (null)); + mask &= ~((ulong) 1 << bit.ToInt32 (null)); } } @@ -78,6 +78,19 @@ namespace Gendarme.Framework { mask = 0; } + public int Count () + { + if (mask == 0) + return 0; + + int count = 0; + for (int i = 0; i < 64; i++) { + if (((mask >> i) & 1) == 1) + count++; + } + return count; + } + /// <summary> /// Get the bit represented by the parameter /// </summary> diff --git a/gendarme/framework/Test/Gendarme.Framework/BitmaskTest.cs b/gendarme/framework/Test/Gendarme.Framework/BitmaskTest.cs index b9ddd364..d8698a2a 100644 --- a/gendarme/framework/Test/Gendarme.Framework/BitmaskTest.cs +++ b/gendarme/framework/Test/Gendarme.Framework/BitmaskTest.cs @@ -56,6 +56,57 @@ namespace Test.Framework { } [Test] + public void Count () + { + Bitmask<long> x = new Bitmask<long> (); + Assert.AreEqual (0, x.Count (), "0"); + x.SetAll (); + Assert.AreEqual (64, x.Count (), "64"); + for (int i = 63; i >= 0; i--) { + x.Clear (i); + Assert.AreEqual (i, x.Count (), i.ToString ()); + } + } + + [Test] + public void Intersect () + { + Bitmask<long> x = new Bitmask<long> (); + Assert.IsTrue (x.Intersect (null), "null"); // special case since they are equals + Assert.IsFalse (x.Intersect (x), "self"); + + Bitmask<long> all = new Bitmask<long> (); + all.SetAll (); + Assert.IsFalse (x.Intersect (all), "x N all"); + Assert.IsFalse (all.Intersect (x), "all N x"); + + x.Set (0); + Assert.IsTrue (x.Intersect (all), "1 N all"); + Assert.IsTrue (all.Intersect (x), "all N 1"); + + Assert.IsTrue (x.Intersect (x), "self 1"); + } + + [Test] + public void IsSubsetOf () + { + Bitmask<long> x = new Bitmask<long> (); + Assert.IsFalse (x.IsSubsetOf (null), "null"); + Assert.IsTrue (x.IsSubsetOf (x), "self"); + + Bitmask<long> all = new Bitmask<long> (); + all.SetAll (); + Assert.IsTrue (x.IsSubsetOf (all), "x < all"); + Assert.IsFalse (all.IsSubsetOf (x), "all < x"); + + x.Set (0); + Assert.IsTrue (x.IsSubsetOf (all), "1 < all"); + Assert.IsFalse (all.IsSubsetOf (x), "all < 1"); + + Assert.IsTrue (x.IsSubsetOf (x), "self 1"); + } + + [Test] public void SetClearAll () { Bitmask<long> x = new Bitmask<long> (); |