Skip to content

Commit

Permalink
Add indexof and similar tests
Browse files Browse the repository at this point in the history
Added/Fixed implementation for indexof and lastindexof
  • Loading branch information
quajak committed Oct 26, 2019
1 parent 6b8055c commit ecfb7fe
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Tests/Kernels/Cosmos.Compiler.Tests.Bcl/System/StringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,50 @@ public static void Execute()
Assert.IsTrue(test.EndsWith("string."), "string.EndsWith(string) is not reporting false even though the string actually does end with the substring.");
Assert.IsFalse(test.EndsWith("sentence."), "string.EndsWith(string) is not reporting true even though the string actually doesn't end with the substring.");

Assert.IsTrue(test.IndexOf(string.Empty, 10) == 10, "string.IndexOf currectly returns for empty string with start index");
Assert.IsTrue(test.IndexOf(string.Empty, 10, 10) == 10, "string.IndexOf currectly returns for empty string with start index and count");
Assert.IsTrue(test.IndexOf('T') == 0, "string.IndexOf finds the only occurance of a letter");
Assert.IsTrue(test.IndexOf('A') == -1, "string.IndexOf correctly returns when it does not find something");
Assert.IsTrue(test.IndexOf("ABCDE") == -1, "string.IndexOf correctly returns when it does not find something");
Assert.IsTrue(test.IndexOf('.') == test.Length - 1, "string.IndexOf finds the only occurance of a letter at the end of the string");
Assert.IsTrue(test.IndexOf('i') == 2, "string.IndexOf finds the first of multiple occurances of a letter");
Assert.IsTrue(test.IndexOf('i', 8) == 18, "string.IndexOf with start point finds the first of multiple occurances of a letter");
Assert.IsTrue(test.IndexOf("is") == 2, "string.IndexOf finds the first of multiple occurances of a string");
Assert.IsTrue(test.IndexOf("is", 3) == 5, "string.IndexOf with start point finds the first of multiple occurances of a string");
Assert.IsTrue(test.IndexOf("is", 3, 5) == 5, "string.IndexOf with start point and count finds the first of multiple occurances of a string");
Assert.IsTrue(test.IndexOf("is", 3, 1) == -1, "string.IndexOf with start point and count correctly returns if it does not find something");

Assert.IsTrue(test.IndexOfAny(new[] { 'T', 'h', 'i', 's' }) == 0, "string.IndexOfAny finds the first one");
Assert.IsTrue(test.IndexOfAny(new[] { 'A', 'B', 'C' }) == -1, "string.IndexOfAny finds none if none are present");

Assert.IsTrue(test.LastIndexOf(string.Empty, 100) == test.Length, "string.LastIndexOf handles empty correctly");
Assert.IsTrue(test.LastIndexOf('T') == 0, "string.LastIndexOf finds the only occurance of a letter");
Assert.IsTrue(test.LastIndexOf('.') == test.Length - 1, "string.LastIndexOf finds the only occurance of a letter at the end of the string");
Assert.IsTrue(test.LastIndexOf('i') == test.Length - 4, "string.IndexOf finds the last of multiple occurances of a letter");

Assert.IsTrue(test.LastIndexOfAny(new[] { 'T', 'h', 'i', 's' }) == 18, "string.LastIndexOfAny finds the first one");
Assert.IsTrue(test.LastIndexOfAny(new[] { 'A', 'B', 'C' }) == -1, "string.LastIndexOfAny finds none if none are present");


Assert.IsTrue(test.Insert(0, "A") != test, "string.Insert creates a new instance");
Assert.IsTrue(test.Insert(1, "A") == "TAhis is a test string.", "string.Insert correctly inserts a single character");
Assert.IsTrue(test.Insert(2, "ABCDE F") == "ThABCDE Fis is a test string.", "string.Insert correctly adds multiple characters");
Assert.IsTrue(test.Insert(test.Length, "END") == "This is a test string.END", "string.Insert correctly inserts at the end of the string");

Assert.IsTrue(test.Remove(1) == "T", "string.Remove correctly removes all other characters");
Assert.IsTrue(test.Remove(0) == "", "string.Remove correctly removes all characters");
Assert.IsTrue(test.Remove(0, 2) == "is is a test string.", "string.Remove works with count");

Assert.IsTrue(" a ".Trim() == "a", "string.Trim trims both front and back");
Assert.IsTrue("abababababa".Trim(new[] { 'a', 'b' }) == "", "string.Trim works with custom chars");
Assert.IsTrue("abCababababa".Trim(new[] { 'a', 'b' }) == "C", "string.Trim works with custom chars");
Assert.IsTrue("a".Trim() == "a", "string.Trim trims both front and back");
Assert.IsTrue(" a ".TrimStart() == "a ", "string.TrimStart trims front");
Assert.IsTrue("a".TrimStart() == "a", "string.Trim trims front");
Assert.IsTrue(" a ".TrimEnd() == " a", "string.TrimEnd trims back");
Assert.IsTrue("a".TrimEnd() == "a", "string.TrimEnd trims back");


string lower_expected = "this is a test string.";
string upper_expected = "THIS IS A TEST STRING.";
Assert.IsTrue((test.ToLower() == lower_expected), "string.ToLower() does not work.");
Expand Down
29 changes: 29 additions & 0 deletions source/Cosmos.Core_Plugs/System/StringImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ private static int boyerMooreHorsepool(string pattern, string text)

public static int IndexOf(string aThis, string aSubstring, int aIdx, int aLength, StringComparison aComparison)
{
if (aSubstring == String.Empty)
{
return aIdx;
}
return boyerMooreHorsepool(aSubstring, aThis.Substring(aIdx, aLength));
}

Expand Down Expand Up @@ -624,6 +628,31 @@ public static string Insert(string aThis, int aStartPos, string aValue)
return aThis.Substring(0, aStartPos) + aValue + aThis.Substring(aStartPos);
}

public static int LastIndexOf(string aThis, string aString, int aIndex)
{
return LastIndexOf(aThis, aString, aIndex, aThis.Length - aIndex);
}

public static int LastIndexOf(string aThis, string aString, int aIndex, int aCount)
{
if (aString == String.Empty)
{
return aIndex;
}

string curr = "";
char[] chars = aString.ToCharArray();
for (int i = 0; i < aCount; i++)
{
curr += chars[aThis.Length - i];
if (curr.StartsWith(aString))
{
return aThis.Length - 1;
}
}
return -1;
}

public static int LastIndexOf(string aThis, char aChar, int aStartIndex, int aCount)
{
return LastIndexOfAny(aThis, new[] { aChar }, aStartIndex, aCount);
Expand Down

0 comments on commit ecfb7fe

Please sign in to comment.