Skip to content

Commit

Permalink
Add testReplaceString to JlibStringTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdelv committed Oct 10, 2024
1 parent 5c2e974 commit 7047f70
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions testing/unittests/jlibtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ class JlibStringTest : public CppUnit::TestFixture
public:
CPPUNIT_TEST_SUITE(JlibStringTest);
CPPUNIT_TEST(testEncodeCSVColumn);
CPPUNIT_TEST(testReplaceString);
CPPUNIT_TEST_SUITE_END();

protected:
Expand All @@ -845,6 +846,59 @@ void testEncodeCSVColumn()
encodeCSVColumn(encodedCSV, csvCol2);
CPPUNIT_ASSERT_EQUAL_STR(encodedCSV.str(), "\"hello world, \"\"how are you?\"\"\"");
}

void testReplaceString()
{
// Match string at the end of the source
StringBuffer source("aaaaaaaaab");
source.replaceString("aaab", "x");
CPPUNIT_ASSERT_EQUAL_STR("aaaaaax", source.str());

// Single char match at end of source
source.set("aaaaaaaaab");
source.replaceString("b", "x");
CPPUNIT_ASSERT_EQUAL_STR("aaaaaaaaax", source.str());

// Match string at the start of the source
source.set("abababab");
source.replaceString("aba", "xxx");
CPPUNIT_ASSERT_EQUAL_STR("xxxbxxxb", source.str());

// Match not found
source.set("aaaaa");
source.replaceString("bb", "xxx");
CPPUNIT_ASSERT_EQUAL_STR("aaaaa", source.str());

// Replace string is empty
source.set("aabaa");
source.replaceString("aa", "");
CPPUNIT_ASSERT_EQUAL_STR("b", source.str());

// Search string is empty
source.set("aaaaaa");
source.replaceString("", "b");
CPPUNIT_ASSERT_EQUAL_STR("aaaaaa", source.str());

// Source string is empty
source.set("");
source.replaceString("a", "b");
CPPUNIT_ASSERT_EQUAL_STR("", source.str());

// Search string is longer than source string
source.set("a");
source.replaceString("aa", "b");
CPPUNIT_ASSERT_EQUAL_STR("a", source.str());

// Replace every character
source.set("aaaaa");
source.replaceString("a", "b");
CPPUNIT_ASSERT_EQUAL_STR("bbbbb", source.str());

// Replace string is longer than search string
source.set("abbabab");
source.replaceString("ab", "xxx");
CPPUNIT_ASSERT_EQUAL_STR("xxxbxxxxxx", source.str());
}
};

CPPUNIT_TEST_SUITE_REGISTRATION( JlibStringTest );
Expand Down

0 comments on commit 7047f70

Please sign in to comment.