lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mhern...@apache.org
Subject [07/50] [abbrv] git commit: Port: more util unit tests
Date Tue, 24 Sep 2013 18:32:43 GMT
Port: more util unit tests


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

Branch: refs/heads/branch_4x
Commit: 27c7d0d946026166e1ef95a5d93df6bf9ef8d368
Parents: 2e4e00b
Author: James Blair <jmblair7@gmail.com>
Authored: Thu Jul 11 17:39:36 2013 -0400
Committer: James Blair <jmblair7@gmail.com>
Committed: Thu Jul 11 17:39:36 2013 -0400

----------------------------------------------------------------------
 test/core/Util/TestPagedBytes.cs                |  12 +-
 test/core/Util/TestPriorityQueue.cs             |  40 ++---
 test/core/Util/TestRamUsageEstimator.cs         | 167 ++++++++++++++-----
 .../Util/TestRamUsageEstimatorOnWildAnimals.cs  |  48 ++++++
 .../Util/TestRecyclingByteBlockAllocator.cs     | 137 +++++++++++++++
 .../core/Util/TestRecyclingIntBlockAllocator.cs | 138 +++++++++++++++
 test/core/Util/TestRollingBuffer.cs             |  83 +++++++++
 test/core/Util/TestSentineIntSet.cs             |  65 ++++++++
 test/core/Util/TestSetOnce.cs                   | 103 ++++++++++++
 9 files changed, 724 insertions(+), 69 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/27c7d0d9/test/core/Util/TestPagedBytes.cs
----------------------------------------------------------------------
diff --git a/test/core/Util/TestPagedBytes.cs b/test/core/Util/TestPagedBytes.cs
index bd80843..4a24369 100644
--- a/test/core/Util/TestPagedBytes.cs
+++ b/test/core/Util/TestPagedBytes.cs
@@ -44,7 +44,7 @@ namespace Lucene.Net.Test.Util
                     }
                 }
 
-                indexOutput.Close();
+                indexOutput.Dispose();
                 IndexInput input = dir.OpenInput("foo", IOContext.DEFAULT);
                 var dataInput = (DataInput)input.Clone();
 
@@ -79,8 +79,8 @@ namespace Lucene.Net.Test.Util
                         assertEquals(answer[pos + byteUpto], slice.bytes[slice.offset + byteUpto]);
                     }
                 }
-                input.Close();
-                dir.Close();
+                input.Dispose();
+                dir.Dispose();
             }
         }
 
@@ -113,7 +113,7 @@ namespace Lucene.Net.Test.Util
                 i += len;
             }
             assertEquals(numBytes, indexOutput.FilePointer);
-            indexOutput.Close();
+            indexOutput.Dispose();
             IndexInput indexInput = dir.OpenInput("foo", IOContext.DEFAULT);
             p.Copy(indexInput, numBytes);
             var reader = p.Freeze(random.NextBool());
@@ -124,8 +124,8 @@ namespace Lucene.Net.Test.Util
                 reader.FillSlice(b, offset, 1);
                 assertEquals(arr[(int)(offset % arr.Length)], b.bytes[b.offset]);
             }
-            indexInput.Close();
-            dir.Close();
+            indexInput.Dispose();
+            dir.Dispose();
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/27c7d0d9/test/core/Util/TestPriorityQueue.cs
----------------------------------------------------------------------
diff --git a/test/core/Util/TestPriorityQueue.cs b/test/core/Util/TestPriorityQueue.cs
index 4a95578..de236b6 100644
--- a/test/core/Util/TestPriorityQueue.cs
+++ b/test/core/Util/TestPriorityQueue.cs
@@ -23,13 +23,13 @@ namespace Lucene.Net.Util
 {
 	
 	[TestFixture]
-	public class TestPriorityQueue:LuceneTestCase
+	public class TestPriorityQueue : LuceneTestCase
 	{
         private class IntegerQueue : PriorityQueue<int?>
         {
             public IntegerQueue(int count)
+                : base(count)
             {
-                Initialize(count);
             }
 
             public override bool LessThan(int? a, int? b)
@@ -41,16 +41,16 @@ namespace Lucene.Net.Util
 		[Test]
 		public virtual void  TestPQ()
 		{
-			TestPQ(10000, NewRandom());
+			TestPQ(AtLeast(10000), new Random());
 		}
 		
-		public static void  TestPQ(int count, System.Random gen)
+		public static void  TestPQ(int count, Random gen)
 		{
 			PriorityQueue<int?> pq = new IntegerQueue(count);
 		    int sum = 0;
             int? sum2 = 0;
 			
-			for (int i = 0; i < count; i++)
+			for (var i = 0; i < count; i++)
 			{
 				int next = gen.Next();
 				sum += next;
@@ -65,9 +65,9 @@ namespace Lucene.Net.Util
 			//      start = new Date();
 			
 			int? last = int.MinValue;
-			for (int i = 0; i < count; i++)
+			for (var i = 0; i < count; i++)
 			{
-				int? next = pq.Pop();
+				var next = pq.Pop();
 				Assert.IsTrue(next >= last);
 				last = next;
 				sum2 += last;
@@ -87,9 +87,9 @@ namespace Lucene.Net.Util
 			pq.Add(2);
             pq.Add(3);
             pq.Add(1);
-			Assert.AreEqual(3, pq.Size());
+			Assert.AreEqual(3, pq.Size);
 			pq.Clear();
-			Assert.AreEqual(0, pq.Size());
+			Assert.AreEqual(0, pq.Size);
 		}
 		
 		[Test]
@@ -102,29 +102,29 @@ namespace Lucene.Net.Util
             pq.InsertWithOverflow(5);
             pq.InsertWithOverflow(7);
             pq.InsertWithOverflow(1);
-			Assert.AreEqual(3, pq.Size());
+			Assert.AreEqual(3, pq.Size);
 			Assert.AreEqual(3, pq.Top());
 		}
 		
 		[Test]
 		public virtual void  TestInsertWithOverflow()
 		{
-			int size = 4;
+			var size = 4;
 			PriorityQueue<int?> pq = new IntegerQueue(size);
-			System.Int32 i1 = 2;
-			System.Int32 i2 = 3;
-			System.Int32 i3 = 1;
-			System.Int32 i4 = 5;
-			System.Int32 i5 = 7;
-			System.Int32 i6 = 1;
+			var i1 = 2;
+			var i2 = 3;
+			var i3 = 1;
+			var i4 = 5;
+			var i5 = 7;
+			var i6 = 1;
 			
 			Assert.IsNull(pq.InsertWithOverflow(i1));
 			Assert.IsNull(pq.InsertWithOverflow(i2));
 			Assert.IsNull(pq.InsertWithOverflow(i3));
 			Assert.IsNull(pq.InsertWithOverflow(i4));
-			Assert.IsTrue((int) pq.InsertWithOverflow(i5) == i3); // i3 should have been dropped
-			Assert.IsTrue((int) pq.InsertWithOverflow(i6) == i6); // i6 should not have been inserted
-			Assert.AreEqual(size, pq.Size());
+			Assert.IsTrue(pq.InsertWithOverflow(i5) == i3); // i3 should have been dropped
+			Assert.IsTrue(pq.InsertWithOverflow(i6) == i6); // i6 should not have been inserted
+			Assert.AreEqual(size, pq.Size);
 			Assert.AreEqual(2, pq.Top());
 		}
 	}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/27c7d0d9/test/core/Util/TestRamUsageEstimator.cs
----------------------------------------------------------------------
diff --git a/test/core/Util/TestRamUsageEstimator.cs b/test/core/Util/TestRamUsageEstimator.cs
index 3ebe8a2..1cef890 100644
--- a/test/core/Util/TestRamUsageEstimator.cs
+++ b/test/core/Util/TestRamUsageEstimator.cs
@@ -15,54 +15,135 @@
  * limitations under the License.
  */
 
-using System;
 
+using System;
 using NUnit.Framework;
 
 namespace Lucene.Net.Util
 {
-	
+
     [TestFixture]
-	public class TestRamUsageEstimator
-	{
-		
+    public class TestRamUsageEstimator
+    {
+
+        [Test]
+        public void TestSanity()
+        {
+            Assert.IsTrue(RamUsageEstimator.SizeOf("test string") > RamUsageEstimator.ShallowSizeOfInstance(typeof(string)));
+
+            var holder = new Holder { holder = new Holder("string2", 5000L) };
+            Assert.IsTrue(RamUsageEstimator.SizeOf(holder) > RamUsageEstimator.ShallowSizeOfInstance(typeof(Holder)));
+            Assert.IsTrue(RamUsageEstimator.SizeOf(holder) > RamUsageEstimator.SizeOf(holder.holder));
+
+            Assert.IsTrue(RamUsageEstimator.ShallowSizeOfInstance(typeof(HolderSubclass))
>= RamUsageEstimator.ShallowSizeOfInstance(typeof(Holder)));
+            Assert.IsTrue(RamUsageEstimator.ShallowSizeOfInstance(typeof(Holder)) == RamUsageEstimator.ShallowSizeOfInstance(typeof(HolderSubclass2)));
+
+            var strings = new string[] {
+                    "test string",
+                    "hollow", 
+                    "catchmaster"
+                };
+            Assert.IsTrue(RamUsageEstimator.SizeOf(strings) > RamUsageEstimator.ShallowSizeOf(strings));
+        }
+
+        [Test]
+        public void TestStaticOverloads()
+        {
+            var rnd = new Random();
+            {
+                var array = new byte[rnd.Next(1024)];
+                Assert.AreEqual(RamUsageEstimator.SizeOf(array), RamUsageEstimator.SizeOf((Object)array));
+            }
+
+            {
+                var array = new bool[rnd.Next(1024)];
+                Assert.AreEqual(RamUsageEstimator.SizeOf(array), RamUsageEstimator.SizeOf((Object)array));
+            }
+
+            {
+                var array = new char[rnd.Next(1024)];
+                Assert.AreEqual(RamUsageEstimator.SizeOf(array), RamUsageEstimator.SizeOf((Object)array));
+            }
+
+            {
+                var array = new short[rnd.Next(1024)];
+                Assert.AreEqual(RamUsageEstimator.SizeOf(array), RamUsageEstimator.SizeOf((Object)array));
+            }
+
+            {
+                var array = new int[rnd.Next(1024)];
+                Assert.AreEqual(RamUsageEstimator.SizeOf(array), RamUsageEstimator.SizeOf((Object)array));
+            }
+
+            {
+                var array = new float[rnd.Next(1024)];
+                Assert.AreEqual(RamUsageEstimator.SizeOf(array), RamUsageEstimator.SizeOf((Object)array));
+            }
+
+            {
+                var array = new long[rnd.Next(1024)];
+                Assert.AreEqual(RamUsageEstimator.SizeOf(array), RamUsageEstimator.SizeOf((Object)array));
+            }
+
+            {
+                var array = new double[rnd.Next(1024)];
+                Assert.AreEqual(RamUsageEstimator.SizeOf(array), RamUsageEstimator.SizeOf((Object)array));
+            }
+        }
+
         [Test]
-		public virtual void  TestBasic()
-		{
-			System.String string_Renamed = new System.Text.StringBuilder("test str").ToString();
-			RamUsageEstimator rue = new RamUsageEstimator();
-			long size = rue.EstimateRamUsage(string_Renamed);
-			System.Console.Out.WriteLine("size:" + size);
-			
-			string_Renamed = new System.Text.StringBuilder("test strin").ToString();
-			size = rue.EstimateRamUsage(string_Renamed);
-			System.Console.Out.WriteLine("size:" + size);
-			
-			Holder holder = new Holder();
-			holder.holder = new Holder("string2", 5000L);
-			size = rue.EstimateRamUsage(holder);
-			System.Console.Out.WriteLine("size:" + size);
-			
-			System.String[] strings = new System.String[]{new System.Text.StringBuilder("test strin").ToString(),
new System.Text.StringBuilder("hollow").ToString(), new System.Text.StringBuilder("catchmaster").ToString()};
-			size = rue.EstimateRamUsage(strings);
-			System.Console.Out.WriteLine("size:" + size);
-		}
-		
-		private sealed class Holder
-		{
-			internal long field1 = 5000L;
-			internal System.String name = "name";
-			internal Holder holder;
-			
-			internal Holder()
-			{
-			}
-			
-			internal Holder(System.String name, long field1)
-			{
-				this.name = name;
-				this.field1 = field1;
-			}
-		}
-	}
+        public void TestReferenceSize()
+        {
+            if (!IsSupportedJVM())
+            {
+                Console.Error.WriteLine("WARN: Your JVM does not support certain Oracle/Sun
extensions.");
+                Console.Error.WriteLine(" Memory estimates may be inaccurate.");
+                Console.Error.WriteLine(" Please report this to the Lucene mailing list.");
+                Console.Error.WriteLine("JVM version: " + RamUsageEstimator.JVM_INFO_STRING);
+                Console.Error.WriteLine("UnsupportedFeatures:");
+                foreach (var f in RamUsageEstimator.GetUnsupportedFeatures())
+                {
+                    Console.Error.Write(" - " + f.ToString());
+                    if (f == RamUsageEstimator.JvmFeature.OBJECT_ALIGNMENT)
+                    {
+                        Console.Error.Write("; Please note: 32bit Oracle/Sun VMs don't allow
exact OBJECT_ALIGNMENT retrieval, this is a known issue.");
+                    }
+                    Console.Error.WriteLine();
+                }
+            }
+
+            Assert.IsTrue(RamUsageEstimator.NUM_BYTES_OBJECT_REF == 4 || RamUsageEstimator.NUM_BYTES_OBJECT_REF
== 8);
+            if (!Constants.JRE_IS_64BIT)
+            {
+                Assert.AreEqual(4, RamUsageEstimator.NUM_BYTES_OBJECT_REF, "For 32bit JVMs,
reference size must always be 4?");
+            }
+        }
+
+        private class Holder
+        {
+            long field1 = 5000L;
+            string name = "name";
+            public Holder holder;
+            long field2, field3, field4;
+
+            public Holder() { }
+
+            public Holder(string name, long field1)
+            {
+                this.name = name;
+                this.field1 = field1;
+            }
+        }
+
+        private class HolderSubclass : Holder
+        {
+            byte foo;
+            int bar;
+        }
+
+        private class HolderSubclass2 : Holder
+        {
+            // empty, only inherits all fields -> size should be identical to superclass
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/27c7d0d9/test/core/Util/TestRamUsageEstimatorOnWildAnimals.cs
----------------------------------------------------------------------
diff --git a/test/core/Util/TestRamUsageEstimatorOnWildAnimals.cs b/test/core/Util/TestRamUsageEstimatorOnWildAnimals.cs
new file mode 100644
index 0000000..73351f9
--- /dev/null
+++ b/test/core/Util/TestRamUsageEstimatorOnWildAnimals.cs
@@ -0,0 +1,48 @@
+using System;
+using Lucene.Net.Util;
+using NUnit.Framework;
+
+namespace Lucene.Net.Test.Util
+{
+    [TestFixture]
+    public class TestRamUsageEstimatorOnWildAnimals : LuceneTestCase
+    {
+        public class ListElement
+        {
+            internal ListElement next;
+        }
+
+        [Test]
+        public void TestOverflowMaxChainLength()
+        {
+            var UPPERLIMIT = 100000;
+            var lower = 0;
+            var upper = UPPERLIMIT;
+
+            while (lower + 1 < upper)
+            {
+                var mid = (lower + upper) / 2;
+                try
+                {
+                    var first = new ListElement();
+                    var last = first;
+                    for (var i = 0; i < mid; i++)
+                    {
+                        last = (last.next = new ListElement());
+                    }
+                    RamUsageEstimator.SizeOf(first); // cause SOE or pass.
+                    lower = mid;
+                }
+                catch (StackOverflowException e)
+                {
+                    upper = mid;
+                }
+            }
+
+            if (lower + 1 < UPPERLIMIT)
+            {
+                Assert.Fail("Max object chain length till stack overflow: " + lower);
+            }
+        }  
+    }
+}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/27c7d0d9/test/core/Util/TestRecyclingByteBlockAllocator.cs
----------------------------------------------------------------------
diff --git a/test/core/Util/TestRecyclingByteBlockAllocator.cs b/test/core/Util/TestRecyclingByteBlockAllocator.cs
new file mode 100644
index 0000000..fed7d04
--- /dev/null
+++ b/test/core/Util/TestRecyclingByteBlockAllocator.cs
@@ -0,0 +1,137 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Lucene.Net.Util;
+using NUnit.Framework;
+
+namespace Lucene.Net.Test.Util
+{
+    [TestFixture]
+    public class TestRecyclingByteBlockAllocator : LuceneTestCase
+    {
+        private Random random = new Random();
+
+        [SetUp]
+        public override void SetUp()
+        {
+            base.SetUp();
+        }
+
+        private RecyclingByteBlockAllocator newAllocator()
+        {
+            return new RecyclingByteBlockAllocator(1 << (2 + random.Next(15)),
+                random.Next(97), Counter.NewCounter());
+        }
+
+        [Test]
+        public void testAllocate()
+        {
+            var allocator = newAllocator();
+            var set = new HashSet<sbyte[]>();
+            var block = allocator.ByteBlock;
+            set.Add(block);
+            assertNotNull(block);
+            var size = block.Length;
+
+            int num = AtLeast(97);
+            for (var i = 0; i < num; i++)
+            {
+                block = allocator.ByteBlock;
+                assertNotNull(block);
+                assertEquals(size, block.Length);
+                assertTrue("block is returned twice", set.Add(block));
+                assertEquals(size * (i + 2), allocator.BytesUsed); // zero based + 1
+                assertEquals(0, allocator.NumBufferedBlocks);
+            }
+        }
+
+        [Test]
+        public void TestAllocateAndRecycle()
+        {
+            var allocator = newAllocator();
+            var allocated = new HashSet<sbyte[]>();
+
+            var block = allocator.ByteBlock;
+            allocated.Add(block);
+            assertNotNull(block);
+            var size = block.Length;
+
+            int numIters = AtLeast(97);
+            for (var i = 0; i < numIters; i++)
+            {
+                var num = 1 + random.Next(39);
+                for (var j = 0; j < num; j++)
+                {
+                    block = allocator.ByteBlock;
+                    assertNotNull(block);
+                    assertEquals(size, block.Length);
+                    assertTrue("block is returned twice", allocated.Add(block));
+                    assertEquals(size * (allocated.Count + allocator.NumBufferedBlocks),
allocator
+                        .BytesUsed);
+                }
+                var array = allocated.ToArray();
+                var begin = random.Next(array.Length);
+                var end = begin + random.Next(array.Length - begin);
+                var selected = new List<sbyte[]>();
+                for (var j = begin; j < end; j++)
+                {
+                    selected.Add(array[j]);
+                }
+                allocator.RecycleByteBlocks(array, begin, end);
+                for (var j = begin; j < end; j++)
+                {
+                    assertNull(array[j]);
+                    var b = selected.Remove(new sbyte[] {0});
+                    assertTrue(allocated.Remove(new sbyte[] { b }));
+                }
+            }
+        }
+
+        [Test]
+        public void TestAllocateAndFree()
+        {
+            var allocator = newAllocator();
+            var allocated = new HashSet<sbyte[]>();
+            var freeButAllocated = 0;
+            var block = allocator.ByteBlock;
+            allocated.Add(block);
+            assertNotNull(block);
+            var size = block.Length;
+
+            int numIters = AtLeast(97);
+            for (var i = 0; i < numIters; i++)
+            {
+                var num = 1 + random.Next(39);
+                for (var j = 0; j < num; j++)
+                {
+                    block = allocator.ByteBlock;
+                    freeButAllocated = Math.Max(0, freeButAllocated - 1);
+                    assertNotNull(block);
+                    assertEquals(size, block.Length);
+                    assertTrue("block is returned twice", allocated.Add(block));
+                    assertEquals(size * (allocated.Count + allocator.NumBufferedBlocks),
+                        allocator.BytesUsed);
+                }
+
+                var array = allocated.ToArray();
+                var begin = random.Next(array.Length);
+                var end = begin + random.Next(array.Length - begin);
+                for (var j = begin; j < end; j++)
+                {
+                    var b = array[j];
+                    assertTrue(allocated.Remove(b));
+                }
+                allocator.RecycleByteBlocks(array, begin, end);
+                for (var j = begin; j < end; j++)
+                {
+                    assertNull(array[j]);
+                }
+                // randomly free blocks
+                var numFreeBlocks = allocator.NumBufferedBlocks;
+                var freeBlocks = allocator.FreeBlocks(random.Next(7 + allocator
+                    .MaxBufferedBlocks));
+                assertEquals(allocator.NumBufferedBlocks, numFreeBlocks - freeBlocks);
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/27c7d0d9/test/core/Util/TestRecyclingIntBlockAllocator.cs
----------------------------------------------------------------------
diff --git a/test/core/Util/TestRecyclingIntBlockAllocator.cs b/test/core/Util/TestRecyclingIntBlockAllocator.cs
new file mode 100644
index 0000000..8c923f9
--- /dev/null
+++ b/test/core/Util/TestRecyclingIntBlockAllocator.cs
@@ -0,0 +1,138 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Lucene.Net.Util;
+using NUnit.Framework;
+
+namespace Lucene.Net.Test.Util
+{
+    [TestFixture]
+    public class TestRecyclingIntBlockAllocator : LuceneTestCase
+    {
+        private Random random = new Random();
+
+        [SetUp]
+        public override void SetUp()
+        {
+            base.SetUp();
+        }
+
+        private RecyclingIntBlockAllocator NewAllocator()
+        {
+            return new RecyclingIntBlockAllocator(1 << (2 + random.Next(15)),
+                random.Next(97), Counter.NewCounter());
+        }
+
+        [Test]
+        public void TestAllocate()
+        {
+            var allocator = NewAllocator();
+            var set = new HashSet<int[]>();
+            var block = allocator.IntBlock;
+            set.Add(block);
+            assertNotNull(block);
+            var size = block.Length;
+
+            int num = AtLeast(97);
+            for (var i = 0; i < num; i++)
+            {
+                block = allocator.IntBlock;
+                assertNotNull(block);
+                assertEquals(size, block.Length);
+                assertTrue("block is returned twice", set.Add(block));
+                assertEquals(4 * size * (i + 2), allocator.BytesUsed); // zero based + 1
+                assertEquals(0, allocator.NumBufferedBlocks);
+            }
+        }
+
+        [Test]
+        public void TestAllocateAndRecycle()
+        {
+            var allocator = NewAllocator();
+            var allocated = new HashSet<int[]>();
+
+            var block = allocator.IntBlock;
+            allocated.Add(block);
+            assertNotNull(block);
+            var size = block.Length;
+
+            int numIters = AtLeast(97);
+            for (var i = 0; i < numIters; i++)
+            {
+                var num = 1 + random.Next(39);
+                for (var j = 0; j < num; j++)
+                {
+                    block = allocator.IntBlock;
+                    assertNotNull(block);
+                    assertEquals(size, block.Length);
+                    assertTrue("block is returned twice", allocated.Add(block));
+                    assertEquals(4 * size * (allocated.Count + allocator.NumBufferedBlocks),
allocator
+                        .BytesUsed);
+                }
+                var array = allocated.ToArray();
+                var begin = random.Next(array.Length);
+                var end = begin + random.Next(array.Length - begin);
+                var selected = new List<int[]>();
+                for (var j = begin; j < end; j++)
+                {
+                    selected.Add(array[j]);
+                }
+                allocator.RecycleIntBlocks(array, begin, end);
+                for (var j = begin; j < end; j++)
+                {
+                    assertNull(array[j]);
+                    int[] b = selected.Remove(0);
+                    assertTrue(allocated.Remove(b));
+                }
+            }
+        }
+
+        [Test]
+        public void TestAllocateAndFree()
+        {
+            var allocator = NewAllocator();
+            var allocated = new HashSet<int[]>();
+            var freeButAllocated = 0;
+            var block = allocator.IntBlock;
+            allocated.Add(block);
+            assertNotNull(block);
+            var size = block.Length;
+
+            int numIters = AtLeast(97);
+            for (var i = 0; i < numIters; i++)
+            {
+                var num = 1 + random.Next(39);
+                for (var j = 0; j < num; j++)
+                {
+                    block = allocator.IntBlock;
+                    freeButAllocated = Math.Max(0, freeButAllocated - 1);
+                    assertNotNull(block);
+                    assertEquals(size, block.Length);
+                    assertTrue("block is returned twice", allocated.Add(block));
+                    assertEquals("" + (4 * size * (allocated.Count + allocator.NumBufferedBlocks)
- allocator.BytesUsed),
+                        4 * size * (allocated.Count + allocator.NumBufferedBlocks),
+                        allocator.BytesUsed);
+                }
+
+                var array = allocated.ToArray();
+                var begin = random.Next(array.Length);
+                var end = begin + random.Next(array.Length - begin);
+                for (var j = begin; j < end; j++)
+                {
+                    var b = array[j];
+                    assertTrue(allocated.Remove(b));
+                }
+                allocator.RecycleIntBlocks(array, begin, end);
+                for (var j = begin; j < end; j++)
+                {
+                    assertNull(array[j]);
+                }
+                // randomly free blocks
+                var numFreeBlocks = allocator.NumBufferedBlocks;
+                var freeBlocks = allocator.FreeBlocks(random.Next(7 + allocator
+                    .MaxBufferedBlocks));
+                assertEquals(allocator.NumBufferedBlocks, numFreeBlocks - freeBlocks);
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/27c7d0d9/test/core/Util/TestRollingBuffer.cs
----------------------------------------------------------------------
diff --git a/test/core/Util/TestRollingBuffer.cs b/test/core/Util/TestRollingBuffer.cs
new file mode 100644
index 0000000..b68b3e4
--- /dev/null
+++ b/test/core/Util/TestRollingBuffer.cs
@@ -0,0 +1,83 @@
+using System;
+using Lucene.Net.Util;
+using NUnit.Framework;
+
+namespace Lucene.Net.Test.Util
+{
+    [TestFixture]
+    public class TestRollingBuffer : LuceneTestCase
+    {
+        private class Position : RollingBuffer.Resettable
+        {
+            public int pos;
+
+            public void Reset()
+            {
+                pos = -1;
+            }
+        }
+
+        private sealed class AnonymousRollingBuffer : RollingBuffer<Position>
+        {
+            protected override Position NewInstance()
+            {
+                var pos = new Position { pos = -1 };
+                return pos;
+            }
+        }
+
+        [Test]
+        public void Test()
+        {
+            RollingBuffer<Position> buffer = new AnonymousRollingBuffer();
+
+            for (var iter = 0; iter < 100 * RANDOM_MULTIPLIER; iter++)
+            {
+
+                var freeBeforePos = 0;
+                int maxPos = AtLeast(10000);
+                var posSet = new FixedBitSet(maxPos + 1000);
+                var posUpto = 0;
+                var random = new Random();
+                while (freeBeforePos < maxPos)
+                {
+                    if (random.Next(4) == 1)
+                    {
+                        var limit = Rarely() ? 1000 : 20;
+                        var inc = random.Next(limit);
+                        var pos = freeBeforePos + inc;
+                        posUpto = Math.Max(posUpto, pos);
+                        if (VERBOSE)
+                        {
+                            Console.WriteLine("  check pos=" + pos + " posUpto=" + posUpto);
+                        }
+                        var posData = buffer.Get(pos);
+                        if (!posSet.GetAndSet(pos))
+                        {
+                            assertEquals(-1, posData.pos);
+                            posData.pos = pos;
+                        }
+                        else
+                        {
+                            assertEquals(pos, posData.pos);
+                        }
+                    }
+                    else
+                    {
+                        if (posUpto > freeBeforePos)
+                        {
+                            freeBeforePos += random.Next(posUpto - freeBeforePos);
+                        }
+                        if (VERBOSE)
+                        {
+                            Console.WriteLine("  freeBeforePos=" + freeBeforePos);
+                        }
+                        buffer.FreeBefore(freeBeforePos);
+                    }
+                }
+
+                buffer.Reset();
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/27c7d0d9/test/core/Util/TestSentineIntSet.cs
----------------------------------------------------------------------
diff --git a/test/core/Util/TestSentineIntSet.cs b/test/core/Util/TestSentineIntSet.cs
new file mode 100644
index 0000000..5120ae8
--- /dev/null
+++ b/test/core/Util/TestSentineIntSet.cs
@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using Lucene.Net.Test.Support;
+using Lucene.Net.Util;
+using NUnit.Framework;
+
+namespace Lucene.Net.Test.Util
+{
+    [TestFixture]
+    public class TestSentineIntSet : LuceneTestCase
+    {
+        private Random random = new Random();
+
+        [Test]
+        public void test()
+        {
+            var set = new SentinelIntSet(10, -1);
+            Assert.IsFalse(set.Exists(50));
+            set.Put(50);
+            assertTrue(set.Exists(50));
+            assertEquals(1, set.Size);
+            assertEquals(-11, set.Find(10));
+            assertEquals(1, set.Size);
+            set.Clear();
+            assertEquals(0, set.Size);
+            assertEquals(50, set.Hash(50));
+            //force a rehash
+            for (int i = 0; i < 20; i++)
+            {
+                set.Put(i);
+            }
+            assertEquals(20, set.Size);
+            assertEquals(24, set.rehashCount);
+        }
+
+
+        [Test]
+        public void TestRandom()
+        {
+            for (var i = 0; i < 10000; i++)
+            {
+                var initSz = random.Next(20);
+                var num = random.Next(30);
+                var maxVal = (random.NextBool() ? random.Next(50) : random.Next(int.MaxValue))
+ 1;
+
+                var a = new HashSet<int>();
+                var b = new SentinelIntSet(initSz, -1);
+
+                for (var j = 0; j < num; j++)
+                {
+                    var val = random.Next(maxVal);
+                    var exists = !a.Add(val);
+                    var existsB = b.Exists(val);
+                    assertEquals(exists, existsB);
+                    var slot = b.Find(val);
+                    assertEquals(exists, slot >= 0);
+                    b.Put(val);
+
+                    assertEquals(a.Count, b.Size);
+                }
+            }
+
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/27c7d0d9/test/core/Util/TestSetOnce.cs
----------------------------------------------------------------------
diff --git a/test/core/Util/TestSetOnce.cs b/test/core/Util/TestSetOnce.cs
new file mode 100644
index 0000000..8df4ce3
--- /dev/null
+++ b/test/core/Util/TestSetOnce.cs
@@ -0,0 +1,103 @@
+using System;
+using System.Threading;
+using Lucene.Net.Support;
+using Lucene.Net.Util;
+using NUnit.Framework;
+
+namespace Lucene.Net.Test.Util
+{
+    [TestFixture]
+    public class TestSetOnce : LuceneTestCase
+    {
+        private class SetOnceThread : ThreadClass
+        {
+            internal SetOnce<int> set;
+            internal bool success = false;
+            internal Random RAND;
+
+            public SetOnceThread(Random random)
+            {
+                RAND = new Random(random.Next());
+            }
+
+            public override void Run()
+            {
+                try
+                {
+                    Sleep(RAND.Next(10)); // sleep for a short time
+                    set.Set(int.Parse(Name.Substring(2)));
+                    success = true;
+                }
+                catch (ThreadInterruptedException e)
+                {
+                    // ignore
+                }
+                catch (SystemException e)
+                {
+                    // TODO: change exception type
+                    // expected.
+                    success = false;
+                }
+            }
+        }
+
+        //@Test
+        [Test]
+        public void TestEmptyCtor()
+        {
+            var set = new SetOnce<int>();
+            assertNull(set.Get());
+        }
+
+        //@Test(expected=SetOnce<>.AlreadySetException.class)
+        [Test]
+        [ExpectedException(typeof(SetOnce<>.AlreadySetException))]
+        public void TestSettingCtor()
+        {
+            var set = new SetOnce<int>(5);
+            assertEquals(5, set.Get());
+            set.Set(7);
+        }
+
+        //@Test(expected=SetOnce<>.AlreadySetException.class)
+        [Test]
+        [ExpectedException(typeof(SetOnce<>.AlreadySetException))]
+        public void testSetOnce()
+        {
+            var set = new SetOnce<int>();
+            set.Set(5);
+            assertEquals(5, set.Get());
+            set.Set(7);
+        }
+
+        [Test]
+        public void TestSetMultiThreaded()
+        {
+            var set = new SetOnce<int>();
+            var threads = new SetOnceThread[10];
+            for (var i = 0; i < threads.Length; i++)
+            {
+                threads[i] = new SetOnceThread(new Random()) {Name = "t-" + (i + 1), set
= set};
+            }
+
+            foreach (var t in threads)
+            {
+                t.Start();
+            }
+
+            foreach (var t in threads)
+            {
+                t.Join();
+            }
+
+            foreach (SetOnceThread t in threads)
+            {
+                if (t.success)
+                {
+                    var expectedVal = int.Parse(t.Name.Substring(2));
+                    assertEquals("thread " + t.Name, expectedVal, t.set.Get());
+                }
+            }
+        }
+    }
+}


Mime
View raw message