swap.pass.cpp 2.2 KB

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