Пример #1
0
        public virtual void TestToString()
        {
            char[] b = new char[] { 'a', 'l', 'o', 'h', 'a' };
            Token  t = new Token("", 0, 5);

            t.CopyBuffer(b, 0, 5);
            Assert.AreEqual("aloha", t.ToString());

            t.SetEmpty().Append("hi there");
            Assert.AreEqual("hi there", t.ToString());
        }
Пример #2
0
        public virtual void TestGrow()
        {
            Token         t   = new Token();
            StringBuilder buf = new StringBuilder("ab");

            for (int i = 0; i < 20; i++)
            {
                char[] content = buf.ToString().ToCharArray();
                t.CopyBuffer(content, 0, content.Length);
                Assert.AreEqual(buf.Length, t.Length);
                Assert.AreEqual(buf.ToString(), t.ToString());
                buf.Append(buf.ToString());
            }
            Assert.AreEqual(1048576, t.Length);

            // now as a string, second variant
            t   = new Token();
            buf = new StringBuilder("ab");
            for (int i = 0; i < 20; i++)
            {
                t.SetEmpty().Append(buf);
                string content = buf.ToString();
                Assert.AreEqual(content.Length, t.Length);
                Assert.AreEqual(content, t.ToString());
                buf.Append(content);
            }
            Assert.AreEqual(1048576, t.Length);

            // Test for slow growth to a long term
            t   = new Token();
            buf = new StringBuilder("a");
            for (int i = 0; i < 20000; i++)
            {
                t.SetEmpty().Append(buf);
                string content = buf.ToString();
                Assert.AreEqual(content.Length, t.Length);
                Assert.AreEqual(content, t.ToString());
                buf.Append("a");
            }
            Assert.AreEqual(20000, t.Length);

            // Test for slow growth to a long term
            t   = new Token();
            buf = new StringBuilder("a");
            for (int i = 0; i < 20000; i++)
            {
                t.SetEmpty().Append(buf);
                string content = buf.ToString();
                Assert.AreEqual(content.Length, t.Length);
                Assert.AreEqual(content, t.ToString());
                buf.Append("a");
            }
            Assert.AreEqual(20000, t.Length);
        }
Пример #3
0
        public virtual void TestResize()
        {
            Token t = new Token();

            char[] content = "hello".ToCharArray();
            t.CopyBuffer(content, 0, content.Length);
            for (int i = 0; i < 2000; i++)
            {
                t.ResizeBuffer(i);
                Assert.IsTrue(i <= t.Buffer().Length);
                Assert.AreEqual("hello", t.ToString());
            }
        }
Пример #4
0
        public virtual void TestMixedStringArray()
        {
            Token t = new Token("hello", 0, 5);

            Assert.AreEqual(t.Length, 5);
            Assert.AreEqual(t.ToString(), "hello");
            t.SetEmpty().Append("hello2");
            Assert.AreEqual(t.Length, 6);
            Assert.AreEqual(t.ToString(), "hello2");
            t.CopyBuffer("hello3".ToCharArray(), 0, 6);
            Assert.AreEqual(t.ToString(), "hello3");

            char[] buffer = t.Buffer();
            buffer[1] = 'o';
            Assert.AreEqual(t.ToString(), "hollo3");
        }
Пример #5
0
        public virtual void TestTermBufferEquals()
        {
            Token t1a = new Token();

            char[] content1a = "hello".ToCharArray();
            t1a.CopyBuffer(content1a, 0, 5);
            Token t1b = new Token();

            char[] content1b = "hello".ToCharArray();
            t1b.CopyBuffer(content1b, 0, 5);
            Token t2 = new Token();

            char[] content2 = "hello2".ToCharArray();
            t2.CopyBuffer(content2, 0, 6);
            Assert.IsTrue(t1a.Equals(t1b));
            Assert.IsFalse(t1a.Equals(t2));
            Assert.IsFalse(t2.Equals(t1b));
        }
Пример #6
0
        public virtual void TestClone()
        {
            Token t = new Token(0, 5);

            char[] content = "hello".ToCharArray();
            t.CopyBuffer(content, 0, 5);
            char[] buf  = t.Buffer();
            Token  copy = AssertCloneIsEqual(t);

            Assert.AreEqual(t.ToString(), copy.ToString());
            Assert.AreNotSame(buf, copy.Buffer());

            BytesRef pl = new BytesRef(new sbyte[] { 1, 2, 3, 4 });

            t.Payload = pl;
            copy      = AssertCloneIsEqual(t);
            Assert.AreEqual(pl, copy.Payload);
            Assert.AreNotSame(pl, copy.Payload);
        }
Пример #7
0
        public virtual void TestCtor()
        {
            Token t = new Token();

            char[] content = "hello".ToCharArray();
            t.CopyBuffer(content, 0, content.Length);
            Assert.AreNotSame(t.Buffer(), content);
            Assert.AreEqual(0, t.StartOffset());
            Assert.AreEqual(0, t.EndOffset());
            Assert.AreEqual("hello", t.ToString());
            Assert.AreEqual("word", t.Type);
            Assert.AreEqual(0, t.Flags);

            t = new Token(6, 22);
            t.CopyBuffer(content, 0, content.Length);
            Assert.AreEqual("hello", t.ToString());
            Assert.AreEqual("hello", t.ToString());
            Assert.AreEqual(6, t.StartOffset());
            Assert.AreEqual(22, t.EndOffset());
            Assert.AreEqual("word", t.Type);
            Assert.AreEqual(0, t.Flags);

            t = new Token(6, 22, 7);
            t.CopyBuffer(content, 0, content.Length);
            Assert.AreEqual("hello", t.ToString());
            Assert.AreEqual("hello", t.ToString());
            Assert.AreEqual(6, t.StartOffset());
            Assert.AreEqual(22, t.EndOffset());
            Assert.AreEqual("word", t.Type);
            Assert.AreEqual(7, t.Flags);

            t = new Token(6, 22, "junk");
            t.CopyBuffer(content, 0, content.Length);
            Assert.AreEqual("hello", t.ToString());
            Assert.AreEqual("hello", t.ToString());
            Assert.AreEqual(6, t.StartOffset());
            Assert.AreEqual(22, t.EndOffset());
            Assert.AreEqual("junk", t.Type);
            Assert.AreEqual(0, t.Flags);
        }