move_alloc.pass.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // UNSUPPORTED: c++98, c++03
  10. // <string>
  11. // basic_string(basic_string&& str, const Allocator& alloc);
  12. #include <string>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "test_allocator.h"
  16. #include "min_allocator.h"
  17. template <class S>
  18. void
  19. test(S s0, const typename S::allocator_type& a)
  20. {
  21. S s1 = s0;
  22. S s2(std::move(s0), a);
  23. LIBCPP_ASSERT(s2.__invariants());
  24. LIBCPP_ASSERT(s0.__invariants());
  25. assert(s2 == s1);
  26. assert(s2.capacity() >= s2.size());
  27. assert(s2.get_allocator() == a);
  28. }
  29. int main()
  30. {
  31. {
  32. typedef test_allocator<char> A;
  33. typedef std::basic_string<char, std::char_traits<char>, A> S;
  34. #if TEST_STD_VER > 14
  35. static_assert((noexcept(S{})), "" );
  36. #elif TEST_STD_VER >= 11
  37. static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
  38. #endif
  39. test(S(), A(3));
  40. test(S("1"), A(5));
  41. test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7));
  42. }
  43. int alloc_count = test_alloc_base::alloc_count;
  44. {
  45. typedef test_allocator<char> A;
  46. typedef std::basic_string<char, std::char_traits<char>, A> S;
  47. #if TEST_STD_VER > 14
  48. static_assert((noexcept(S{})), "" );
  49. #elif TEST_STD_VER >= 11
  50. static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
  51. #endif
  52. S s1 ( "Twas brillig, and the slivy toves did gyre and gymbal in the wabe" );
  53. S s2 (std::move(s1), A(1));
  54. }
  55. assert ( test_alloc_base::alloc_count == alloc_count );
  56. {
  57. typedef min_allocator<char> A;
  58. typedef std::basic_string<char, std::char_traits<char>, A> S;
  59. #if TEST_STD_VER > 14
  60. static_assert((noexcept(S{})), "" );
  61. #elif TEST_STD_VER >= 11
  62. static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
  63. #endif
  64. test(S(), A());
  65. test(S("1"), A());
  66. test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());
  67. }
  68. }