shrink_to_fit.pass.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 shrink_to_fit();
  10. #include <string>
  11. #include <cassert>
  12. #include "test_macros.h"
  13. #include "min_allocator.h"
  14. template <class S>
  15. void
  16. test(S s)
  17. {
  18. typename S::size_type old_cap = s.capacity();
  19. S s0 = s;
  20. s.shrink_to_fit();
  21. LIBCPP_ASSERT(s.__invariants());
  22. assert(s == s0);
  23. assert(s.capacity() <= old_cap);
  24. assert(s.capacity() >= s.size());
  25. }
  26. int main(int, char**)
  27. {
  28. {
  29. typedef std::string S;
  30. S s;
  31. test(s);
  32. s.assign(10, 'a');
  33. s.erase(5);
  34. test(s);
  35. s.assign(100, 'a');
  36. s.erase(50);
  37. test(s);
  38. }
  39. #if TEST_STD_VER >= 11
  40. {
  41. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  42. S s;
  43. test(s);
  44. s.assign(10, 'a');
  45. s.erase(5);
  46. test(s);
  47. s.assign(100, 'a');
  48. s.erase(50);
  49. test(s);
  50. }
  51. #endif
  52. return 0;
  53. }