move_alloc.pass.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_allocator.h"
  15. #include "min_allocator.h"
  16. template <class S>
  17. void
  18. test(S s0, const typename S::allocator_type& a)
  19. {
  20. S s1 = s0;
  21. S s2(std::move(s0), a);
  22. assert(s2.__invariants());
  23. assert(s0.__invariants());
  24. assert(s2 == s1);
  25. assert(s2.capacity() >= s2.size());
  26. assert(s2.get_allocator() == a);
  27. }
  28. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  29. int main()
  30. {
  31. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  32. {
  33. typedef test_allocator<char> A;
  34. typedef std::basic_string<char, std::char_traits<char>, A> S;
  35. test(S(), A(3));
  36. test(S("1"), A(5));
  37. test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7));
  38. }
  39. int alloc_count = test_alloc_base::alloc_count;
  40. {
  41. typedef test_allocator<char> A;
  42. typedef std::basic_string<char, std::char_traits<char>, A> S;
  43. S s1 ( "Twas brillig, and the slivy toves did gyre and gymbal in the wabe" );
  44. S s2 (std::move(s1), A(1));
  45. }
  46. assert ( test_alloc_base::alloc_count == alloc_count );
  47. #if __cplusplus >= 201103L
  48. {
  49. typedef min_allocator<char> A;
  50. typedef std::basic_string<char, std::char_traits<char>, A> S;
  51. test(S(), A());
  52. test(S("1"), A());
  53. test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());
  54. }
  55. #endif
  56. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  57. }