lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject [05/12] lucenenet git commit: Added one more test.
Date Tue, 17 Feb 2015 17:29:47 GMT
Added one more test.


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

Branch: refs/heads/master
Commit: 707647d20fc482abcec0713fd47e8f6da39d2c3f
Parents: 84e29b5
Author: Guido Tagliavini Ponce <t-gupon@microsoft.com>
Authored: Thu Feb 12 15:01:58 2015 -0800
Committer: Guido Tagliavini Ponce <t-gupon@microsoft.com>
Committed: Thu Feb 12 15:01:58 2015 -0800

----------------------------------------------------------------------
 .../core/Util/TestPriorityQueue.cs              | 145 +++++++++++++------
 1 file changed, 98 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/707647d2/src/Lucene.Net.Tests/core/Util/TestPriorityQueue.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/TestPriorityQueue.cs b/src/Lucene.Net.Tests/core/Util/TestPriorityQueue.cs
index 1209854..21d2d5b 100644
--- a/src/Lucene.Net.Tests/core/Util/TestPriorityQueue.cs
+++ b/src/Lucene.Net.Tests/core/Util/TestPriorityQueue.cs
@@ -59,6 +59,28 @@ namespace Lucene.Net.Util
             }
         }
 
+        private class MyType
+        {
+            public MyType(int field)
+            {
+                Field = field;
+            }
+            public int Field { get; set; }
+        }
+
+        private class MyQueue : PriorityQueue<MyType>
+        {
+            public MyQueue(int count)
+                : base(count)
+            {
+            }
+
+            public override bool LessThan(MyType a, MyType b)
+            {
+                return (a.Field < b.Field);
+            }
+        }
+
         [Ignore] // Increase heap size to run this test
         [Test]
         public static void TestMaxSizeBounds()
@@ -252,6 +274,35 @@ namespace Lucene.Net.Util
         }
 
         [Test]
+        public static void TestUpdateTop()
+        {
+            // Mostly to reflect the usage of UpdateTop
+            int maxSize = 10;
+            PriorityQueue<MyType> pq = new MyQueue(maxSize);
+
+            pq.Add(new MyType(1));
+            pq.Add(new MyType(20));
+            pq.Add(new MyType(1));
+            pq.Add(new MyType(15));
+            pq.Add(new MyType(4));
+            pq.Add(new MyType(12));
+            pq.Add(new MyType(1000));
+            pq.Add(new MyType(-300));
+
+            Assert.AreEqual(pq.Top().Field, -300);
+            MyType topElement = pq.Top();
+            topElement.Field = 25;  // Now this should no longer be at the top of the queue
+            pq.UpdateTop();         // Hence we need to update the top queue
+            Assert.AreEqual(pq.Top().Field, 1);
+
+            // The less eficient way to do this is the following
+            topElement = pq.Pop();
+            topElement.Field = 678;
+            pq.Add(topElement);
+            Assert.AreEqual(pq.Top().Field, 1);
+        }
+
+        [Test]
         public static void TestOverflow()
         {
             // Tests adding elements to full queues
@@ -289,53 +340,6 @@ namespace Lucene.Net.Util
         }
 
         [Test]
-        public static void TestStress()
-        {
-            int maxSize = AtLeast(100000);
-
-            PriorityQueue<int?> pq = new IntegerQueue(maxSize);
-            int sum = 0, sum2 = 0;
-
-            DateTime start, end;
-            TimeSpan total;
-            start = DateTime.Now;
-
-            // Add a lot of elements
-            for (int i = 0; i < maxSize; i++)
-            {
-                int next = Random().Next();
-                sum += next;
-                pq.Add(next);
-            }
-
-            end = DateTime.Now;
-            total = end - start;
-            // Note that this measurement considers the random number generation
-            System.Console.WriteLine("Total adding time: {0} ticks or {1}ms", total.Ticks,
total.Milliseconds);
-            System.Console.WriteLine("Time per add: {0} ticks", total.Ticks / maxSize);
-
-            // Pop them and check that the elements are taken in sorted order
-            start = DateTime.Now;
-            int last = int.MinValue;
-            for (int i = 0; i < maxSize; i++)
-            {
-                int? next = pq.Pop();
-                Assert.IsTrue((int)next >= last);
-                last = (int)next;
-                sum2 += last;
-            }
-
-            end = DateTime.Now;
-            total = end - start;
-            // Note that this measurement considers the random number generation
-            System.Console.WriteLine("Total poping time: {0} ticks or {1}ms", total.Ticks,
total.Milliseconds);
-            System.Console.WriteLine("Time per pop: {0} ticks", total.Ticks / maxSize);
-
-            // Loose checking that we didn't lose data in the process
-            Assert.AreEqual(sum, sum2);
-        }
-
-        [Test]
         public virtual void TestClear()
         {
             PriorityQueue<int?> pq = new IntegerQueue(3);
@@ -394,5 +398,52 @@ namespace Lucene.Net.Util
             pq.Pop();
             Assert.AreEqual((int?)7, pq.Top());
         }
+
+        [Test]
+        public static void TestStress()
+        {
+            int maxSize = AtLeast(100000);
+
+            PriorityQueue<int?> pq = new IntegerQueue(maxSize);
+            int sum = 0, sum2 = 0;
+
+            DateTime start, end;
+            TimeSpan total;
+            start = DateTime.Now;
+
+            // Add a lot of elements
+            for (int i = 0; i < maxSize; i++)
+            {
+                int next = Random().Next();
+                sum += next;
+                pq.Add(next);
+            }
+
+            end = DateTime.Now;
+            total = end - start;
+            // Note that this measurement considers the random number generation
+            System.Console.WriteLine("Total adding time: {0} ticks or {1}ms", total.Ticks,
total.Milliseconds);
+            System.Console.WriteLine("Time per add: {0} ticks", total.Ticks / maxSize);
+
+            // Pop them and check that the elements are taken in sorted order
+            start = DateTime.Now;
+            int last = int.MinValue;
+            for (int i = 0; i < maxSize; i++)
+            {
+                int? next = pq.Pop();
+                Assert.IsTrue((int)next >= last);
+                last = (int)next;
+                sum2 += last;
+            }
+
+            end = DateTime.Now;
+            total = end - start;
+            // Note that this measurement considers the random number generation
+            System.Console.WriteLine("Total poping time: {0} ticks or {1}ms", total.Ticks,
total.Milliseconds);
+            System.Console.WriteLine("Time per pop: {0} ticks", total.Ticks / maxSize);
+
+            // Loose checking that we didn't lose data in the process
+            Assert.AreEqual(sum, sum2);
+        }
     }
 }
\ No newline at end of file


Mime
View raw message