move_alloc.pass.cpp 2.5 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. // basic_string(basic_string&& str, const Allocator& alloc);
  11. #include <string>
  12. #include <cassert>
  13. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  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. assert(s2.__invariants());
  24. assert(s0.__invariants());
  25. assert(s2 == s1);
  26. assert(s2.capacity() >= s2.size());
  27. assert(s2.get_allocator() == a);
  28. }
  29. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  30. // #if _LIBCPP_STD_VER <= 14
  31. // _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
  32. // #else
  33. // _NOEXCEPT;
  34. // #endif
  35. int main()
  36. {
  37. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  38. {
  39. typedef test_allocator<char> A;
  40. typedef std::basic_string<char, std::char_traits<char>, A> S;
  41. #if TEST_STD_VER > 14
  42. static_assert((noexcept(S{})), "" );
  43. #elif TEST_STD_VER >= 11
  44. static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
  45. #endif
  46. test(S(), A(3));
  47. test(S("1"), A(5));
  48. test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7));
  49. }
  50. int alloc_count = test_alloc_base::alloc_count;
  51. {
  52. typedef test_allocator<char> A;
  53. typedef std::basic_string<char, std::char_traits<char>, A> S;
  54. #if TEST_STD_VER > 14
  55. static_assert((noexcept(S{})), "" );
  56. #elif TEST_STD_VER >= 11
  57. static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
  58. #endif
  59. S s1 ( "Twas brillig, and the slivy toves did gyre and gymbal in the wabe" );
  60. S s2 (std::move(s1), A(1));
  61. }
  62. assert ( test_alloc_base::alloc_count == alloc_count );
  63. #if TEST_STD_VER >= 11
  64. {
  65. typedef min_allocator<char> A;
  66. typedef std::basic_string<char, std::char_traits<char>, A> S;
  67. #if TEST_STD_VER > 14
  68. static_assert((noexcept(S{})), "" );
  69. #elif TEST_STD_VER >= 11
  70. static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
  71. #endif
  72. test(S(), A());
  73. test(S("1"), A());
  74. test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());
  75. }
  76. #endif
  77. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  78. }