const_string_test.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // (C) Copyright Gennadiy Rozental 2001-2014.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile: const_string_test.cpp,v $
  8. //
  9. // Version : $Revision$
  10. //
  11. // Description : simple string class test
  12. // ***************************************************************************
  13. #define BOOST_TEST_MODULE const_string test
  14. #include <boost/test/included/unit_test.hpp>
  15. #include "const_string.hpp"
  16. using common_layer::const_string;
  17. BOOST_AUTO_TEST_CASE( constructors_test )
  18. {
  19. const_string cs0( "" );
  20. BOOST_CHECK_EQUAL( cs0.length(), (size_t)0 );
  21. BOOST_CHECK_EQUAL( cs0.begin(), "" );
  22. BOOST_CHECK_EQUAL( cs0.end(), "" );
  23. BOOST_CHECK( cs0.is_empty() );
  24. const_string cs01( NULL );
  25. BOOST_CHECK_EQUAL( cs01.length(), (size_t)0 );
  26. BOOST_CHECK_EQUAL( cs01.begin(), "" );
  27. BOOST_CHECK_EQUAL( cs01.end(), "" );
  28. BOOST_CHECK( cs01.is_empty() );
  29. const_string cs1( "test_string" );
  30. BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
  31. BOOST_CHECK_EQUAL( cs1.length(), std::strlen("test_string") );
  32. std::string s( "test_string" );
  33. const_string cs2( s );
  34. BOOST_CHECK_EQUAL( std::strcmp( cs2.data(), "test_string" ), 0 );
  35. const_string cs3( cs1 );
  36. BOOST_CHECK_EQUAL( std::strcmp( cs3.data(), "test_string" ), 0 );
  37. const_string cs4( "test_string", 4 );
  38. BOOST_CHECK_EQUAL( std::strncmp( cs4.data(), "test", cs4.length() ), 0 );
  39. const_string cs5( s.data(), s.data() + s.length() );
  40. BOOST_CHECK_EQUAL( std::strncmp( cs5.data(), "test_string", cs5.length() ), 0 );
  41. const_string cs_array[] = { "str1", "str2" };
  42. BOOST_CHECK_EQUAL( cs_array[0], "str1" );
  43. BOOST_CHECK_EQUAL( cs_array[1], "str2" );
  44. }
  45. BOOST_AUTO_TEST_CASE( data_access_test )
  46. {
  47. const_string cs1( "test_string" );
  48. BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
  49. BOOST_CHECK_EQUAL( cs1[(size_t)0], 't' );
  50. BOOST_CHECK_EQUAL( cs1[(size_t)4], '_' );
  51. BOOST_CHECK_EQUAL( cs1[cs1.length()-1], 'g' );
  52. BOOST_CHECK_EQUAL( cs1[(size_t)0], cs1.at( 0 ) );
  53. BOOST_CHECK_EQUAL( cs1[(size_t)2], cs1.at( 5 ) );
  54. BOOST_CHECK_EQUAL( cs1.at( cs1.length() - 1 ), 'g' );
  55. BOOST_CHECK_EQUAL( common_layer::first_char()( cs1 ), 't' );
  56. BOOST_CHECK_EQUAL( common_layer::last_char()( cs1 ) , 'g' );
  57. }
  58. BOOST_AUTO_TEST_CASE( length_test )
  59. {
  60. const_string cs1;
  61. BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
  62. BOOST_CHECK( cs1.is_empty() );
  63. cs1 = "";
  64. BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
  65. BOOST_CHECK( cs1.is_empty() );
  66. cs1 = "test_string";
  67. BOOST_CHECK_EQUAL( cs1.length(), (size_t)11 );
  68. cs1.erase();
  69. BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
  70. BOOST_CHECK_EQUAL( cs1.data(), "" );
  71. cs1 = const_string( "test_string", 4 );
  72. BOOST_CHECK_EQUAL( cs1.length(), (size_t)4 );
  73. cs1.resize( 5 );
  74. BOOST_CHECK_EQUAL( cs1.length(), (size_t)4 );
  75. cs1.resize( 3 );
  76. BOOST_CHECK_EQUAL( cs1.length(), (size_t)3 );
  77. cs1.rshorten();
  78. BOOST_CHECK_EQUAL( cs1.length(), (size_t)2 );
  79. BOOST_CHECK_EQUAL( cs1[(size_t)0], 't' );
  80. cs1.lshorten();
  81. BOOST_CHECK_EQUAL( cs1.length(), (size_t)1 );
  82. BOOST_CHECK_EQUAL( cs1[(size_t)0], 'e' );
  83. cs1.lshorten();
  84. BOOST_CHECK( cs1.is_empty() );
  85. BOOST_CHECK_EQUAL( cs1.data(), "" );
  86. cs1 = "test_string";
  87. cs1.lshorten( 11 );
  88. BOOST_CHECK( cs1.is_empty() );
  89. BOOST_CHECK_EQUAL( cs1.data(), "" );
  90. }
  91. BOOST_AUTO_TEST_CASE( asignment_test )
  92. {
  93. const_string cs1;
  94. std::string s( "test_string" );
  95. cs1 = "test";
  96. BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test" ), 0 );
  97. cs1 = s;
  98. BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
  99. cs1.assign( "test" );
  100. BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test" ), 0 );
  101. const_string cs2( "test_string" );
  102. cs1.swap( cs2 );
  103. BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
  104. BOOST_CHECK_EQUAL( std::strcmp( cs2.data(), "test" ), 0 );
  105. }
  106. BOOST_AUTO_TEST_CASE( comparison_test )
  107. {
  108. const_string cs1( "test_string" );
  109. const_string cs2( "test_string" );
  110. std::string s( "test_string" );
  111. BOOST_CHECK_EQUAL( cs1, "test_string" );
  112. BOOST_CHECK_EQUAL( "test_string", cs1 );
  113. BOOST_CHECK_EQUAL( cs1, cs2 );
  114. BOOST_CHECK_EQUAL( cs1, s );
  115. BOOST_CHECK_EQUAL( s , cs1 );
  116. cs1.resize( 4 );
  117. BOOST_CHECK( cs1 != "test_string" );
  118. BOOST_CHECK( "test_string" != cs1 );
  119. BOOST_CHECK( cs1 != cs2 );
  120. BOOST_CHECK( cs1 != s );
  121. BOOST_CHECK( s != cs1 );
  122. BOOST_CHECK_EQUAL( cs1, "test" );
  123. }
  124. BOOST_AUTO_TEST_CASE( iterators_test )
  125. {
  126. const_string cs1( "test_string" );
  127. std::string s;
  128. std::copy( cs1.begin(), cs1.end(), std::back_inserter( s ) );
  129. BOOST_CHECK_EQUAL( cs1, s );
  130. s.erase();
  131. std::copy( cs1.rbegin(), cs1.rend(), std::back_inserter( s ) );
  132. BOOST_CHECK_EQUAL( const_string( s ), "gnirts_tset" );
  133. }
  134. // EOF