string_view_test1.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright (c) Marshall Clow 2012-2012.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. For more information, see http://www.boost.org
  6. */
  7. #include <iostream>
  8. #include <algorithm>
  9. #include <string>
  10. #include <boost/utility/string_view.hpp>
  11. #include <boost/container_hash/hash.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. typedef boost::string_view string_view;
  14. // Should be equal
  15. void interop ( const std::string &str, string_view ref ) {
  16. // BOOST_TEST ( str == ref );
  17. BOOST_TEST ( str.size () == ref.size ());
  18. BOOST_TEST ( std::equal ( str.begin (), str.end (), ref.begin ()));
  19. BOOST_TEST ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ()));
  20. }
  21. void null_tests ( const char *p ) {
  22. // All zero-length string-refs should be equal
  23. string_view sr1; // NULL, 0
  24. string_view sr2 ( NULL, 0 );
  25. string_view sr3 ( p, 0 );
  26. string_view sr4 ( p );
  27. sr4.clear ();
  28. BOOST_TEST ( sr1 == sr2 );
  29. BOOST_TEST ( sr1 == sr3 );
  30. BOOST_TEST ( sr2 == sr3 );
  31. BOOST_TEST ( sr1 == sr4 );
  32. }
  33. // make sure that substrings work just like strings
  34. void test_substr ( const std::string &str ) {
  35. const size_t sz = str.size ();
  36. string_view ref ( str );
  37. // Substrings at the end
  38. for ( size_t i = 0; i <= sz; ++ i )
  39. interop ( str.substr ( i ), ref.substr ( i ));
  40. // Substrings at the beginning
  41. for ( size_t i = 0; i <= sz; ++ i )
  42. interop ( str.substr ( 0, i ), ref.substr ( 0, i ));
  43. // All possible substrings
  44. for ( size_t i = 0; i < sz; ++i )
  45. for ( size_t j = i; j < sz; ++j )
  46. interop ( str.substr ( i, j ), ref.substr ( i, j ));
  47. }
  48. // make sure that removing prefixes and suffixes work just like strings
  49. void test_remove ( const std::string &str ) {
  50. const size_t sz = str.size ();
  51. std::string work;
  52. string_view ref;
  53. for ( size_t i = 1; i <= sz; ++i ) {
  54. work = str;
  55. ref = str;
  56. while ( ref.size () >= i ) {
  57. interop ( work, ref );
  58. work.erase ( 0, i );
  59. ref.remove_prefix (i);
  60. }
  61. }
  62. for ( size_t i = 1; i < sz; ++ i ) {
  63. work = str;
  64. ref = str;
  65. while ( ref.size () >= i ) {
  66. interop ( work, ref );
  67. work.erase ( work.size () - i, i );
  68. ref.remove_suffix (i);
  69. }
  70. }
  71. }
  72. void test_hash(const std::string& str) {
  73. string_view ref = str;
  74. BOOST_TEST(boost::hash_value(ref) == boost::hash_value(str));
  75. boost::hash<std::string> hstr;
  76. boost::hash<string_view> hsv;
  77. BOOST_TEST(hsv(ref) == hstr(str));
  78. }
  79. const char *test_strings [] = {
  80. "",
  81. "1",
  82. "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
  83. "0123456789",
  84. NULL
  85. };
  86. int main()
  87. {
  88. const char **p = &test_strings[0];
  89. while ( *p != NULL ) {
  90. interop ( *p, *p );
  91. test_substr ( *p );
  92. test_remove ( *p );
  93. null_tests ( *p );
  94. test_hash( *p );
  95. p++;
  96. }
  97. return boost::report_errors();
  98. }