resize_size.pass.cpp 2.9 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);
  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, S expected)
  22. {
  23. if (n <= s.max_size())
  24. {
  25. s.resize(n);
  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);
  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, S());
  49. test(S(), 1, S(1, '\0'));
  50. test(S(), 10, S(10, '\0'));
  51. test(S(), 100, S(100, '\0'));
  52. test(S("12345"), 0, S());
  53. test(S("12345"), 2, S("12"));
  54. test(S("12345"), 5, S("12345"));
  55. test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
  56. test(S("12345678901234567890123456789012345678901234567890"), 0, S());
  57. test(S("12345678901234567890123456789012345678901234567890"), 10,
  58. S("1234567890"));
  59. test(S("12345678901234567890123456789012345678901234567890"), 50,
  60. S("12345678901234567890123456789012345678901234567890"));
  61. test(S("12345678901234567890123456789012345678901234567890"), 60,
  62. S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
  63. test(S(), S::npos, 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, S());
  69. test(S(), 1, S(1, '\0'));
  70. test(S(), 10, S(10, '\0'));
  71. test(S(), 100, S(100, '\0'));
  72. test(S("12345"), 0, S());
  73. test(S("12345"), 2, S("12"));
  74. test(S("12345"), 5, S("12345"));
  75. test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
  76. test(S("12345678901234567890123456789012345678901234567890"), 0, S());
  77. test(S("12345678901234567890123456789012345678901234567890"), 10,
  78. S("1234567890"));
  79. test(S("12345678901234567890123456789012345678901234567890"), 50,
  80. S("12345678901234567890123456789012345678901234567890"));
  81. test(S("12345678901234567890123456789012345678901234567890"), 60,
  82. S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
  83. test(S(), S::npos, S("not going to happen"));
  84. }
  85. #endif
  86. return 0;
  87. }