iter_iter_pointer.pass.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. // <string>
  10. // basic_string<charT,traits,Allocator>&
  11. // replace(const_iterator i1, const_iterator i2, const charT* s);
  12. #include <stdio.h>
  13. #include <string>
  14. #include <algorithm>
  15. #include <cassert>
  16. #include "min_allocator.h"
  17. template <class S>
  18. void
  19. test(S s, typename S::size_type pos1, typename S::size_type n1, const typename S::value_type* str, S expected)
  20. {
  21. typename S::size_type old_size = s.size();
  22. typename S::const_iterator first = s.begin() + pos1;
  23. typename S::const_iterator last = s.begin() + pos1 + n1;
  24. typename S::size_type xlen = last - first;
  25. s.replace(first, last, str);
  26. LIBCPP_ASSERT(s.__invariants());
  27. assert(s == expected);
  28. typename S::size_type rlen = S::traits_type::length(str);
  29. assert(s.size() == old_size - xlen + rlen);
  30. }
  31. template <class S>
  32. void test0()
  33. {
  34. test(S(""), 0, 0, "", S(""));
  35. test(S(""), 0, 0, "12345", S("12345"));
  36. test(S(""), 0, 0, "1234567890", S("1234567890"));
  37. test(S(""), 0, 0, "12345678901234567890", S("12345678901234567890"));
  38. test(S("abcde"), 0, 0, "", S("abcde"));
  39. test(S("abcde"), 0, 0, "12345", S("12345abcde"));
  40. test(S("abcde"), 0, 0, "1234567890", S("1234567890abcde"));
  41. test(S("abcde"), 0, 0, "12345678901234567890", S("12345678901234567890abcde"));
  42. test(S("abcde"), 0, 1, "", S("bcde"));
  43. test(S("abcde"), 0, 1, "12345", S("12345bcde"));
  44. test(S("abcde"), 0, 1, "1234567890", S("1234567890bcde"));
  45. test(S("abcde"), 0, 1, "12345678901234567890", S("12345678901234567890bcde"));
  46. test(S("abcde"), 0, 2, "", S("cde"));
  47. test(S("abcde"), 0, 2, "12345", S("12345cde"));
  48. test(S("abcde"), 0, 2, "1234567890", S("1234567890cde"));
  49. test(S("abcde"), 0, 2, "12345678901234567890", S("12345678901234567890cde"));
  50. test(S("abcde"), 0, 4, "", S("e"));
  51. test(S("abcde"), 0, 4, "12345", S("12345e"));
  52. test(S("abcde"), 0, 4, "1234567890", S("1234567890e"));
  53. test(S("abcde"), 0, 4, "12345678901234567890", S("12345678901234567890e"));
  54. test(S("abcde"), 0, 5, "", S(""));
  55. test(S("abcde"), 0, 5, "12345", S("12345"));
  56. test(S("abcde"), 0, 5, "1234567890", S("1234567890"));
  57. test(S("abcde"), 0, 5, "12345678901234567890", S("12345678901234567890"));
  58. test(S("abcde"), 1, 0, "", S("abcde"));
  59. test(S("abcde"), 1, 0, "12345", S("a12345bcde"));
  60. test(S("abcde"), 1, 0, "1234567890", S("a1234567890bcde"));
  61. test(S("abcde"), 1, 0, "12345678901234567890", S("a12345678901234567890bcde"));
  62. test(S("abcde"), 1, 1, "", S("acde"));
  63. test(S("abcde"), 1, 1, "12345", S("a12345cde"));
  64. test(S("abcde"), 1, 1, "1234567890", S("a1234567890cde"));
  65. test(S("abcde"), 1, 1, "12345678901234567890", S("a12345678901234567890cde"));
  66. test(S("abcde"), 1, 2, "", S("ade"));
  67. test(S("abcde"), 1, 2, "12345", S("a12345de"));
  68. test(S("abcde"), 1, 2, "1234567890", S("a1234567890de"));
  69. test(S("abcde"), 1, 2, "12345678901234567890", S("a12345678901234567890de"));
  70. test(S("abcde"), 1, 3, "", S("ae"));
  71. test(S("abcde"), 1, 3, "12345", S("a12345e"));
  72. test(S("abcde"), 1, 3, "1234567890", S("a1234567890e"));
  73. test(S("abcde"), 1, 3, "12345678901234567890", S("a12345678901234567890e"));
  74. test(S("abcde"), 1, 4, "", S("a"));
  75. test(S("abcde"), 1, 4, "12345", S("a12345"));
  76. test(S("abcde"), 1, 4, "1234567890", S("a1234567890"));
  77. test(S("abcde"), 1, 4, "12345678901234567890", S("a12345678901234567890"));
  78. test(S("abcde"), 2, 0, "", S("abcde"));
  79. test(S("abcde"), 2, 0, "12345", S("ab12345cde"));
  80. test(S("abcde"), 2, 0, "1234567890", S("ab1234567890cde"));
  81. test(S("abcde"), 2, 0, "12345678901234567890", S("ab12345678901234567890cde"));
  82. test(S("abcde"), 2, 1, "", S("abde"));
  83. test(S("abcde"), 2, 1, "12345", S("ab12345de"));
  84. test(S("abcde"), 2, 1, "1234567890", S("ab1234567890de"));
  85. test(S("abcde"), 2, 1, "12345678901234567890", S("ab12345678901234567890de"));
  86. test(S("abcde"), 2, 2, "", S("abe"));
  87. test(S("abcde"), 2, 2, "12345", S("ab12345e"));
  88. test(S("abcde"), 2, 2, "1234567890", S("ab1234567890e"));
  89. test(S("abcde"), 2, 2, "12345678901234567890", S("ab12345678901234567890e"));
  90. test(S("abcde"), 2, 3, "", S("ab"));
  91. test(S("abcde"), 2, 3, "12345", S("ab12345"));
  92. test(S("abcde"), 2, 3, "1234567890", S("ab1234567890"));
  93. test(S("abcde"), 2, 3, "12345678901234567890", S("ab12345678901234567890"));
  94. test(S("abcde"), 4, 0, "", S("abcde"));
  95. test(S("abcde"), 4, 0, "12345", S("abcd12345e"));
  96. test(S("abcde"), 4, 0, "1234567890", S("abcd1234567890e"));
  97. test(S("abcde"), 4, 0, "12345678901234567890", S("abcd12345678901234567890e"));
  98. test(S("abcde"), 4, 1, "", S("abcd"));
  99. test(S("abcde"), 4, 1, "12345", S("abcd12345"));
  100. test(S("abcde"), 4, 1, "1234567890", S("abcd1234567890"));
  101. test(S("abcde"), 4, 1, "12345678901234567890", S("abcd12345678901234567890"));
  102. test(S("abcde"), 5, 0, "", S("abcde"));
  103. test(S("abcde"), 5, 0, "12345", S("abcde12345"));
  104. test(S("abcde"), 5, 0, "1234567890", S("abcde1234567890"));
  105. test(S("abcde"), 5, 0, "12345678901234567890", S("abcde12345678901234567890"));
  106. test(S("abcdefghij"), 0, 0, "", S("abcdefghij"));
  107. test(S("abcdefghij"), 0, 0, "12345", S("12345abcdefghij"));
  108. test(S("abcdefghij"), 0, 0, "1234567890", S("1234567890abcdefghij"));
  109. test(S("abcdefghij"), 0, 0, "12345678901234567890", S("12345678901234567890abcdefghij"));
  110. test(S("abcdefghij"), 0, 1, "", S("bcdefghij"));
  111. test(S("abcdefghij"), 0, 1, "12345", S("12345bcdefghij"));
  112. test(S("abcdefghij"), 0, 1, "1234567890", S("1234567890bcdefghij"));
  113. test(S("abcdefghij"), 0, 1, "12345678901234567890", S("12345678901234567890bcdefghij"));
  114. test(S("abcdefghij"), 0, 5, "", S("fghij"));
  115. test(S("abcdefghij"), 0, 5, "12345", S("12345fghij"));
  116. test(S("abcdefghij"), 0, 5, "1234567890", S("1234567890fghij"));
  117. test(S("abcdefghij"), 0, 5, "12345678901234567890", S("12345678901234567890fghij"));
  118. test(S("abcdefghij"), 0, 9, "", S("j"));
  119. test(S("abcdefghij"), 0, 9, "12345", S("12345j"));
  120. test(S("abcdefghij"), 0, 9, "1234567890", S("1234567890j"));
  121. test(S("abcdefghij"), 0, 9, "12345678901234567890", S("12345678901234567890j"));
  122. test(S("abcdefghij"), 0, 10, "", S(""));
  123. test(S("abcdefghij"), 0, 10, "12345", S("12345"));
  124. test(S("abcdefghij"), 0, 10, "1234567890", S("1234567890"));
  125. test(S("abcdefghij"), 0, 10, "12345678901234567890", S("12345678901234567890"));
  126. test(S("abcdefghij"), 1, 0, "", S("abcdefghij"));
  127. test(S("abcdefghij"), 1, 0, "12345", S("a12345bcdefghij"));
  128. test(S("abcdefghij"), 1, 0, "1234567890", S("a1234567890bcdefghij"));
  129. test(S("abcdefghij"), 1, 0, "12345678901234567890", S("a12345678901234567890bcdefghij"));
  130. test(S("abcdefghij"), 1, 1, "", S("acdefghij"));
  131. test(S("abcdefghij"), 1, 1, "12345", S("a12345cdefghij"));
  132. test(S("abcdefghij"), 1, 1, "1234567890", S("a1234567890cdefghij"));
  133. test(S("abcdefghij"), 1, 1, "12345678901234567890", S("a12345678901234567890cdefghij"));
  134. }
  135. template <class S>
  136. void test1()
  137. {
  138. test(S("abcdefghij"), 1, 4, "", S("afghij"));
  139. test(S("abcdefghij"), 1, 4, "12345", S("a12345fghij"));
  140. test(S("abcdefghij"), 1, 4, "1234567890", S("a1234567890fghij"));
  141. test(S("abcdefghij"), 1, 4, "12345678901234567890", S("a12345678901234567890fghij"));
  142. test(S("abcdefghij"), 1, 8, "", S("aj"));
  143. test(S("abcdefghij"), 1, 8, "12345", S("a12345j"));
  144. test(S("abcdefghij"), 1, 8, "1234567890", S("a1234567890j"));
  145. test(S("abcdefghij"), 1, 8, "12345678901234567890", S("a12345678901234567890j"));
  146. test(S("abcdefghij"), 1, 9, "", S("a"));
  147. test(S("abcdefghij"), 1, 9, "12345", S("a12345"));
  148. test(S("abcdefghij"), 1, 9, "1234567890", S("a1234567890"));
  149. test(S("abcdefghij"), 1, 9, "12345678901234567890", S("a12345678901234567890"));
  150. test(S("abcdefghij"), 5, 0, "", S("abcdefghij"));
  151. test(S("abcdefghij"), 5, 0, "12345", S("abcde12345fghij"));
  152. test(S("abcdefghij"), 5, 0, "1234567890", S("abcde1234567890fghij"));
  153. test(S("abcdefghij"), 5, 0, "12345678901234567890", S("abcde12345678901234567890fghij"));
  154. test(S("abcdefghij"), 5, 1, "", S("abcdeghij"));
  155. test(S("abcdefghij"), 5, 1, "12345", S("abcde12345ghij"));
  156. test(S("abcdefghij"), 5, 1, "1234567890", S("abcde1234567890ghij"));
  157. test(S("abcdefghij"), 5, 1, "12345678901234567890", S("abcde12345678901234567890ghij"));
  158. test(S("abcdefghij"), 5, 2, "", S("abcdehij"));
  159. test(S("abcdefghij"), 5, 2, "12345", S("abcde12345hij"));
  160. test(S("abcdefghij"), 5, 2, "1234567890", S("abcde1234567890hij"));
  161. test(S("abcdefghij"), 5, 2, "12345678901234567890", S("abcde12345678901234567890hij"));
  162. test(S("abcdefghij"), 5, 4, "", S("abcdej"));
  163. test(S("abcdefghij"), 5, 4, "12345", S("abcde12345j"));
  164. test(S("abcdefghij"), 5, 4, "1234567890", S("abcde1234567890j"));
  165. test(S("abcdefghij"), 5, 4, "12345678901234567890", S("abcde12345678901234567890j"));
  166. test(S("abcdefghij"), 5, 5, "", S("abcde"));
  167. test(S("abcdefghij"), 5, 5, "12345", S("abcde12345"));
  168. test(S("abcdefghij"), 5, 5, "1234567890", S("abcde1234567890"));
  169. test(S("abcdefghij"), 5, 5, "12345678901234567890", S("abcde12345678901234567890"));
  170. test(S("abcdefghij"), 9, 0, "", S("abcdefghij"));
  171. test(S("abcdefghij"), 9, 0, "12345", S("abcdefghi12345j"));
  172. test(S("abcdefghij"), 9, 0, "1234567890", S("abcdefghi1234567890j"));
  173. test(S("abcdefghij"), 9, 0, "12345678901234567890", S("abcdefghi12345678901234567890j"));
  174. test(S("abcdefghij"), 9, 1, "", S("abcdefghi"));
  175. test(S("abcdefghij"), 9, 1, "12345", S("abcdefghi12345"));
  176. test(S("abcdefghij"), 9, 1, "1234567890", S("abcdefghi1234567890"));
  177. test(S("abcdefghij"), 9, 1, "12345678901234567890", S("abcdefghi12345678901234567890"));
  178. test(S("abcdefghij"), 10, 0, "", S("abcdefghij"));
  179. test(S("abcdefghij"), 10, 0, "12345", S("abcdefghij12345"));
  180. test(S("abcdefghij"), 10, 0, "1234567890", S("abcdefghij1234567890"));
  181. test(S("abcdefghij"), 10, 0, "12345678901234567890", S("abcdefghij12345678901234567890"));
  182. test(S("abcdefghijklmnopqrst"), 0, 0, "", S("abcdefghijklmnopqrst"));
  183. test(S("abcdefghijklmnopqrst"), 0, 0, "12345", S("12345abcdefghijklmnopqrst"));
  184. test(S("abcdefghijklmnopqrst"), 0, 0, "1234567890", S("1234567890abcdefghijklmnopqrst"));
  185. test(S("abcdefghijklmnopqrst"), 0, 0, "12345678901234567890", S("12345678901234567890abcdefghijklmnopqrst"));
  186. test(S("abcdefghijklmnopqrst"), 0, 1, "", S("bcdefghijklmnopqrst"));
  187. test(S("abcdefghijklmnopqrst"), 0, 1, "12345", S("12345bcdefghijklmnopqrst"));
  188. test(S("abcdefghijklmnopqrst"), 0, 1, "1234567890", S("1234567890bcdefghijklmnopqrst"));
  189. test(S("abcdefghijklmnopqrst"), 0, 1, "12345678901234567890", S("12345678901234567890bcdefghijklmnopqrst"));
  190. test(S("abcdefghijklmnopqrst"), 0, 10, "", S("klmnopqrst"));
  191. test(S("abcdefghijklmnopqrst"), 0, 10, "12345", S("12345klmnopqrst"));
  192. test(S("abcdefghijklmnopqrst"), 0, 10, "1234567890", S("1234567890klmnopqrst"));
  193. test(S("abcdefghijklmnopqrst"), 0, 10, "12345678901234567890", S("12345678901234567890klmnopqrst"));
  194. test(S("abcdefghijklmnopqrst"), 0, 19, "", S("t"));
  195. test(S("abcdefghijklmnopqrst"), 0, 19, "12345", S("12345t"));
  196. test(S("abcdefghijklmnopqrst"), 0, 19, "1234567890", S("1234567890t"));
  197. test(S("abcdefghijklmnopqrst"), 0, 19, "12345678901234567890", S("12345678901234567890t"));
  198. test(S("abcdefghijklmnopqrst"), 0, 20, "", S(""));
  199. test(S("abcdefghijklmnopqrst"), 0, 20, "12345", S("12345"));
  200. test(S("abcdefghijklmnopqrst"), 0, 20, "1234567890", S("1234567890"));
  201. test(S("abcdefghijklmnopqrst"), 0, 20, "12345678901234567890", S("12345678901234567890"));
  202. test(S("abcdefghijklmnopqrst"), 1, 0, "", S("abcdefghijklmnopqrst"));
  203. test(S("abcdefghijklmnopqrst"), 1, 0, "12345", S("a12345bcdefghijklmnopqrst"));
  204. test(S("abcdefghijklmnopqrst"), 1, 0, "1234567890", S("a1234567890bcdefghijklmnopqrst"));
  205. test(S("abcdefghijklmnopqrst"), 1, 0, "12345678901234567890", S("a12345678901234567890bcdefghijklmnopqrst"));
  206. test(S("abcdefghijklmnopqrst"), 1, 1, "", S("acdefghijklmnopqrst"));
  207. test(S("abcdefghijklmnopqrst"), 1, 1, "12345", S("a12345cdefghijklmnopqrst"));
  208. test(S("abcdefghijklmnopqrst"), 1, 1, "1234567890", S("a1234567890cdefghijklmnopqrst"));
  209. test(S("abcdefghijklmnopqrst"), 1, 1, "12345678901234567890", S("a12345678901234567890cdefghijklmnopqrst"));
  210. test(S("abcdefghijklmnopqrst"), 1, 9, "", S("aklmnopqrst"));
  211. test(S("abcdefghijklmnopqrst"), 1, 9, "12345", S("a12345klmnopqrst"));
  212. test(S("abcdefghijklmnopqrst"), 1, 9, "1234567890", S("a1234567890klmnopqrst"));
  213. test(S("abcdefghijklmnopqrst"), 1, 9, "12345678901234567890", S("a12345678901234567890klmnopqrst"));
  214. test(S("abcdefghijklmnopqrst"), 1, 18, "", S("at"));
  215. test(S("abcdefghijklmnopqrst"), 1, 18, "12345", S("a12345t"));
  216. test(S("abcdefghijklmnopqrst"), 1, 18, "1234567890", S("a1234567890t"));
  217. test(S("abcdefghijklmnopqrst"), 1, 18, "12345678901234567890", S("a12345678901234567890t"));
  218. test(S("abcdefghijklmnopqrst"), 1, 19, "", S("a"));
  219. test(S("abcdefghijklmnopqrst"), 1, 19, "12345", S("a12345"));
  220. test(S("abcdefghijklmnopqrst"), 1, 19, "1234567890", S("a1234567890"));
  221. test(S("abcdefghijklmnopqrst"), 1, 19, "12345678901234567890", S("a12345678901234567890"));
  222. test(S("abcdefghijklmnopqrst"), 10, 0, "", S("abcdefghijklmnopqrst"));
  223. test(S("abcdefghijklmnopqrst"), 10, 0, "12345", S("abcdefghij12345klmnopqrst"));
  224. test(S("abcdefghijklmnopqrst"), 10, 0, "1234567890", S("abcdefghij1234567890klmnopqrst"));
  225. test(S("abcdefghijklmnopqrst"), 10, 0, "12345678901234567890", S("abcdefghij12345678901234567890klmnopqrst"));
  226. test(S("abcdefghijklmnopqrst"), 10, 1, "", S("abcdefghijlmnopqrst"));
  227. test(S("abcdefghijklmnopqrst"), 10, 1, "12345", S("abcdefghij12345lmnopqrst"));
  228. test(S("abcdefghijklmnopqrst"), 10, 1, "1234567890", S("abcdefghij1234567890lmnopqrst"));
  229. test(S("abcdefghijklmnopqrst"), 10, 1, "12345678901234567890", S("abcdefghij12345678901234567890lmnopqrst"));
  230. test(S("abcdefghijklmnopqrst"), 10, 5, "", S("abcdefghijpqrst"));
  231. test(S("abcdefghijklmnopqrst"), 10, 5, "12345", S("abcdefghij12345pqrst"));
  232. test(S("abcdefghijklmnopqrst"), 10, 5, "1234567890", S("abcdefghij1234567890pqrst"));
  233. test(S("abcdefghijklmnopqrst"), 10, 5, "12345678901234567890", S("abcdefghij12345678901234567890pqrst"));
  234. test(S("abcdefghijklmnopqrst"), 10, 9, "", S("abcdefghijt"));
  235. test(S("abcdefghijklmnopqrst"), 10, 9, "12345", S("abcdefghij12345t"));
  236. test(S("abcdefghijklmnopqrst"), 10, 9, "1234567890", S("abcdefghij1234567890t"));
  237. test(S("abcdefghijklmnopqrst"), 10, 9, "12345678901234567890", S("abcdefghij12345678901234567890t"));
  238. }
  239. template <class S>
  240. void test2()
  241. {
  242. test(S("abcdefghijklmnopqrst"), 10, 10, "", S("abcdefghij"));
  243. test(S("abcdefghijklmnopqrst"), 10, 10, "12345", S("abcdefghij12345"));
  244. test(S("abcdefghijklmnopqrst"), 10, 10, "1234567890", S("abcdefghij1234567890"));
  245. test(S("abcdefghijklmnopqrst"), 10, 10, "12345678901234567890", S("abcdefghij12345678901234567890"));
  246. test(S("abcdefghijklmnopqrst"), 19, 0, "", S("abcdefghijklmnopqrst"));
  247. test(S("abcdefghijklmnopqrst"), 19, 0, "12345", S("abcdefghijklmnopqrs12345t"));
  248. test(S("abcdefghijklmnopqrst"), 19, 0, "1234567890", S("abcdefghijklmnopqrs1234567890t"));
  249. test(S("abcdefghijklmnopqrst"), 19, 0, "12345678901234567890", S("abcdefghijklmnopqrs12345678901234567890t"));
  250. test(S("abcdefghijklmnopqrst"), 19, 1, "", S("abcdefghijklmnopqrs"));
  251. test(S("abcdefghijklmnopqrst"), 19, 1, "12345", S("abcdefghijklmnopqrs12345"));
  252. test(S("abcdefghijklmnopqrst"), 19, 1, "1234567890", S("abcdefghijklmnopqrs1234567890"));
  253. test(S("abcdefghijklmnopqrst"), 19, 1, "12345678901234567890", S("abcdefghijklmnopqrs12345678901234567890"));
  254. test(S("abcdefghijklmnopqrst"), 20, 0, "", S("abcdefghijklmnopqrst"));
  255. test(S("abcdefghijklmnopqrst"), 20, 0, "12345", S("abcdefghijklmnopqrst12345"));
  256. test(S("abcdefghijklmnopqrst"), 20, 0, "1234567890", S("abcdefghijklmnopqrst1234567890"));
  257. test(S("abcdefghijklmnopqrst"), 20, 0, "12345678901234567890", S("abcdefghijklmnopqrst12345678901234567890"));
  258. }
  259. int main()
  260. {
  261. {
  262. typedef std::string S;
  263. test0<S>();
  264. test1<S>();
  265. test2<S>();
  266. }
  267. #if TEST_STD_VER >= 11
  268. {
  269. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  270. test0<S>();
  271. test1<S>();
  272. test2<S>();
  273. }
  274. #endif
  275. { // test replacing into self
  276. typedef std::string S;
  277. S s_short = "123/";
  278. S s_long = "Lorem ipsum dolor sit amet, consectetur/";
  279. s_short.replace(s_short.begin(), s_short.begin(), s_short.c_str());
  280. assert(s_short == "123/123/");
  281. s_short.replace(s_short.begin(), s_short.begin(), s_short.c_str());
  282. assert(s_short == "123/123/123/123/");
  283. s_short.replace(s_short.begin(), s_short.begin(), s_short.c_str());
  284. assert(s_short == "123/123/123/123/123/123/123/123/");
  285. s_long.replace(s_long.begin(), s_long.begin(), s_long.c_str());
  286. assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
  287. }
  288. }