string_view.pass.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // basic_string<charT,traits,Allocator>&
  11. // assign(basic_string_view<charT,traits> sv);
  12. #include <string>
  13. #include <string_view>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "min_allocator.h"
  17. #include "test_allocator.h"
  18. template <class S, class SV>
  19. void
  20. test(S s, SV sv, S expected)
  21. {
  22. s.assign(sv);
  23. LIBCPP_ASSERT(s.__invariants());
  24. assert(s == expected);
  25. }
  26. template <class S, class SV>
  27. void
  28. testAlloc(S s, SV sv, const typename S::allocator_type& a)
  29. {
  30. s.assign(sv);
  31. LIBCPP_ASSERT(s.__invariants());
  32. assert(s == sv);
  33. assert(s.get_allocator() == a);
  34. }
  35. int main()
  36. {
  37. {
  38. typedef std::string S;
  39. typedef std::string_view SV;
  40. test(S(), SV(), S());
  41. test(S(), SV("12345"), S("12345"));
  42. test(S(), SV("1234567890"), S("1234567890"));
  43. test(S(), SV("12345678901234567890"), S("12345678901234567890"));
  44. test(S("12345"), SV(), S());
  45. test(S("12345"), SV("12345"), S("12345"));
  46. test(S("12345"), SV("1234567890"), S("1234567890"));
  47. test(S("12345"), SV("12345678901234567890"), S("12345678901234567890"));
  48. test(S("1234567890"), SV(), S());
  49. test(S("1234567890"), SV("12345"), S("12345"));
  50. test(S("1234567890"), SV("1234567890"), S("1234567890"));
  51. test(S("1234567890"), SV("12345678901234567890"), S("12345678901234567890"));
  52. test(S("12345678901234567890"), SV(), S());
  53. test(S("12345678901234567890"), SV("12345"), S("12345"));
  54. test(S("12345678901234567890"), SV("1234567890"), S("1234567890"));
  55. test(S("12345678901234567890"), SV("12345678901234567890"),
  56. S("12345678901234567890"));
  57. testAlloc(S(), SV(), std::allocator<char>());
  58. testAlloc(S(), SV("12345"), std::allocator<char>());
  59. testAlloc(S(), SV("1234567890"), std::allocator<char>());
  60. testAlloc(S(), SV("12345678901234567890"), std::allocator<char>());
  61. }
  62. #if TEST_STD_VER >= 11
  63. {
  64. typedef std::basic_string <char, std::char_traits<char>, min_allocator<char>> S;
  65. typedef std::basic_string_view<char, std::char_traits<char> > SV;
  66. test(S(), SV(), S());
  67. test(S(), SV("12345"), S("12345"));
  68. test(S(), SV("1234567890"), S("1234567890"));
  69. test(S(), SV("12345678901234567890"), S("12345678901234567890"));
  70. test(S("12345"), SV(), S());
  71. test(S("12345"), SV("12345"), S("12345"));
  72. test(S("12345"), SV("1234567890"), S("1234567890"));
  73. test(S("12345"), SV("12345678901234567890"), S("12345678901234567890"));
  74. test(S("1234567890"), SV(), S());
  75. test(S("1234567890"), SV("12345"), S("12345"));
  76. test(S("1234567890"), SV("1234567890"), S("1234567890"));
  77. test(S("1234567890"), SV("12345678901234567890"), S("12345678901234567890"));
  78. test(S("12345678901234567890"), SV(), S());
  79. test(S("12345678901234567890"), SV("12345"), S("12345"));
  80. test(S("12345678901234567890"), SV("1234567890"), S("1234567890"));
  81. test(S("12345678901234567890"), SV("12345678901234567890"),
  82. S("12345678901234567890"));
  83. testAlloc(S(), SV(), min_allocator<char>());
  84. testAlloc(S(), SV("12345"), min_allocator<char>());
  85. testAlloc(S(), SV("1234567890"), min_allocator<char>());
  86. testAlloc(S(), SV("12345678901234567890"), min_allocator<char>());
  87. }
  88. #endif
  89. }