📄 string_test.cpp
字号:
// string-strsum one == (two + three); one != (two + three); // strsum-literal (one + two) == "three"; (one + two) != "three"; // literal-strsum "one" == (two + three); "one" != (two + three); // strsum-strsum (one + two) == (two + three); (one + two) != (two + three); } { string result(one + ' ' + two + ' ' + three); CPPUNIT_CHECK( result == "one two three" ); } { string result(one + ' ' + two + ' ' + three, 4); CPPUNIT_CHECK( result == "two three" ); } { string result(one + ' ' + two + ' ' + three, 4, 3); CPPUNIT_CHECK( result == "two" ); } //2 members expressions: CPPUNIT_CHECK( (' ' + one) == " one" ); CPPUNIT_CHECK( (one + ' ') == "one " ); CPPUNIT_CHECK( (one + " two") == "one two" ); CPPUNIT_CHECK( ("one " + two) == "one two" ); CPPUNIT_CHECK( (one + space) == "one " ); //3 members expressions: CPPUNIT_CHECK( ((one + space) + "two") == "one two" ); CPPUNIT_CHECK( ("one" + (space + two)) == "one two" ); CPPUNIT_CHECK( ((one + space) + two) == "one two" ); CPPUNIT_CHECK( (one + (space + two)) == "one two" ); CPPUNIT_CHECK( ((one + space) + 't') == "one t" ); CPPUNIT_CHECK( ('o' + (space + two)) == "o two" ); //4 members expressions: CPPUNIT_CHECK( ((one + space) + (two + space)) == "one two " ); //special operators { string result; result = one + space + two; CPPUNIT_CHECK( result == "one two" ); result += space + three; CPPUNIT_CHECK( result == "one two three" ); } //special append method { string result; //Use reserve to avoid reallocation and really test auto-referencing problems: result.reserve(64); result.append(one + space + two); CPPUNIT_CHECK( result == "one two" ); result.append(space + result + space + three); CPPUNIT_CHECK( result == "one two one two three" ); result = "one two"; result.append(space + three, 1, 2); CPPUNIT_ASSERT( result == "one twoth" ); result.append(space + result); CPPUNIT_CHECK( result == "one twoth one twoth" ); } //special assign method { string result; //Use reserve to avoid reallocation and really test auto-referencing problems: result.reserve(64); result.assign(one + space + two + space + three); CPPUNIT_CHECK( result == "one two three" ); result.assign(one + space + two + space + three, 3, 5); CPPUNIT_CHECK( result == " two " ); result.assign(one + result + three); CPPUNIT_CHECK( result == "one two three" ); } { CPPUNIT_CHECK( !(one + ' ' + two).empty() ); char result = (one + ' ' + two)[3]; CPPUNIT_CHECK( result == ' ' ); result = (one + ' ' + two).at(3); CPPUNIT_CHECK( result == ' ' );#if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) try { result = (one + ' ' + two).at(10); CPPUNIT_ASSERT(false); } catch (out_of_range const&) { CPPUNIT_ASSERT( result == ' ' ); } catch (...) { CPPUNIT_ASSERT(false); }#endif } /* mystring a("ing"); //gcc failed to compile following expression when template expressions are activated. //MSVC sees no problem. gcc limitation or MSVC is too cool ?? mystring b = "str" + a; */}#if !defined (TE_TMP_TEST_IGNORED)class superstring{ public: superstring() : s("super") {} superstring( const string& str ) : s( str ) {} superstring operator / (const string& str ) { return superstring( s + "/" + str ); } superstring operator / (const char* str ) { return superstring( s + "/" + str ); } private: string s;};#endifvoid StringTest::te_tmp(){#if !defined (TE_TMP_TEST_IGNORED) superstring s; string more( "more" ); string less( "less" ); superstring r = s / (more + less);#endif}void StringTest::template_wexpression(){#if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T)# if !defined (__CYGWIN__) || defined (STLPORT) wstring one(L"one"), two(L"two"), three(L"three"); wstring space(1, L' '); { wstring result(one + L' ' + two + L' ' + three); CPPUNIT_CHECK( result == L"one two three" ); } { wstring result(one + L' ' + two + L' ' + three, 4); CPPUNIT_CHECK( result == L"two three" ); } { wstring result(one + L' ' + two + L' ' + three, 4, 3); CPPUNIT_CHECK( result == L"two" ); } //2 members expressions: CPPUNIT_CHECK( (L' ' + one) == L" one" ); CPPUNIT_CHECK( (one + L' ') == L"one " ); CPPUNIT_CHECK( (one + L" two") == L"one two" ); CPPUNIT_CHECK( (L"one " + two) == L"one two" ); CPPUNIT_CHECK( (one + space) == L"one " ); //3 members expressions: CPPUNIT_CHECK( ((one + space) + L"two") == L"one two" ); CPPUNIT_CHECK( (L"one" + (space + two)) == L"one two" ); CPPUNIT_CHECK( ((one + space) + two) == L"one two" ); CPPUNIT_CHECK( (one + (space + two)) == L"one two" ); CPPUNIT_CHECK( ((one + space) + L't') == L"one t" ); CPPUNIT_CHECK( (L'o' + (space + two)) == L"o two" ); //4 members expressions: CPPUNIT_CHECK( ((one + space) + (two + space)) == L"one two " ); //special operators { wstring result; result = one + space + two; CPPUNIT_CHECK( result == L"one two" ); result += space + three; CPPUNIT_CHECK( result == L"one two three" ); } //special append method { wstring result; //Use reserve to avoid reallocation and really test auto-referencing problems: result.reserve(64); result.append(one + space + two); CPPUNIT_CHECK( result == L"one two" ); result.append(space + result + space + three); CPPUNIT_CHECK( result == L"one two one two three" ); result = L"one two"; result.append(space + three, 1, 2); CPPUNIT_ASSERT( result == L"one twoth" ); result.append(space + result); CPPUNIT_CHECK( result == L"one twoth one twoth" ); } //special assign method { wstring result; //Use reserve to avoid reallocation and really test auto-referencing problems: result.reserve(64); result.assign(one + space + two + space + three); CPPUNIT_CHECK( result == L"one two three" ); result.assign(one + space + two + space + three, 3, 5); CPPUNIT_CHECK( result == L" two " ); result.assign(one + result + three); CPPUNIT_CHECK( result == L"one two three" ); } { CPPUNIT_CHECK( !(one + L' ' + two).empty() ); wchar_t result = (one + L' ' + two)[3]; CPPUNIT_CHECK( result == L' ' ); result = (one + L' ' + two).at(3); CPPUNIT_CHECK( result == L' ' );# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) try { result = (one + L' ' + two).at(10); CPPUNIT_ASSERT(false); } catch (out_of_range const&) { CPPUNIT_ASSERT( result == L' ' ); } catch (...) { CPPUNIT_ASSERT(false); }# endif }# endif#endif}void StringTest::io(){#if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS) string str("STLport"); { ostringstream ostr; ostr << str; CPPUNIT_ASSERT( ostr.good() ); CPPUNIT_ASSERT( ostr.str() == str ); } { istringstream istr(str); string istr_content; istr >> istr_content; CPPUNIT_ASSERT( !istr.fail() && istr.eof() ); CPPUNIT_ASSERT( istr_content == str ); } { istringstream istr(str); istr.width(3); string istr_content; istr >> istr_content; CPPUNIT_ASSERT( !istr.fail() && !istr.eof() ); CPPUNIT_ASSERT( istr_content == "STL" ); }#endif}void StringTest::allocator_with_state(){#if !(defined (STLPORT) && defined (_STLP_NO_CUSTOM_IO)) char buf1[1024]; StackAllocator<char> stack1(buf1, buf1 + sizeof(buf1)); char buf2[1024]; StackAllocator<char> stack2(buf2, buf2 + sizeof(buf2)); typedef basic_string<char, char_traits<char>, StackAllocator<char> > StackString; { StackString str1("string stack1", stack1); StackString str1Cpy(str1); StackString str2("string stack2", stack2); StackString str2Cpy(str2); str1.swap(str2); CPPUNIT_ASSERT( str1.get_allocator().swaped() ); CPPUNIT_ASSERT( str2.get_allocator().swaped() ); CPPUNIT_ASSERT( str1 == str2Cpy ); CPPUNIT_ASSERT( str2 == str1Cpy ); CPPUNIT_ASSERT( str1.get_allocator() == stack2 ); CPPUNIT_ASSERT( str2.get_allocator() == stack1 ); } CPPUNIT_ASSERT( stack1.ok() ); CPPUNIT_ASSERT( stack2.ok() ); stack1.reset(); stack2.reset(); { StackString str1("longer string from stack1 allocator instance for dynamic allocation", stack1); StackString str1Cpy(str1); StackString str2("longer string from stack2 allocator instance for dynamic allocation", stack2); StackString str2Cpy(str2); str1.swap(str2); CPPUNIT_ASSERT( str1.get_allocator().swaped() ); CPPUNIT_ASSERT( str2.get_allocator().swaped() ); CPPUNIT_ASSERT( str1 == str2Cpy ); CPPUNIT_ASSERT( str2 == str1Cpy ); CPPUNIT_ASSERT( str1.get_allocator() == stack2 ); CPPUNIT_ASSERT( str2.get_allocator() == stack1 ); } CPPUNIT_ASSERT( stack1.ok() ); CPPUNIT_ASSERT( stack2.ok() ); stack1.reset(); stack2.reset(); { StackString str1("string stack1", stack1); StackString str1Cpy(str1); StackString str2("longer string from stack2 allocator instance for dynamic allocation", stack2); StackString str2Cpy(str2); str1.swap(str2); CPPUNIT_ASSERT( str1.get_allocator().swaped() ); CPPUNIT_ASSERT( str2.get_allocator().swaped() ); CPPUNIT_ASSERT( str1 == str2Cpy ); CPPUNIT_ASSERT( str2 == str1Cpy ); CPPUNIT_ASSERT( str1.get_allocator() == stack2 ); CPPUNIT_ASSERT( str2.get_allocator() == stack1 ); } CPPUNIT_ASSERT( stack1.ok() ); CPPUNIT_ASSERT( stack2.ok() ); stack1.reset(); stack2.reset(); { StackString str1("longer string from stack1 allocator instance for dynamic allocation", stack1); StackString str1Cpy(str1); StackString str2("string stack2", stack2); StackString str2Cpy(str2); str1.swap(str2); CPPUNIT_ASSERT( str1.get_allocator().swaped() ); CPPUNIT_ASSERT( str2.get_allocator().swaped() ); CPPUNIT_ASSERT( str1 == str2Cpy ); CPPUNIT_ASSERT( str2 == str1Cpy ); CPPUNIT_ASSERT( str1.get_allocator() == stack2 ); CPPUNIT_ASSERT( str2.get_allocator() == stack1 ); } CPPUNIT_ASSERT( stack1.ok() ); CPPUNIT_ASSERT( stack2.ok() ); stack1.reset(); stack2.reset();#endif}void StringTest::capacity(){ string s; CPPUNIT_CHECK( s.capacity() > 0 ); CPPUNIT_CHECK( s.capacity() < s.max_size() ); CPPUNIT_CHECK( s.capacity() >= s.size() );#ifndef _STLP_SHORT_STRING_SZ# define _STLP_SHORT_STRING_SZ 16 // see stlport/stl/_string_base.h#endif for ( int i = 0; i < _STLP_SHORT_STRING_SZ + 2; ++i ) { s += ' '; CPPUNIT_CHECK( s.capacity() > 0 ); CPPUNIT_CHECK( s.capacity() < s.max_size() ); CPPUNIT_CHECK( s.capacity() >= s.size() ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -