max_size.pass.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // UNSUPPORTED: libcpp-no-exceptions
  10. // <string>
  11. // size_type max_size() const;
  12. // NOTE: asan and msan will fail for one of two reasons
  13. // 1. If allocator_may_return_null=0 then they will fail because the allocation
  14. // returns null.
  15. // 2. If allocator_may_return_null=1 then they will fail because the allocation
  16. // is too large to succeed.
  17. // UNSUPPORTED: sanitizer-new-delete
  18. #include <string>
  19. #include <cassert>
  20. #include "min_allocator.h"
  21. template <class S>
  22. void
  23. test1(const S& s)
  24. {
  25. S s2(s);
  26. const size_t sz = s2.max_size() - 1;
  27. try { s2.resize(sz, 'x'); }
  28. catch ( const std::bad_alloc & ) { return ; }
  29. assert ( s2.size() == sz );
  30. }
  31. template <class S>
  32. void
  33. test2(const S& s)
  34. {
  35. S s2(s);
  36. const size_t sz = s2.max_size();
  37. try { s2.resize(sz, 'x'); }
  38. catch ( const std::bad_alloc & ) { return ; }
  39. assert ( s.size() == sz );
  40. }
  41. template <class S>
  42. void
  43. test(const S& s)
  44. {
  45. assert(s.max_size() >= s.size());
  46. test1(s);
  47. test2(s);
  48. }
  49. int main()
  50. {
  51. {
  52. typedef std::string S;
  53. test(S());
  54. test(S("123"));
  55. test(S("12345678901234567890123456789012345678901234567890"));
  56. }
  57. #if TEST_STD_VER >= 11
  58. {
  59. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  60. test(S());
  61. test(S("123"));
  62. test(S("12345678901234567890123456789012345678901234567890"));
  63. }
  64. #endif
  65. }