string01.cpp

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C++ 代码 · 共 776 行 · 第 1/2 页

CPP
776
字号
  s3.insert( 13, s0 );
  if( s3 != "Hello, World!INSERTED" || s3.size( ) != 21 || INSANE( s3 ) ) {
    FAIL
  }

  std::string s4( "Hello, World!" );
  s4.insert( 0, s0, 2, 2 );
  if( s4 != "SEHello, World!" || s4.size( ) != 15 || INSANE( s4 ) ) {
    FAIL
  }

  std::string s5( "Hello, World!" );
  s5.insert( 0, s0, 2, 128 );
  if( s5 != "SERTEDHello, World!" || s5.size( ) != 19 || INSANE( s5 ) ) {
    FAIL
  }

  std::string s6( "Hello, World!" );
  s6.insert( s6.begin( ), 'x' );
  if( s6 != "xHello, World!" || s6.size( ) != 14 || INSANE( s6 ) ) {
    FAIL
  }

  std::string s7( "Hello, World!" );
  s7.insert( s7.end( ), 3, 'z' );
  if( s7 != "Hello, World!zzz" || s7.size( ) != 16 || INSANE( s7 ) ) {
    FAIL
  }

  // Need to test other insert methods.

  return( rc );
}


bool erase_test( )
{
  bool rc = true;

  std::string s1( "Hello, World!" );
  s1.erase( );
  if( s1 != "" || s1.size( ) != 0 || INSANE( s1 ) ) FAIL

  std::string s2( "Hello, World!" );
  s2.erase( 2, 3 );
  if( s2 != "He, World!" || s2.size( ) != 10 || INSANE( s2 ) ) FAIL

  std::string s3( "Hello, World!" );
  s3.erase( 7, 6 );
  if( s3 != "Hello, " || s3.size( ) != 7 || INSANE( s3 ) ) FAIL

  std::string s4( "Hello, World!" );
  s4.erase( s4.begin( ) );
  if( s4 != "ello, World!" || s4.size( ) != 12 || INSANE( s4 ) ) FAIL

  std::string s5( "Hello, World!" );
  s5.erase( s5.begin( ) + 2, s5.begin( ) + 5 );
  if( s5 != "He, World!" || s5.size( ) != 10 || INSANE( s5 ) ) FAIL

  return( rc );
}


bool replace_test( )
{
  bool rc = true;
  const std::string s1( "Insert me!" );

  std::string t1( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
  t1.replace( 0, std::string::npos, s1, 0, std::string::npos );
  if( t1 != "Insert me!" || INSANE( t1 ) ) FAIL

  std::string t2( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
  t2.replace( 0, 1, s1, 0, std::string::npos );
  if( t2 != "Insert me!BCDEFGHIJKLMNOPQRSTUVWXYZ" || INSANE( t2 ) ) FAIL

  std::string t3( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
  t3.replace( 1, 0, s1, 0, 1 );
  if( t3 != "AIBCDEFGHIJKLMNOPQRSTUVWXYZ" || INSANE( t3 ) ) FAIL

  std::string t4( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
  t4.replace( 26, 0, s1, 0, std::string::npos );
  if( t4 != "ABCDEFGHIJKLMNOPQRSTUVWXYZInsert me!" || INSANE( t4 ) ) FAIL

  std::string t5( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
  t5.replace( 4, 3, s1, 2, 5 );
  if( t5 != "ABCDsert HIJKLMNOPQRSTUVWXYZ" || INSANE( t5 ) ) FAIL

  std::string t6( "Shorty" );
  t6.replace( 1, 2, s1, 0, 2 );
  if( t6 != "SInrty" || INSANE( t6 ) ) FAIL

  // Need to test other replace methods.

  return( rc );
}


bool iterator_replace_test( )
{
  bool rc = true;
  const std::string s1( "Insert me!" );

  std::string t1( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
  t1.replace( t1.begin( ), t1.end( ), s1 );
  if( t1 != "Insert me!" || INSANE( t1 ) ) FAIL

  std::string t2( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
  t2.replace( t2.begin( ), t2.begin( ) + 1, s1 );
  if( t2 != "Insert me!BCDEFGHIJKLMNOPQRSTUVWXYZ" || INSANE( t2 ) ) FAIL

  std::string t3( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
  t3.replace( t3.end( ), t3.end( ), s1 );
  if( t3 != "ABCDEFGHIJKLMNOPQRSTUVWXYZInsert me!" || INSANE( t3 ) ) FAIL

  std::string t4( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
  t4.replace( t4.begin( ) + 4, t4.begin( ) + 7, s1 );
  if( t4 != "ABCDInsert me!HIJKLMNOPQRSTUVWXYZ" || INSANE( t4 ) ) FAIL

  std::string t5( "Shorty" );
  t5.replace( t5.begin( ), t5.begin( ) + 2, 2, 'x' );
  if( t5 != "xxorty" || INSANE( t5 ) ) FAIL

  // Need to test other iterator replace methods.

  return( rc );
}


bool copy_test( )
{
  bool rc = true;

  char buffer[128];
  std::string s1( "Hello, World!" );

  buffer[ s1.copy( buffer, 2, 0 ) ] = '\0';
  if( std::strcmp( buffer, "He" ) != 0 ) FAIL

  buffer[ s1.copy( buffer, s1.size( ), 0 ) ] = '\0';
  if( std::strcmp( buffer, "Hello, World!" ) != 0 ) FAIL

  buffer[ s1.copy( buffer, 3, 2 ) ] = '\0';
  if( std::strcmp( buffer, "llo" ) != 0 ) FAIL

  buffer[ s1.copy( buffer, 10, 7 ) ] = '\0';
  if( std::strcmp( buffer, "World!" ) != 0 ) FAIL

  buffer[ s1.copy( buffer, 0, 13 ) ] = '\0';
  if( std::strcmp( buffer, "" ) != 0 ) FAIL

  try {
    buffer[ s1.copy( buffer, 1, 14 ) ] = '\0';
    FAIL
  }
  catch( std::out_of_range ) {
    // Okay
  }
  return( rc );
}


bool swap_test( )
{
  bool rc = true;

  std::string s1("ABC");
  std::string s2("XYZ");

  s1.swap( s2 );
  if( s1 != "XYZ" || s2 != "ABC" ) FAIL

  #ifdef __NEVER
  std::swap( s1, s2 );
  if( s1 != "ABC" || s2 != "XYZ" ) FAIL
  #endif

  return( rc );
}


bool cstr_test( )
{
        bool  rc = true;
  const char *p;

  std::string s1( "Hello, World!" );
  p = s1.c_str( );
  if( std::strcmp( p, "Hello, World!" ) != 0 ) FAIL

  std::string s2;
  p = s2.c_str( );
  if ( std::strcmp( p, "" ) != 0 ) FAIL

  return( rc );
}


bool find_test( )
{
  bool rc = true;

  const std::string s1( "Hello, World" );
  if( s1.find( "Hello",  0 ) != 0                 ) FAIL
  if( s1.find( "Hello",  1 ) != std::string::npos ) FAIL
  if( s1.find( "World",  0 ) != 7                 ) FAIL
  if( s1.find( "Hello", 11 ) != std::string::npos ) FAIL
  if( s1.find( "Hello", 12 ) != std::string::npos ) FAIL
  if( s1.find( "Hello", 13 ) != std::string::npos ) FAIL
  if( s1.find( "",      12 ) != 12                ) FAIL

  return( rc );
}


bool rfind_test( )
{
  bool rc = true;

  const std::string s1( "Hello, World" );
  if( s1.rfind( "World" ) != 7 ) FAIL
  if( s1.rfind( "Hello" ) != 0 ) FAIL
  if( s1.rfind( "ell", 7 ) != 1 ) FAIL
  if( s1.rfind( "Fizzle" ) != std::string::npos ) FAIL
  if( s1.rfind( "Hello, World..." ) != std::string::npos ) FAIL

  return( rc );
}


bool find_first_of_test()
{
  bool rc = true;

  const std::string s1( "Hello, World!" );
  if( s1.find_first_of( "eoW" )    !=  1 ) FAIL
  if( s1.find_first_of( "eoW", 1 ) !=  1 ) FAIL
  if( s1.find_first_of( "eoW", 2 ) !=  4 ) FAIL
  if( s1.find_first_of( "!" )      != 12 ) FAIL
  if( s1.find_first_of( "!", 13 )  != std::string::npos ) FAIL
  if( s1.find_first_of( "z#+" )    != std::string::npos ) FAIL

  return( rc );
}


bool find_last_of_test()
{
  bool rc = true;

  const std::string s1( "Hello, World!" );
  if( s1.find_last_of( "eoW" )    !=  8 ) FAIL
  if( s1.find_last_of( "eoW", 1 ) !=  1 ) FAIL
  if( s1.find_last_of( "oWe", 2 ) !=  1 ) FAIL
  if( s1.find_last_of( "!" )      != 12 ) FAIL
  if( s1.find_last_of( "!", 13 )  != 12 ) FAIL
  if( s1.find_last_of( "z#+" )    != std::string::npos ) FAIL

  return( rc );
}


bool find_first_not_of_test()
{
  bool rc = true;

  const std::string good_chars("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  const std::string s1("xAWQKSMQIFJJWXGV"); // Be sure both 'A' and 'Z'.
  const std::string s2("KFNNAWEZxZNEKGDW");
  const std::string s3("QMVVXZKRIGJJWTAx");
  const std::string s4("UUDHHAKVVLZVFLQP");
  const std::string s5("");

  if( s1.find_first_not_of( good_chars )     !=  0 ) FAIL
  if( s2.find_first_not_of( good_chars )     !=  8 ) FAIL
  if( s3.find_first_not_of( good_chars )     != 15 ) FAIL
  if( s4.find_first_not_of( good_chars )     != std::string::npos ) FAIL
  if( s2.find_first_not_of( good_chars,  8 ) !=  8 ) FAIL
  if( s3.find_first_not_of( good_chars, 15 ) != 15 ) FAIL
  if( s3.find_first_not_of( good_chars, 16 ) != std::string::npos ) FAIL
  if( s3.find_first_not_of( good_chars, 17 ) != std::string::npos ) FAIL
  if( s5.find_first_not_of( good_chars )     != std::string::npos ) FAIL
  if( s4.find_first_not_of( s5 )             != 0 ) FAIL

  return( rc );
}


bool find_last_not_of_test()
{
  bool rc = true;

  const std::string good_chars("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  const std::string s1("xAWQKSMQIFJJWXGV");  // Be sure both 'A' and 'Z'.
  const std::string s2("KFNNAWEZxZNEKGDW");
  const std::string s3("QMVVXZKRIGJJWTAx");
  const std::string s4("UUDHHAKVVLZVFLQP");
  const std::string s5("");

  if( s1.find_last_not_of( good_chars )     !=  0 ) FAIL
  if( s2.find_last_not_of( good_chars )     !=  8 ) FAIL
  if( s3.find_last_not_of( good_chars )     != 15 ) FAIL
  if( s4.find_last_not_of( good_chars )     != std::string::npos ) FAIL
  if( s2.find_last_not_of( good_chars,  8 ) !=  8 ) FAIL
  if( s3.find_last_not_of( good_chars, 15 ) != 15 ) FAIL
  if( s3.find_last_not_of( good_chars, 16 ) != 15 ) FAIL
  if( s3.find_last_not_of( good_chars, 17 ) != 15 ) FAIL
  if( s5.find_last_not_of( good_chars )     != std::string::npos ) FAIL
  if( s4.find_last_not_of( s5 )             != 15 ) FAIL

  return( rc );
}


bool substr_test()
{
  bool rc = true;

  const std::string s1("Hello, World!");

  if( s1.substr( )      != "Hello, World!" ) FAIL
  if( s1.substr( 1 )    != "ello, World!"  ) FAIL
  if( s1.substr( 0, 1 ) != "H"             ) FAIL
  if( s1.substr( 0, 0 ) != ""              ) FAIL
  if( s1.substr( 12 )   != "!"             ) FAIL
  if( s1.substr( 3, 4 ) != "lo, "          ) FAIL
  if( s1.substr( 13 )   != ""              ) FAIL
  try {
    if( s1.substr( 14 ) != "" ) FAIL
  }
  catch( std::out_of_range ) {
    // Okay.
  }
  return( rc );
}


// Main Program
// ============

int main( )
{
  int rc = 0;
  int original_count = heap_count( );

  try {
    if( !construct_test( )         || !heap_ok( "t01" ) ) rc = 1;
    if( !assign_test( )            || !heap_ok( "t02" ) ) rc = 1;
    if( !access_test( )            || !heap_ok( "t03" ) ) rc = 1;
    if( !relational_test( )        || !heap_ok( "t04" ) ) rc = 1;
    if( !capacity_test( )          || !heap_ok( "t05" ) ) rc = 1;
    if( !iterator_test( )          || !heap_ok( "t06" ) ) rc = 1;
    if( !append_test( )            || !heap_ok( "t07" ) ) rc = 1;
    if( !insert_test( )            || !heap_ok( "t08" ) ) rc = 1;
    if( !erase_test( )             || !heap_ok( "t09" ) ) rc = 1;
    if( !replace_test( )           || !heap_ok( "t10" ) ) rc = 1;
    if( !iterator_replace_test( )  || !heap_ok( "t11" ) ) rc = 1;
    if( !copy_test( )              || !heap_ok( "t12" ) ) rc = 1;
    if( !swap_test( )              || !heap_ok( "t13" ) ) rc = 1;
    if( !cstr_test( )              || !heap_ok( "t14" ) ) rc = 1;
    if( !find_test( )              || !heap_ok( "t15" ) ) rc = 1;
    if( !rfind_test( )             || !heap_ok( "t16" ) ) rc = 1;
    if( !find_first_of_test( )     || !heap_ok( "t17" ) ) rc = 1;
    if( !find_last_of_test( )      || !heap_ok( "t18" ) ) rc = 1;
    if( !find_first_not_of_test( ) || !heap_ok( "t19" ) ) rc = 1;
    if( !find_last_not_of_test( )  || !heap_ok( "t20" ) ) rc = 1;
    if( !substr_test( )            || !heap_ok( "t21" ) ) rc = 1;
  }
  catch( std::out_of_range e ) {
    std::cout << "Unexpected out_of_range exception: " << e.what( ) << "\n";
    rc = 1;
  }
  catch( std::length_error e ) {
    std::cout << "Unexpected length_error exception: " << e.what( ) << "\n";
    rc = 1;
  }
  catch( ... ) {
    std::cout << "Unexpected exception of unexpected type.\n";
    rc = 1;
  }

  if( heap_count( ) != original_count ) {
    std::cout << "Possible memory leak!\n";
    rc = 1;
  }
  return( rc );
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?