Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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;
            }
        }
Exemplo n.º 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");
        }
Exemplo n.º 4
0
        public void TrimShortStrings_RopeStringEquivalence()
        {
            string str = " ";
            Rope   r   = RopeBuilder.BUILD(str);

            str = str.Trim();
            r   = r.Trim();
            Compare(r, str);
        }
Exemplo n.º 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();
        }
Exemplo n.º 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));
        }
Exemplo n.º 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");
        }
Exemplo n.º 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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 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()));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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()));
        }
Exemplo n.º 15
0
        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));
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
        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);
            }
        }
Exemplo n.º 18
0
        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));
        }
Exemplo n.º 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())));
        }
Exemplo n.º 20
0
        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);
        }
Exemplo n.º 21
0
        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);
        }
Exemplo n.º 22
0
        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);
        }
Exemplo n.º 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();
        }
Exemplo n.º 24
0
 public static Rope AsRope(this string v) => RopeBuilder.BUILD(v);
Exemplo n.º 25
0
 public Rope Append(string suffix)
 {
     return(RopeUtilities.INSTANCE.Concatenate(this, RopeBuilder.BUILD(suffix)));
 }
Exemplo n.º 26
0
 public Rope Append(char c)
 {
     return(RopeUtilities.INSTANCE.Concatenate(this, RopeBuilder.BUILD(c.ToString())));
 }
Exemplo n.º 27
0
 public Rope Append(string suffix, int start, int end)
 {
     return(RopeUtilities.INSTANCE.Concatenate(this, RopeBuilder.BUILD(suffix).SubSequence(start, end)));
 }