resize_size_char.pass.cpp 2.6 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. // XFAIL: libcpp-no-exceptions
  10. // <string>
  11. // void resize(size_type n, charT c);
  12. #include <string>
  13. #include <stdexcept>
  14. #include <cassert>
  15. #include "min_allocator.h"
  16. template <class S>
  17. void
  18. test(S s, typename S::size_type n, typename S::value_type c, S expected)
  19. {
  20. try
  21. {
  22. s.resize(n, c);
  23. assert(s.__invariants());
  24. assert(n <= s.max_size());
  25. assert(s == expected);
  26. }
  27. catch (std::length_error&)
  28. {
  29. assert(n > s.max_size());
  30. }
  31. }
  32. int main()
  33. {
  34. {
  35. typedef std::string S;
  36. test(S(), 0, 'a', S());
  37. test(S(), 1, 'a', S("a"));
  38. test(S(), 10, 'a', S(10, 'a'));
  39. test(S(), 100, 'a', S(100, 'a'));
  40. test(S("12345"), 0, 'a', S());
  41. test(S("12345"), 2, 'a', S("12"));
  42. test(S("12345"), 5, 'a', S("12345"));
  43. test(S("12345"), 15, 'a', S("12345aaaaaaaaaa"));
  44. test(S("12345678901234567890123456789012345678901234567890"), 0, 'a', S());
  45. test(S("12345678901234567890123456789012345678901234567890"), 10, 'a',
  46. S("1234567890"));
  47. test(S("12345678901234567890123456789012345678901234567890"), 50, 'a',
  48. S("12345678901234567890123456789012345678901234567890"));
  49. test(S("12345678901234567890123456789012345678901234567890"), 60, 'a',
  50. S("12345678901234567890123456789012345678901234567890aaaaaaaaaa"));
  51. test(S(), S::npos, 'a', S("not going to happen"));
  52. }
  53. #if __cplusplus >= 201103L
  54. {
  55. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  56. test(S(), 0, 'a', S());
  57. test(S(), 1, 'a', S("a"));
  58. test(S(), 10, 'a', S(10, 'a'));
  59. test(S(), 100, 'a', S(100, 'a'));
  60. test(S("12345"), 0, 'a', S());
  61. test(S("12345"), 2, 'a', S("12"));
  62. test(S("12345"), 5, 'a', S("12345"));
  63. test(S("12345"), 15, 'a', S("12345aaaaaaaaaa"));
  64. test(S("12345678901234567890123456789012345678901234567890"), 0, 'a', S());
  65. test(S("12345678901234567890123456789012345678901234567890"), 10, 'a',
  66. S("1234567890"));
  67. test(S("12345678901234567890123456789012345678901234567890"), 50, 'a',
  68. S("12345678901234567890123456789012345678901234567890"));
  69. test(S("12345678901234567890123456789012345678901234567890"), 60, 'a',
  70. S("12345678901234567890123456789012345678901234567890aaaaaaaaaa"));
  71. test(S(), S::npos, 'a', S("not going to happen"));
  72. }
  73. #endif
  74. }