string_view.pass.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <string>
  10. // explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator());
  11. #include <string>
  12. #include <string_view>
  13. #include <stdexcept>
  14. #include <algorithm>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. #include "test_allocator.h"
  18. #include "min_allocator.h"
  19. template <class charT>
  20. void
  21. test(std::basic_string_view<charT> sv)
  22. {
  23. typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
  24. typedef typename S::traits_type T;
  25. typedef typename S::allocator_type A;
  26. S s2(sv);
  27. LIBCPP_ASSERT(s2.__invariants());
  28. assert(s2.size() == sv.size());
  29. assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
  30. assert(s2.get_allocator() == A());
  31. assert(s2.capacity() >= s2.size());
  32. }
  33. template <class charT, class A>
  34. void
  35. test(std::basic_string_view<charT> sv, const A& a)
  36. {
  37. typedef std::basic_string<charT, std::char_traits<charT>, A> S;
  38. typedef typename S::traits_type T;
  39. S s2(sv, a);
  40. LIBCPP_ASSERT(s2.__invariants());
  41. assert(s2.size() == sv.size());
  42. assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
  43. assert(s2.get_allocator() == a);
  44. assert(s2.capacity() >= s2.size());
  45. }
  46. int main()
  47. {
  48. {
  49. typedef test_allocator<char> A;
  50. typedef std::basic_string_view<char, std::char_traits<char> > SV;
  51. test(SV(""));
  52. test(SV(""), A(2));
  53. test(SV("1"));
  54. test(SV("1") ,A(2));
  55. test(SV("1234567980"));
  56. test(SV("1234567980"), A(2));
  57. test(SV("123456798012345679801234567980123456798012345679801234567980"));
  58. test(SV("123456798012345679801234567980123456798012345679801234567980"), A(2));
  59. }
  60. #if TEST_STD_VER >= 11
  61. {
  62. typedef min_allocator<char> A;
  63. typedef std::basic_string_view<char, std::char_traits<char> > SV;
  64. test(SV(""));
  65. test(SV(""), A());
  66. test(SV("1"));
  67. test(SV("1") ,A());
  68. test(SV("1234567980"));
  69. test(SV("1234567980"), A());
  70. test(SV("123456798012345679801234567980123456798012345679801234567980"));
  71. test(SV("123456798012345679801234567980123456798012345679801234567980"), A());
  72. }
  73. #endif
  74. }