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(); }
private void StartsEndsWith(Rope ropeCC) { log.WriteLine(GetCurrentMethod()); log.WriteLine("Starts with a? " + ropeCC.StartsWith("a")); log.WriteLine("Ends with a? " + ropeCC.EndsWith("a")); }