IndexedListTest.cc 8.2 KB

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