rfind_char_size.pass.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <string_view>
  9. // constexpr size_type rfind(charT c, size_type pos = npos) const;
  10. #include <string_view>
  11. #include <cassert>
  12. #include "test_macros.h"
  13. #include "constexpr_char_traits.h"
  14. template <class S>
  15. void
  16. test(const S& s, typename S::value_type c, typename S::size_type pos,
  17. typename S::size_type x)
  18. {
  19. assert(s.rfind(c, pos) == x);
  20. if (x != S::npos)
  21. assert(x <= pos && x + 1 <= s.size());
  22. }
  23. template <class S>
  24. void
  25. test(const S& s, typename S::value_type c, typename S::size_type x)
  26. {
  27. assert(s.rfind(c) == x);
  28. if (x != S::npos)
  29. assert(x + 1 <= s.size());
  30. }
  31. int main(int, char**)
  32. {
  33. {
  34. typedef std::string_view S;
  35. test(S(""), 'b', 0, S::npos);
  36. test(S(""), 'b', 1, S::npos);
  37. test(S("abcde"), 'b', 0, S::npos);
  38. test(S("abcde"), 'b', 1, 1);
  39. test(S("abcde"), 'b', 2, 1);
  40. test(S("abcde"), 'b', 4, 1);
  41. test(S("abcde"), 'b', 5, 1);
  42. test(S("abcde"), 'b', 6, 1);
  43. test(S("abcdeabcde"), 'b', 0, S::npos);
  44. test(S("abcdeabcde"), 'b', 1, 1);
  45. test(S("abcdeabcde"), 'b', 5, 1);
  46. test(S("abcdeabcde"), 'b', 9, 6);
  47. test(S("abcdeabcde"), 'b', 10, 6);
  48. test(S("abcdeabcde"), 'b', 11, 6);
  49. test(S("abcdeabcdeabcdeabcde"), 'b', 0, S::npos);
  50. test(S("abcdeabcdeabcdeabcde"), 'b', 1, 1);
  51. test(S("abcdeabcdeabcdeabcde"), 'b', 10, 6);
  52. test(S("abcdeabcdeabcdeabcde"), 'b', 19, 16);
  53. test(S("abcdeabcdeabcdeabcde"), 'b', 20, 16);
  54. test(S("abcdeabcdeabcdeabcde"), 'b', 21, 16);
  55. test(S(""), 'b', S::npos);
  56. test(S("abcde"), 'b', 1);
  57. test(S("abcdeabcde"), 'b', 6);
  58. test(S("abcdeabcdeabcdeabcde"), 'b', 16);
  59. }
  60. #if TEST_STD_VER > 11
  61. {
  62. typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
  63. constexpr SV sv1;
  64. constexpr SV sv2 { "abcde", 5 };
  65. static_assert (sv1.rfind( 'b', 0 ) == SV::npos, "" );
  66. static_assert (sv1.rfind( 'b', 1 ) == SV::npos, "" );
  67. static_assert (sv2.rfind( 'b', 0 ) == SV::npos, "" );
  68. static_assert (sv2.rfind( 'b', 1 ) == 1, "" );
  69. static_assert (sv2.rfind( 'b', 2 ) == 1, "" );
  70. static_assert (sv2.rfind( 'b', 3 ) == 1, "" );
  71. static_assert (sv2.rfind( 'b', 4 ) == 1, "" );
  72. }
  73. #endif
  74. return 0;
  75. }