substr.pass.cpp 6.1 KB

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