resize_size_char.pass.cpp 2.7 KB

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