lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject [03/13] git commit: Fixes TestOpenBitSet
Date Mon, 15 Sep 2014 22:52:54 GMT
Fixes TestOpenBitSet

Mainly fixed problems with conversion from Java's BitSet.get and
Bitset.set to C#'s BitArray.get and BitSet.set. The latter throws
exceptions in cases where the former wouldn't.


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/fd5e63c5
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/fd5e63c5
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/fd5e63c5

Branch: refs/heads/master
Commit: fd5e63c5138c57818e125fc47ef5b2bcd42b6a71
Parents: 8d49ec7
Author: Prad Nelluru <prnell@microsoft.com>
Authored: Wed Sep 10 14:27:24 2014 -0700
Committer: Prad Nelluru <prnell@microsoft.com>
Committed: Wed Sep 10 14:27:24 2014 -0700

----------------------------------------------------------------------
 src/Lucene.Net.Core/Support/BitSetSupport.cs     | 17 +++++++++++++++++
 src/Lucene.Net.Tests/core/Util/TestOpenBitSet.cs | 18 +++++++++---------
 2 files changed, 26 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/fd5e63c5/src/Lucene.Net.Core/Support/BitSetSupport.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/BitSetSupport.cs b/src/Lucene.Net.Core/Support/BitSetSupport.cs
index 9c00317..46ae2b4 100644
--- a/src/Lucene.Net.Core/Support/BitSetSupport.cs
+++ b/src/Lucene.Net.Core/Support/BitSetSupport.cs
@@ -228,6 +228,23 @@ namespace Lucene.Net.Support
             }
         }
 
+
+        // Emulates the Java BitSet.Get() method.
+        // Prevents exceptions from being thrown when the index is too high.
+        public static bool SafeGet(this BitArray a, int loc)
+        {
+            return loc >= a.Count ? false : a.Get(loc);
+        }
+
+        //Emulates the Java BitSet.Set() method. Required to reconcile differences between
Java BitSet and C# BitArray
+        public static void SafeSet(this BitArray a, int loc, bool value)
+        {
+            if (loc >= a.Length)
+                a.Length = loc + 1;
+
+            a.Set(loc, value);
+        }
+
         // Clears all bits in this BitArray that correspond to a set bit in the parameter
BitArray
         public static void AndNot(this BitArray bitsA, BitArray bitsB)
         {

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/fd5e63c5/src/Lucene.Net.Tests/core/Util/TestOpenBitSet.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/TestOpenBitSet.cs b/src/Lucene.Net.Tests/core/Util/TestOpenBitSet.cs
index 71caee9..03959df 100644
--- a/src/Lucene.Net.Tests/core/Util/TestOpenBitSet.cs
+++ b/src/Lucene.Net.Tests/core/Util/TestOpenBitSet.cs
@@ -98,7 +98,7 @@ namespace Lucene.Net.Util
             {
                 // aa = a.PrevSetBit(aa-1);
                 aa--;
-                while ((aa >= 0) && (!a.Get(aa)))
+                while ((aa >= 0) && (!a.SafeGet(aa)))
                 {
                     aa--;
                 }
@@ -115,7 +115,7 @@ namespace Lucene.Net.Util
             {
                 // aa = a.PrevSetBit(aa-1);
                 aa--;
-                while ((aa >= 0) && (!a.Get(aa)))
+                while ((aa >= 0) && (!a.SafeGet(aa)))
                 {
                     aa--;
                 }
@@ -181,23 +181,23 @@ namespace Lucene.Net.Util
                         int idx;
 
                         idx = Random().Next(sz);
-                        a.Set(idx, true);
+                        a.SafeSet(idx, true);
                         b.FastSet(idx);
 
                         idx = Random().Next(sz);
-                        a.Set(idx, true);
+                        a.SafeSet(idx, true);
                         b.FastSet((long)idx);
 
                         idx = Random().Next(sz);
-                        a.Set(idx, false);
+                        a.SafeSet(idx, false);
                         b.FastClear(idx);
 
                         idx = Random().Next(sz);
-                        a.Set(idx, false);
+                        a.SafeSet(idx, false);
                         b.FastClear((long)idx);
 
                         idx = Random().Next(sz);
-                        a.Set(idx, !a.Get(idx));
+                        a.SafeSet(idx, !a.SafeGet(idx));
                         b.FastFlip(idx);
 
                         bool val = b.FlipAndGet(idx);
@@ -205,7 +205,7 @@ namespace Lucene.Net.Util
                         Assert.IsTrue(val != val2);
 
                         idx = Random().Next(sz);
-                        a.Set(idx, !a.Get(idx));
+                        a.SafeSet(idx, !a.SafeGet(idx));
                         b.FastFlip((long)idx);
 
                         val = b.FlipAndGet((long)idx);
@@ -379,7 +379,7 @@ namespace Lucene.Net.Util
             BitArray bs = new BitArray(a.Length);
             foreach (int e in a)
             {
-                bs.Set(e, true);
+                bs.SafeSet(e, true);
             }
             return bs;
         }


Mime
View raw message