find_char_size.pass.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 find(charT c, size_type pos = 0) 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.find(c, pos) == x);
  20. if (x != S::npos)
  21. assert(pos <= x && 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.find(c) == x);
  28. if (x != S::npos)
  29. assert(0 <= x && x + 1 <= s.size());
  30. }
  31. int main(int, char**)
  32. {
  33. {
  34. typedef std::string_view S;
  35. test(S(""), 'c', 0, S::npos);
  36. test(S(""), 'c', 1, S::npos);
  37. test(S("abcde"), 'c', 0, 2);
  38. test(S("abcde"), 'c', 1, 2);
  39. test(S("abcde"), 'c', 2, 2);
  40. test(S("abcde"), 'c', 4, S::npos);
  41. test(S("abcde"), 'c', 5, S::npos);
  42. test(S("abcde"), 'c', 6, S::npos);
  43. test(S("abcdeabcde"), 'c', 0, 2);
  44. test(S("abcdeabcde"), 'c', 1, 2);
  45. test(S("abcdeabcde"), 'c', 5, 7);
  46. test(S("abcdeabcde"), 'c', 9, S::npos);
  47. test(S("abcdeabcde"), 'c', 10, S::npos);
  48. test(S("abcdeabcde"), 'c', 11, S::npos);
  49. test(S("abcdeabcdeabcdeabcde"), 'c', 0, 2);
  50. test(S("abcdeabcdeabcdeabcde"), 'c', 1, 2);
  51. test(S("abcdeabcdeabcdeabcde"), 'c', 10, 12);
  52. test(S("abcdeabcdeabcdeabcde"), 'c', 19, S::npos);
  53. test(S("abcdeabcdeabcdeabcde"), 'c', 20, S::npos);
  54. test(S("abcdeabcdeabcdeabcde"), 'c', 21, S::npos);
  55. test(S(""), 'c', S::npos);
  56. test(S("abcde"), 'c', 2);
  57. test(S("abcdeabcde"), 'c', 2);
  58. test(S("abcdeabcdeabcdeabcde"), 'c', 2);
  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.find( 'c', 0 ) == SV::npos, "" );
  66. static_assert (sv1.find( 'c', 1 ) == SV::npos, "" );
  67. static_assert (sv2.find( 'c', 0 ) == 2, "" );
  68. static_assert (sv2.find( 'c', 1 ) == 2, "" );
  69. static_assert (sv2.find( 'c', 2 ) == 2, "" );
  70. static_assert (sv2.find( 'c', 3 ) == SV::npos, "" );
  71. static_assert (sv2.find( 'c', 4 ) == SV::npos, "" );
  72. }
  73. #endif
  74. return 0;
  75. }