IndexedListTest.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_SUITE_END();
  22. public:
  23. void setUp()
  24. {}
  25. void testPushBack();
  26. void testPushFront();
  27. void testRemove();
  28. void testErase();
  29. void testPopFront();
  30. void testMove();
  31. void testGet();
  32. void testInsert();
  33. void testInsert_keyFunc();
  34. };
  35. CPPUNIT_TEST_SUITE_REGISTRATION( IndexedListTest );
  36. void IndexedListTest::testPushBack()
  37. {
  38. int a[] = {1,2,3,4,5};
  39. IndexedList<int, int*> list;
  40. for(int i = 0; i < 5; ++i) {
  41. CPPUNIT_ASSERT(list.push_back(i, &a[i]));
  42. }
  43. for(int i = 0; i < 5; ++i) {
  44. CPPUNIT_ASSERT_EQUAL(a[i], *list.get(i));
  45. }
  46. int ai = 0;
  47. for(IndexedList<int, int*>::SeqType::iterator i = list.begin();
  48. i != list.end(); ++i) {
  49. CPPUNIT_ASSERT_EQUAL(a[ai++], *((*i).second));
  50. }
  51. }
  52. void IndexedListTest::testPushFront()
  53. {
  54. int a[] = {1,2,3,4,5};
  55. IndexedList<int, int*> list;
  56. for(int i = 0; i < 5; ++i) {
  57. CPPUNIT_ASSERT(list.push_front(i, &a[i]));
  58. }
  59. for(int i = 0; i < 5; ++i) {
  60. CPPUNIT_ASSERT_EQUAL(a[i], *list.get(i));
  61. }
  62. int ai = 4;
  63. for(IndexedList<int, int*>::SeqType::iterator i = list.begin();
  64. i != list.end(); ++i) {
  65. CPPUNIT_ASSERT_EQUAL(a[ai--], *((*i).second));
  66. }
  67. }
  68. void IndexedListTest::testRemove()
  69. {
  70. int a[] = {1,2,3,4,5};
  71. IndexedList<int, int*> list;
  72. for(int i = 0; i < 5; ++i) {
  73. list.push_back(i, &a[i]);
  74. }
  75. for(int i = 0; i < 5; ++i) {
  76. CPPUNIT_ASSERT(list.remove(i));
  77. CPPUNIT_ASSERT_EQUAL((size_t)5-i-1, list.size());
  78. for(int j = i+1; j < 5; ++j) {
  79. CPPUNIT_ASSERT_EQUAL(a[j], *list.get(j));
  80. }
  81. }
  82. }
  83. void IndexedListTest::testErase()
  84. {
  85. int a[] = {1,2,3,4,5};
  86. IndexedList<int, int*> list;
  87. for(int i = 0; i < 5; ++i) {
  88. list.push_back(i, &a[i]);
  89. }
  90. int* p = a;
  91. for(IndexedList<int, int*>::SeqType::iterator i = list.begin();
  92. i != list.end();) {
  93. i = list.erase(i);
  94. CPPUNIT_ASSERT_EQUAL((size_t)(std::distance(i, list.end())), list.size());
  95. int* pp = ++p;
  96. for(IndexedList<int, int*>::SeqType::iterator j = list.begin();
  97. j != list.end(); ++j, ++pp) {
  98. CPPUNIT_ASSERT_EQUAL(*pp, *(*j).second);
  99. }
  100. }
  101. }
  102. void IndexedListTest::testPopFront()
  103. {
  104. int a[] = {1,2,3,4,5};
  105. IndexedList<int, int*> list;
  106. for(int i = 0; i < 5; ++i) {
  107. list.push_back(i, &a[i]);
  108. }
  109. for(int i = 0; i < 5; ++i) {
  110. CPPUNIT_ASSERT(list.pop_front());
  111. CPPUNIT_ASSERT_EQUAL((size_t)5-i-1, list.size());
  112. for(int j = i+1; j < 5; ++j) {
  113. CPPUNIT_ASSERT_EQUAL(a[j], *list.get(j));
  114. }
  115. }
  116. }
  117. #define LIST_CHECK(a, list) \
  118. { \
  119. int ai = 0; \
  120. for(IndexedList<int, int*>::SeqType::iterator i = list.begin(); \
  121. i != list.end(); ++i) { \
  122. CPPUNIT_ASSERT_EQUAL(a[ai++], *((*i).second)); \
  123. } \
  124. }
  125. void IndexedListTest::testMove()
  126. {
  127. int a[] = {0,1,2,3,4};
  128. IndexedList<int, int*> list;
  129. for(int i = 0; i < 5; ++i) {
  130. list.push_back(i, &a[i]);
  131. }
  132. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, list.move(100, 0, A2_POS_SET));
  133. int a0[] = {0,1,2,3,4};
  134. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(0, 0, A2_POS_SET));
  135. LIST_CHECK(a0, list);
  136. int a1[] = {0,2,3,4,1};
  137. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(1, 4, A2_POS_SET));
  138. LIST_CHECK(a1, list);
  139. int a2[] = {0,3,4,2,1};
  140. CPPUNIT_ASSERT_EQUAL((ssize_t)3, list.move(2, 3, A2_POS_SET));
  141. LIST_CHECK(a2, list);
  142. int a3[] = {0,2,3,4,1};
  143. CPPUNIT_ASSERT_EQUAL((ssize_t)1, list.move(2, 1, A2_POS_SET));
  144. LIST_CHECK(a3, list);
  145. int a4[] = {1,0,2,3,4};
  146. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(1, 0, A2_POS_SET));
  147. LIST_CHECK(a4, list);
  148. int a5[] = {1,0,3,2,4};
  149. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(3, 2, A2_POS_SET));
  150. LIST_CHECK(a5, list);
  151. int a6[] = {1,3,2,4,0};
  152. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(0, 5, A2_POS_SET));
  153. LIST_CHECK(a6, list);
  154. int a7[] = {3,1,2,4,0};
  155. CPPUNIT_ASSERT_EQUAL((ssize_t)1, list.move(1, 1, A2_POS_CUR));
  156. LIST_CHECK(a7, list);
  157. int a8[] = {3,2,4,1,0};
  158. CPPUNIT_ASSERT_EQUAL((ssize_t)3, list.move(1, 2, A2_POS_CUR));
  159. LIST_CHECK(a8, list);
  160. int a9[] = {3,2,1,4,0};
  161. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(1, -1, A2_POS_CUR));
  162. LIST_CHECK(a9, list);
  163. int a10[] = {1,3,2,4,0};
  164. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(1, -1233, A2_POS_CUR));
  165. LIST_CHECK(a10, list);
  166. int a11[] = {3,2,4,0,1};
  167. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(1, 8733, A2_POS_CUR));
  168. LIST_CHECK(a11, list);
  169. int a12[] = {3,2,4,0,1};
  170. CPPUNIT_ASSERT_EQUAL((ssize_t)3, list.move(0, -1, A2_POS_END));
  171. LIST_CHECK(a12, list);
  172. int a13[] = {3,2,0,4,1};
  173. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(0, -2, A2_POS_END));
  174. LIST_CHECK(a13, list);
  175. int a14[] = {0,3,2,4,1};
  176. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(0, -8733, A2_POS_END));
  177. LIST_CHECK(a14, list);
  178. int a15[] = {0,2,4,1,3};
  179. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(3, 0, A2_POS_END));
  180. LIST_CHECK(a15, list);
  181. int a16[] = {2,4,1,3,0};
  182. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(0, 1000, A2_POS_END));
  183. LIST_CHECK(a16, list);
  184. int a17[] = {2,1,4,3,0};
  185. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(4, 2, A2_POS_SET));
  186. LIST_CHECK(a17, list);
  187. }
  188. void IndexedListTest::testGet()
  189. {
  190. IndexedList<int, int*> list;
  191. int a = 1000;
  192. int b = 1;
  193. list.push_back(123, &a);
  194. list.push_back(1, &b);
  195. CPPUNIT_ASSERT_EQUAL((int*)0, list.get(1000));
  196. CPPUNIT_ASSERT_EQUAL(&a, list.get(123));
  197. }
  198. namespace {
  199. struct KeyFunc {
  200. int n;
  201. KeyFunc(int n):n(n) {}
  202. int operator()(const SharedHandle<std::string>& x)
  203. {
  204. return n++;
  205. }
  206. };
  207. } // namespace
  208. void IndexedListTest::testInsert_keyFunc()
  209. {
  210. SharedHandle<std::string> s[] = {
  211. SharedHandle<std::string>(new std::string("a")),
  212. SharedHandle<std::string>(new std::string("b")),
  213. SharedHandle<std::string>(new std::string("c")),
  214. SharedHandle<std::string>(new std::string("d"))
  215. };
  216. size_t slen = sizeof(s)/sizeof(s[0]);
  217. IndexedList<int, SharedHandle<std::string> > list;
  218. list.insert(list.begin(), KeyFunc(0), vbegin(s), vend(s));
  219. CPPUNIT_ASSERT_EQUAL((size_t)slen, list.size());
  220. for(size_t i = 0; i < slen; ++i) {
  221. CPPUNIT_ASSERT_EQUAL(*s[i], *list.get(i));
  222. }
  223. list.insert(list.begin()+2, KeyFunc(slen), vbegin(s), vend(s));
  224. CPPUNIT_ASSERT_EQUAL((size_t)slen*2, list.size());
  225. for(size_t i = slen; i < slen*2; ++i) {
  226. CPPUNIT_ASSERT_EQUAL(*s[i - slen], *list.get(i));
  227. }
  228. IndexedList<int, SharedHandle<std::string> >::SeqType::iterator itr;
  229. itr = list.begin();
  230. CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++).second);
  231. CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++).second);
  232. CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++).second);
  233. CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++).second);
  234. CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++).second);
  235. CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++).second);
  236. CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++).second);
  237. CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++).second);
  238. list.insert(list.begin(), KeyFunc(2*slen-1), vbegin(s), vend(s));
  239. CPPUNIT_ASSERT_EQUAL((size_t)slen*3-1, list.size());
  240. itr = list.begin();
  241. CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++).second);
  242. CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++).second);
  243. CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++).second);
  244. CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++).second);
  245. }
  246. void IndexedListTest::testInsert()
  247. {
  248. int a[] = {0,1,2,3,4,5,6,7,8,9};
  249. IndexedList<int, int*> list;
  250. IndexedList<int, int*>::SeqType::iterator itr;
  251. CPPUNIT_ASSERT(list.end() == list.insert(1, 0, &a[5]));
  252. itr = list.insert(0, 5, &a[5]);
  253. CPPUNIT_ASSERT_EQUAL(5, *(*itr).second);
  254. itr = list.insert(1, 3, &a[3]);
  255. CPPUNIT_ASSERT_EQUAL(3, *(*itr).second);
  256. itr = list.insert(1, 4, &a[4]);
  257. CPPUNIT_ASSERT_EQUAL(4, *(*itr).second);
  258. itr = list.insert(0, 9, &a[9]);
  259. CPPUNIT_ASSERT_EQUAL(9, *(*itr).second);
  260. int a1[] = { 9,5,4,3 };
  261. LIST_CHECK(a1, list);
  262. // use iterator to insert
  263. itr = list.insert(itr, 2, &a[2]);
  264. CPPUNIT_ASSERT_EQUAL(2, *(*itr).second);
  265. itr = list.insert(list.end(), 1, &a[1]);
  266. CPPUNIT_ASSERT_EQUAL(1, *(*itr).second);
  267. int a2[] = { 2,9,5,4,3,1 };
  268. LIST_CHECK(a2, list);
  269. // 2 has been already added.
  270. CPPUNIT_ASSERT(list.end() == list.insert(list.end(), 2, &a[2]));
  271. }
  272. } // namespace aria2