resize_size_char.pass.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <string>
  9. // void resize(size_type n, charT c);
  10. // When back-deploying to macosx10.7, the RTTI for exception classes
  11. // incorrectly provided by libc++.dylib is mixed with the one in
  12. // libc++abi.dylib and exceptions are not caught properly.
  13. // XFAIL: with_system_cxx_lib=macosx10.7
  14. #include <string>
  15. #include <stdexcept>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "min_allocator.h"
  19. template <class S>
  20. void
  21. test(S s, typename S::size_type n, typename S::value_type c, S expected)
  22. {
  23. if (n <= s.max_size())
  24. {
  25. s.resize(n, c);
  26. LIBCPP_ASSERT(s.__invariants());
  27. assert(s == expected);
  28. }
  29. #ifndef TEST_HAS_NO_EXCEPTIONS
  30. else
  31. {
  32. try
  33. {
  34. s.resize(n, c);
  35. assert(false);
  36. }
  37. catch (std::length_error&)
  38. {
  39. assert(n > s.max_size());
  40. }
  41. }
  42. #endif
  43. }
  44. int main(int, char**)
  45. {
  46. {
  47. typedef std::string S;
  48. test(S(), 0, 'a', S());
  49. test(S(), 1, 'a', S("a"));
  50. test(S(), 10, 'a', S(10, 'a'));
  51. test(S(), 100, 'a', S(100, 'a'));
  52. test(S("12345"), 0, 'a', S());
  53. test(S("12345"), 2, 'a', S("12"));
  54. test(S("12345"), 5, 'a', S("12345"));
  55. test(S("12345"), 15, 'a', S("12345aaaaaaaaaa"));
  56. test(S("12345678901234567890123456789012345678901234567890"), 0, 'a', S());
  57. test(S("12345678901234567890123456789012345678901234567890"), 10, 'a',
  58. S("1234567890"));
  59. test(S("12345678901234567890123456789012345678901234567890"), 50, 'a',
  60. S("12345678901234567890123456789012345678901234567890"));
  61. test(S("12345678901234567890123456789012345678901234567890"), 60, 'a',
  62. S("12345678901234567890123456789012345678901234567890aaaaaaaaaa"));
  63. test(S(), S::npos, 'a', S("not going to happen"));
  64. }
  65. #if TEST_STD_VER >= 11
  66. {
  67. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  68. test(S(), 0, 'a', S());
  69. test(S(), 1, 'a', S("a"));
  70. test(S(), 10, 'a', S(10, 'a'));
  71. test(S(), 100, 'a', S(100, 'a'));
  72. test(S("12345"), 0, 'a', S());
  73. test(S("12345"), 2, 'a', S("12"));
  74. test(S("12345"), 5, 'a', S("12345"));
  75. test(S("12345"), 15, 'a', S("12345aaaaaaaaaa"));
  76. test(S("12345678901234567890123456789012345678901234567890"), 0, 'a', S());
  77. test(S("12345678901234567890123456789012345678901234567890"), 10, 'a',
  78. S("1234567890"));
  79. test(S("12345678901234567890123456789012345678901234567890"), 50, 'a',
  80. S("12345678901234567890123456789012345678901234567890"));
  81. test(S("12345678901234567890123456789012345678901234567890"), 60, 'a',
  82. S("12345678901234567890123456789012345678901234567890aaaaaaaaaa"));
  83. test(S(), S::npos, 'a', S("not going to happen"));
  84. }
  85. #endif
  86. return 0;
  87. }