string.pass.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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(const basic_string<charT,traits>& str);
  12. #include <string>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "min_allocator.h"
  16. #include "test_allocator.h"
  17. template <class S>
  18. void
  19. test(S s, S str, S expected)
  20. {
  21. s.assign(str);
  22. LIBCPP_ASSERT(s.__invariants());
  23. assert(s == expected);
  24. }
  25. template <class S>
  26. void
  27. testAlloc(S s, S str, const typename S::allocator_type& a)
  28. {
  29. s.assign(str);
  30. LIBCPP_ASSERT(s.__invariants());
  31. assert(s == str);
  32. assert(s.get_allocator() == a);
  33. }
  34. int main()
  35. {
  36. {
  37. typedef std::string S;
  38. test(S(), S(), S());
  39. test(S(), S("12345"), S("12345"));
  40. test(S(), S("1234567890"), S("1234567890"));
  41. test(S(), S("12345678901234567890"), S("12345678901234567890"));
  42. test(S("12345"), S(), S());
  43. test(S("12345"), S("12345"), S("12345"));
  44. test(S("12345"), S("1234567890"), S("1234567890"));
  45. test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
  46. test(S("1234567890"), S(), S());
  47. test(S("1234567890"), S("12345"), S("12345"));
  48. test(S("1234567890"), S("1234567890"), S("1234567890"));
  49. test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
  50. test(S("12345678901234567890"), S(), S());
  51. test(S("12345678901234567890"), S("12345"), S("12345"));
  52. test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
  53. test(S("12345678901234567890"), S("12345678901234567890"),
  54. S("12345678901234567890"));
  55. testAlloc(S(), S(), std::allocator<char>());
  56. testAlloc(S(), S("12345"), std::allocator<char>());
  57. testAlloc(S(), S("1234567890"), std::allocator<char>());
  58. testAlloc(S(), S("12345678901234567890"), std::allocator<char>());
  59. }
  60. { // LWG#5579 make sure assign takes the allocators where appropriate
  61. typedef other_allocator<char> A; // has POCCA --> true
  62. typedef std::basic_string<char, std::char_traits<char>, A> S;
  63. testAlloc(S(A(5)), S(A(3)), A(3));
  64. testAlloc(S(A(5)), S("1"), A());
  65. testAlloc(S(A(5)), S("1", A(7)), A(7));
  66. testAlloc(S(A(5)), S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), A(7));
  67. }
  68. #if TEST_STD_VER >= 11
  69. {
  70. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  71. test(S(), S(), S());
  72. test(S(), S("12345"), S("12345"));
  73. test(S(), S("1234567890"), S("1234567890"));
  74. test(S(), S("12345678901234567890"), S("12345678901234567890"));
  75. test(S("12345"), S(), S());
  76. test(S("12345"), S("12345"), S("12345"));
  77. test(S("12345"), S("1234567890"), S("1234567890"));
  78. test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
  79. test(S("1234567890"), S(), S());
  80. test(S("1234567890"), S("12345"), S("12345"));
  81. test(S("1234567890"), S("1234567890"), S("1234567890"));
  82. test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
  83. test(S("12345678901234567890"), S(), S());
  84. test(S("12345678901234567890"), S("12345"), S("12345"));
  85. test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
  86. test(S("12345678901234567890"), S("12345678901234567890"),
  87. S("12345678901234567890"));
  88. testAlloc(S(), S(), min_allocator<char>());
  89. testAlloc(S(), S("12345"), min_allocator<char>());
  90. testAlloc(S(), S("1234567890"), min_allocator<char>());
  91. testAlloc(S(), S("12345678901234567890"), min_allocator<char>());
  92. }
  93. #endif
  94. #if TEST_STD_VER > 14
  95. {
  96. typedef std::string S;
  97. static_assert(noexcept(S().assign(S())), ""); // LWG#2063
  98. }
  99. #endif
  100. }