capacity.pass.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // size_type capacity() const;
  10. #include <string>
  11. #include <cassert>
  12. #include "test_allocator.h"
  13. #include "min_allocator.h"
  14. #include "test_macros.h"
  15. template <class S>
  16. void
  17. test(S s)
  18. {
  19. S::allocator_type::throw_after = 0;
  20. #ifndef TEST_HAS_NO_EXCEPTIONS
  21. try
  22. #endif
  23. {
  24. while (s.size() < s.capacity())
  25. s.push_back(typename S::value_type());
  26. assert(s.size() == s.capacity());
  27. }
  28. #ifndef TEST_HAS_NO_EXCEPTIONS
  29. catch (...)
  30. {
  31. assert(false);
  32. }
  33. #endif
  34. S::allocator_type::throw_after = INT_MAX;
  35. }
  36. int main(int, char**)
  37. {
  38. {
  39. typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > S;
  40. S s;
  41. test(s);
  42. s.assign(10, 'a');
  43. s.erase(5);
  44. test(s);
  45. s.assign(100, 'a');
  46. s.erase(50);
  47. test(s);
  48. }
  49. #if TEST_STD_VER >= 11
  50. {
  51. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  52. S s;
  53. assert(s.capacity() > 0);
  54. }
  55. #endif
  56. return 0;
  57. }