queue_ut.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "queue.h"
  2. #include "list.h"
  3. #include "vector.h"
  4. #include <library/cpp/testing/unittest/registar.h>
  5. #include <utility>
  6. Y_UNIT_TEST_SUITE(TYQueueTest) {
  7. Y_UNIT_TEST(ConstructorsAndAssignments) {
  8. {
  9. using container = TQueue<int>;
  10. container c1;
  11. UNIT_ASSERT(!c1);
  12. c1.push(100);
  13. c1.push(200);
  14. UNIT_ASSERT(c1);
  15. container c2(c1);
  16. UNIT_ASSERT_VALUES_EQUAL(2, c1.size());
  17. UNIT_ASSERT_VALUES_EQUAL(2, c2.size());
  18. container c3(std::move(c1));
  19. UNIT_ASSERT_VALUES_EQUAL(0, c1.size());
  20. UNIT_ASSERT_VALUES_EQUAL(2, c3.size());
  21. c2.push(300);
  22. c3 = c2;
  23. UNIT_ASSERT_VALUES_EQUAL(3, c2.size());
  24. UNIT_ASSERT_VALUES_EQUAL(3, c3.size());
  25. c2.push(400);
  26. c3 = std::move(c2);
  27. UNIT_ASSERT_VALUES_EQUAL(0, c2.size());
  28. UNIT_ASSERT_VALUES_EQUAL(4, c3.size());
  29. }
  30. {
  31. using container = TPriorityQueue<int>;
  32. container c1;
  33. UNIT_ASSERT(!c1);
  34. c1.push(100);
  35. c1.push(200);
  36. UNIT_ASSERT(c1);
  37. container c2(c1);
  38. UNIT_ASSERT_VALUES_EQUAL(2, c1.size());
  39. UNIT_ASSERT_VALUES_EQUAL(2, c2.size());
  40. container c3(std::move(c1));
  41. UNIT_ASSERT_VALUES_EQUAL(0, c1.size());
  42. UNIT_ASSERT_VALUES_EQUAL(2, c3.size());
  43. c2.push(300);
  44. c3 = c2;
  45. UNIT_ASSERT_VALUES_EQUAL(3, c2.size());
  46. UNIT_ASSERT_VALUES_EQUAL(3, c3.size());
  47. c2.push(400);
  48. c3 = std::move(c2);
  49. UNIT_ASSERT_VALUES_EQUAL(0, c2.size());
  50. UNIT_ASSERT_VALUES_EQUAL(4, c3.size());
  51. }
  52. }
  53. Y_UNIT_TEST(pqueue1) {
  54. TPriorityQueue<int, TDeque<int>, TLess<int>> q;
  55. q.push(42);
  56. q.push(101);
  57. q.push(69);
  58. UNIT_ASSERT(q.top() == 101);
  59. q.pop();
  60. UNIT_ASSERT(q.top() == 69);
  61. q.pop();
  62. UNIT_ASSERT(q.top() == 42);
  63. q.pop();
  64. UNIT_ASSERT(q.empty());
  65. }
  66. Y_UNIT_TEST(pqueue2) {
  67. using TPQueue = TPriorityQueue<int, TDeque<int>, TLess<int>>;
  68. TPQueue q;
  69. {
  70. TPQueue qq;
  71. qq.push(42);
  72. qq.push(101);
  73. qq.push(69);
  74. qq.swap(q);
  75. }
  76. UNIT_ASSERT(q.top() == 101);
  77. q.pop();
  78. UNIT_ASSERT(q.top() == 69);
  79. q.pop();
  80. UNIT_ASSERT(q.top() == 42);
  81. q.pop();
  82. UNIT_ASSERT(q.empty());
  83. }
  84. Y_UNIT_TEST(pqueue3) {
  85. TPriorityQueue<int, TDeque<int>, TLess<int>> q;
  86. q.push(42);
  87. q.push(101);
  88. q.push(69);
  89. q.clear();
  90. UNIT_ASSERT(q.empty());
  91. }
  92. Y_UNIT_TEST(pqueue4) {
  93. TDeque<int> c;
  94. c.push_back(42);
  95. c.push_back(101);
  96. c.push_back(69);
  97. TPriorityQueue<int, TDeque<int>, TLess<int>> q(TLess<int>(), std::move(c));
  98. UNIT_ASSERT(c.empty());
  99. UNIT_ASSERT_EQUAL(q.size(), 3);
  100. UNIT_ASSERT(q.top() == 101);
  101. q.pop();
  102. UNIT_ASSERT(q.top() == 69);
  103. q.pop();
  104. UNIT_ASSERT(q.top() == 42);
  105. q.pop();
  106. UNIT_ASSERT(q.empty());
  107. }
  108. Y_UNIT_TEST(queue1) {
  109. TQueue<int, TList<int>> q;
  110. q.push(42);
  111. q.push(101);
  112. q.push(69);
  113. UNIT_ASSERT(q.front() == 42);
  114. q.pop();
  115. UNIT_ASSERT(q.front() == 101);
  116. q.pop();
  117. UNIT_ASSERT(q.front() == 69);
  118. q.pop();
  119. UNIT_ASSERT(q.empty());
  120. }
  121. Y_UNIT_TEST(queue2) {
  122. using TQueueType = TQueue<int>;
  123. TQueueType q;
  124. {
  125. TQueueType qq;
  126. qq.push(42);
  127. qq.push(101);
  128. qq.push(69);
  129. qq.swap(q);
  130. }
  131. UNIT_ASSERT(q.front() == 42);
  132. q.pop();
  133. UNIT_ASSERT(q.front() == 101);
  134. q.pop();
  135. UNIT_ASSERT(q.front() == 69);
  136. q.pop();
  137. UNIT_ASSERT(q.empty());
  138. }
  139. Y_UNIT_TEST(queue3) {
  140. using TQueueType = TQueue<int>;
  141. TQueueType q;
  142. q.push(42);
  143. q.push(101);
  144. q.push(69);
  145. q.clear();
  146. UNIT_ASSERT(q.empty());
  147. }
  148. }