Skip to content

Commit

Permalink
Add unit tests for string operations, fix bug in string.Insert (#1104)
Browse files Browse the repository at this point in the history
* Add unit tests for string operations, fix bug in string.Insert

* Flip bool instead of checking false, correct insert test

(cherry picked from commit c02de78)

---------

Co-authored-by: Phil Garcia <phil@thinkedge.com>
  • Loading branch information
jvyden and tgiphil committed Aug 2, 2023
1 parent 5648425 commit 39b1200
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions Source/Mosa.Korlib/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public string Replace(string s1, string s2)

public string Insert(int startIndex, string str)
{
if (startIndex == 0) return str + this;
return this.Substring(0, startIndex) + str + this.Substring(startIndex);
}

Expand Down
72 changes: 72 additions & 0 deletions Source/Mosa.UnitTests/Primitive/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,76 @@ public static bool NotEqual1()

return string.Equals(a, b);
}

[MosaUnitTest]
public static bool SubstringTest1()
{
return "abc".Substring(1).Equals("bc");
}

[MosaUnitTest]
public static bool SubstringTest2()
{
return "abcdef".Substring(3).Equals("def");
}

[MosaUnitTest]
public static bool SubstringTest3() {
return "abcdef".Substring(0, 3).Equals("abc");
}

[MosaUnitTest]
public static bool SubstringTest4()
{
return "abcdef".Substring(2, 1).Equals("c");
}

[MosaUnitTest]
public static bool InsertTest1()
{
return "aaa".Insert(0, "bbb").Equals("bbbaaa");
}

[MosaUnitTest]
public static bool InsertTest2()
{
return "aaa".Insert(2, "bbb").Equals("aabbba");
}

[MosaUnitTest]
public static bool RemoveTest1()
{
return "aaabbb".Remove(0, 3).Equals("bbb");
}

[MosaUnitTest]
public static bool RemoveTest2()
{
return "aabbba".Remove(2, 3).Equals("aaa");
}

[MosaUnitTest]
public static bool IsNullOrWhiteSpaceTest1()
{
return string.IsNullOrWhiteSpace("");
}

[MosaUnitTest]
public static bool IsNullOrWhiteSpaceTest2()
{
return string.IsNullOrWhiteSpace(" ");
}

[MosaUnitTest]
public static bool IsNullOrWhiteSpaceTest3()
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
return string.IsNullOrWhiteSpace(null);
}

[MosaUnitTest]
public static bool IsNullOrWhiteSpaceTest4()
{
return !string.IsNullOrWhiteSpace("test");
}
}

0 comments on commit 39b1200

Please sign in to comment.