string_ref_test1.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_ref.hpp>
  11. #include <boost/core/lightweight_test.hpp>
  12. typedef boost::string_ref string_ref;
  13. // Should be equal
  14. void interop ( const std::string &str, string_ref ref ) {
  15. // BOOST_TEST ( str == ref );
  16. BOOST_TEST ( str.size () == ref.size ());
  17. BOOST_TEST ( std::equal ( str.begin (), str.end (), ref.begin ()));
  18. BOOST_TEST ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ()));
  19. }
  20. void null_tests ( const char *p ) {
  21. // All zero-length string-refs should be equal
  22. string_ref sr1; // NULL, 0
  23. string_ref sr2 ( NULL, 0 );
  24. string_ref sr3 ( p, 0 );
  25. string_ref sr4 ( p );
  26. sr4.clear ();
  27. BOOST_TEST ( sr1 == sr2 );
  28. BOOST_TEST ( sr1 == sr3 );
  29. BOOST_TEST ( sr2 == sr3 );
  30. BOOST_TEST ( sr1 == sr4 );
  31. }
  32. // make sure that substrings work just like strings
  33. void test_substr ( const std::string &str ) {
  34. const size_t sz = str.size ();
  35. string_ref ref ( str );
  36. // Substrings at the end
  37. for ( size_t i = 0; i <= sz; ++ i )
  38. interop ( str.substr ( i ), ref.substr ( i ));
  39. // Substrings at the beginning
  40. for ( size_t i = 0; i <= sz; ++ i )
  41. interop ( str.substr ( 0, i ), ref.substr ( 0, i ));
  42. // All possible substrings
  43. for ( size_t i = 0; i < sz; ++i )
  44. for ( size_t j = i; j < sz; ++j )
  45. interop ( str.substr ( i, j ), ref.substr ( i, j ));
  46. }
  47. // make sure that removing prefixes and suffixes work just like strings
  48. void test_remove ( const std::string &str ) {
  49. const size_t sz = str.size ();
  50. std::string work;
  51. string_ref ref;
  52. for ( size_t i = 1; i <= sz; ++i ) {
  53. work = str;
  54. ref = str;
  55. while ( ref.size () >= i ) {
  56. interop ( work, ref );
  57. work.erase ( 0, i );
  58. ref.remove_prefix (i);
  59. }
  60. }
  61. for ( size_t i = 1; i < sz; ++ i ) {
  62. work = str;
  63. ref = str;
  64. while ( ref.size () >= i ) {
  65. interop ( work, ref );
  66. work.erase ( work.size () - i, i );
  67. ref.remove_suffix (i);
  68. }
  69. }
  70. }
  71. const char *test_strings [] = {
  72. "",
  73. "1",
  74. "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
  75. "0123456789",
  76. NULL
  77. };
  78. int main()
  79. {
  80. const char **p = &test_strings[0];
  81. while ( *p != NULL ) {
  82. interop ( *p, *p );
  83. test_substr ( *p );
  84. test_remove ( *p );
  85. null_tests ( *p );
  86. p++;
  87. }
  88. return boost::report_errors();
  89. }