📄 string_test.cpp
字号:
catch (out_of_range const&) { CPPUNIT_ASSERT( true ); } catch ( ... ) { CPPUNIT_ASSERT( false ); }#endif}void StringTest::insert(){ string strorg = "This is test string for string calls"; string str; /* * In case of reallocation there is no auto reference problem * so we reserve a big enough string to be sure to test this * particular point. */ str.reserve(100); str = strorg; //test self insertion: str.insert(10, str.c_str() + 5, 15); CPPUNIT_ASSERT( str == "This is teis test string st string for string calls" ); str = strorg; str.insert(15, str.c_str() + 5, 25); CPPUNIT_ASSERT( str == "This is test stis test string for stringring for string calls" ); str = strorg; str.insert(0, str.c_str() + str.size() - 4, 4); CPPUNIT_ASSERT( str == "allsThis is test string for string calls" ); str = strorg; str.insert(0, str.c_str() + str.size() / 2 - 1, str.size() / 2 + 1); CPPUNIT_ASSERT( str == "ng for string callsThis is test string for string calls" ); str = strorg; string::iterator b = str.begin(); string::const_iterator s = str.begin() + str.size() / 2 - 1; string::const_iterator e = str.end(); str.insert( b, s, e ); CPPUNIT_ASSERT( str == "ng for string callsThis is test string for string calls" ); str = strorg; str.insert(str.begin(), str.begin() + str.size() / 2 - 1, str.end()); CPPUNIT_ASSERT( str == "ng for string callsThis is test string for string calls" );#ifdef _STLP_MEMBER_TEMPLATES vector<int> int_vect; //Just a compile time test: str.insert(str.end(), int_vect.begin(), int_vect.end());#endif string str0; str0.insert(str0.begin(), 5, '0'); CPPUNIT_ASSERT( str0 == "00000" ); string str1; { string::size_type pos = 0, nb = 2; str1.insert(pos, nb, '1'); } CPPUNIT_ASSERT( str1 == "11" ); str0.insert(0, str1); CPPUNIT_ASSERT( str0 == "1100000" ); string str2("2345"); str0.insert(str0.size(), str2, 1, 2); CPPUNIT_ASSERT( str0 == "110000034" ); str1.insert(str1.begin() + 1, 2, '2'); CPPUNIT_ASSERT( str1 == "1221" ); str1.insert(2, "333333", 3); CPPUNIT_ASSERT( str1 == "1233321" ); str1.insert(4, "4444"); CPPUNIT_ASSERT( str1 == "12334444321" ); str1.insert(str1.begin() + 6, '5'); CPPUNIT_ASSERT( str1 == "123344544321" );}void StringTest::replace(){ /* * This test case is for the non template basic_string::replace method, * this is why we play with the const iterators and reference to guaranty * that the right method is called. */ const string v( "78" ); string s( "123456" ); string const& cs = s; string::iterator i = s.begin() + 1; s.replace(i, i + 3, v.begin(), v.end()); CPPUNIT_ASSERT( s == "17856" ); s = "123456"; i = s.begin() + 1; s.replace(i, i + 1, v.begin(), v.end()); CPPUNIT_ASSERT( s == "1783456" ); s = "123456"; i = s.begin() + 1; string::const_iterator ci = s.begin() + 1; s.replace(i, i + 3, ci + 3, cs.end()); CPPUNIT_ASSERT( s == "15656" ); s = "123456"; i = s.begin() + 1; ci = s.begin() + 1; s.replace(i, i + 3, ci, ci + 2); CPPUNIT_ASSERT( s == "12356" ); s = "123456"; i = s.begin() + 1; ci = s.begin() + 1; s.replace(i, i + 3, ci + 1, cs.end()); CPPUNIT_ASSERT( s == "1345656" ); s = "123456"; s.replace(s.begin() + 4, s.end(), cs.begin(), cs.end()); CPPUNIT_ASSERT( s == "1234123456" ); /* * This is the test for the template replace method. */ s = "123456"; string::iterator b = s.begin() + 4; string::iterator e = s.end(); string::const_iterator rb = s.begin(); string::const_iterator re = s.end(); s.replace(b, e, rb, re); CPPUNIT_ASSERT( s == "1234123456" ); s = "123456"; s.replace(s.begin() + 4, s.end(), s.begin(), s.end()); CPPUNIT_ASSERT( s == "1234123456" ); string strorg("This is test string for string calls"); string str = strorg; str.replace(5, 15, str.c_str(), 10); CPPUNIT_ASSERT( str == "This This is tefor string calls" ); str = strorg; str.replace(5, 5, str.c_str(), 10); CPPUNIT_ASSERT( str == "This This is test string for string calls" );#if !defined (STLPORT) || defined (_STLP_MEMBER_TEMPLATES) deque<char> cdeque; cdeque.push_back('I'); str.replace(str.begin(), str.begin() + 11, cdeque.begin(), cdeque.end()); CPPUNIT_ASSERT( str == "Is test string for string calls" );#endif}void StringTest::resize(){ string s; s.resize(0); CPPUNIT_ASSERT( *s.c_str() == 0 ); s = "1234567"; s.resize(0); CPPUNIT_ASSERT( *s.c_str() == 0 ); s = "1234567"; s.resize(1); CPPUNIT_ASSERT( s.size() == 1 ); CPPUNIT_ASSERT( *s.c_str() == '1' ); CPPUNIT_ASSERT( *(s.c_str() + 1) == 0 ); s = "1234567"; s.resize(10); CPPUNIT_ASSERT( s.size() == 10 ); CPPUNIT_ASSERT( s[6] == '7' ); CPPUNIT_ASSERT( s[7] == 0 ); CPPUNIT_ASSERT( s[8] == 0 ); CPPUNIT_ASSERT( s[9] == 0 );}void StringTest::find(){ string s("one two three one two three"); CPPUNIT_ASSERT( s.find("one") == 0 ); CPPUNIT_ASSERT( s.find('t') == 4 ); CPPUNIT_ASSERT( s.find('t', 5) == 8 ); //We are trying to get a const reference to the npos string static member to //force the compiler to allocate memory for this variable. It used to reveal //a bug of STLport which was simply declaring npos without instanciating it.#if !defined (STLPORT) || !defined (_STLP_STATIC_CONST_INIT_BUG) string::size_type const& npos_local = string::npos;#else# define npos_local string::npos#endif CPPUNIT_ASSERT( s.find("four") == npos_local ); CPPUNIT_ASSERT( s.find("one", string::npos) == npos_local ); CPPUNIT_ASSERT( s.find_first_of("abcde") == 2 ); CPPUNIT_ASSERT( s.find_first_not_of("enotw ") == 9 );}void StringTest::rfind(){ // 21.3.6.2 string s("one two three one two three"); CPPUNIT_ASSERT( s.rfind("two") == 18 ); CPPUNIT_ASSERT( s.rfind("two", 0) == string::npos ); CPPUNIT_ASSERT( s.rfind("two", 11) == 4 ); CPPUNIT_ASSERT( s.rfind('w') == 19 ); string test( "aba" ); CPPUNIT_CHECK( test.rfind( "a", 2, 1 ) == 2 ); CPPUNIT_CHECK( test.rfind( "a", 1, 1 ) == 0 ); CPPUNIT_CHECK( test.rfind( "a", 0, 1 ) == 0 ); CPPUNIT_CHECK( test.rfind( 'a', 2 ) == 2 ); CPPUNIT_CHECK( test.rfind( 'a', 1 ) == 0 ); CPPUNIT_CHECK( test.rfind( 'a', 0 ) == 0 );}void StringTest::find_last_of(){ // 21.3.6.4 string s("one two three one two three"); CPPUNIT_ASSERT( s.find_last_of("abcde") == 26 ); string test( "aba" ); CPPUNIT_CHECK( test.find_last_of( "a", 2, 1 ) == 2 ); CPPUNIT_CHECK( test.find_last_of( "a", 1, 1 ) == 0 ); CPPUNIT_CHECK( test.find_last_of( "a", 0, 1 ) == 0 ); CPPUNIT_CHECK( test.find_last_of( 'a', 2 ) == 2 ); CPPUNIT_CHECK( test.find_last_of( 'a', 1 ) == 0 ); CPPUNIT_CHECK( test.find_last_of( 'a', 0 ) == 0 );}void StringTest::find_last_not_of(){ // 21.3.6.6 string s("one two three one two three"); CPPUNIT_ASSERT( s.find_last_not_of("ehortw ") == 15 ); string test( "aba" ); CPPUNIT_CHECK( test.find_last_not_of( "a", 2, 1 ) == 1 ); CPPUNIT_CHECK( test.find_last_not_of( "b", 2, 1 ) == 2 ); CPPUNIT_CHECK( test.find_last_not_of( "a", 1, 1 ) == 1 ); CPPUNIT_CHECK( test.find_last_not_of( "b", 1, 1 ) == 0 ); CPPUNIT_CHECK( test.find_last_not_of( "a", 0, 1 ) == string::npos ); CPPUNIT_CHECK( test.find_last_not_of( "b", 0, 1 ) == 0 ); CPPUNIT_CHECK( test.find_last_not_of( 'a', 2 ) == 1 ); CPPUNIT_CHECK( test.find_last_not_of( 'b', 2 ) == 2 ); CPPUNIT_CHECK( test.find_last_not_of( 'a', 1 ) == 1 ); CPPUNIT_CHECK( test.find_last_not_of( 'b', 1 ) == 0 ); CPPUNIT_CHECK( test.find_last_not_of( 'a', 0 ) == string::npos ); CPPUNIT_CHECK( test.find_last_not_of( 'b', 0 ) == 0 );}void StringTest::copy(){ string s("foo"); char dest[4]; dest[0] = dest[1] = dest[2] = dest[3] = 1; s.copy(dest, 4); int pos = 0; CPPUNIT_ASSERT( dest[pos++] == 'f' ); CPPUNIT_ASSERT( dest[pos++] == 'o' ); CPPUNIT_ASSERT( dest[pos++] == 'o' ); CPPUNIT_ASSERT( dest[pos++] == 1 ); dest[0] = dest[1] = dest[2] = dest[3] = 1; s.copy(dest, 4, 2); pos = 0; CPPUNIT_ASSERT( dest[pos++] == 'o' ); CPPUNIT_ASSERT( dest[pos++] == 1 );#if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) try { s.copy(dest, 4, 5); CPPUNIT_ASSERT( false ); } catch (out_of_range const&) { CPPUNIT_ASSERT( true ); } catch ( ... ) { CPPUNIT_ASSERT( false ); }#endif}void StringTest::assign(){ string s; char const* cstr = "test string for assign"; s.assign(cstr, cstr + 22); CPPUNIT_ASSERT( s == "test string for assign" ); string s2("other test string"); s.assign(s2); CPPUNIT_ASSERT( s == s2 ); static string str1; static string str2; // short string optim: str1 = "123456"; // longer than short string: str2 = "1234567890123456789012345678901234567890"; CPPUNIT_ASSERT(str1[5] == '6'); CPPUNIT_ASSERT(str2[29] == '0');}/* This test is to check if std::string properly supports the short string * optimization. It has been found out that eMbedded Visual C++ 3.0 and .NET * compilers for the ARM platform fail to pass structs and classes of certain * size per value. This seems to be a known compiler bug. For other processors * (e.g. x86) the error doesn't occur. * (The ARM compiler creates a temporary object from teststr on the stack, to * pass it to the helper function. It uses the copy constructor for this. * After this the temporary object is copied to another place on the stack. * The result is that the _M_finish pointer then points to the wrong buffer * end and the size of the short string is incorrectly calculated.) */void StringTest::short_string_optim_bug(){ string teststr("shortest"); bool short_string_optim_bug_helper(string teststr); CPPUNIT_ASSERT(true == short_string_optim_bug_helper(teststr));}bool short_string_optim_bug_helper(string teststr){ size_t ss = teststr.size(); return (ss == 8);}void StringTest::compare(){ string str1("abcdef"); string str2; str2 = "abcdef"; CPPUNIT_ASSERT( str1.compare(str2) == 0 ); str2 = "abcde"; CPPUNIT_ASSERT( str1.compare(str2) > 0 ); str2 = "abcdefg"; CPPUNIT_ASSERT( str1.compare(str2) < 0 ); CPPUNIT_ASSERT( str1.compare("abcdef") == 0 ); CPPUNIT_ASSERT( str1.compare("abcde") > 0 ); CPPUNIT_ASSERT( str1.compare("abcdefg") < 0 ); str2 = "cde"; CPPUNIT_ASSERT( str1.compare(2, 3, str2) == 0 ); str2 = "cd"; CPPUNIT_ASSERT( str1.compare(2, 3, str2) > 0 ); str2 = "cdef"; CPPUNIT_ASSERT( str1.compare(2, 3, str2) < 0 ); str2 = "abcdef"; CPPUNIT_ASSERT( str1.compare(2, 3, str2, 2, 3) == 0 ); CPPUNIT_ASSERT( str1.compare(2, 3, str2, 2, 2) > 0 ); CPPUNIT_ASSERT( str1.compare(2, 3, str2, 2, 4) < 0 ); CPPUNIT_ASSERT( str1.compare(2, 3, "cdefgh", 3) == 0 ); CPPUNIT_ASSERT( str1.compare(2, 3, "cdefgh", 2) > 0 ); CPPUNIT_ASSERT( str1.compare(2, 3, "cdefgh", 4) < 0 );}/*class mystring : public string {public: mystring() {} mystring(string const& s) : string(s) {} mystring& operator = (string const& s) { string::operator = (s); return *this; };};*/void StringTest::template_expression(){ string one("one"), two("two"), three("three"); string space(1, ' '); // check availability of [un]equality operators { // string-string one == two; one != two; // string-literal one == "two"; one != "two"; // literal-string "one" == two; "one" != two; // strsum-string (one + two) == three; (one + two) != three;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -