reserve.pass.cpp 2.0 KB

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