lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mhern...@apache.org
Subject [8/9] git commit: adding ByteBlockPool and Counter with tests
Date Wed, 20 Aug 2014 02:32:12 GMT
adding ByteBlockPool and Counter with tests


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

Branch: refs/heads/pcl
Commit: b83890770e5387a69aca8afa47b4389cc979d1b2
Parents: 37b2670
Author: Michael Herndon <mherndon@michaelherndon.com>
Authored: Tue Aug 19 12:11:25 2014 -0400
Committer: Michael Herndon <mherndon@michaelherndon.com>
Committed: Tue Aug 19 12:11:25 2014 -0400

----------------------------------------------------------------------
 .../Util/TestByteBlockPool.cs                   | 95 ++++++++++++++++++++
 1 file changed, 95 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b8389077/test/Lucene.Net.Core.Tests/Util/TestByteBlockPool.cs
----------------------------------------------------------------------
diff --git a/test/Lucene.Net.Core.Tests/Util/TestByteBlockPool.cs b/test/Lucene.Net.Core.Tests/Util/TestByteBlockPool.cs
new file mode 100644
index 0000000..b632a4c
--- /dev/null
+++ b/test/Lucene.Net.Core.Tests/Util/TestByteBlockPool.cs
@@ -0,0 +1,95 @@
+
+
+namespace Lucene.Net.Util
+{
+    using Lucene.Net.Random;
+    using System.Collections.Generic;
+
+    /*
+     * Licensed to the Apache Software Foundation (ASF) under one or more
+     * contributor license agreements. See the NOTICE file distributed with this
+     * work for additional information regarding copyright ownership. The ASF
+     * licenses this file to You under the Apache License, Version 2.0 (the
+     * "License"); you may not use this file except in compliance with the License.
+     * You may obtain a copy of the License at
+     *
+     * http://www.apache.org/licenses/LICENSE-2.0
+     *
+     * Unless required by applicable law or agreed to in writing, software
+     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+     * License for the specific language governing permissions and limitations under
+     * the License.
+     */
+
+    public class TestByteBlockPool : LuceneTestCase
+    {
+        public class BytesRefProxy : BytesRef
+        {
+            public BytesRefProxy()
+            {
+            
+            }
+
+            public BytesRefProxy(string value) : base(value)
+            {
+                
+            }
+
+            internal void ForceGrow(int length)
+            {
+                this.Grow(length);
+            }
+
+            internal void SetLength(int length)
+            {
+                base.Length = length;
+            }
+        }
+
+        [Test]
+        public virtual void TestReadAndWrite()
+        {
+            var bytesUsed = Counter.NewCounter();
+            var  pool = new ByteBlockPool(new ByteBlockPool.DirectTrackingAllocator(bytesUsed));
+            pool.NextBuffer();
+            var reuseFirst = this.Random.NextBoolean();
+            for (var j = 0; j < 2; j++)
+            {
+                IList<BytesRefProxy> list = new List<BytesRefProxy>();
+                int maxLength = this.AtLeast(500),
+                    numValues = this.AtLeast(100);
+                
+                var @ref = new BytesRefProxy();
+                numValues.Times(i =>
+                {
+                    string value = this.Random.RandomRealisticUnicodeString(maxLength: maxLength);
+                    list.Add(new BytesRefProxy(value));
+                    @ref.CopyChars(value);
+                    pool.Append(@ref);
+                });
+      
+                // verify
+                long position = 0;
+                foreach (var expected in list)
+                {
+                    @ref.ForceGrow(expected.Length);
+                    @ref.SetLength(expected.Length);
+                    pool.ReadBytes(position, @ref.Bytes, @ref.Offset, @ref.Length);
+                    Equal(expected, @ref);
+                    position += @ref.Length;
+                }
+                pool.Reset(this.Random.NextBoolean(), reuseFirst);
+                if (reuseFirst)
+                {
+                    Equal(ByteBlockPool.BYTE_BLOCK_SIZE, bytesUsed.Count);
+                }
+                else
+                {
+                    Equal(0, bytesUsed.Count);
+                    pool.NextBuffer(); // prepare for next iter
+                }
+            }
+        }
+    }
+}
\ No newline at end of file


Mime
View raw message