reserve.pass.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // void reserve(size_type res_arg=0);
  12. #include <string>
  13. #include <stdexcept>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "min_allocator.h"
  17. template <class S>
  18. void
  19. test(S s)
  20. {
  21. typename S::size_type old_cap = s.capacity();
  22. S s0 = s;
  23. s.reserve();
  24. LIBCPP_ASSERT(s.__invariants());
  25. assert(s == s0);
  26. assert(s.capacity() <= old_cap);
  27. assert(s.capacity() >= s.size());
  28. }
  29. template <class S>
  30. void
  31. test(S s, typename S::size_type res_arg)
  32. {
  33. typename S::size_type old_cap = s.capacity();
  34. S s0 = s;
  35. try
  36. {
  37. s.reserve(res_arg);
  38. assert(res_arg <= s.max_size());
  39. assert(s == s0);
  40. assert(s.capacity() >= res_arg);
  41. assert(s.capacity() >= s.size());
  42. }
  43. catch (std::length_error&)
  44. {
  45. assert(res_arg > s.max_size());
  46. }
  47. }
  48. int main()
  49. {
  50. {
  51. typedef std::string S;
  52. {
  53. S s;
  54. test(s);
  55. s.assign(10, 'a');
  56. s.erase(5);
  57. test(s);
  58. s.assign(100, 'a');
  59. s.erase(50);
  60. test(s);
  61. }
  62. {
  63. S s;
  64. test(s, 5);
  65. test(s, 10);
  66. test(s, 50);
  67. }
  68. {
  69. S s(100, 'a');
  70. s.erase(50);
  71. test(s, 5);
  72. test(s, 10);
  73. test(s, 50);
  74. test(s, 100);
  75. test(s, S::npos);
  76. }
  77. }
  78. #if TEST_STD_VER >= 11
  79. {
  80. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  81. {
  82. S s;
  83. test(s);
  84. s.assign(10, 'a');
  85. s.erase(5);
  86. test(s);
  87. s.assign(100, 'a');
  88. s.erase(50);
  89. test(s);
  90. }
  91. {
  92. S s;
  93. test(s, 5);
  94. test(s, 10);
  95. test(s, 50);
  96. }
  97. {
  98. S s(100, 'a');
  99. s.erase(50);
  100. test(s, 5);
  101. test(s, 10);
  102. test(s, 50);
  103. test(s, 100);
  104. test(s, S::npos);
  105. }
  106. }
  107. #endif
  108. }