queue_ut.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "queue.h"
  2. #include "deque.h"
  3. #include "list.h"
  4. #include "ptr.h"
  5. #include <library/cpp/testing/unittest/registar.h>
  6. #include <utility>
  7. Y_UNIT_TEST_SUITE(TYQueueTest) {
  8. Y_UNIT_TEST(ConstructorsAndAssignments) {
  9. {
  10. using container = TQueue<int>;
  11. container c1;
  12. UNIT_ASSERT(!c1);
  13. c1.push(100);
  14. c1.push(200);
  15. UNIT_ASSERT(c1);
  16. container c2(c1);
  17. UNIT_ASSERT_VALUES_EQUAL(2, c1.size());
  18. UNIT_ASSERT_VALUES_EQUAL(2, c2.size());
  19. container c3(std::move(c1));
  20. UNIT_ASSERT_VALUES_EQUAL(0, c1.size());
  21. UNIT_ASSERT_VALUES_EQUAL(2, c3.size());
  22. c2.push(300);
  23. c3 = c2;
  24. UNIT_ASSERT_VALUES_EQUAL(3, c2.size());
  25. UNIT_ASSERT_VALUES_EQUAL(3, c3.size());
  26. c2.push(400);
  27. c3 = std::move(c2);
  28. UNIT_ASSERT_VALUES_EQUAL(0, c2.size());
  29. UNIT_ASSERT_VALUES_EQUAL(4, c3.size());
  30. }
  31. {
  32. using container = TPriorityQueue<int>;
  33. container c1;
  34. UNIT_ASSERT(!c1);
  35. c1.push(100);
  36. c1.push(200);
  37. UNIT_ASSERT(c1);
  38. container c2(c1);
  39. UNIT_ASSERT_VALUES_EQUAL(2, c1.size());
  40. UNIT_ASSERT_VALUES_EQUAL(2, c2.size());
  41. container c3(std::move(c1));
  42. UNIT_ASSERT_VALUES_EQUAL(0, c1.size());
  43. UNIT_ASSERT_VALUES_EQUAL(2, c3.size());
  44. c2.push(300);
  45. c3 = c2;
  46. UNIT_ASSERT_VALUES_EQUAL(3, c2.size());
  47. UNIT_ASSERT_VALUES_EQUAL(3, c3.size());
  48. c2.push(400);
  49. c3 = std::move(c2);
  50. UNIT_ASSERT_VALUES_EQUAL(0, c2.size());
  51. UNIT_ASSERT_VALUES_EQUAL(4, c3.size());
  52. }
  53. }
  54. Y_UNIT_TEST(pqueue1) {
  55. TPriorityQueue<int, TDeque<int>, TLess<int>> q;
  56. q.push(42);
  57. q.push(101);
  58. q.push(69);
  59. UNIT_ASSERT(q.top() == 101);
  60. q.pop();
  61. UNIT_ASSERT(q.top() == 69);
  62. q.pop();
  63. UNIT_ASSERT(q.top() == 42);
  64. q.pop();
  65. UNIT_ASSERT(q.empty());
  66. }
  67. Y_UNIT_TEST(pqueue2) {
  68. using TPQueue = TPriorityQueue<int, TDeque<int>, TLess<int>>;
  69. TPQueue q;
  70. {
  71. TPQueue qq;
  72. qq.push(42);
  73. qq.push(101);
  74. qq.push(69);
  75. qq.swap(q);
  76. }
  77. UNIT_ASSERT(q.top() == 101);
  78. q.pop();
  79. UNIT_ASSERT(q.top() == 69);
  80. q.pop();
  81. UNIT_ASSERT(q.top() == 42);
  82. q.pop();
  83. UNIT_ASSERT(q.empty());
  84. }
  85. Y_UNIT_TEST(pqueue3) {
  86. TPriorityQueue<int, TDeque<int>, TLess<int>> q;
  87. q.push(42);
  88. q.push(101);
  89. q.push(69);
  90. q.clear();
  91. UNIT_ASSERT(q.empty());
  92. }
  93. Y_UNIT_TEST(pqueue4) {
  94. TDeque<int> c;
  95. c.push_back(42);
  96. c.push_back(101);
  97. c.push_back(69);
  98. TPriorityQueue<int, TDeque<int>, TLess<int>> q(TLess<int>(), std::move(c));
  99. UNIT_ASSERT(c.empty());
  100. UNIT_ASSERT_EQUAL(q.size(), 3);
  101. UNIT_ASSERT(q.top() == 101);
  102. q.pop();
  103. UNIT_ASSERT(q.top() == 69);
  104. q.pop();
  105. UNIT_ASSERT(q.top() == 42);
  106. q.pop();
  107. UNIT_ASSERT(q.empty());
  108. }
  109. struct THolderWithPriority {
  110. THolderWithPriority(const TString& value, int priority)
  111. : Value(MakeHolder<TString>(value))
  112. , Priority(priority)
  113. {
  114. }
  115. THolder<TString> Value; // THolder to test move-ctor
  116. int Priority;
  117. std::weak_ordering operator<=>(const THolderWithPriority& other) const noexcept {
  118. return Priority <=> other.Priority;
  119. }
  120. };
  121. Y_UNIT_TEST(pqueue5) {
  122. // Test move-and-pop
  123. TPriorityQueue<THolderWithPriority> q;
  124. UNIT_ASSERT(q.empty());
  125. q.emplace("min", 1);
  126. q.emplace("max", 3);
  127. q.emplace("middle", 2);
  128. auto value = q.PopValue().Value;
  129. UNIT_ASSERT(*value == "max");
  130. value = q.PopValue().Value;
  131. UNIT_ASSERT(*value == "middle");
  132. value = q.PopValue().Value;
  133. UNIT_ASSERT(*value == "min");
  134. UNIT_ASSERT(q.empty());
  135. }
  136. Y_UNIT_TEST(queue1) {
  137. TQueue<int, TList<int>> q;
  138. q.push(42);
  139. q.push(101);
  140. q.push(69);
  141. UNIT_ASSERT(q.front() == 42);
  142. q.pop();
  143. UNIT_ASSERT(q.front() == 101);
  144. q.pop();
  145. UNIT_ASSERT(q.front() == 69);
  146. q.pop();
  147. UNIT_ASSERT(q.empty());
  148. }
  149. Y_UNIT_TEST(queue2) {
  150. using TQueueType = TQueue<int>;
  151. TQueueType q;
  152. {
  153. TQueueType qq;
  154. qq.push(42);
  155. qq.push(101);
  156. qq.push(69);
  157. qq.swap(q);
  158. }
  159. UNIT_ASSERT(q.front() == 42);
  160. q.pop();
  161. UNIT_ASSERT(q.front() == 101);
  162. q.pop();
  163. UNIT_ASSERT(q.front() == 69);
  164. q.pop();
  165. UNIT_ASSERT(q.empty());
  166. }
  167. Y_UNIT_TEST(queue3) {
  168. using TQueueType = TQueue<int>;
  169. TQueueType q;
  170. q.push(42);
  171. q.push(101);
  172. q.push(69);
  173. q.clear();
  174. UNIT_ASSERT(q.empty());
  175. }
  176. } // Y_UNIT_TEST_SUITE(TYQueueTest)