soft_iterator_invalidation.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // Demonstration of rules when an iterator is considered to be valid if the soft
  2. // iterator invalidation definition is applied.
  3. // Note: The soft iterator invalidation definition CAN NOT be applied
  4. // to the space optimized circular buffer.
  5. // Copyright (c) 2003-2008 Jan Gaspar
  6. // Use, modification, and distribution is subject to the Boost Software
  7. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #include "test.hpp"
  10. // test of the example (introduced in the documentation)
  11. void validity_example_test() {
  12. circular_buffer<int> cb(3);
  13. cb.push_back(1);
  14. cb.push_back(2);
  15. cb.push_back(3);
  16. circular_buffer<int>::iterator it = cb.begin();
  17. BOOST_TEST(*it == 1);
  18. cb.push_back(4);
  19. BOOST_TEST(*it == 4);
  20. }
  21. void validity_insert_test() {
  22. int array[] = { 1, 2, 3 };
  23. // memory placement: { 1, 2, 3 }
  24. // circular buffer: { 1, 2, 3 }
  25. circular_buffer<int> cb(4, array, array + 3);
  26. // it1 -> 1, it2 -> 2, it3 -> 3
  27. circular_buffer<int>::iterator it1 = cb.begin();
  28. circular_buffer<int>::iterator it2 = cb.begin() + 1;
  29. circular_buffer<int>::iterator it3 = cb.begin() + 2;
  30. cb.insert(cb.begin() + 1, 4);
  31. // memory placement: { 1, 4, 2, 3 }
  32. // circular buffer: { 1, 4, 2, 3 }
  33. BOOST_TEST(*it1 == 1);
  34. BOOST_TEST(*it2 == 4);
  35. BOOST_TEST(*it3 == 2);
  36. BOOST_TEST(cb[0] == 1);
  37. BOOST_TEST(cb[1] == 4);
  38. BOOST_TEST(cb[2] == 2);
  39. BOOST_TEST(cb[3] == 3);
  40. // it4 -> 3
  41. circular_buffer<int>::iterator it4 = it1 + 3;
  42. cb.insert(cb.begin() + 1, 5);
  43. // memory placement: { 3, 5, 4, 2 }
  44. // circular buffer: { 5, 4, 2, 3 }
  45. BOOST_TEST(*it1 == 3);
  46. BOOST_TEST(*it2 == 5);
  47. BOOST_TEST(*it3 == 4);
  48. BOOST_TEST(*it4 == 2);
  49. BOOST_TEST(cb[0] == 5);
  50. BOOST_TEST(cb[1] == 4);
  51. BOOST_TEST(cb[2] == 2);
  52. BOOST_TEST(cb[3] == 3);
  53. }
  54. void validity_insert_n_test() {
  55. // memory placement: { 1, 2, 3 }
  56. // circular buffer: { 1, 2, 3 }
  57. circular_buffer<int> cb(5);
  58. cb.push_back(1);
  59. cb.push_back(2);
  60. cb.push_back(3);
  61. // it1 -> 1, it2 -> 2, it3 -> 3
  62. circular_buffer<int>::iterator it1 = cb.begin();
  63. circular_buffer<int>::iterator it2 = cb.begin() + 1;
  64. circular_buffer<int>::iterator it3 = cb.begin() + 2;
  65. cb.insert(cb.begin() + 1, 2, 4);
  66. // memory placement: { 1, 4, 4, 2, 3 }
  67. // circular buffer: { 1, 4, 4, 2, 3 }
  68. BOOST_TEST(*it1 == 1);
  69. BOOST_TEST(*it2 == 4);
  70. BOOST_TEST(*it3 == 4);
  71. BOOST_TEST(cb[0] == 1);
  72. BOOST_TEST(cb[1] == 4);
  73. BOOST_TEST(cb[2] == 4);
  74. BOOST_TEST(cb[3] == 2);
  75. BOOST_TEST(cb[4] == 3);
  76. // it4 -> 2, it5 -> 3
  77. circular_buffer<int>::iterator it4 = it1 + 3;
  78. circular_buffer<int>::iterator it5 = it1 + 4;
  79. cb.insert(cb.begin() + 1, 2, 5);
  80. // memory placement: { 3, 5, 4, 4, 2 } - 5 inserted only once
  81. // circular buffer: { 5, 4, 4, 2, 3 }
  82. BOOST_TEST(*it1 == 3);
  83. BOOST_TEST(*it2 == 5);
  84. BOOST_TEST(*it3 == 4);
  85. BOOST_TEST(*it4 == 4);
  86. BOOST_TEST(*it5 == 2);
  87. BOOST_TEST(cb[0] == 5);
  88. BOOST_TEST(cb[1] == 4);
  89. BOOST_TEST(cb[2] == 4);
  90. BOOST_TEST(cb[3] == 2);
  91. BOOST_TEST(cb[4] == 3);
  92. }
  93. void validity_insert_range_test() {
  94. vector<int> v1;
  95. v1.push_back(4);
  96. v1.push_back(5);
  97. vector<int> v2;
  98. v2.push_back(6);
  99. v2.push_back(7);
  100. // memory placement: { 1, 2, 3 }
  101. // circular buffer: { 1, 2, 3 }
  102. circular_buffer<int> cb1(5);
  103. cb1.push_back(1);
  104. cb1.push_back(2);
  105. cb1.push_back(3);
  106. // it11 -> 1, it12 -> 2, it13 -> 3
  107. circular_buffer<int>::iterator it11 = cb1.begin();
  108. circular_buffer<int>::iterator it12 = cb1.begin() + 1;
  109. circular_buffer<int>::iterator it13 = cb1.begin() + 2;
  110. cb1.insert(cb1.begin() + 1, v1.begin(), v1.end());
  111. // memory placement: { 1, 4, 5, 2, 3 }
  112. // circular buffer: { 1, 4, 5, 2, 3 }
  113. BOOST_TEST(*it11 == 1);
  114. BOOST_TEST(*it12 == 4);
  115. BOOST_TEST(*it13 == 5);
  116. BOOST_TEST(cb1[0] == 1);
  117. BOOST_TEST(cb1[1] == 4);
  118. BOOST_TEST(cb1[2] == 5);
  119. BOOST_TEST(cb1[3] == 2);
  120. BOOST_TEST(cb1[4] == 3);
  121. // it14 -> 2, it15 -> 3
  122. circular_buffer<int>::iterator it14 = it11 + 3;
  123. circular_buffer<int>::iterator it15 = it11 + 4;
  124. cb1.insert(cb1.begin() + 1, v2.begin(), v2.end());
  125. // memory placement: { 3, 7, 4, 5, 2 } - 7 inserted only
  126. // circular buffer: { 7, 4, 5, 2, 3 }
  127. BOOST_TEST(*it11 == 3);
  128. BOOST_TEST(*it12 == 7);
  129. BOOST_TEST(*it13 == 4);
  130. BOOST_TEST(*it14 == 5);
  131. BOOST_TEST(*it15 == 2);
  132. BOOST_TEST(cb1[0] == 7);
  133. BOOST_TEST(cb1[1] == 4);
  134. BOOST_TEST(cb1[2] == 5);
  135. BOOST_TEST(cb1[3] == 2);
  136. BOOST_TEST(cb1[4] == 3);
  137. // memory placement: { 1, 2, 3 }
  138. // circular buffer: { 1, 2, 3 }
  139. circular_buffer<int> cb2(5);
  140. cb2.push_back(1);
  141. cb2.push_back(2);
  142. cb2.push_back(3);
  143. // it21 -> 1, it22 -> 2, it23 -> 3
  144. circular_buffer<int>::iterator it21 = cb2.begin();
  145. circular_buffer<int>::iterator it22 = cb2.begin() + 1;
  146. circular_buffer<int>::iterator it23 = cb2.begin() + 2;
  147. cb2.insert(cb2.begin() + 1, MyInputIterator(v1.begin()), MyInputIterator(v1.end()));
  148. // memory placement: { 1, 4, 5, 2, 3 }
  149. // circular buffer: { 1, 4, 5, 2, 3 }
  150. BOOST_TEST(*it21 == 1);
  151. BOOST_TEST(*it22 == 4);
  152. BOOST_TEST(*it23 == 5);
  153. BOOST_TEST(cb2[0] == 1);
  154. BOOST_TEST(cb2[1] == 4);
  155. BOOST_TEST(cb2[2] == 5);
  156. BOOST_TEST(cb2[3] == 2);
  157. BOOST_TEST(cb2[4] == 3);
  158. // it24 -> 2, it25 -> 3
  159. circular_buffer<int>::iterator it24 = it21 + 3;
  160. circular_buffer<int>::iterator it25 = it21 + 4;
  161. cb2.insert(cb2.begin() + 1, MyInputIterator(v2.begin()), MyInputIterator(v2.end()));
  162. // memory placement: { 2, 3, 7, 4, 5 } - using input iterator inserts all items even if they are later replaced
  163. // circular buffer: { 7, 4, 5, 2, 3 }
  164. BOOST_TEST(*it21 == 2);
  165. BOOST_TEST(*it22 == 3);
  166. BOOST_TEST(*it23 == 7);
  167. BOOST_TEST(*it24 == 4);
  168. BOOST_TEST(*it25 == 5);
  169. BOOST_TEST(cb2[0] == 7);
  170. BOOST_TEST(cb2[1] == 4);
  171. BOOST_TEST(cb2[2] == 5);
  172. BOOST_TEST(cb2[3] == 2);
  173. BOOST_TEST(cb2[4] == 3);
  174. }
  175. void validity_rinsert_test() {
  176. int array[] = { 1, 2, 3 };
  177. // memory placement: { 1, 2, 3 }
  178. // circular buffer: { 1, 2, 3 }
  179. circular_buffer<int> cb(4, array, array + 3);
  180. // it1 -> 1, it2 -> 2, it3 -> 3
  181. circular_buffer<int>::iterator it1 = cb.begin();
  182. circular_buffer<int>::iterator it2 = cb.begin() + 1;
  183. circular_buffer<int>::iterator it3 = cb.begin() + 2;
  184. cb.rinsert(cb.begin() + 2, 4);
  185. // memory placement: { 2, 4, 3, 1 }
  186. // circular buffer: { 1, 2, 4, 3 }
  187. BOOST_TEST(*it1 == 2);
  188. BOOST_TEST(*it2 == 4);
  189. BOOST_TEST(*it3 == 3);
  190. BOOST_TEST(cb[0] == 1);
  191. BOOST_TEST(cb[1] == 2);
  192. BOOST_TEST(cb[2] == 4);
  193. BOOST_TEST(cb[3] == 3);
  194. // it4 -> 1
  195. circular_buffer<int>::iterator it4 = it1 - 1;
  196. cb.rinsert(cb.begin() + 2, 5);
  197. // memory placement: { 5, 4, 1, 2 }
  198. // circular buffer: { 1, 2, 5, 4 }
  199. BOOST_TEST(*it1 == 5);
  200. BOOST_TEST(*it2 == 4);
  201. BOOST_TEST(*it3 == 1);
  202. BOOST_TEST(*it4 == 2);
  203. BOOST_TEST(cb[0] == 1);
  204. BOOST_TEST(cb[1] == 2);
  205. BOOST_TEST(cb[2] == 5);
  206. BOOST_TEST(cb[3] == 4);
  207. }
  208. void validity_rinsert_n_test() {
  209. // memory placement: { 1, 2, 3 }
  210. // circular buffer: { 1, 2, 3 }
  211. circular_buffer<int> cb(5);
  212. cb.push_back(1);
  213. cb.push_back(2);
  214. cb.push_back(3);
  215. // it1 -> 1, it2 -> 2, it3 -> 3
  216. circular_buffer<int>::iterator it1 = cb.begin();
  217. circular_buffer<int>::iterator it2 = cb.begin() + 1;
  218. circular_buffer<int>::iterator it3 = cb.begin() + 2;
  219. cb.rinsert(cb.begin() + 2, 2, 4);
  220. // memory placement: { 4, 4, 3, 1, 2 }
  221. // circular buffer: { 1, 2, 4, 4, 3 }
  222. BOOST_TEST(*it1 == 4);
  223. BOOST_TEST(*it2 == 4);
  224. BOOST_TEST(*it3 == 3);
  225. BOOST_TEST(cb[0] == 1);
  226. BOOST_TEST(cb[1] == 2);
  227. BOOST_TEST(cb[2] == 4);
  228. BOOST_TEST(cb[3] == 4);
  229. BOOST_TEST(cb[4] == 3);
  230. // it4 -> 1, it5 -> 2
  231. circular_buffer<int>::iterator it4 = it1 - 2;
  232. circular_buffer<int>::iterator it5 = it1 - 1;
  233. cb.rinsert(cb.begin() + 4, 2, 5);
  234. // memory placement: { 4, 5, 1, 2, 4 } - 5 inserted only once
  235. // circular buffer: { 1, 2, 4, 4, 5 }
  236. BOOST_TEST(*it1 == 4);
  237. BOOST_TEST(*it2 == 5);
  238. BOOST_TEST(*it3 == 1);
  239. BOOST_TEST(*it4 == 2);
  240. BOOST_TEST(*it5 == 4);
  241. BOOST_TEST(cb[0] == 1);
  242. BOOST_TEST(cb[1] == 2);
  243. BOOST_TEST(cb[2] == 4);
  244. BOOST_TEST(cb[3] == 4);
  245. BOOST_TEST(cb[4] == 5);
  246. }
  247. void validity_rinsert_range_test() {
  248. vector<int> v1;
  249. v1.push_back(4);
  250. v1.push_back(5);
  251. vector<int> v2;
  252. v2.push_back(6);
  253. v2.push_back(7);
  254. // memory placement: { 1, 2, 3 }
  255. // circular buffer: { 1, 2, 3 }
  256. circular_buffer<int> cb1(5);
  257. cb1.push_back(1);
  258. cb1.push_back(2);
  259. cb1.push_back(3);
  260. // it1 -> 1, it2 -> 2, it3 -> 3
  261. circular_buffer<int>::iterator it11 = cb1.begin();
  262. circular_buffer<int>::iterator it12 = cb1.begin() + 1;
  263. circular_buffer<int>::iterator it13 = cb1.begin() + 2;
  264. cb1.rinsert(cb1.begin() + 2, v1.begin(), v1.end());
  265. // memory placement: { 4, 5, 3, 1, 2 }
  266. // circular buffer: { 1, 2, 4, 5, 3 }
  267. BOOST_TEST(*it11 == 4);
  268. BOOST_TEST(*it12 == 5);
  269. BOOST_TEST(*it13 == 3);
  270. BOOST_TEST(cb1[0] == 1);
  271. BOOST_TEST(cb1[1] == 2);
  272. BOOST_TEST(cb1[2] == 4);
  273. BOOST_TEST(cb1[3] == 5);
  274. BOOST_TEST(cb1[4] == 3);
  275. // it14 -> 1, it15 -> 2
  276. circular_buffer<int>::iterator it14 = it11 - 2;
  277. circular_buffer<int>::iterator it15 = it11 - 1;
  278. cb1.rinsert(cb1.begin() + 4, v2.begin(), v2.end());
  279. // memory placement: { 5, 6, 1, 2, 4 } - 6 inserted only
  280. // circular buffer: { 1, 2, 4, 5, 6 }
  281. BOOST_TEST(*it11 == 5);
  282. BOOST_TEST(*it12 == 6);
  283. BOOST_TEST(*it13 == 1);
  284. BOOST_TEST(*it14 == 2);
  285. BOOST_TEST(*it15 == 4);
  286. BOOST_TEST(cb1[0] == 1);
  287. BOOST_TEST(cb1[1] == 2);
  288. BOOST_TEST(cb1[2] == 4);
  289. BOOST_TEST(cb1[3] == 5);
  290. BOOST_TEST(cb1[4] == 6);
  291. // memory placement: { 1, 2, 3 }
  292. // circular buffer: { 1, 2, 3 }
  293. circular_buffer<int> cb2(5);
  294. cb2.push_back(1);
  295. cb2.push_back(2);
  296. cb2.push_back(3);
  297. // it1 -> 1, it2 -> 2, it3 -> 3
  298. circular_buffer<int>::iterator it21 = cb2.begin();
  299. circular_buffer<int>::iterator it22 = cb2.begin() + 1;
  300. circular_buffer<int>::iterator it23 = cb2.begin() + 2;
  301. cb2.rinsert(cb2.begin() + 2, MyInputIterator(v1.begin()), MyInputIterator(v1.end()));
  302. // memory placement: { 4, 5, 3, 1, 2 }
  303. // circular buffer: { 1, 2, 4, 5, 3 }
  304. BOOST_TEST(*it21 == 4);
  305. BOOST_TEST(*it22 == 5);
  306. BOOST_TEST(*it23 == 3);
  307. BOOST_TEST(cb2[0] == 1);
  308. BOOST_TEST(cb2[1] == 2);
  309. BOOST_TEST(cb2[2] == 4);
  310. BOOST_TEST(cb2[3] == 5);
  311. BOOST_TEST(cb2[4] == 3);
  312. // it24 -> 1, it25 -> 2
  313. circular_buffer<int>::iterator it24 = it21 - 2;
  314. circular_buffer<int>::iterator it25 = it21 - 1;
  315. cb2.rinsert(cb2.begin() + 4, MyInputIterator(v2.begin()), MyInputIterator(v2.end()));
  316. // memory placement: { 5, 6, 1, 2, 4 }
  317. // circular buffer: { 1, 2, 4, 5, 6 }
  318. BOOST_TEST(*it21 == 5);
  319. BOOST_TEST(*it22 == 6);
  320. BOOST_TEST(*it23 == 1);
  321. BOOST_TEST(*it24 == 2);
  322. BOOST_TEST(*it25 == 4);
  323. BOOST_TEST(cb2[0] == 1);
  324. BOOST_TEST(cb2[1] == 2);
  325. BOOST_TEST(cb2[2] == 4);
  326. BOOST_TEST(cb2[3] == 5);
  327. BOOST_TEST(cb2[4] == 6);
  328. }
  329. void validity_erase_test() {
  330. // memory placement: { 4, 5, 1, 2, 3 }
  331. // circular buffer: { 1, 2, 3, 4, 5 }
  332. circular_buffer<int> cb(5);
  333. cb.push_back(-1);
  334. cb.push_back(0);
  335. cb.push_back(1);
  336. cb.push_back(2);
  337. cb.push_back(3);
  338. cb.push_back(4);
  339. cb.push_back(5);
  340. // it1 -> 1, it2 -> 2, it3 -> 3, it4 -> 4
  341. circular_buffer<int>::iterator it1 = cb.begin();
  342. circular_buffer<int>::iterator it2 = cb.begin() + 1;
  343. circular_buffer<int>::iterator it3 = cb.begin() + 2;
  344. circular_buffer<int>::iterator it4 = cb.begin() + 3;
  345. cb.erase(cb.begin() + 1);
  346. // memory placement: { 5, X, 1, 3, 4 }
  347. // circular buffer: { 1, 3, 4, 5 }
  348. BOOST_TEST(*it1 == 1);
  349. BOOST_TEST(*it2 == 3);
  350. BOOST_TEST(*it3 == 4);
  351. BOOST_TEST(*it4 == 5);
  352. BOOST_TEST(cb[0] == 1);
  353. BOOST_TEST(cb[1] == 3);
  354. BOOST_TEST(cb[2] == 4);
  355. BOOST_TEST(cb[3] == 5);
  356. }
  357. void validity_erase_range_test() {
  358. // memory placement: { 4, 5, 6, 1, 2, 3 }
  359. // circular buffer: { 1, 2, 3, 4, 5, 6 }
  360. circular_buffer<int> cb(6);
  361. cb.push_back(-2);
  362. cb.push_back(-1);
  363. cb.push_back(0);
  364. cb.push_back(1);
  365. cb.push_back(2);
  366. cb.push_back(3);
  367. cb.push_back(4);
  368. cb.push_back(5);
  369. cb.push_back(6);
  370. // it1 -> 1, it2 -> 2, it3 -> 3, it4 -> 4
  371. circular_buffer<int>::iterator it1 = cb.begin();
  372. circular_buffer<int>::iterator it2 = cb.begin() + 1;
  373. circular_buffer<int>::iterator it3 = cb.begin() + 2;
  374. circular_buffer<int>::iterator it4 = cb.begin() + 3;
  375. cb.erase(cb.begin() + 2, cb.begin() + 4);
  376. // memory placement: { 6, X, X, 1, 2, 5 }
  377. // circular buffer: { 1, 2, 5, 6 }
  378. BOOST_TEST(*it1 == 1);
  379. BOOST_TEST(*it2 == 2);
  380. BOOST_TEST(*it3 == 5);
  381. BOOST_TEST(*it4 == 6);
  382. BOOST_TEST(cb[0] == 1);
  383. BOOST_TEST(cb[1] == 2);
  384. BOOST_TEST(cb[2] == 5);
  385. BOOST_TEST(cb[3] == 6);
  386. }
  387. void validity_rerase_test() {
  388. // memory placement: { 4, 5, 1, 2, 3 }
  389. // circular buffer: { 1, 2, 3, 4, 5 }
  390. circular_buffer<int> cb(5);
  391. cb.push_back(-1);
  392. cb.push_back(0);
  393. cb.push_back(1);
  394. cb.push_back(2);
  395. cb.push_back(3);
  396. cb.push_back(4);
  397. cb.push_back(5);
  398. // it1 -> 2, it2 -> 3, it3 -> 4, it4 -> 5
  399. circular_buffer<int>::iterator it1 = cb.begin() + 1;
  400. circular_buffer<int>::iterator it2 = cb.begin() + 2;
  401. circular_buffer<int>::iterator it3 = cb.begin() + 3;
  402. circular_buffer<int>::iterator it4 = cb.begin() + 4;
  403. cb.rerase(cb.begin() + 1);
  404. // memory placement: { 4, 5, X, 1, 3 }
  405. // circular buffer: { 1, 3, 4, 5 }
  406. BOOST_TEST(*it1 == 1);
  407. BOOST_TEST(*it2 == 3);
  408. BOOST_TEST(*it3 == 4);
  409. BOOST_TEST(*it4 == 5);
  410. BOOST_TEST(cb[0] == 1);
  411. BOOST_TEST(cb[1] == 3);
  412. BOOST_TEST(cb[2] == 4);
  413. BOOST_TEST(cb[3] == 5);
  414. }
  415. void validity_rerase_range_test() {
  416. // memory placement: { 4, 5, 6, 1, 2, 3 }
  417. // circular buffer: { 1, 2, 3, 4, 5, 6 }
  418. circular_buffer<int> cb(6);
  419. cb.push_back(-2);
  420. cb.push_back(-1);
  421. cb.push_back(0);
  422. cb.push_back(1);
  423. cb.push_back(2);
  424. cb.push_back(3);
  425. cb.push_back(4);
  426. cb.push_back(5);
  427. cb.push_back(6);
  428. // it1 -> 3, it2 -> 4, it3 -> 5, it4 -> 6
  429. circular_buffer<int>::iterator it1 = cb.begin() + 2;
  430. circular_buffer<int>::iterator it2 = cb.begin() + 3;
  431. circular_buffer<int>::iterator it3 = cb.begin() + 4;
  432. circular_buffer<int>::iterator it4 = cb.begin() + 5;
  433. cb.rerase(cb.begin() + 2, cb.begin() + 4);
  434. // memory placement: { 2, 5, 6, X, X, 1 }
  435. // circular buffer: { 1, 2, 5, 6 }
  436. BOOST_TEST(*it1 == 1);
  437. BOOST_TEST(*it2 == 2);
  438. BOOST_TEST(*it3 == 5);
  439. BOOST_TEST(*it4 == 6);
  440. BOOST_TEST(cb[0] == 1);
  441. BOOST_TEST(cb[1] == 2);
  442. BOOST_TEST(cb[2] == 5);
  443. BOOST_TEST(cb[3] == 6);
  444. }
  445. void validity_linearize_test() {
  446. // memory placement: { 3, 1, 2 }
  447. // circular buffer: { 1, 2, 3 }
  448. circular_buffer<int> cb(3);
  449. cb.push_back(0);
  450. cb.push_back(1);
  451. cb.push_back(2);
  452. cb.push_back(3);
  453. // it1 -> 1, it2 -> 2, it3 -> 3
  454. circular_buffer<int>::iterator it1 = cb.begin();
  455. circular_buffer<int>::iterator it2 = cb.begin() + 1;
  456. circular_buffer<int>::iterator it3 = cb.begin() + 2;
  457. cb.linearize();
  458. // memory placement: { 1, 2, 3 }
  459. // circular buffer: { 1, 2, 3 }
  460. BOOST_TEST(*it1 == 2);
  461. BOOST_TEST(*it2 == 3);
  462. BOOST_TEST(*it3 == 1);
  463. BOOST_TEST(cb[0] == 1);
  464. BOOST_TEST(cb[1] == 2);
  465. BOOST_TEST(cb[2] == 3);
  466. }
  467. void validity_swap_test() {
  468. // memory placement: { 3, 1, 2 }
  469. // circular buffer: { 1, 2, 3 }
  470. circular_buffer<int> cb1(3);
  471. cb1.push_back(0);
  472. cb1.push_back(1);
  473. cb1.push_back(2);
  474. cb1.push_back(3);
  475. // it11 -> 1, it12 -> 2, it13 -> 3
  476. circular_buffer<int>::iterator it11 = cb1.begin();
  477. circular_buffer<int>::iterator it12 = cb1.begin() + 1;
  478. circular_buffer<int>::iterator it13 = cb1.begin() + 2;
  479. // memory placement: { 4, 5, 6 }
  480. // circular buffer: { 4, 5, 6 }
  481. circular_buffer<int> cb2(5);
  482. cb2.push_back(4);
  483. cb2.push_back(5);
  484. cb2.push_back(6);
  485. // it21 -> 4, it22 -> 5, it23 -> 6
  486. circular_buffer<int>::iterator it21 = cb2.begin();
  487. circular_buffer<int>::iterator it22 = cb2.begin() + 1;
  488. circular_buffer<int>::iterator it23 = cb2.begin() + 2;
  489. cb1.swap(cb2);
  490. // Although iterators refer to the original elements,
  491. // their internal state is inconsistent and no other operation
  492. // (except dereferencing) can be invoked on them any more.
  493. BOOST_TEST(*it11 == 1);
  494. BOOST_TEST(*it12 == 2);
  495. BOOST_TEST(*it13 == 3);
  496. BOOST_TEST(*it21 == 4);
  497. BOOST_TEST(*it22 == 5);
  498. BOOST_TEST(*it23 == 6);
  499. BOOST_TEST(cb1[0] == 4);
  500. BOOST_TEST(cb1[1] == 5);
  501. BOOST_TEST(cb1[2] == 6);
  502. BOOST_TEST(cb2[0] == 1);
  503. BOOST_TEST(cb2[1] == 2);
  504. BOOST_TEST(cb2[2] == 3);
  505. }
  506. void validity_push_back_test() {
  507. // memory placement: { 3, 1, 2 }
  508. // circular buffer: { 1, 2, 3 }
  509. circular_buffer<int> cb(3);
  510. cb.push_back(0);
  511. cb.push_back(1);
  512. cb.push_back(2);
  513. cb.push_back(3);
  514. // it1 -> 1, it2 -> 2, it3 -> 3
  515. circular_buffer<int>::iterator it1 = cb.begin();
  516. circular_buffer<int>::iterator it2 = cb.begin() + 1;
  517. circular_buffer<int>::iterator it3 = cb.begin() + 2;
  518. cb.push_back(4);
  519. // memory placement: { 3, 4, 2 }
  520. // circular buffer: { 2, 3, 4 }
  521. BOOST_TEST(*it1 == 4);
  522. BOOST_TEST(*it2 == 2);
  523. BOOST_TEST(*it3 == 3);
  524. BOOST_TEST(cb[0] == 2);
  525. BOOST_TEST(cb[1] == 3);
  526. BOOST_TEST(cb[2] == 4);
  527. }
  528. void validity_push_front_test() {
  529. // memory placement: { 3, 1, 2 }
  530. // circular buffer: { 1, 2, 3 }
  531. circular_buffer<int> cb(3);
  532. cb.push_back(0);
  533. cb.push_back(1);
  534. cb.push_back(2);
  535. cb.push_back(3);
  536. // it1 -> 1, it2 -> 2, it3 -> 3
  537. circular_buffer<int>::iterator it1 = cb.begin();
  538. circular_buffer<int>::iterator it2 = cb.begin() + 1;
  539. circular_buffer<int>::iterator it3 = cb.begin() + 2;
  540. cb.push_front(4);
  541. // memory placement: { 4, 1, 2 }
  542. // circular buffer: { 4, 1, 2 }
  543. BOOST_TEST(*it1 == 1);
  544. BOOST_TEST(*it2 == 2);
  545. BOOST_TEST(*it3 == 4);
  546. BOOST_TEST(cb[0] == 4);
  547. BOOST_TEST(cb[1] == 1);
  548. BOOST_TEST(cb[2] == 2);
  549. }
  550. void validity_pop_back_test() {
  551. // memory placement: { 3, 1, 2 }
  552. // circular buffer: { 1, 2, 3 }
  553. circular_buffer<int> cb(3);
  554. cb.push_back(0);
  555. cb.push_back(1);
  556. cb.push_back(2);
  557. cb.push_back(3);
  558. // it1 -> 1, it2 -> 2
  559. circular_buffer<int>::iterator it1 = cb.begin();
  560. circular_buffer<int>::iterator it2 = cb.begin() + 1;
  561. cb.pop_back();
  562. // memory placement: { X, 1, 2 }
  563. // circular buffer: { 1, 2 }
  564. BOOST_TEST(*it1 == 1);
  565. BOOST_TEST(*it2 == 2);
  566. BOOST_TEST(cb[0] == 1);
  567. BOOST_TEST(cb[1] == 2);
  568. }
  569. void validity_pop_front_test() {
  570. // memory placement: { 3, 1, 2 }
  571. // circular buffer: { 1, 2, 3 }
  572. circular_buffer<int> cb(3);
  573. cb.push_back(0);
  574. cb.push_back(1);
  575. cb.push_back(2);
  576. cb.push_back(3);
  577. // it1 -> 2, it2 -> 3
  578. circular_buffer<int>::iterator it1 = cb.begin() + 1;
  579. circular_buffer<int>::iterator it2 = cb.begin() + 2;
  580. cb.pop_front();
  581. // memory placement: { 3, X, 2 }
  582. // circular buffer: { 2, 3 }
  583. BOOST_TEST(*it1 == 2);
  584. BOOST_TEST(*it2 == 3);
  585. BOOST_TEST(cb[0] == 2);
  586. BOOST_TEST(cb[1] == 3);
  587. }
  588. // test main
  589. int main()
  590. {
  591. validity_example_test();
  592. validity_insert_test();
  593. validity_insert_n_test();
  594. validity_insert_range_test();
  595. validity_rinsert_test();
  596. validity_rinsert_n_test();
  597. validity_rinsert_range_test();
  598. validity_erase_test();
  599. validity_erase_range_test();
  600. validity_rerase_test();
  601. validity_rerase_range_test();
  602. validity_linearize_test();
  603. validity_swap_test();
  604. validity_push_back_test();
  605. validity_push_front_test();
  606. validity_pop_back_test();
  607. validity_pop_front_test();
  608. return boost::report_errors();
  609. }