deque_ut.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "deque.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <utility>
  4. #include "yexception.h"
  5. class TDequeTest: public TTestBase {
  6. UNIT_TEST_SUITE(TDequeTest);
  7. UNIT_TEST(TestConstructorsAndAssignments);
  8. UNIT_TEST(TestDeque1);
  9. UNIT_TEST(TestAt);
  10. UNIT_TEST(TestInsert);
  11. UNIT_TEST(TestErase);
  12. UNIT_TEST(TestAutoRef);
  13. UNIT_TEST_SUITE_END();
  14. protected:
  15. void TestConstructorsAndAssignments();
  16. void TestDeque1();
  17. void TestInsert();
  18. void TestErase();
  19. void TestAt();
  20. void TestAutoRef();
  21. };
  22. UNIT_TEST_SUITE_REGISTRATION(TDequeTest);
  23. void TDequeTest::TestConstructorsAndAssignments() {
  24. using container = TDeque<int>;
  25. container c1;
  26. c1.push_back(100);
  27. c1.push_back(200);
  28. container c2(c1);
  29. UNIT_ASSERT_VALUES_EQUAL(2, c1.size());
  30. UNIT_ASSERT_VALUES_EQUAL(2, c2.size());
  31. UNIT_ASSERT_VALUES_EQUAL(100, c1.at(0));
  32. UNIT_ASSERT_VALUES_EQUAL(200, c2.at(1));
  33. container c3(std::move(c1));
  34. UNIT_ASSERT_VALUES_EQUAL(0, c1.size());
  35. UNIT_ASSERT_VALUES_EQUAL(2, c3.size());
  36. UNIT_ASSERT_VALUES_EQUAL(100, c3.at(0));
  37. c2.push_back(300);
  38. c3 = c2;
  39. UNIT_ASSERT_VALUES_EQUAL(3, c2.size());
  40. UNIT_ASSERT_VALUES_EQUAL(3, c3.size());
  41. UNIT_ASSERT_VALUES_EQUAL(300, c3.at(2));
  42. c2.push_back(400);
  43. c3 = std::move(c2);
  44. UNIT_ASSERT_VALUES_EQUAL(0, c2.size());
  45. UNIT_ASSERT_VALUES_EQUAL(4, c3.size());
  46. UNIT_ASSERT_VALUES_EQUAL(400, c3.at(3));
  47. int array[] = {2, 3, 4};
  48. container c4 = {2, 3, 4};
  49. UNIT_ASSERT_VALUES_EQUAL(c4, container(std::begin(array), std::end(array)));
  50. }
  51. void TDequeTest::TestDeque1() {
  52. TDeque<int> d;
  53. UNIT_ASSERT(!d);
  54. d.push_back(4);
  55. d.push_back(9);
  56. d.push_back(16);
  57. d.push_front(1);
  58. UNIT_ASSERT(d);
  59. UNIT_ASSERT(d[0] == 1);
  60. UNIT_ASSERT(d[1] == 4);
  61. UNIT_ASSERT(d[2] == 9);
  62. UNIT_ASSERT(d[3] == 16);
  63. d.pop_front();
  64. d[2] = 25;
  65. UNIT_ASSERT(d[0] == 4);
  66. UNIT_ASSERT(d[1] == 9);
  67. UNIT_ASSERT(d[2] == 25);
  68. //Some compile time tests:
  69. TDeque<int>::iterator dit = d.begin();
  70. TDeque<int>::const_iterator cdit(d.begin());
  71. UNIT_ASSERT((dit - cdit) == 0);
  72. UNIT_ASSERT((cdit - dit) == 0);
  73. UNIT_ASSERT((dit - dit) == 0);
  74. UNIT_ASSERT((cdit - cdit) == 0);
  75. UNIT_ASSERT(!((dit < cdit) || (dit > cdit) || (dit != cdit) || !(dit <= cdit) || !(dit >= cdit)));
  76. }
  77. void TDequeTest::TestInsert() {
  78. TDeque<int> d;
  79. d.push_back(0);
  80. d.push_back(1);
  81. d.push_back(2);
  82. UNIT_ASSERT(d.size() == 3);
  83. TDeque<int>::iterator dit;
  84. //Insertion before begin:
  85. dit = d.insert(d.begin(), 3);
  86. UNIT_ASSERT(dit != d.end());
  87. UNIT_ASSERT(*dit == 3);
  88. UNIT_ASSERT(d.size() == 4);
  89. UNIT_ASSERT(d[0] == 3);
  90. //Insertion after begin:
  91. dit = d.insert(d.begin() + 1, 4);
  92. UNIT_ASSERT(dit != d.end());
  93. UNIT_ASSERT(*dit == 4);
  94. UNIT_ASSERT(d.size() == 5);
  95. UNIT_ASSERT(d[1] == 4);
  96. //Insertion at end:
  97. dit = d.insert(d.end(), 5);
  98. UNIT_ASSERT(dit != d.end());
  99. UNIT_ASSERT(*dit == 5);
  100. UNIT_ASSERT(d.size() == 6);
  101. UNIT_ASSERT(d[5] == 5);
  102. //Insertion before last element:
  103. dit = d.insert(d.end() - 1, 6);
  104. UNIT_ASSERT(dit != d.end());
  105. UNIT_ASSERT(*dit == 6);
  106. UNIT_ASSERT(d.size() == 7);
  107. UNIT_ASSERT(d[5] == 6);
  108. //Insertion of several elements before begin
  109. d.insert(d.begin(), 2, 7);
  110. UNIT_ASSERT(d.size() == 9);
  111. UNIT_ASSERT(d[0] == 7);
  112. UNIT_ASSERT(d[1] == 7);
  113. //Insertion of several elements after begin
  114. //There is more elements to insert than elements before insertion position
  115. d.insert(d.begin() + 1, 2, 8);
  116. UNIT_ASSERT(d.size() == 11);
  117. UNIT_ASSERT(d[1] == 8);
  118. UNIT_ASSERT(d[2] == 8);
  119. //There is less elements to insert than elements before insertion position
  120. d.insert(d.begin() + 3, 2, 9);
  121. UNIT_ASSERT(d.size() == 13);
  122. UNIT_ASSERT(d[3] == 9);
  123. UNIT_ASSERT(d[4] == 9);
  124. //Insertion of several elements at end:
  125. d.insert(d.end(), 2, 10);
  126. UNIT_ASSERT(d.size() == 15);
  127. UNIT_ASSERT(d[14] == 10);
  128. UNIT_ASSERT(d[13] == 10);
  129. //Insertion of several elements before last:
  130. //There is more elements to insert than elements after insertion position
  131. d.insert(d.end() - 1, 2, 11);
  132. UNIT_ASSERT(d.size() == 17);
  133. UNIT_ASSERT(d[15] == 11);
  134. UNIT_ASSERT(d[14] == 11);
  135. //There is less elements to insert than elements after insertion position
  136. d.insert(d.end() - 3, 2, 12);
  137. UNIT_ASSERT(d.size() == 19);
  138. UNIT_ASSERT(d[15] == 12);
  139. UNIT_ASSERT(d[14] == 12);
  140. }
  141. void TDequeTest::TestAt() {
  142. TDeque<int> d;
  143. TDeque<int> const& cd = d;
  144. d.push_back(10);
  145. UNIT_ASSERT(d.at(0) == 10);
  146. d.at(0) = 20;
  147. UNIT_ASSERT(cd.at(0) == 20);
  148. UNIT_ASSERT_EXCEPTION(d.at(1) = 20, std::out_of_range);
  149. }
  150. void TDequeTest::TestAutoRef() {
  151. int i;
  152. TDeque<int> ref;
  153. for (i = 0; i < 5; ++i) {
  154. ref.push_back(i);
  155. }
  156. TDeque<TDeque<int>> d_d_int(1, ref);
  157. d_d_int.push_back(d_d_int[0]);
  158. d_d_int.push_back(ref);
  159. d_d_int.push_back(d_d_int[0]);
  160. d_d_int.push_back(d_d_int[0]);
  161. d_d_int.push_back(ref);
  162. for (i = 0; i < 5; ++i) {
  163. UNIT_ASSERT(d_d_int[i] == ref);
  164. }
  165. }
  166. void TDequeTest::TestErase() {
  167. TDeque<int> dint;
  168. dint.push_back(3);
  169. dint.push_front(2);
  170. dint.push_back(4);
  171. dint.push_front(1);
  172. dint.push_back(5);
  173. dint.push_front(0);
  174. dint.push_back(6);
  175. TDeque<int>::iterator it(dint.begin() + 1);
  176. UNIT_ASSERT(*it == 1);
  177. dint.erase(dint.begin());
  178. UNIT_ASSERT(*it == 1);
  179. it = dint.end() - 2;
  180. UNIT_ASSERT(*it == 5);
  181. dint.erase(dint.end() - 1);
  182. UNIT_ASSERT(*it == 5);
  183. dint.push_back(6);
  184. dint.push_front(0);
  185. it = dint.begin() + 2;
  186. UNIT_ASSERT(*it == 2);
  187. dint.erase(dint.begin(), dint.begin() + 2);
  188. UNIT_ASSERT(*it == 2);
  189. it = dint.end() - 3;
  190. UNIT_ASSERT(*it == 4);
  191. dint.erase(dint.end() - 2, dint.end());
  192. UNIT_ASSERT(*it == 4);
  193. }