starts_with.ptr.pass.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
  9. // <string_view>
  10. // constexpr bool starts_with(string_view x) const noexcept;
  11. #include <string_view>
  12. #include <cassert>
  13. #include "test_macros.h"
  14. #include "constexpr_char_traits.h"
  15. int main(int, char**)
  16. {
  17. {
  18. typedef std::string_view SV;
  19. const char *s = "abcde";
  20. SV sv0 {};
  21. SV sv1 { s, 1 };
  22. SV sv2 { s, 2 };
  23. // SV sv3 { s, 3 };
  24. // SV sv4 { s, 4 };
  25. // SV sv5 { s, 5 };
  26. SV svNot {"def", 3 };
  27. LIBCPP_ASSERT_NOEXCEPT(sv0.starts_with(""));
  28. assert ( sv0.starts_with(""));
  29. assert (!sv0.starts_with("a"));
  30. assert ( sv1.starts_with(""));
  31. assert ( sv1.starts_with("a"));
  32. assert (!sv1.starts_with("ab"));
  33. assert (!sv1.starts_with("abc"));
  34. assert (!sv1.starts_with("abcd"));
  35. assert (!sv1.starts_with("abcde"));
  36. assert (!sv1.starts_with("def"));
  37. assert ( sv2.starts_with(s + 5));
  38. assert ( sv2.starts_with("a"));
  39. assert ( sv2.starts_with("ab"));
  40. assert (!sv2.starts_with("abc"));
  41. assert (!sv2.starts_with("abcd"));
  42. assert (!sv2.starts_with("abcde"));
  43. assert (!sv2.starts_with("def"));
  44. assert ( svNot.starts_with(""));
  45. assert (!svNot.starts_with("a"));
  46. assert (!svNot.starts_with("ab"));
  47. assert (!svNot.starts_with("abc"));
  48. assert (!svNot.starts_with("abcd"));
  49. assert (!svNot.starts_with("abcde"));
  50. assert ( svNot.starts_with("def"));
  51. }
  52. #if TEST_STD_VER > 11
  53. {
  54. typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
  55. constexpr const char *s = "abcde";
  56. constexpr SV sv0 {};
  57. constexpr SV sv1 { s, 1 };
  58. constexpr SV sv2 { s, 2 };
  59. // constexpr SV sv3 { s, 3 };
  60. // constexpr SV sv4 { s, 4 };
  61. // constexpr SV sv5 { s, 5 };
  62. constexpr SV svNot {"def", 3 };
  63. static_assert ( sv0.starts_with(""), "" );
  64. static_assert (!sv0.starts_with("a"), "" );
  65. static_assert ( sv1.starts_with(""), "" );
  66. static_assert ( sv1.starts_with("a"), "" );
  67. static_assert (!sv1.starts_with("ab"), "" );
  68. static_assert (!sv1.starts_with("abc"), "" );
  69. static_assert (!sv1.starts_with("abcd"), "" );
  70. static_assert (!sv1.starts_with("abcde"), "" );
  71. static_assert (!sv1.starts_with("def"), "" );
  72. static_assert ( sv2.starts_with(s + 5), "" );
  73. static_assert ( sv2.starts_with("a"), "" );
  74. static_assert ( sv2.starts_with("ab"), "" );
  75. static_assert (!sv2.starts_with("abc"), "" );
  76. static_assert (!sv2.starts_with("abcd"), "" );
  77. static_assert (!sv2.starts_with("abcde"), "" );
  78. static_assert (!sv2.starts_with("def"), "" );
  79. static_assert ( svNot.starts_with(""), "" );
  80. static_assert (!svNot.starts_with("a"), "" );
  81. static_assert (!svNot.starts_with("ab"), "" );
  82. static_assert (!svNot.starts_with("abc"), "" );
  83. static_assert (!svNot.starts_with("abcd"), "" );
  84. static_assert (!svNot.starts_with("abcde"), "" );
  85. static_assert ( svNot.starts_with("def"), "" );
  86. }
  87. #endif
  88. return 0;
  89. }