IndexedListTest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_SUITE_END();
  20. public:
  21. void setUp()
  22. {}
  23. void testPushBack();
  24. void testPushFront();
  25. void testErase();
  26. void testPopFront();
  27. void testMove();
  28. void testGet();
  29. void testInsert();
  30. };
  31. CPPUNIT_TEST_SUITE_REGISTRATION( IndexedListTest );
  32. void IndexedListTest::testPushBack()
  33. {
  34. int a[] = {1,2,3,4,5};
  35. IndexedList<int, int*> list;
  36. for(int i = 0; i < 5; ++i) {
  37. CPPUNIT_ASSERT(list.push_back(i, &a[i]));
  38. }
  39. for(int i = 0; i < 5; ++i) {
  40. CPPUNIT_ASSERT_EQUAL(a[i], *list.get(i));
  41. }
  42. int ai = 0;
  43. for(IndexedList<int, int*>::SeqType::iterator i = list.begin();
  44. i != list.end(); ++i) {
  45. CPPUNIT_ASSERT_EQUAL(a[ai++], *((*i).second));
  46. }
  47. }
  48. void IndexedListTest::testPushFront()
  49. {
  50. int a[] = {1,2,3,4,5};
  51. IndexedList<int, int*> list;
  52. for(int i = 0; i < 5; ++i) {
  53. CPPUNIT_ASSERT(list.push_front(i, &a[i]));
  54. }
  55. for(int i = 0; i < 5; ++i) {
  56. CPPUNIT_ASSERT_EQUAL(a[i], *list.get(i));
  57. }
  58. int ai = 4;
  59. for(IndexedList<int, int*>::SeqType::iterator i = list.begin();
  60. i != list.end(); ++i) {
  61. CPPUNIT_ASSERT_EQUAL(a[ai--], *((*i).second));
  62. }
  63. }
  64. void IndexedListTest::testErase()
  65. {
  66. int a[] = {1,2,3,4,5};
  67. IndexedList<int, int*> list;
  68. for(int i = 0; i < 5; ++i) {
  69. list.push_back(i, &a[i]);
  70. }
  71. for(int i = 0; i < 5; ++i) {
  72. CPPUNIT_ASSERT(list.erase(i));
  73. CPPUNIT_ASSERT_EQUAL((size_t)5-i-1, list.size());
  74. for(int j = i+1; j < 5; ++j) {
  75. CPPUNIT_ASSERT_EQUAL(a[j], *list.get(j));
  76. }
  77. }
  78. }
  79. void IndexedListTest::testPopFront()
  80. {
  81. int a[] = {1,2,3,4,5};
  82. IndexedList<int, int*> list;
  83. for(int i = 0; i < 5; ++i) {
  84. list.push_back(i, &a[i]);
  85. }
  86. for(int i = 0; i < 5; ++i) {
  87. CPPUNIT_ASSERT(list.pop_front());
  88. CPPUNIT_ASSERT_EQUAL((size_t)5-i-1, list.size());
  89. for(int j = i+1; j < 5; ++j) {
  90. CPPUNIT_ASSERT_EQUAL(a[j], *list.get(j));
  91. }
  92. }
  93. }
  94. #define LIST_CHECK(a, list) \
  95. { \
  96. int ai = 0; \
  97. for(IndexedList<int, int*>::SeqType::iterator i = list.begin(); \
  98. i != list.end(); ++i) { \
  99. CPPUNIT_ASSERT_EQUAL(a[ai++], *((*i).second)); \
  100. } \
  101. }
  102. void IndexedListTest::testMove()
  103. {
  104. int a[] = {0,1,2,3,4};
  105. IndexedList<int, int*> list;
  106. for(int i = 0; i < 5; ++i) {
  107. list.push_back(i, &a[i]);
  108. }
  109. CPPUNIT_ASSERT_EQUAL((ssize_t)-1, list.move(100, 0, A2_POS_SET));
  110. int a0[] = {0,1,2,3,4};
  111. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(0, 0, A2_POS_SET));
  112. LIST_CHECK(a0, list);
  113. int a1[] = {0,2,3,4,1};
  114. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(1, 4, A2_POS_SET));
  115. LIST_CHECK(a1, list);
  116. int a2[] = {0,3,4,2,1};
  117. CPPUNIT_ASSERT_EQUAL((ssize_t)3, list.move(2, 3, A2_POS_SET));
  118. LIST_CHECK(a2, list);
  119. int a3[] = {0,2,3,4,1};
  120. CPPUNIT_ASSERT_EQUAL((ssize_t)1, list.move(2, 1, A2_POS_SET));
  121. LIST_CHECK(a3, list);
  122. int a4[] = {1,0,2,3,4};
  123. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(1, 0, A2_POS_SET));
  124. LIST_CHECK(a4, list);
  125. int a5[] = {1,0,3,2,4};
  126. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(3, 2, A2_POS_SET));
  127. LIST_CHECK(a5, list);
  128. int a6[] = {1,3,2,4,0};
  129. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(0, 5, A2_POS_SET));
  130. LIST_CHECK(a6, list);
  131. int a7[] = {3,1,2,4,0};
  132. CPPUNIT_ASSERT_EQUAL((ssize_t)1, list.move(1, 1, A2_POS_CUR));
  133. LIST_CHECK(a7, list);
  134. int a8[] = {3,2,4,1,0};
  135. CPPUNIT_ASSERT_EQUAL((ssize_t)3, list.move(1, 2, A2_POS_CUR));
  136. LIST_CHECK(a8, list);
  137. int a9[] = {3,2,1,4,0};
  138. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(1, -1, A2_POS_CUR));
  139. LIST_CHECK(a9, list);
  140. int a10[] = {1,3,2,4,0};
  141. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(1, -1233, A2_POS_CUR));
  142. LIST_CHECK(a10, list);
  143. int a11[] = {3,2,4,0,1};
  144. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(1, 8733, A2_POS_CUR));
  145. LIST_CHECK(a11, list);
  146. int a12[] = {3,2,4,0,1};
  147. CPPUNIT_ASSERT_EQUAL((ssize_t)3, list.move(0, -1, A2_POS_END));
  148. LIST_CHECK(a12, list);
  149. int a13[] = {3,2,0,4,1};
  150. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(0, -2, A2_POS_END));
  151. LIST_CHECK(a13, list);
  152. int a14[] = {0,3,2,4,1};
  153. CPPUNIT_ASSERT_EQUAL((ssize_t)0, list.move(0, -8733, A2_POS_END));
  154. LIST_CHECK(a14, list);
  155. int a15[] = {0,2,4,1,3};
  156. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(3, 0, A2_POS_END));
  157. LIST_CHECK(a15, list);
  158. int a16[] = {2,4,1,3,0};
  159. CPPUNIT_ASSERT_EQUAL((ssize_t)4, list.move(0, 1000, A2_POS_END));
  160. LIST_CHECK(a16, list);
  161. int a17[] = {2,1,4,3,0};
  162. CPPUNIT_ASSERT_EQUAL((ssize_t)2, list.move(4, 2, A2_POS_SET));
  163. LIST_CHECK(a17, list);
  164. }
  165. void IndexedListTest::testGet()
  166. {
  167. IndexedList<int, int*> list;
  168. int a = 1000;
  169. int b = 1;
  170. list.push_back(123, &a);
  171. list.push_back(1, &b);
  172. CPPUNIT_ASSERT_EQUAL((int*)0, list.get(1000));
  173. CPPUNIT_ASSERT_EQUAL(&a, list.get(123));
  174. CPPUNIT_ASSERT(list.begin() == list.find(123));
  175. CPPUNIT_ASSERT(list.end() == list.find(124));
  176. }
  177. void IndexedListTest::testInsert()
  178. {
  179. int a[] = {0,1,2,3,4,5,6,7,8,9};
  180. IndexedList<int, int*> list;
  181. IndexedList<int, int*>::SeqType::iterator itr;
  182. CPPUNIT_ASSERT(list.end() == list.insert(1, 0, &a[5]));
  183. itr = list.insert(0, 5, &a[5]);
  184. CPPUNIT_ASSERT_EQUAL(5, *(*itr).second);
  185. itr = list.insert(1, 3, &a[3]);
  186. CPPUNIT_ASSERT_EQUAL(3, *(*itr).second);
  187. itr = list.insert(1, 4, &a[4]);
  188. CPPUNIT_ASSERT_EQUAL(4, *(*itr).second);
  189. itr = list.insert(0, 9, &a[9]);
  190. CPPUNIT_ASSERT_EQUAL(9, *(*itr).second);
  191. int a1[] = { 9,5,4,3 };
  192. LIST_CHECK(a1, list);
  193. // use iterator to insert
  194. itr = list.insert(itr, 2, &a[2]);
  195. CPPUNIT_ASSERT_EQUAL(2, *(*itr).second);
  196. itr = list.insert(list.end(), 1, &a[1]);
  197. CPPUNIT_ASSERT_EQUAL(1, *(*itr).second);
  198. int a2[] = { 2,9,5,4,3,1 };
  199. LIST_CHECK(a2, list);
  200. // 2 has been already added.
  201. CPPUNIT_ASSERT(list.end() == list.insert(list.end(), 2, &a[2]));
  202. }
  203. } // namespace aria2