pointer.pass.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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<charT,traits,Allocator>& append(const charT* s);
  11. #include <string>
  12. #include <stdexcept>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "min_allocator.h"
  16. template <class S>
  17. void
  18. test(S s, const typename S::value_type* str, S expected)
  19. {
  20. s.append(str);
  21. LIBCPP_ASSERT(s.__invariants());
  22. assert(s == expected);
  23. }
  24. int main()
  25. {
  26. {
  27. typedef std::string S;
  28. test(S(), "", S());
  29. test(S(), "12345", S("12345"));
  30. test(S(), "12345678901234567890", S("12345678901234567890"));
  31. test(S("12345"), "", S("12345"));
  32. test(S("12345"), "12345", S("1234512345"));
  33. test(S("12345"), "1234567890", S("123451234567890"));
  34. test(S("12345678901234567890"), "", S("12345678901234567890"));
  35. test(S("12345678901234567890"), "12345", S("1234567890123456789012345"));
  36. test(S("12345678901234567890"), "12345678901234567890",
  37. S("1234567890123456789012345678901234567890"));
  38. }
  39. #if TEST_STD_VER >= 11
  40. {
  41. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  42. test(S(), "", S());
  43. test(S(), "12345", S("12345"));
  44. test(S(), "12345678901234567890", S("12345678901234567890"));
  45. test(S("12345"), "", S("12345"));
  46. test(S("12345"), "12345", S("1234512345"));
  47. test(S("12345"), "1234567890", S("123451234567890"));
  48. test(S("12345678901234567890"), "", S("12345678901234567890"));
  49. test(S("12345678901234567890"), "12345", S("1234567890123456789012345"));
  50. test(S("12345678901234567890"), "12345678901234567890",
  51. S("1234567890123456789012345678901234567890"));
  52. }
  53. #endif
  54. { // test appending to self
  55. typedef std::string S;
  56. S s_short = "123/";
  57. S s_long = "Lorem ipsum dolor sit amet, consectetur/";
  58. s_short.append(s_short.c_str());
  59. assert(s_short == "123/123/");
  60. s_short.append(s_short.c_str());
  61. assert(s_short == "123/123/123/123/");
  62. s_short.append(s_short.c_str());
  63. assert(s_short == "123/123/123/123/123/123/123/123/");
  64. s_long.append(s_long.c_str());
  65. assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
  66. }
  67. }