from_ptr_len.pass.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 basic_string_view(const _CharT* _s, size_type _len)
  10. // : __data (_s), __size(_len) {}
  11. #include <string_view>
  12. #include <string>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. template<typename CharT>
  16. void test ( const CharT *s, size_t sz ) {
  17. {
  18. typedef std::basic_string_view<CharT> SV;
  19. LIBCPP_ASSERT_NOEXCEPT(SV(s, sz));
  20. SV sv1 ( s, sz );
  21. assert ( sv1.size() == sz );
  22. assert ( sv1.data() == s );
  23. }
  24. }
  25. int main(int, char**) {
  26. test ( "QBCDE", 5 );
  27. test ( "QBCDE", 2 );
  28. test ( "", 0 );
  29. #if TEST_STD_VER > 11
  30. {
  31. constexpr const char *s = "QBCDE";
  32. constexpr std::basic_string_view<char> sv1 ( s, 2 );
  33. static_assert ( sv1.size() == 2, "" );
  34. static_assert ( sv1.data() == s, "" );
  35. }
  36. #endif
  37. test ( L"QBCDE", 5 );
  38. test ( L"QBCDE", 2 );
  39. test ( L"", 0 );
  40. #if TEST_STD_VER > 11
  41. {
  42. constexpr const wchar_t *s = L"QBCDE";
  43. constexpr std::basic_string_view<wchar_t> sv1 ( s, 2 );
  44. static_assert ( sv1.size() == 2, "" );
  45. static_assert ( sv1.data() == s, "" );
  46. }
  47. #endif
  48. #if TEST_STD_VER >= 11
  49. test ( u"QBCDE", 5 );
  50. test ( u"QBCDE", 2 );
  51. test ( u"", 0 );
  52. #if TEST_STD_VER > 11
  53. {
  54. constexpr const char16_t *s = u"QBCDE";
  55. constexpr std::basic_string_view<char16_t> sv1 ( s, 2 );
  56. static_assert ( sv1.size() == 2, "" );
  57. static_assert ( sv1.data() == s, "" );
  58. }
  59. #endif
  60. test ( U"QBCDE", 5 );
  61. test ( U"QBCDE", 2 );
  62. test ( U"", 0 );
  63. #if TEST_STD_VER > 11
  64. {
  65. constexpr const char32_t *s = U"QBCDE";
  66. constexpr std::basic_string_view<char32_t> sv1 ( s, 2 );
  67. static_assert ( sv1.size() == 2, "" );
  68. static_assert ( sv1.data() == s, "" );
  69. }
  70. #endif
  71. #endif
  72. return 0;
  73. }