shrink_to_fit.pass.cpp 1.2 KB

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