reserve.pass.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // Split into two calls for C++20
  10. // void reserve();
  11. // void reserve(size_type res_arg);
  12. // When back-deploying to macosx10.7, the RTTI for exception classes
  13. // incorrectly provided by libc++.dylib is mixed with the one in
  14. // libc++abi.dylib and exceptions are not caught properly.
  15. // XFAIL: with_system_cxx_lib=macosx10.7
  16. #include <string>
  17. #include <stdexcept>
  18. #include <cassert>
  19. #include "test_macros.h"
  20. #include "min_allocator.h"
  21. template <class S>
  22. void
  23. test(S s)
  24. {
  25. typename S::size_type old_cap = s.capacity();
  26. S s0 = s;
  27. s.reserve();
  28. LIBCPP_ASSERT(s.__invariants());
  29. assert(s == s0);
  30. assert(s.capacity() <= old_cap);
  31. assert(s.capacity() >= s.size());
  32. }
  33. template <class S>
  34. void
  35. test(S s, typename S::size_type res_arg)
  36. {
  37. typename S::size_type old_cap = s.capacity();
  38. ((void)old_cap); // Prevent unused warning
  39. S s0 = s;
  40. if (res_arg <= s.max_size())
  41. {
  42. s.reserve(res_arg);
  43. assert(s == s0);
  44. assert(s.capacity() >= res_arg);
  45. assert(s.capacity() >= s.size());
  46. #if TEST_STD_VER > 17
  47. assert(s.capacity() >= old_cap); // resize never shrinks as of P0966
  48. #endif
  49. }
  50. #ifndef TEST_HAS_NO_EXCEPTIONS
  51. else
  52. {
  53. try
  54. {
  55. s.reserve(res_arg);
  56. assert(false);
  57. }
  58. catch (std::length_error&)
  59. {
  60. assert(res_arg > s.max_size());
  61. }
  62. }
  63. #endif
  64. }
  65. int main(int, char**)
  66. {
  67. {
  68. typedef std::string S;
  69. {
  70. S s;
  71. test(s);
  72. s.assign(10, 'a');
  73. s.erase(5);
  74. test(s);
  75. s.assign(100, 'a');
  76. s.erase(50);
  77. test(s);
  78. }
  79. {
  80. S s;
  81. test(s, 5);
  82. test(s, 10);
  83. test(s, 50);
  84. }
  85. {
  86. S s(100, 'a');
  87. s.erase(50);
  88. test(s, 5);
  89. test(s, 10);
  90. test(s, 50);
  91. test(s, 100);
  92. test(s, 1000);
  93. test(s, S::npos);
  94. }
  95. }
  96. #if TEST_STD_VER >= 11
  97. {
  98. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  99. {
  100. S s;
  101. test(s);
  102. s.assign(10, 'a');
  103. s.erase(5);
  104. test(s);
  105. s.assign(100, 'a');
  106. s.erase(50);
  107. test(s);
  108. }
  109. {
  110. S s;
  111. test(s, 5);
  112. test(s, 10);
  113. test(s, 50);
  114. }
  115. {
  116. S s(100, 'a');
  117. s.erase(50);
  118. test(s, 5);
  119. test(s, 10);
  120. test(s, 50);
  121. test(s, 100);
  122. test(s, 1000);
  123. test(s, S::npos);
  124. }
  125. }
  126. #endif
  127. return 0;
  128. }