public virtual void TestSize() { BytesRef @ref = new BytesRef(); int num = AtLeast(2); for (int j = 0; j < num; j++) { int mod = 1 + Random.Next(39); for (int i = 0; i < 797; i++) { string str; do { str = TestUtil.RandomRealisticUnicodeString(Random, 1000); } while (str.Length == 0); @ref.CopyChars(str); int count = Hash.Count; int key = Hash.Add(@ref); if (key < 0) { Assert.AreEqual(Hash.Count, count); } else { Assert.AreEqual(Hash.Count, count + 1); } if (i % mod == 0) { Hash.Clear(); Assert.AreEqual(0, Hash.Count); Hash.Reinit(); } } } }
public virtual void TestAddByPoolOffset() { BytesRef @ref = new BytesRef(); BytesRef scratch = new BytesRef(); BytesRefHash offsetHash = NewHash(Pool); int num = AtLeast(2); for (int j = 0; j < num; j++) { ISet <string> strings = new JCG.HashSet <string>(); int uniqueCount = 0; for (int i = 0; i < 797; i++) { string str; do { str = TestUtil.RandomRealisticUnicodeString(Random, 1000); } while (str.Length == 0); @ref.CopyChars(str); int count = Hash.Count; int key = Hash.Add(@ref); if (key >= 0) { Assert.IsTrue(strings.Add(str)); Assert.AreEqual(uniqueCount, key); Assert.AreEqual(Hash.Count, count + 1); int offsetKey = offsetHash.AddByPoolOffset(Hash.ByteStart(key)); Assert.AreEqual(uniqueCount, offsetKey); Assert.AreEqual(offsetHash.Count, count + 1); uniqueCount++; } else { Assert.IsFalse(strings.Add(str)); Assert.IsTrue((-key) - 1 < count); Assert.AreEqual(str, Hash.Get((-key) - 1, scratch).Utf8ToString()); Assert.AreEqual(count, Hash.Count); int offsetKey = offsetHash.AddByPoolOffset(Hash.ByteStart((-key) - 1)); Assert.IsTrue((-offsetKey) - 1 < count); Assert.AreEqual(str, Hash.Get((-offsetKey) - 1, scratch).Utf8ToString()); Assert.AreEqual(count, Hash.Count); } } AssertAllIn(strings, Hash); foreach (string @string in strings) { @ref.CopyChars(@string); int key = Hash.Add(@ref); BytesRef bytesRef = offsetHash.Get((-key) - 1, scratch); Assert.AreEqual(@ref, bytesRef); } Hash.Clear(); Assert.AreEqual(0, Hash.Count); offsetHash.Clear(); Assert.AreEqual(0, offsetHash.Count); Hash.Reinit(); // init for the next round offsetHash.Reinit(); } }