max_size.pass.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // UNSUPPORTED: libcpp-no-exceptions
  9. // <string>
  10. // size_type max_size() const;
  11. // NOTE: asan and msan will fail for one of two reasons
  12. // 1. If allocator_may_return_null=0 then they will fail because the allocation
  13. // returns null.
  14. // 2. If allocator_may_return_null=1 then they will fail because the allocation
  15. // is too large to succeed.
  16. // UNSUPPORTED: sanitizer-new-delete
  17. #include <string>
  18. #include <cassert>
  19. #include "test_macros.h"
  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(int, char**)
  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. return 0;
  66. }