IndexedListTest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #include "IndexedList.h"
  2. #include <vector>
  3. #include <deque>
  4. #include <iostream>
  5. #include <cppunit/extensions/HelperMacros.h>
  6. #include "TestUtil.h"
  7. #include "array_fun.h"
  8. #include "TimerA2.h"
  9. namespace aria2 {
  10. class IndexedListTest : public CppUnit::TestFixture {
  11. CPPUNIT_TEST_SUITE(IndexedListTest);
  12. CPPUNIT_TEST(testPushBack);
  13. CPPUNIT_TEST(testPushFront);
  14. CPPUNIT_TEST(testRemove);
  15. CPPUNIT_TEST(testErase);
  16. CPPUNIT_TEST(testPopFront);
  17. CPPUNIT_TEST(testMove);
  18. CPPUNIT_TEST(testGet);
  19. CPPUNIT_TEST(testInsert);
  20. CPPUNIT_TEST(testInsert_keyFunc);
  21. CPPUNIT_TEST(testIterator);
  22. CPPUNIT_TEST(testRemoveIf);
  23. CPPUNIT_TEST_SUITE_END();
  24. public:
  25. void setUp() {}
  26. void testPushBack();
  27. void testPushFront();
  28. void testRemove();
  29. void testErase();
  30. void testPopFront();
  31. void testMove();
  32. void testGet();
  33. void testInsert();
  34. void testInsert_keyFunc();
  35. void testIterator();
  36. void testRemoveIf();
  37. };
  38. CPPUNIT_TEST_SUITE_REGISTRATION(IndexedListTest);
  39. void IndexedListTest::testPushBack()
  40. {
  41. int a[] = {1, 2, 3, 4, 5};
  42. IndexedList<int, int*> list;
  43. for (int i = 0; i < 5; ++i) {
  44. CPPUNIT_ASSERT(list.push_back(i, &a[i]));
  45. }
  46. for (int i = 0; i < 5; ++i) {
  47. CPPUNIT_ASSERT_EQUAL(a[i], *list.get(i));
  48. }
  49. int ai = 0;
  50. for (IndexedList<int, int*>::iterator i = list.begin(); i != list.end();
  51. ++i) {
  52. CPPUNIT_ASSERT_EQUAL(a[ai++], **i);
  53. }
  54. }
  55. void IndexedListTest::testPushFront()
  56. {
  57. int a[] = {1, 2, 3, 4, 5};
  58. IndexedList<int, int*> list;
  59. for (int i = 0; i < 5; ++i) {
  60. CPPUNIT_ASSERT(list.push_front(i, &a[i]));
  61. }
  62. for (int i = 0; i < 5; ++i) {
  63. CPPUNIT_ASSERT_EQUAL(a[i], *list.get(i));
  64. }
  65. int ai = 4;
  66. for (IndexedList<int, int*>::iterator i = list.begin(); i != list.end();
  67. ++i) {
  68. CPPUNIT_ASSERT_EQUAL(a[ai--], **i);
  69. }
  70. }
  71. void IndexedListTest::testRemove()
  72. {
  73. int a[] = {1, 2, 3, 4, 5};
  74. IndexedList<int, int*> list;
  75. for (int i = 0; i < 5; ++i) {
  76. list.push_back(i, &a[i]);
  77. }
  78. for (int i = 0; i < 5; ++i) {
  79. CPPUNIT_ASSERT(list.remove(i));
  80. CPPUNIT_ASSERT_EQUAL((size_t)5 - i - 1, list.size());
  81. for (int j = i + 1; j < 5; ++j) {
  82. CPPUNIT_ASSERT_EQUAL(a[j], *list.get(j));
  83. }
  84. }
  85. }
  86. void IndexedListTest::testErase()
  87. {
  88. int a[] = {1, 2, 3, 4, 5};
  89. IndexedList<int, int*> list;
  90. for (int i = 0; i < 5; ++i) {
  91. list.push_back(i, &a[i]);
  92. }
  93. int* p = a;
  94. for (IndexedList<int, int*>::iterator i = list.begin(); i != list.end();) {
  95. i = list.erase(i);
  96. CPPUNIT_ASSERT_EQUAL((size_t)(std::distance(i, list.end())), list.size());
  97. int* pp = ++p;
  98. for (IndexedList<int, int *>::iterator j = list.begin(); j != list.end();
  99. ++j, ++pp) {
  100. CPPUNIT_ASSERT_EQUAL(*pp, **j);
  101. }
  102. }
  103. }
  104. void IndexedListTest::testPopFront()
  105. {
  106. int a[] = {1, 2, 3, 4, 5};
  107. IndexedList<int, int*> list;
  108. for (int i = 0; i < 5; ++i) {
  109. list.push_back(i, &a[i]);
  110. }
  111. for (int i = 0; i < 5; ++i) {
  112. CPPUNIT_ASSERT(list.pop_front());
  113. CPPUNIT_ASSERT_EQUAL((size_t)5 - i - 1, list.size());
  114. for (int j = i + 1; j < 5; ++j) {
  115. CPPUNIT_ASSERT_EQUAL(a[j], *list.get(j));
  116. }
  117. }
  118. }
  119. #define LIST_CHECK(a, list) \
  120. { \
  121. int ai = 0; \
  122. for (IndexedList<int, int*>::iterator i = list.begin(); i != list.end(); \
  123. ++i) { \
  124. CPPUNIT_ASSERT_EQUAL(a[ai++], **i); \
  125. } \
  126. }
  127. void IndexedListTest::testMove()
  128. {
  129. int a[] = {0, 1, 2, 3, 4};
  130. IndexedList<int, int*> list;
  131. for (int i = 0; i < 5; ++i) {
  132. list.push_back(i, &a[i]);
  133. }
  134. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, list.move(100, 0, OFFSET_MODE_SET));
  135. int a0[] = {0, 1, 2, 3, 4};
  136. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(0, 0, OFFSET_MODE_SET));
  137. LIST_CHECK(a0, list);
  138. int a1[] = {0, 2, 3, 4, 1};
  139. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(1, 4, OFFSET_MODE_SET));
  140. LIST_CHECK(a1, list);
  141. int a2[] = {0, 3, 4, 2, 1};
  142. CPPUNIT_ASSERT_EQUAL((ssize_t)3, list.move(2, 3, OFFSET_MODE_SET));
  143. LIST_CHECK(a2, list);
  144. int a3[] = {0, 2, 3, 4, 1};
  145. CPPUNIT_ASSERT_EQUAL((ssize_t)1, list.move(2, 1, OFFSET_MODE_SET));
  146. LIST_CHECK(a3, list);
  147. int a4[] = {1, 0, 2, 3, 4};
  148. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(1, 0, OFFSET_MODE_SET));
  149. LIST_CHECK(a4, list);
  150. int a5[] = {1, 0, 3, 2, 4};
  151. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(3, 2, OFFSET_MODE_SET));
  152. LIST_CHECK(a5, list);
  153. int a6[] = {1, 3, 2, 4, 0};
  154. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(0, 5, OFFSET_MODE_SET));
  155. LIST_CHECK(a6, list);
  156. int a7[] = {3, 1, 2, 4, 0};
  157. CPPUNIT_ASSERT_EQUAL((ssize_t)1, list.move(1, 1, OFFSET_MODE_CUR));
  158. LIST_CHECK(a7, list);
  159. int a8[] = {3, 2, 4, 1, 0};
  160. CPPUNIT_ASSERT_EQUAL((ssize_t)3, list.move(1, 2, OFFSET_MODE_CUR));
  161. LIST_CHECK(a8, list);
  162. int a9[] = {3, 2, 1, 4, 0};
  163. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(1, -1, OFFSET_MODE_CUR));
  164. LIST_CHECK(a9, list);
  165. int a10[] = {1, 3, 2, 4, 0};
  166. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(1, -1233, OFFSET_MODE_CUR));
  167. LIST_CHECK(a10, list);
  168. int a11[] = {3, 2, 4, 0, 1};
  169. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(1, 8733, OFFSET_MODE_CUR));
  170. LIST_CHECK(a11, list);
  171. int a12[] = {3, 2, 4, 0, 1};
  172. CPPUNIT_ASSERT_EQUAL((ssize_t)3, list.move(0, -1, OFFSET_MODE_END));
  173. LIST_CHECK(a12, list);
  174. int a13[] = {3, 2, 0, 4, 1};
  175. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(0, -2, OFFSET_MODE_END));
  176. LIST_CHECK(a13, list);
  177. int a14[] = {0, 3, 2, 4, 1};
  178. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(0, -8733, OFFSET_MODE_END));
  179. LIST_CHECK(a14, list);
  180. int a15[] = {0, 2, 4, 1, 3};
  181. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(3, 0, OFFSET_MODE_END));
  182. LIST_CHECK(a15, list);
  183. int a16[] = {2, 4, 1, 3, 0};
  184. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(0, 1000, OFFSET_MODE_END));
  185. LIST_CHECK(a16, list);
  186. int a17[] = {2, 1, 4, 3, 0};
  187. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(4, 2, OFFSET_MODE_SET));
  188. LIST_CHECK(a17, list);
  189. }
  190. void IndexedListTest::testGet()
  191. {
  192. IndexedList<int, int*> list;
  193. int a = 1000;
  194. int b = 1;
  195. list.push_back(123, &a);
  196. list.push_back(1, &b);
  197. CPPUNIT_ASSERT_EQUAL((int*)nullptr, list.get(1000));
  198. CPPUNIT_ASSERT_EQUAL(&a, list.get(123));
  199. }
  200. namespace {
  201. struct KeyFunc {
  202. int n;
  203. KeyFunc(int n) : n(n) {}
  204. int operator()(const std::shared_ptr<std::string>& x) { return n++; }
  205. };
  206. } // namespace
  207. void IndexedListTest::testInsert_keyFunc()
  208. {
  209. std::shared_ptr<std::string> s[] = {
  210. std::shared_ptr<std::string>(new std::string("a")),
  211. std::shared_ptr<std::string>(new std::string("b")),
  212. std::shared_ptr<std::string>(new std::string("c")),
  213. std::shared_ptr<std::string>(new std::string("d"))};
  214. size_t slen = sizeof(s) / sizeof(s[0]);
  215. IndexedList<int, std::shared_ptr<std::string>> list;
  216. list.insert(list.begin(), KeyFunc(0), std::begin(s), std::end(s));
  217. CPPUNIT_ASSERT_EQUAL((size_t)slen, list.size());
  218. for (size_t i = 0; i < slen; ++i) {
  219. CPPUNIT_ASSERT_EQUAL(*s[i], *list.get(i));
  220. }
  221. list.insert(list.begin() + 2, KeyFunc(slen), std::begin(s), std::end(s));
  222. CPPUNIT_ASSERT_EQUAL((size_t)slen * 2, list.size());
  223. for (size_t i = slen; i < slen * 2; ++i) {
  224. CPPUNIT_ASSERT_EQUAL(*s[i - slen], *list.get(i));
  225. }
  226. IndexedList<int, std::shared_ptr<std::string>>::iterator itr;
  227. itr = list.begin();
  228. CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++));
  229. CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++));
  230. CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++));
  231. CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++));
  232. CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++));
  233. CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++));
  234. CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++));
  235. CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++));
  236. list.insert(list.begin(), KeyFunc(2 * slen - 1), std::begin(s), std::end(s));
  237. CPPUNIT_ASSERT_EQUAL((size_t)slen * 3 - 1, list.size());
  238. itr = list.begin();
  239. CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++));
  240. CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++));
  241. CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++));
  242. CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++));
  243. }
  244. void IndexedListTest::testInsert()
  245. {
  246. int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  247. IndexedList<int, int*> list;
  248. IndexedList<int, int*>::iterator itr;
  249. CPPUNIT_ASSERT(list.end() == list.insert(1, 0, &a[5]));
  250. itr = list.insert(0, 5, &a[5]);
  251. CPPUNIT_ASSERT_EQUAL(5, **itr);
  252. itr = list.insert(1, 3, &a[3]);
  253. CPPUNIT_ASSERT_EQUAL(3, **itr);
  254. itr = list.insert(1, 4, &a[4]);
  255. CPPUNIT_ASSERT_EQUAL(4, **itr);
  256. itr = list.insert(0, 9, &a[9]);
  257. CPPUNIT_ASSERT_EQUAL(9, **itr);
  258. int a1[] = {9, 5, 4, 3};
  259. LIST_CHECK(a1, list);
  260. // use iterator to insert
  261. itr = list.insert(itr, 2, &a[2]);
  262. CPPUNIT_ASSERT_EQUAL(2, **itr);
  263. itr = list.insert(list.end(), 1, &a[1]);
  264. CPPUNIT_ASSERT_EQUAL(1, **itr);
  265. int a2[] = {2, 9, 5, 4, 3, 1};
  266. LIST_CHECK(a2, list);
  267. // 2 has been already added.
  268. CPPUNIT_ASSERT(list.end() == list.insert(list.end(), 2, &a[2]));
  269. }
  270. void IndexedListTest::testIterator()
  271. {
  272. int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  273. IndexedList<int, int*> list;
  274. IndexedList<int, int*>::iterator itr;
  275. IndexedList<int, int*>::const_iterator citr;
  276. for (auto& i : a) {
  277. CPPUNIT_ASSERT(list.push_back(i, &i));
  278. }
  279. CPPUNIT_ASSERT(list.begin() == list.begin());
  280. itr = list.begin();
  281. citr = list.begin();
  282. // operator*()
  283. CPPUNIT_ASSERT_EQUAL(&a[0], *itr);
  284. CPPUNIT_ASSERT_EQUAL(&a[0], *citr);
  285. // operator==(iterator, iterator)
  286. CPPUNIT_ASSERT(itr == list.begin());
  287. CPPUNIT_ASSERT(!(itr == list.end()));
  288. CPPUNIT_ASSERT(citr == list.begin());
  289. CPPUNIT_ASSERT(!(citr == list.end()));
  290. // operator++()
  291. ++itr;
  292. ++citr;
  293. // operator!=(iterator, iterator)
  294. CPPUNIT_ASSERT(itr != list.begin());
  295. CPPUNIT_ASSERT(!(itr != itr));
  296. CPPUNIT_ASSERT(citr != list.begin());
  297. CPPUNIT_ASSERT(!(citr != citr));
  298. // operator+(difference_type)
  299. CPPUNIT_ASSERT(itr == list.begin() + 1);
  300. CPPUNIT_ASSERT(citr == list.begin() + 1);
  301. // operator-(difference_type)
  302. CPPUNIT_ASSERT(itr - 1 == list.begin());
  303. CPPUNIT_ASSERT(citr - 1 == list.begin());
  304. // operator++(int)
  305. IndexedList<int, int*>::iterator itr2 = itr++;
  306. IndexedList<int, int*>::const_iterator citr2 = citr++;
  307. CPPUNIT_ASSERT(itr2 + 1 == itr);
  308. CPPUNIT_ASSERT(citr2 + 1 == citr);
  309. // operator+(difference_type, iterator)
  310. CPPUNIT_ASSERT(-1 + itr == itr2);
  311. CPPUNIT_ASSERT(-1 + citr == citr2);
  312. // operator<(iterator, iterator)
  313. CPPUNIT_ASSERT(list.begin() < itr);
  314. CPPUNIT_ASSERT(!(itr < list.begin()));
  315. CPPUNIT_ASSERT(list.begin() < citr);
  316. CPPUNIT_ASSERT(!(citr < list.begin()));
  317. // operator>(iterator, iterator)
  318. CPPUNIT_ASSERT(itr > list.begin());
  319. CPPUNIT_ASSERT(!(list.begin() > itr));
  320. CPPUNIT_ASSERT(citr > list.begin());
  321. CPPUNIT_ASSERT(!(list.begin() > citr));
  322. // operator<=(iterator, iterator)
  323. CPPUNIT_ASSERT(itr <= itr);
  324. CPPUNIT_ASSERT(list.begin() <= itr);
  325. CPPUNIT_ASSERT(!(itr <= list.begin()));
  326. CPPUNIT_ASSERT(citr <= citr);
  327. CPPUNIT_ASSERT(list.begin() <= citr);
  328. CPPUNIT_ASSERT(!(citr <= list.begin()));
  329. // operator>=(iterator, iterator)
  330. CPPUNIT_ASSERT(itr >= itr);
  331. CPPUNIT_ASSERT(itr >= list.begin());
  332. CPPUNIT_ASSERT(!(list.begin() >= itr));
  333. CPPUNIT_ASSERT(citr >= citr);
  334. CPPUNIT_ASSERT(citr >= list.begin());
  335. CPPUNIT_ASSERT(!(list.begin() >= citr));
  336. // operator-(iterator, iterator)
  337. CPPUNIT_ASSERT(2 == itr - list.begin());
  338. CPPUNIT_ASSERT(-2 == list.begin() - itr);
  339. CPPUNIT_ASSERT(2 == citr - list.begin());
  340. CPPUNIT_ASSERT(-2 == list.begin() - citr);
  341. // operator+=(difference_type)
  342. itr = list.begin();
  343. itr += 2;
  344. CPPUNIT_ASSERT(itr == list.begin() + 2);
  345. citr = list.begin();
  346. citr += 2;
  347. CPPUNIT_ASSERT(citr == list.begin() + 2);
  348. // operator-=(difference_type)
  349. itr -= 2;
  350. CPPUNIT_ASSERT(itr == list.begin());
  351. citr -= 2;
  352. CPPUNIT_ASSERT(citr == list.begin());
  353. // operator[](size_type)
  354. itr = list.begin();
  355. itr += 3;
  356. CPPUNIT_ASSERT_EQUAL(*(itr[1]), a[4]);
  357. citr = list.begin();
  358. citr += 3;
  359. CPPUNIT_ASSERT_EQUAL(*(citr[1]), a[4]);
  360. }
  361. namespace {
  362. struct RemoveOdd {
  363. bool operator()(int* p) const { return *p % 2 == 1; }
  364. };
  365. }
  366. void IndexedListTest::testRemoveIf()
  367. {
  368. int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  369. IndexedList<int, int*> list;
  370. for (auto& i : a) {
  371. CPPUNIT_ASSERT(list.push_back(i, &i));
  372. }
  373. list.remove_if(RemoveOdd());
  374. CPPUNIT_ASSERT_EQUAL((size_t)5, list.size());
  375. for (int i = 0; i < 5; ++i) {
  376. CPPUNIT_ASSERT_EQUAL(i * 2, *list[i]);
  377. }
  378. }
  379. } // namespace aria2