iter_char.pass.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // iterator insert(const_iterator p, charT c);
  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, typename S::const_iterator p, typename S::value_type c, S expected)
  19. {
  20. bool sufficient_cap = s.size() < s.capacity();
  21. typename S::difference_type pos = p - s.begin();
  22. typename S::iterator i = s.insert(p, c);
  23. LIBCPP_ASSERT(s.__invariants());
  24. assert(s == expected);
  25. assert(i - s.begin() == pos);
  26. assert(*i == c);
  27. if (sufficient_cap)
  28. assert(i == p);
  29. }
  30. int main()
  31. {
  32. {
  33. typedef std::string S;
  34. S s;
  35. test(s, s.begin(), '1', S("1"));
  36. test(s, s.begin(), 'a', S("a1"));
  37. test(s, s.end(), 'b', S("a1b"));
  38. test(s, s.end()-1, 'c', S("a1cb"));
  39. test(s, s.end()-2, 'd', S("a1dcb"));
  40. test(s, s.end()-3, '2', S("a12dcb"));
  41. test(s, s.end()-4, '3', S("a132dcb"));
  42. test(s, s.end()-5, '4', S("a1432dcb"));
  43. test(s, s.begin()+1, '5', S("a51432dcb"));
  44. test(s, s.begin()+2, '6', S("a561432dcb"));
  45. test(s, s.begin()+3, '7', S("a5671432dcb"));
  46. test(s, s.begin()+4, 'A', S("a567A1432dcb"));
  47. test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
  48. test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
  49. }
  50. #if TEST_STD_VER >= 11
  51. {
  52. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  53. S s;
  54. test(s, s.begin(), '1', S("1"));
  55. test(s, s.begin(), 'a', S("a1"));
  56. test(s, s.end(), 'b', S("a1b"));
  57. test(s, s.end()-1, 'c', S("a1cb"));
  58. test(s, s.end()-2, 'd', S("a1dcb"));
  59. test(s, s.end()-3, '2', S("a12dcb"));
  60. test(s, s.end()-4, '3', S("a132dcb"));
  61. test(s, s.end()-5, '4', S("a1432dcb"));
  62. test(s, s.begin()+1, '5', S("a51432dcb"));
  63. test(s, s.begin()+2, '6', S("a561432dcb"));
  64. test(s, s.begin()+3, '7', S("a5671432dcb"));
  65. test(s, s.begin()+4, 'A', S("a567A1432dcb"));
  66. test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
  67. test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
  68. }
  69. #endif
  70. }