swap.pass.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // template<class charT, class traits, class Allocator>
  11. // void swap(basic_string<charT,traits,Allocator>& lhs,
  12. // basic_string<charT,traits,Allocator>& rhs);
  13. #include <string>
  14. #include <stdexcept>
  15. #include <algorithm>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "min_allocator.h"
  19. template <class S>
  20. void
  21. test(S s1, S s2)
  22. {
  23. S s1_ = s1;
  24. S s2_ = s2;
  25. swap(s1, s2);
  26. LIBCPP_ASSERT(s1.__invariants());
  27. LIBCPP_ASSERT(s2.__invariants());
  28. assert(s1 == s2_);
  29. assert(s2 == s1_);
  30. }
  31. int main()
  32. {
  33. {
  34. typedef std::string S;
  35. test(S(""), S(""));
  36. test(S(""), S("12345"));
  37. test(S(""), S("1234567890"));
  38. test(S(""), S("12345678901234567890"));
  39. test(S("abcde"), S(""));
  40. test(S("abcde"), S("12345"));
  41. test(S("abcde"), S("1234567890"));
  42. test(S("abcde"), S("12345678901234567890"));
  43. test(S("abcdefghij"), S(""));
  44. test(S("abcdefghij"), S("12345"));
  45. test(S("abcdefghij"), S("1234567890"));
  46. test(S("abcdefghij"), S("12345678901234567890"));
  47. test(S("abcdefghijklmnopqrst"), S(""));
  48. test(S("abcdefghijklmnopqrst"), S("12345"));
  49. test(S("abcdefghijklmnopqrst"), S("1234567890"));
  50. test(S("abcdefghijklmnopqrst"), S("12345678901234567890"));
  51. }
  52. #if TEST_STD_VER >= 11
  53. {
  54. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  55. test(S(""), S(""));
  56. test(S(""), S("12345"));
  57. test(S(""), S("1234567890"));
  58. test(S(""), S("12345678901234567890"));
  59. test(S("abcde"), S(""));
  60. test(S("abcde"), S("12345"));
  61. test(S("abcde"), S("1234567890"));
  62. test(S("abcde"), S("12345678901234567890"));
  63. test(S("abcdefghij"), S(""));
  64. test(S("abcdefghij"), S("12345"));
  65. test(S("abcdefghij"), S("1234567890"));
  66. test(S("abcdefghij"), S("12345678901234567890"));
  67. test(S("abcdefghijklmnopqrst"), S(""));
  68. test(S("abcdefghijklmnopqrst"), S("12345"));
  69. test(S("abcdefghijklmnopqrst"), S("1234567890"));
  70. test(S("abcdefghijklmnopqrst"), S("12345678901234567890"));
  71. }
  72. #endif
  73. }