substr.pass.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // basic_string(const basic_string<charT,traits,Allocator>& str,
  12. // size_type pos, size_type n,
  13. // const Allocator& a = Allocator());
  14. //
  15. // basic_string(const basic_string<charT,traits,Allocator>& str,
  16. // size_type pos,
  17. // const Allocator& a = Allocator());
  18. #include <string>
  19. #include <stdexcept>
  20. #include <algorithm>
  21. #include <vector>
  22. #include <scoped_allocator>
  23. #include <cassert>
  24. #include "test_macros.h"
  25. #include "test_allocator.h"
  26. #include "min_allocator.h"
  27. template <class S>
  28. void
  29. test(S str, unsigned pos)
  30. {
  31. typedef typename S::traits_type T;
  32. typedef typename S::allocator_type A;
  33. try
  34. {
  35. S s2(str, pos);
  36. LIBCPP_ASSERT(s2.__invariants());
  37. assert(pos <= str.size());
  38. unsigned rlen = str.size() - pos;
  39. assert(s2.size() == rlen);
  40. assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
  41. assert(s2.get_allocator() == A());
  42. assert(s2.capacity() >= s2.size());
  43. }
  44. catch (std::out_of_range&)
  45. {
  46. assert(pos > str.size());
  47. }
  48. }
  49. template <class S>
  50. void
  51. test(S str, unsigned pos, unsigned n)
  52. {
  53. typedef typename S::traits_type T;
  54. typedef typename S::allocator_type A;
  55. try
  56. {
  57. S s2(str, pos, n);
  58. LIBCPP_ASSERT(s2.__invariants());
  59. assert(pos <= str.size());
  60. unsigned rlen = std::min<unsigned>(str.size() - pos, n);
  61. assert(s2.size() == rlen);
  62. assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
  63. assert(s2.get_allocator() == A());
  64. assert(s2.capacity() >= s2.size());
  65. }
  66. catch (std::out_of_range&)
  67. {
  68. assert(pos > str.size());
  69. }
  70. }
  71. template <class S>
  72. void
  73. test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a)
  74. {
  75. typedef typename S::traits_type T;
  76. typedef typename S::allocator_type A;
  77. try
  78. {
  79. S s2(str, pos, n, a);
  80. LIBCPP_ASSERT(s2.__invariants());
  81. assert(pos <= str.size());
  82. unsigned rlen = std::min<unsigned>(str.size() - pos, n);
  83. assert(s2.size() == rlen);
  84. assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
  85. assert(s2.get_allocator() == a);
  86. assert(s2.capacity() >= s2.size());
  87. }
  88. catch (std::out_of_range&)
  89. {
  90. assert(pos > str.size());
  91. }
  92. }
  93. #if TEST_STD_VER >= 11
  94. void test2583()
  95. { // LWG #2583
  96. typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > StringA;
  97. std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA>>> vs;
  98. StringA s{"1234"};
  99. vs.emplace_back(s, 2);
  100. try { vs.emplace_back(s, 5); }
  101. catch (const std::out_of_range&) { return; }
  102. assert(false);
  103. }
  104. #endif
  105. int main()
  106. {
  107. {
  108. typedef test_allocator<char> A;
  109. typedef std::basic_string<char, std::char_traits<char>, A> S;
  110. test(S(A(3)), 0);
  111. test(S(A(3)), 1);
  112. test(S("1", A(5)), 0);
  113. test(S("1", A(5)), 1);
  114. test(S("1", A(5)), 2);
  115. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0);
  116. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5);
  117. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50);
  118. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500);
  119. test(S(A(3)), 0, 0);
  120. test(S(A(3)), 0, 1);
  121. test(S(A(3)), 1, 0);
  122. test(S(A(3)), 1, 1);
  123. test(S(A(3)), 1, 2);
  124. test(S("1", A(5)), 0, 0);
  125. test(S("1", A(5)), 0, 1);
  126. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0);
  127. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1);
  128. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10);
  129. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100);
  130. test(S(A(3)), 0, 0, A(4));
  131. test(S(A(3)), 0, 1, A(4));
  132. test(S(A(3)), 1, 0, A(4));
  133. test(S(A(3)), 1, 1, A(4));
  134. test(S(A(3)), 1, 2, A(4));
  135. test(S("1", A(5)), 0, 0, A(6));
  136. test(S("1", A(5)), 0, 1, A(6));
  137. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8));
  138. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8));
  139. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8));
  140. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8));
  141. }
  142. #if TEST_STD_VER >= 11
  143. {
  144. typedef min_allocator<char> A;
  145. typedef std::basic_string<char, std::char_traits<char>, A> S;
  146. test(S(A()), 0);
  147. test(S(A()), 1);
  148. test(S("1", A()), 0);
  149. test(S("1", A()), 1);
  150. test(S("1", A()), 2);
  151. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0);
  152. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5);
  153. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50);
  154. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500);
  155. test(S(A()), 0, 0);
  156. test(S(A()), 0, 1);
  157. test(S(A()), 1, 0);
  158. test(S(A()), 1, 1);
  159. test(S(A()), 1, 2);
  160. test(S("1", A()), 0, 0);
  161. test(S("1", A()), 0, 1);
  162. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0);
  163. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1);
  164. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10);
  165. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100);
  166. test(S(A()), 0, 0, A());
  167. test(S(A()), 0, 1, A());
  168. test(S(A()), 1, 0, A());
  169. test(S(A()), 1, 1, A());
  170. test(S(A()), 1, 2, A());
  171. test(S("1", A()), 0, 0, A());
  172. test(S("1", A()), 0, 1, A());
  173. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A());
  174. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A());
  175. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A());
  176. test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A());
  177. }
  178. test2583();
  179. #endif
  180. }