capacity.pass.cpp 1.2 KB

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