Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate string manipulation tests from commons-lang to boost #115

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions test/migrate_test/test_group1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#define BOOST_TEST_MODULE TestGroup1
#include <boost/test/included/unit_test.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
using namespace std;
using namespace boost::algorithm;

#include <boost/test/test_tools.hpp>

const string foo = "foo";
const string bar = "bar";
const string foobar = "foobar";
const string FOO = "FOO";
const string BAR = "BAR";
const string FOOBAR = "FOOBAR";

const wstring CharU20000 = L"\xD840\xDC00";
const wstring CharU20001 = L"\xD840\xDC01";

const wstring CharUSuppCharHigh = L"\xDC00";
const wstring CharUSuppCharLow= L"\xD840";

//test1 verify if the prefix of a string matches a specified prefix.
BOOST_AUTO_TEST_CASE(testStartsWith) {

BOOST_CHECK(starts_with("",""));
BOOST_CHECK(starts_with(FOOBAR,""));
BOOST_CHECK(!starts_with("",FOO));

BOOST_CHECK(starts_with(foobar,foo));
BOOST_CHECK(starts_with(FOOBAR,FOO));
BOOST_CHECK(!starts_with(foobar,FOO));
BOOST_CHECK(!starts_with(FOOBAR,foo));

BOOST_CHECK(!starts_with(foo,foobar));
BOOST_CHECK(!starts_with(bar,foobar));

BOOST_CHECK(!starts_with(foobar,bar));
BOOST_CHECK(!starts_with(FOOBAR,BAR));
BOOST_CHECK(!starts_with(foobar,BAR));
BOOST_CHECK(!starts_with(FOOBAR,bar));
}

//test2 verify if it can remove leading and trailing whitespace from a string.
BOOST_AUTO_TEST_CASE(testTrim) {
BOOST_CHECK_EQUAL(FOO, trim_copy(FOO + " "));
BOOST_CHECK_EQUAL(FOO, trim_copy(" " + FOO + " "));
BOOST_CHECK_EQUAL(FOO, trim_copy(" " + FOO));
BOOST_CHECK_EQUAL(FOO, trim_copy(FOO + ""));
// BOOST_CHECK_EQUAL("", trim_copy(string(" \t\r\n ")));
// \b
BOOST_CHECK_EQUAL("", trim_copy(string(" \t\r\n\b ")));
}


//test3 verify if a string is entirely composed of whitespace characters.
BOOST_AUTO_TEST_CASE(testIsBlank)
{
BOOST_CHECK(!all_of("", is_space()));
BOOST_CHECK(all_of(string(""), is_space()));
BOOST_CHECK(!all_of("foo", is_space()));
BOOST_CHECK(!all_of(" foo ", is_space()));
}

// test4 verify if a string is not entirely composed of whitespace characters.
BOOST_AUTO_TEST_CASE(testIsNotBlank){
BOOST_CHECK(!all_of("a", is_space()));
BOOST_CHECK(!all_of("foo", is_space()));
BOOST_CHECK(!all_of(" foo ", is_space()));
}

//test5 verify if one string contains another string.
BOOST_AUTO_TEST_CASE(testContains_String) {
BOOST_CHECK(contains("", ""));
BOOST_CHECK(!contains("", "a"));
BOOST_CHECK(contains("abc", "a"));
BOOST_CHECK(contains("abc", "b"));
BOOST_CHECK(contains("abc", "c"));
BOOST_CHECK(contains("abc", "abc"));
BOOST_CHECK(!contains("abc", "z"));
}


// test6 verify if a character is present in a string.
BOOST_AUTO_TEST_CASE(testContains_Char) {
BOOST_CHECK(!contains(string(""), string(1, ' ')));
BOOST_CHECK(contains(string("abc"), string(1, 'a')));
BOOST_CHECK(contains(string("abc"), string(1, 'b')));
BOOST_CHECK(contains(string("abc"), string(1, 'c')));
BOOST_CHECK(!contains(string("abc"), string(1, 'z')));
}


//test7 verify if a string contains Unicode supplementary characters
BOOST_AUTO_TEST_CASE(testContains_StringWithSupplementaryChars) {
BOOST_CHECK(contains(CharU20000 + CharU20001, CharU20000));
BOOST_CHECK(contains(CharU20000 + CharU20001, CharU20001));
BOOST_CHECK(contains(CharU20000, CharU20000));
BOOST_CHECK(!contains(CharU20000, CharU20001));
}

//test8 verify if a string contains invalid Unicode supplementary characters.
BOOST_AUTO_TEST_CASE(testContains_StringWithBadSupplementaryChars) {
BOOST_CHECK(!contains(CharUSuppCharHigh, CharU20001));
BOOST_CHECK(!contains(CharUSuppCharLow, CharU20001));
BOOST_CHECK(!contains(CharU20001, CharUSuppCharHigh));
//
BOOST_CHECK(contains(CharU20001, CharUSuppCharLow));

BOOST_CHECK(contains(CharU20001 + CharUSuppCharLow + L"a", L"a"));
BOOST_CHECK(contains(CharU20001 + CharUSuppCharHigh + L"a", L"a"));
}


//test9 verify if two strings are equal.
BOOST_AUTO_TEST_CASE(testEquals)
{
string fooCs = FOO;
string barCs = BAR;
string foobarCs = FOOBAR;

BOOST_CHECK(equals(fooCs, fooCs));
BOOST_CHECK(!equals(fooCs, string("foo")));

BOOST_CHECK(iequals(fooCs, string("fOo")));
BOOST_CHECK(!equals(fooCs, barCs));
BOOST_CHECK(!equals(fooCs, foobarCs));
BOOST_CHECK(!equals(foobarCs, fooCs));
}

//test10 verify if two strings are equal.
BOOST_AUTO_TEST_CASE(testEqualsOnStrings) {
BOOST_CHECK(equals(FOO, FOO));
BOOST_CHECK(!equals(FOO, string("foo")));
BOOST_CHECK(!equals(FOO, string("fOo")));
BOOST_CHECK(!equals(FOO, BAR));
BOOST_CHECK(!equals(FOO, FOOBAR));
BOOST_CHECK(!equals(FOOBAR, FOO));
}

171 changes: 171 additions & 0 deletions test/migrate_test/test_group2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#define BOOST_TEST_MODULE TestGroup2
#include <boost/test/included/unit_test.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
using namespace std;
using namespace boost::algorithm;

#include <boost/test/test_tools.hpp>

const string foo = "foo";
const string bar = "bar";
const string foobar = "foobar";
const string FOO = "FOO";
const string BAR = "BAR";
const string FOOBAR = "FOOBAR";

const wstring CharU20000 = L"\xD840\xDC00";
const wstring CharU20001 = L"\xD840\xDC01";

const wstring CharUSuppCharHigh = L"\xDC00";
const wstring CharUSuppCharLow= L"\xD840";

const string TEXT_LIST_NOSEP = "foobarbaz";
const string SEPARATOR_CHAR = ";";
const string COMMA_SEPARATOR_CHAR = ",";
const string TEXT_LIST_CHAR = "foo;bar;baz";

const vector<string> ARRAY_LIST = {"foo", "bar", "baz"};
const vector<string> EMPTY_ARRAY_LIST = {};
const vector<string> MIXED_ARRAY_LIST = {"", "foo"};
const vector<string> MIXED_TYPE_LIST = {"foo", to_string(2L)};
const vector<string> PRIM_LIST = {"1", "2"};
const vector<string> STRING_LIST = {"foo", "bar", "baz"};
const vector<string> EMPTY_STRING_LIST = {};
const vector<string> ARRAY_FALSE_FALSE = {"false", "false"};
const vector<string> ARRAY_FALSE_TRUE = {"false", "true"};
const vector<string> ARRAY_FALSE_TRUE_FALSE = {"false", "true", "false"};


//test11 verify if a string is equal to any of a set of other strings.
BOOST_AUTO_TEST_CASE(testEqualsAny) {
BOOST_CHECK(!equals(FOO, ""));
BOOST_CHECK(!equals(FOO, string("")));

BOOST_CHECK(equals(FOO, FOO));
BOOST_CHECK(!equals(FOO, "foo"));
BOOST_CHECK(!equals(FOO, "fOo"));
BOOST_CHECK(!equals(FOO, "bar"));
}

// test12 verify if a string contains only alphabetic characters.
BOOST_AUTO_TEST_CASE(testIsAlpha) {
BOOST_CHECK(all(string(""), is_alpha()));

BOOST_CHECK(!all(string(" "), is_alpha()));
BOOST_CHECK(all(string("a"), is_alpha()));
BOOST_CHECK(all(string("A"), is_alpha()));
BOOST_CHECK(all(string("kgKgKgKgkgkGkjkjlJlOKLgHdGdHgl"), is_alpha()));
BOOST_CHECK(!all(string("ham kso"), is_alpha()));
BOOST_CHECK(!all(string("1"), is_alpha()));
// 6,7,87
BOOST_CHECK(!all(string("hkHKHik6iUGHKJgU7tUJgKJGI87GIkug"), is_alpha()));
BOOST_CHECK(!all(string("_"), is_alpha()));
// *
BOOST_CHECK(!all(string("hkHKHik*khbkuh"), is_alpha()));
}

//test13 verify if it can join a vector of strings using a specified delimiter.
BOOST_AUTO_TEST_CASE(testJoin_Objects) {
BOOST_CHECK_EQUAL("abc", join(vector<string>({"a", "b", "c"}), ""));
BOOST_CHECK_EQUAL("a", join(vector<string>({"", "", "a"}), ""));
BOOST_CHECK(join(vector<string>({}), "").empty());
}

//test14 verify if it can join an array of strings using a specified delimiter.
BOOST_AUTO_TEST_CASE(testJoin_Objectarray) {
BOOST_CHECK(join(vector<string>({}), "").empty());

BOOST_CHECK_EQUAL("", join(vector<string>({}), ""));
BOOST_CHECK_EQUAL("", join(vector<string>({""}), ""));

BOOST_CHECK_EQUAL("", join(EMPTY_ARRAY_LIST, ""));
BOOST_CHECK_EQUAL("abc", join(vector<string>({"a", "b", "c"}), ""));
BOOST_CHECK_EQUAL("a", join(vector<string>({ "", "a", "" }), ""));
BOOST_CHECK_EQUAL("foo", join(MIXED_ARRAY_LIST, ""));
BOOST_CHECK_EQUAL("foo2", join(MIXED_TYPE_LIST, ""));
}

// test15 verify if it can join an array of strings without using a delimiter.
BOOST_AUTO_TEST_CASE(testJoin_ArrayString_EmptyDelimiter) {
BOOST_CHECK(join(vector<string>({}), "").empty());

BOOST_CHECK_EQUAL(TEXT_LIST_NOSEP, join(ARRAY_LIST, ""));
BOOST_CHECK_EQUAL(TEXT_LIST_NOSEP, join(ARRAY_LIST, ""));

BOOST_CHECK_EQUAL("", join(EMPTY_ARRAY_LIST, ""));
BOOST_CHECK_EQUAL("", join(EMPTY_ARRAY_LIST, ""));

BOOST_CHECK_EQUAL("", join(vector<string>(MIXED_ARRAY_LIST.begin(), MIXED_ARRAY_LIST.end() - 1), ""));
}

//test16 verify if it can join an array of doubles using a specified delimiter.
BOOST_AUTO_TEST_CASE(testJoin_ArrayOfDoubles) {
BOOST_CHECK(join(vector<string>({}), "").empty());

BOOST_CHECK_EQUAL("1;2", join(PRIM_LIST, SEPARATOR_CHAR));
BOOST_CHECK("2" != join(PRIM_LIST, SEPARATOR_CHAR));
BOOST_CHECK("" != join(PRIM_LIST, SEPARATOR_CHAR));
}

//test17 verify if it can join an array of Number using a specified delimiter.
BOOST_AUTO_TEST_CASE(testJoin_ArrayOfFloats) {
BOOST_CHECK(join(vector<string>({}), "").empty());

BOOST_CHECK_EQUAL("1;2", join(PRIM_LIST, SEPARATOR_CHAR));
BOOST_CHECK("2" != join(PRIM_LIST, SEPARATOR_CHAR));
BOOST_CHECK("" != join(PRIM_LIST, SEPARATOR_CHAR));
}

//test18 verify if it can join an array of booleans using a specified delimiter.
BOOST_AUTO_TEST_CASE(testJoin_ArrayOfBooleans) {
BOOST_CHECK_EQUAL("false;false", join(ARRAY_FALSE_FALSE, SEPARATOR_CHAR));
// BOOST_CHECK_EQUAL("", join("", SEPARATOR_CHAR));
BOOST_CHECK_EQUAL("false,true,false", join(ARRAY_FALSE_TRUE_FALSE, COMMA_SEPARATOR_CHAR));
BOOST_CHECK_EQUAL("false;true", join(ARRAY_FALSE_TRUE, SEPARATOR_CHAR));
BOOST_CHECK("" != join(ARRAY_FALSE_FALSE, SEPARATOR_CHAR));
BOOST_CHECK("" != join(ARRAY_FALSE_TRUE_FALSE, SEPARATOR_CHAR));
}

//test19 verify if it can join an array of shorts using a specified delimiter.
BOOST_AUTO_TEST_CASE(testJoin_ArrayOfShorts) {
BOOST_CHECK_EQUAL("1;2", join(PRIM_LIST, SEPARATOR_CHAR));
BOOST_CHECK("2" != join(PRIM_LIST, SEPARATOR_CHAR));
}

//test20 verify if it can join an array of strings using a specified character delimiter.
BOOST_AUTO_TEST_CASE(testJoin_ArrayCharSeparator) {
BOOST_CHECK_EQUAL( TEXT_LIST_CHAR ,join( ARRAY_LIST, SEPARATOR_CHAR));
BOOST_CHECK_EQUAL("", join( EMPTY_ARRAY_LIST, SEPARATOR_CHAR));
BOOST_CHECK_EQUAL(";foo", join( MIXED_ARRAY_LIST, SEPARATOR_CHAR));
BOOST_CHECK_EQUAL("foo;2", join( MIXED_TYPE_LIST, SEPARATOR_CHAR));
}

//test21 verify if it can join an array of ints using a specified delimiter.
BOOST_AUTO_TEST_CASE(testJoin_ArrayOfInts) {
BOOST_CHECK_EQUAL("1;2", join(PRIM_LIST, SEPARATOR_CHAR));
BOOST_CHECK("2" != join(PRIM_LIST, SEPARATOR_CHAR));
}

//test22 verify if it can join a list of strings without using a delimiter.
BOOST_AUTO_TEST_CASE(testJoin_List_EmptyDelimiter) {
BOOST_CHECK_EQUAL(TEXT_LIST_NOSEP, join(STRING_LIST, ""));
BOOST_CHECK_EQUAL("", join(EMPTY_STRING_LIST, ""));
}

//test23 verify if it can replace all occurrences of a substring in a string with a specified replacement string.
BOOST_AUTO_TEST_CASE(testreplace_all_copy_StringStringString){
string emptyStr ="";
string FOO ="FOO";
string foofoofoo= "foofoofoo";
BOOST_CHECK_EQUAL("", replace_all_copy (emptyStr, "any", "any"));
BOOST_CHECK_EQUAL ("FOO",replace_all_copy (FOO,"","any"));
BOOST_CHECK_EQUAL ("", replace_all_copy (foofoofoo,"foo",""));
BOOST_CHECK_EQUAL ("barbarbar", replace_all_copy (foofoofoo,"foo","bar"));
BOOST_CHECK_EQUAL ("farfarfar", replace_all_copy (foofoofoo,"oo","ar"));
}