/** * @file bsl_test_string.hpp * @author chenxm(chenxiaoming@baidu.com) * @date 2008/09/12 17:29:08 * @version $Revision: 1.10 $ * @brief * **/ #ifndef __BSL_TEST_SHALLOW_COPY_STRING_HPP_ #define __BSL_TEST_SHALLOW_COPY_STRING_HPP_ #include #include #include #include #include #include #include #include #include #include #define see(x) do{ \ std::cerr<<__FILE__<<":"<<__LINE__<<": " <<(#x)<<" = "<<(x)<<" ("< * example: CXM_ASSERT( 3 + 3 != 6, <<"test: "< class bsl_test_shallow_copy_string{ private: typedef bsl::basic_string<_CharT,_Alloc> string_t; _CharT *_cs_0; const size_t _cs_0_len; const size_t _cs_0_size; _CharT* _cs_1; _CharT* _cs_2; _CharT* _cs_3; _CharT* _cs_4; _CharT* _cs_5; _CharT* _empty_cs; _CharT* _cs_0_add_1; static _CharT * malloc_clone( const char * str ){ size_t len = strlen(str); _CharT * p = static_cast<_CharT*>(malloc( (len+1)*sizeof(_CharT) )); for( size_t i = 0; i<=len; ++i){ p[i] = str[i]; } return p; } public: bsl_test_shallow_copy_string() : _cs_0(malloc_clone("bsl_string")), _cs_0_len(strlen("bsl_string")), _cs_0_size(_cs_0_len * sizeof(_CharT)), _cs_1(malloc_clone("acumon")), _cs_2(malloc_clone("ACUMON")), _cs_3(malloc_clone("acumo")), _cs_4(malloc_clone("acumon")), // == _cs_1 _cs_5(malloc_clone("pudding")), _empty_cs(malloc_clone("")), _cs_0_add_1(malloc_clone("bsl_stringacumon")) { } ~bsl_test_shallow_copy_string(){ free( _cs_0 ); free( _cs_1 ); free( _cs_0_add_1 ); free( _cs_2 ); free( _cs_3 ); free( _cs_4 ); free( _cs_5 ); free( _empty_cs ); } void test_shallow_copy() { const size_t char_size = sizeof(_CharT); _Alloc all; { string_t s(_cs_0, 3,all,true); TS_ASSERT( s.size() == 3 ); TS_ASSERT( 0 == memcmp( s.c_str(), _cs_0, 3*char_size ) ); // TS_ASSERT( s.c_str()[3] == '\0' ); } { string_t s(_cs_0, 3,all,false); TS_ASSERT( s.size() == 3 ); TS_ASSERT( 0 == memcmp( s.c_str(), _cs_0, 3*char_size ) ); TS_ASSERT( s.c_str()[3] == '\0' ); } /* { string_t a(_cs_0); string_t s(a,all,false); TS_ASSERT( s.size() == a.size() ); TS_ASSERT( 0 == memcmp( s.c_str(), a.c_str(), a.size() )); TS_ASSERT( s.c_str()[s.size()]== '\0' ); } { string_t a(_cs_0); string_t s(a,all,true); TS_ASSERT( s.size() == a.size() ); TS_ASSERT( 0 == memcmp( s.c_str(), a.c_str(), a.size() )); //TS_ASSERT( s.c_str()[s.size()]== '\0' ); } */ { string_t a(_cs_0); string_t s(a); TS_ASSERT( s.size() == a.size() ); TS_ASSERT( 0 == memcmp( s.c_str(),a.c_str(), s.size() ) ); TS_ASSERT( s.c_str() != a.c_str() ); } { string_t a(_cs_0, 3,all,true); string_t s(a); TS_ASSERT( s.size() == a.size() ); TS_ASSERT( 0 == memcmp( s.c_str(),a.c_str(), s.size() ) ); TS_ASSERT( s.c_str() == a.c_str() ); } } }; #endif //__BSL_TEST_STRING_HPP_ /* vim: set ts=4 sw=4 sts=4 tw=100 */