예제 #1
0
        public virtual void TestAdd()
        {
            BytesRef @ref    = new BytesRef();
            BytesRef scratch = new BytesRef();
            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);
                        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);
                    }
                }

                AssertAllIn(strings, hash);
                hash.Clear();
                Assert.AreEqual(0, hash.Count);
                hash.Reinit();
            }
        }
예제 #2
0
        public virtual void TestReadAndWrite()
        {
            Counter       bytesUsed = Util.Counter.NewCounter();
            ByteBlockPool pool      = new ByteBlockPool(new ByteBlockPool.DirectTrackingAllocator(bytesUsed));

            pool.NextBuffer();
            bool reuseFirst = Random.NextBoolean();

            for (int j = 0; j < 2; j++)
            {
                IList <BytesRef> list = new List <BytesRef>();
                int      maxLength    = AtLeast(500);
                int      numValues    = AtLeast(100);
                BytesRef @ref         = new BytesRef();
                for (int i = 0; i < numValues; i++)
                {
                    string value = TestUtil.RandomRealisticUnicodeString(Random, maxLength);
                    list.Add(new BytesRef(value));
                    @ref.CopyChars(value);
                    pool.Append(@ref);
                }
                // verify
                long position = 0;
                foreach (BytesRef expected in list)
                {
                    @ref.Grow(expected.Length);
                    @ref.Length = expected.Length;
                    pool.ReadBytes(position, @ref.Bytes, @ref.Offset, @ref.Length);
                    Assert.AreEqual(expected, @ref);
                    position += @ref.Length;
                }
                pool.Reset(Random.NextBoolean(), reuseFirst);
                if (reuseFirst)
                {
                    Assert.AreEqual(ByteBlockPool.BYTE_BLOCK_SIZE, bytesUsed.Get());
                }
                else
                {
                    Assert.AreEqual(0, bytesUsed.Get());
                    pool.NextBuffer(); // prepare for next iter
                }
            }
        }
예제 #3
0
        public virtual void TestFind()
        {
            BytesRef @ref    = new BytesRef();
            BytesRef scratch = new BytesRef();
            int      num     = AtLeast(2);

            for (int j = 0; j < num; j++)
            {
                HashSet <string> strings = new 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.Size();
                    int key   = Hash.Find(@ref); //hash.Add(ref);
                    if (key >= 0)                // string found in hash
                    {
                        Assert.IsFalse(strings.Add(str));
                        Assert.IsTrue(key < count);
                        Assert.AreEqual(str, Hash.Get(key, scratch).Utf8ToString());
                        Assert.AreEqual(count, Hash.Size());
                    }
                    else
                    {
                        key = Hash.Add(@ref);
                        Assert.IsTrue(strings.Add(str));
                        Assert.AreEqual(uniqueCount, key);
                        Assert.AreEqual(Hash.Size(), count + 1);
                        uniqueCount++;
                    }
                }

                AssertAllIn(strings, Hash);
                Hash.Clear();
                Assert.AreEqual(0, Hash.Size());
                Hash.Reinit();
            }
        }
예제 #4
0
        public virtual void TestSort()
        {
            Random        random     = Random;
            BytesRefArray list       = new BytesRefArray(Util.Counter.NewCounter());
            List <string> stringList = new List <string>();

            for (int j = 0; j < 2; j++)
            {
                if (j > 0 && random.NextBoolean())
                {
                    list.Clear();
                    stringList.Clear();
                }
                int      entries  = AtLeast(500);
                BytesRef spare    = new BytesRef();
                int      initSize = list.Length;
                for (int i = 0; i < entries; i++)
                {
                    string randomRealisticUnicodeString = TestUtil.RandomRealisticUnicodeString(random);
                    spare.CopyChars(randomRealisticUnicodeString);
                    Assert.AreEqual(initSize + i, list.Append(spare));
                    stringList.Add(randomRealisticUnicodeString);
                }

                // LUCENENET NOTE: Must sort using ArrayUtil.GetNaturalComparator<T>()
                // to ensure culture isn't taken into consideration during the sort,
                // which will match the sort order of BytesRef.UTF8SortedAsUTF16Comparer.
                CollectionUtil.TimSort(stringList);
#pragma warning disable 612, 618
                IBytesRefIterator iter = list.GetIterator(BytesRef.UTF8SortedAsUTF16Comparer);
#pragma warning restore 612, 618
                int a = 0;
                while ((spare = iter.Next()) != null)
                {
                    Assert.AreEqual(stringList[a], spare.Utf8ToString(), "entry " + a + " doesn't match");
                    a++;
                }
                Assert.IsNull(iter.Next());
                Assert.AreEqual(a, stringList.Count);
            }
        }
예제 #5
0
        public virtual void TestSort()
        {
            Random        random     = Random();
            BytesRefArray list       = new BytesRefArray(Util.Counter.NewCounter());
            List <string> stringList = new List <string>();

            for (int j = 0; j < 2; j++)
            {
                if (j > 0 && random.NextBoolean())
                {
                    list.Clear();
                    stringList.Clear();
                }
                int      entries  = AtLeast(500);
                BytesRef spare    = new BytesRef();
                int      initSize = list.Size();
                for (int i = 0; i < entries; i++)
                {
                    string randomRealisticUnicodeString = TestUtil.RandomRealisticUnicodeString(random);
                    spare.CopyChars(randomRealisticUnicodeString);
                    Assert.AreEqual(initSize + i, list.Append(spare));
                    stringList.Add(randomRealisticUnicodeString);
                }

                stringList.Sort();
                BytesRefIterator iter = list.Iterator(BytesRef.UTF8SortedAsUTF16Comparer);
                int a = 0;
                while ((spare = iter.Next()) != null)
                {
                    Assert.AreEqual(stringList[a], spare.Utf8ToString(), "entry " + a + " doesn't match");
                    a++;
                }
                Assert.IsNull(iter.Next());
                Assert.AreEqual(a, stringList.Count);
            }
        }
예제 #6
0
        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();
            }
        }