from_literal.pass.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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)
  10. // : __data (_s), __size(_Traits::length(_s)) {}
  11. #include <string_view>
  12. #include <string>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "constexpr_char_traits.h"
  16. template<typename CharT>
  17. size_t StrLen ( const CharT *s ) {
  18. size_t retVal = 0;
  19. while ( *s != 0 ) { ++retVal; ++s; }
  20. return retVal;
  21. }
  22. template<typename CharT>
  23. void test ( const CharT *s ) {
  24. typedef std::basic_string_view<CharT> SV;
  25. // I'd love to do this, but it would require traits::length() to be noexcept
  26. // LIBCPP_ASSERT_NOEXCEPT(SV(s));
  27. SV sv1 ( s );
  28. assert ( sv1.size() == StrLen( s ));
  29. assert ( sv1.data() == s );
  30. }
  31. int main(int, char**) {
  32. test ( "QBCDE" );
  33. test ( "A" );
  34. test ( "" );
  35. test ( L"QBCDE" );
  36. test ( L"A" );
  37. test ( L"" );
  38. #if TEST_STD_VER >= 11
  39. test ( u"QBCDE" );
  40. test ( u"A" );
  41. test ( u"" );
  42. test ( U"QBCDE" );
  43. test ( U"A" );
  44. test ( U"" );
  45. #endif
  46. #if TEST_STD_VER > 11
  47. {
  48. constexpr std::basic_string_view<char, constexpr_char_traits<char>> sv1 ( "ABCDE" );
  49. static_assert ( sv1.size() == 5, "");
  50. }
  51. #endif
  52. return 0;
  53. }