Inheritance: MonoBehaviour
示例#1
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void TrimWhitespace_RopeStringEquivalence()
        {
            string str = "11111";
            Rope   r   = RopeBuilder.BUILD(new RepeatedCharacterSequence('1', 5));

            str = str.Insert(1, "aa");
            r   = r.Insert(1, "aa");
            Compare(r, str);

            // Add spaces onto the end and trim
            r   = r.Append("  ");
            str = str + "  ";
            Compare(r, str);

            r   = r.Trim();
            str = str.Trim();

            Compare(r, str);

            // Trim without any whitespace
            r   = r.Trim();
            str = str.Trim();

            Compare(r, str);

            // Lots of whitespace at the beginning
            r   = r.PadStart(r.Length() + 2);
            str = "  " + str;
            Compare(r, str);

            r   = r.Trim();
            str = str.Trim();

            Compare(r, str);
        }
示例#2
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void TestIterator()
        {
            Rope r = RopeBuilder.BUILD("01234aaa56789");

            r = r.Remove(5, 8);             // "0123456789"
            for (int i = 0; i < r.Length(); ++i)
            {
                Assert.AreEqual("0123456789"[i], r.CharAt(i));
            }

            r = r.SubSequence(1, r.Length());             // "123456789"
            for (int i = 0; i < r.Length(); ++i)
            {
                Assert.AreEqual("123456789"[i], r.CharAt(i));
            }

            r = r.Append('0');             // "1234567890"
            int j = 0;

            foreach (char c in r)
            {
                Assert.AreEqual("1234567890"[j], c);
                ++j;
            }
        }
示例#3
0
        public void IndexToLineColumn(string text, int idx, int row, int col)
        {
            var r     = RopeBuilder.BUILD(text);
            var coord = r.IndexToLineColumn(idx, '\n');

            Assert.AreEqual(coord.Row, row, "Rows should be the same");
            Assert.AreEqual(coord.Col, col, "Column should be the same");
        }
示例#4
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void TrimShortStrings_RopeStringEquivalence()
        {
            string str = " ";
            Rope   r   = RopeBuilder.BUILD(str);

            str = str.Trim();
            r   = r.Trim();
            Compare(r, str);
        }
示例#5
0
        public void Random100Actions()
        {
            // Reads in A Christmas Carol and randomly performs 100 actions on that rope
            Rope ropeCC = RopeBuilder.BUILD(ReadChristmasCarol());

            for (int i = 0; i < 100; ++i)
            {
                Array values = Enum.GetValues(typeof(Action));
                switch ((Action)values.GetValue(rand.Next(values.Length)))
                {
                case Action.Append:
                    ropeCC = AppendRandom(ropeCC);
                    break;

                case Action.Delete:
                    ropeCC = DeleteRandom(ropeCC);
                    break;

                case Action.Enumerate:
                    Enumerate(ropeCC);
                    break;

                case Action.Reverse:
                    ropeCC = Reverse(ropeCC);
                    break;

                case Action.IndexOf:
                    IndexOfRandom(ropeCC);
                    break;

                case Action.Insert:
                    ropeCC = InsertRandom(ropeCC);
                    break;

                case Action.TrimStart:
                case Action.TrimEnd:
                    ropeCC = Trim(ropeCC);
                    break;

                case Action.Subsequence:
                    ropeCC = SubsequenceRandom(ropeCC);
                    break;

                case Action.PadStart:
                case Action.PadEnd:
                    ropeCC = PadRandom(ropeCC);
                    break;

                case Action.StartsWith:
                case Action.EndsWith:
                    StartsEndsWith(ropeCC);
                    break;
                }
            }
            log.Flush();
        }
示例#6
0
        public Rope PadStart(int toLength, char padChar)
        {
            int toPad = toLength - this.Length();

            if (toPad < 1)
            {
                return(this);
            }
            return(RopeUtilities.INSTANCE.Concatenate(RopeBuilder.BUILD(new RepeatedCharacterSequence(padChar, toPad)), this));
        }
示例#7
0
        public void SplitByShouldSplit(string text)
        {
            var rope     = RopeBuilder.BUILD(text);
            var expected = text.Split('\n').ToQueue <string>();

            foreach (var l in rope.SplitBy('\n'))
            {
                Assert.AreEqual(expected.Dequeue(), l.ToString());
            }
            Assert.AreEqual(0, expected.Count, "Queue should be empty after tests");
        }
示例#8
0
        public void ChristmasCarolPerf_Read()
        {
            string strCC = ReadChristmasCarol();

            sw.Start();
            Rope ropeCC = RopeBuilder.BUILD(strCC);

            sw.Stop();

            Report("Constructed rope from ChristmasCarol string", sw.Elapsed);
        }
示例#9
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void TestIndexOfAcrossConcatenateRopes()
        {
            Rope r = RopeBuilder.BUILD("aaaaaaaaaaaaaaaa12");

            r = r.Append("3456789aaaaaa");
            r = r.SubSequence(1, r.Length());
            Assert.AreEqual(r.IndexOf("123456789", 3), 15);

            r = RopeBuilder.BUILD("aaaaaaaaaaaa1111");
            r = r.Append("1bbbbbbbbbbbbbbbb");
            Assert.AreEqual(r.IndexOf("1111", 3), 12);
        }
示例#10
0
        private Rope AppendRandom(Rope ropeCC)
        {
            log.WriteLine(GetCurrentMethod());

            if (rand.Next() % 2 == 0) // append a new rope
            {
                log.WriteLine(String.Format("Appending random rope"));
                return(Append(ropeCC, RopeBuilder.BUILD(GetRandomString())));
            }

            log.WriteLine(String.Format("Appending random string"));
            return(Append(ropeCC, GetRandomString()));
        }
示例#11
0
        public void ChristmasCarolPerf_InsertBack()
        {
            string strCC  = new string(ReadChristmasCarol());
            Rope   ropeCC = RopeBuilder.BUILD(strCC);

            sw.Start();
            strCC = strCC.Insert(strCC.Length - 1, "hello!");
            sw.Stop();
            Report("Insertion time for ChristmasCarol string", sw.Elapsed);

            sw.Restart();
            ropeCC = ropeCC.Insert(ropeCC.Length() - 1, "hello!");
            sw.Stop();
            Report("Insertion time for ChristmasCarol Rope", sw.Elapsed);
        }
示例#12
0
        public void ChristmasCarolPerf_DeleteFront()
        {
            string strCC  = new string(ReadChristmasCarol());
            Rope   ropeCC = RopeBuilder.BUILD(strCC);

            sw.Start();
            strCC = strCC.Remove(5, 550);
            sw.Stop();
            Report("Substring time for ChristmasCarol string", sw.Elapsed);

            sw.Restart();
            ropeCC = ropeCC.SubSequence(5, 555);
            sw.Stop();
            Report("Substring time for ChristmasCarol Rope", sw.Elapsed);
        }
示例#13
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void DeleteFromMiddleOfReverse_RopeStringEquivalence()
        {
            string str = "Hello darkness, my old 12345friend";
            Rope   r   = RopeBuilder.BUILD(str);

            str = StringReverse(str);
            r   = r.Reverse();
            Compare(r, str);

            str = str.Remove(6, 5);
            string s1 = r.SubSequence(0, 6).ToString();
            string s2 = r.SubSequence(11, r.Length()).ToString();

            r = r.Remove(6, 11);
            Compare(r, str);
        }
示例#14
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void TestSubstringAppend()
        {
            Rope r = RopeBuilder.BUILD("");

            r = r.Append("round ");
            r = r.Append((0).ToString());
            r = r.Append(" 1234567890");

            Assert.AreEqual("round ", RopeToString(r, 0, 6));
            Assert.AreEqual("round 0", RopeToString(r, 0, 7));
            Assert.AreEqual("round 0 ", RopeToString(r, 0, 8));
            Assert.AreEqual("round 0 1", RopeToString(r, 0, 9));
            Assert.AreEqual("round 0 12", RopeToString(r, 0, 10));
            Assert.AreEqual("round 0 1234567890", RopeToString(r, 0, 18));
            Assert.AreEqual("round 0 1234567890", RopeToString(r, 0, r.Length()));
        }
示例#15
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void TestSubstringDelete()
        {
            string s = "12345678902234567890";

            Rope rope = RopeBuilder.BUILD(s);

            rope = rope.Remove(0, 1);
            Assert.AreEqual("23", RopeToString(rope, 0, 2));
            Assert.AreEqual("", RopeToString(rope, 0, 0));
            Assert.AreEqual("902", RopeToString(rope, 7, 10));

            rope = RopeBuilder.BUILD(s.ToCharArray());
            rope = rope.Remove(0, 1);
            Assert.AreEqual("23", RopeToString(rope, 0, 2));
            Assert.AreEqual("", RopeToString(rope, 0, 0));
            Assert.AreEqual("902", RopeToString(rope, 7, 10));
        }
示例#16
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void SubstringRopeReverse_RopeStringEquivalence()
        {
            string str = "abcdefghijklmnopqrstuvwxyz";
            Rope   r   = RopeBuilder.BUILD(str);

            str = str.Substring(15);
            r   = r.SubSequence(15, r.Length());
            Compare(r, str);

            str = StringReverse(str);
            r   = r.Reverse();
            Compare(r, str);

            str = str + "  ";
            r   = r.PadEnd(r.Length() + 2);
            Compare(r, str);
        }
示例#17
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void TestIEnumerator()
        {
            Rope r = RopeBuilder.BUILD("01234aaa56789");

            r = r.Insert(4, "0000001123456");
            r = r.Insert(3, "hello, stranger");
            r = r.Remove(4, 10);
            r = r.Insert(0, "      ");
            r = r.SubSequence(3, 17);

            IEnumerator enumerator = r.GetEnumerator();

            while (enumerator.MoveNext())
            {
                Assert.IsTrue(enumerator.Current != null);
            }
        }
示例#18
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void AddRemoveFromMiddle_RopeStringEquivalence()
        {
            string str = "11111";
            Rope   r   = RopeBuilder.BUILD(new RepeatedCharacterSequence('1', 5));

            str = str.Insert(1, "aa");
            r   = r.Insert(1, "aa");
            Compare(r, str);

            Assert.AreEqual(str.IndexOf("aa", 1), r.IndexOf("aa", 1));

            str = str.Remove(1, 2);
            r   = r.Remove(1, 3);
            Compare(r, str);

            Assert.AreEqual(str.IndexOf("aa", 1), r.IndexOf("aa", 1));
            Assert.AreEqual(str.IndexOf("doesn't exist", 1), r.IndexOf("doesn't exist", 1));
        }
示例#19
0
        public Rope Insert(int dstOffset, string s)
        {
            Rope r = (s == null) ? RopeBuilder.BUILD("null") : RopeBuilder.BUILD(s);

            if (dstOffset == 0)
            {
                return(r.Append(this));
            }
            else if (dstOffset == this.Length())
            {
                return(this.Append(r));
            }
            else if (dstOffset < 0 || dstOffset > this.Length())
            {
                throw new IndexOutOfRangeException(dstOffset + " is out of range [" + 0 + ":" + this.Length() + "]");
            }
            return(this.SubSequence(0, dstOffset).Append(r).Append(this.SubSequence(dstOffset, this.Length())));
        }
示例#20
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void AppendReversedSubstrings_RopeStringEquivalence()
        {
            string str = "abcdefghijklmnopqrstuvwxyz   ";
            Rope   r   = RopeBuilder.BUILD(str);

            str = "   " + str;
            r   = r.PadStart(r.Length() + 3);
            Compare(r, str);

            str = StringReverse(str.Substring(2)) + StringReverse(str.Substring(2, 6));
            r   = r.SubSequence(2).Reverse().Append(r.SubSequence(2, 8).Reverse());
            Compare(r, str);

            str = str.Trim();
            r   = r.Trim();
            Compare(r, str);

            str = str.Substring(5);
            r   = r.SubSequence(5);
            Compare(r, str);
        }
示例#21
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void TrimPaddedReverseRope_RopeStringEquivalence()
        {
            string str = "abcdefghijklmnopqrstuvwxyz   ";
            Rope   r   = RopeBuilder.BUILD(str);

            str = "   " + str;
            r   = r.PadStart(r.Length() + 3);
            Compare(r, str);

            str = str.Substring(2, 18);
            r   = r.SubSequence(2, 20);
            Compare(r, str);

            str = StringReverse(str);
            r   = r.Reverse();
            Compare(r, str);

            str = str.Trim();
            r   = r.Trim();
            Compare(r, str);
        }
示例#22
0
文件: RopeTest.cs 项目: thyer/Ropes
        public void TestIndexOf()
        {
            string strTest  = "Hello, world!";
            Rope   ropeTest = RopeBuilder.BUILD(strTest);

            Compare(ropeTest, strTest);

            ropeTest = ropeTest.Insert(7, "under");
            strTest  = "Hello, underworld!";
            Compare(ropeTest, strTest);

            ropeTest = ropeTest.SubSequence(1, ropeTest.Length());
            strTest  = "ello, underworld!";
            Compare(ropeTest, strTest);

            ropeTest = ropeTest.Append("123456789");
            strTest  = strTest + "123456789";
            Compare(ropeTest, strTest);

            ropeTest = ropeTest.Append("123456789");
            strTest  = strTest + "123456789";
            Compare(ropeTest, strTest);
        }
示例#23
0
        public void RandomActionsCompareToString()
        {
            // Performs random actions on both a string and a rope, comparing the two after each action
            string output = ReadChristmasCarol();
            Rope   ropeCC = RopeBuilder.BUILD(output);
            string strCC  = new string(output);

            for (int i = 0; i < 50000; ++i)
            {
                // useful values for random access
                int length = ropeCC.Length();
                int start  = rand.Next(length / 2);
                int end    = Math.Max(start + 1, length - rand.Next(length / 3));
                end = Math.Min(end, length);

                Array  values        = Enum.GetValues(typeof(Action));
                Action performAction = (Action)values.GetValue(rand.Next(values.Length));
                switch (performAction)
                {
                case Action.Append:
                    string randomAppend = GetRandomString();
                    ropeCC = Append(ropeCC, randomAppend);
                    strCC += randomAppend;
                    break;

                case Action.Delete:
                    ropeCC = ropeCC.Remove(start, end);
                    strCC  = strCC.Remove(start, end - start);
                    break;

                case Action.Enumerate:
                    Enumerate(ropeCC);
                    break;

                case Action.Reverse:
                    ropeCC = Reverse(ropeCC);
                    char[] charCC = strCC.ToCharArray();
                    Array.Reverse(charCC);
                    strCC = new String(charCC);
                    break;

                case Action.IndexOf:
                    int fromIndex = rand.Next(ropeCC.Length() / 4);
                    // take a random sequence from the string
                    int    iStart        = fromIndex + rand.Next((ropeCC.Length() - fromIndex) / 2);
                    int    randLength    = Math.Min(rand.Next(5), strCC.Length - iStart);
                    string randSubstring = strCC.Substring(iStart, randLength);
                    Assert.AreEqual(strCC.IndexOf(randSubstring, fromIndex), ropeCC.IndexOf(randSubstring, fromIndex));
                    break;

                case Action.Insert:
                    int    iOffset     = rand.Next(strCC.Length / 2);
                    string strToInsert = GetRandomString();
                    ropeCC = Insert(ropeCC, iOffset, strToInsert);
                    strCC  = strCC.Substring(0, iOffset) + strToInsert + strCC.Substring(iOffset);
                    break;

                case Action.TrimStart:
                case Action.TrimEnd:
                    ropeCC = Trim(ropeCC);
                    strCC  = strCC.Trim();
                    break;

                case Action.Subsequence:
                    ropeCC = Subsequence(ropeCC, start, end);
                    strCC  = strCC.Substring(start, end - start);
                    break;

                case Action.PadStart:
                case Action.PadEnd:
                    ropeCC = ropeCC.PadStart(length + 5, ' ');
                    strCC  = strCC.PadLeft(length + 5, ' ');
                    break;

                case Action.StartsWith:
                case Action.EndsWith:
                    Assert.AreEqual(ropeCC.StartsWith("a"), strCC.StartsWith("a"));
                    Assert.AreEqual(ropeCC.StartsWith("e"), strCC.StartsWith("e"));
                    Assert.AreEqual(ropeCC.EndsWith("a"), strCC.EndsWith("a"));
                    Assert.AreEqual(ropeCC.EndsWith("e"), strCC.EndsWith("e"));
                    Assert.AreEqual(ropeCC.StartsWith("he"), strCC.StartsWith("he"));
                    Assert.AreEqual(ropeCC.StartsWith("the"), strCC.StartsWith("the"));
                    Assert.AreEqual(ropeCC.EndsWith("he"), strCC.EndsWith("he"));
                    Assert.AreEqual(ropeCC.EndsWith("m."), strCC.EndsWith("m."));
                    break;
                }

                log.Flush();
                Assert.AreEqual(strCC.Length, ropeCC.Length());
                int j = 0;
                foreach (char c in ropeCC)
                {
                    Assert.AreEqual(c, strCC[j]);
                    j++;
                }
            }
            log.Flush();
        }
示例#24
0
 public static Rope AsRope(this string v) => RopeBuilder.BUILD(v);
示例#25
0
 public Rope Append(string suffix)
 {
     return(RopeUtilities.INSTANCE.Concatenate(this, RopeBuilder.BUILD(suffix)));
 }
示例#26
0
 public Rope Append(char c)
 {
     return(RopeUtilities.INSTANCE.Concatenate(this, RopeBuilder.BUILD(c.ToString())));
 }
示例#27
0
 public Rope Append(string suffix, int start, int end)
 {
     return(RopeUtilities.INSTANCE.Concatenate(this, RopeBuilder.BUILD(suffix).SubSequence(start, end)));
 }