iter_char.pass.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. 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. template <class S>
  15. void
  16. test(S& s, typename S::const_iterator p, typename S::value_type c, S expected)
  17. {
  18. bool sufficient_cap = s.size() < s.capacity();
  19. typename S::difference_type pos = p - s.begin();
  20. typename S::iterator i = s.insert(p, c);
  21. assert(s.__invariants());
  22. assert(s == expected);
  23. assert(i - s.begin() == pos);
  24. assert(*i == c);
  25. if (sufficient_cap)
  26. assert(i == p);
  27. }
  28. int main()
  29. {
  30. typedef std::string S;
  31. S s;
  32. test(s, s.begin(), '1', S("1"));
  33. test(s, s.begin(), 'a', S("a1"));
  34. test(s, s.end(), 'b', S("a1b"));
  35. test(s, s.end()-1, 'c', S("a1cb"));
  36. test(s, s.end()-2, 'd', S("a1dcb"));
  37. test(s, s.end()-3, '2', S("a12dcb"));
  38. test(s, s.end()-4, '3', S("a132dcb"));
  39. test(s, s.end()-5, '4', S("a1432dcb"));
  40. test(s, s.begin()+1, '5', S("a51432dcb"));
  41. test(s, s.begin()+2, '6', S("a561432dcb"));
  42. test(s, s.begin()+3, '7', S("a5671432dcb"));
  43. test(s, s.begin()+4, 'A', S("a567A1432dcb"));
  44. test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
  45. test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
  46. }