123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- #include "queue.h"
- #include "deque.h"
- #include "list.h"
- #include "ptr.h"
- #include <library/cpp/testing/unittest/registar.h>
- #include <utility>
- Y_UNIT_TEST_SUITE(TYQueueTest) {
- Y_UNIT_TEST(ConstructorsAndAssignments) {
- {
- using container = TQueue<int>;
- container c1;
- UNIT_ASSERT(!c1);
- c1.push(100);
- c1.push(200);
- UNIT_ASSERT(c1);
- container c2(c1);
- UNIT_ASSERT_VALUES_EQUAL(2, c1.size());
- UNIT_ASSERT_VALUES_EQUAL(2, c2.size());
- container c3(std::move(c1));
- UNIT_ASSERT_VALUES_EQUAL(0, c1.size());
- UNIT_ASSERT_VALUES_EQUAL(2, c3.size());
- c2.push(300);
- c3 = c2;
- UNIT_ASSERT_VALUES_EQUAL(3, c2.size());
- UNIT_ASSERT_VALUES_EQUAL(3, c3.size());
- c2.push(400);
- c3 = std::move(c2);
- UNIT_ASSERT_VALUES_EQUAL(0, c2.size());
- UNIT_ASSERT_VALUES_EQUAL(4, c3.size());
- }
- {
- using container = TPriorityQueue<int>;
- container c1;
- UNIT_ASSERT(!c1);
- c1.push(100);
- c1.push(200);
- UNIT_ASSERT(c1);
- container c2(c1);
- UNIT_ASSERT_VALUES_EQUAL(2, c1.size());
- UNIT_ASSERT_VALUES_EQUAL(2, c2.size());
- container c3(std::move(c1));
- UNIT_ASSERT_VALUES_EQUAL(0, c1.size());
- UNIT_ASSERT_VALUES_EQUAL(2, c3.size());
- c2.push(300);
- c3 = c2;
- UNIT_ASSERT_VALUES_EQUAL(3, c2.size());
- UNIT_ASSERT_VALUES_EQUAL(3, c3.size());
- c2.push(400);
- c3 = std::move(c2);
- UNIT_ASSERT_VALUES_EQUAL(0, c2.size());
- UNIT_ASSERT_VALUES_EQUAL(4, c3.size());
- }
- }
- Y_UNIT_TEST(pqueue1) {
- TPriorityQueue<int, TDeque<int>, TLess<int>> q;
- q.push(42);
- q.push(101);
- q.push(69);
- UNIT_ASSERT(q.top() == 101);
- q.pop();
- UNIT_ASSERT(q.top() == 69);
- q.pop();
- UNIT_ASSERT(q.top() == 42);
- q.pop();
- UNIT_ASSERT(q.empty());
- }
- Y_UNIT_TEST(pqueue2) {
- using TPQueue = TPriorityQueue<int, TDeque<int>, TLess<int>>;
- TPQueue q;
- {
- TPQueue qq;
- qq.push(42);
- qq.push(101);
- qq.push(69);
- qq.swap(q);
- }
- UNIT_ASSERT(q.top() == 101);
- q.pop();
- UNIT_ASSERT(q.top() == 69);
- q.pop();
- UNIT_ASSERT(q.top() == 42);
- q.pop();
- UNIT_ASSERT(q.empty());
- }
- Y_UNIT_TEST(pqueue3) {
- TPriorityQueue<int, TDeque<int>, TLess<int>> q;
- q.push(42);
- q.push(101);
- q.push(69);
- q.clear();
- UNIT_ASSERT(q.empty());
- }
- Y_UNIT_TEST(pqueue4) {
- TDeque<int> c;
- c.push_back(42);
- c.push_back(101);
- c.push_back(69);
- TPriorityQueue<int, TDeque<int>, TLess<int>> q(TLess<int>(), std::move(c));
- UNIT_ASSERT(c.empty());
- UNIT_ASSERT_EQUAL(q.size(), 3);
- UNIT_ASSERT(q.top() == 101);
- q.pop();
- UNIT_ASSERT(q.top() == 69);
- q.pop();
- UNIT_ASSERT(q.top() == 42);
- q.pop();
- UNIT_ASSERT(q.empty());
- }
- struct THolderWithPriority {
- THolderWithPriority(const TString& value, int priority)
- : Value(MakeHolder<TString>(value))
- , Priority(priority)
- {
- }
- THolder<TString> Value; // THolder to test move-ctor
- int Priority;
- std::weak_ordering operator<=>(const THolderWithPriority& other) const noexcept {
- return Priority <=> other.Priority;
- }
- };
- Y_UNIT_TEST(pqueue5) {
- // Test move-and-pop
- TPriorityQueue<THolderWithPriority> q;
- UNIT_ASSERT(q.empty());
- q.emplace("min", 1);
- q.emplace("max", 3);
- q.emplace("middle", 2);
- auto value = q.PopValue().Value;
- UNIT_ASSERT(*value == "max");
- value = q.PopValue().Value;
- UNIT_ASSERT(*value == "middle");
- value = q.PopValue().Value;
- UNIT_ASSERT(*value == "min");
- UNIT_ASSERT(q.empty());
- }
- Y_UNIT_TEST(queue1) {
- TQueue<int, TList<int>> q;
- q.push(42);
- q.push(101);
- q.push(69);
- UNIT_ASSERT(q.front() == 42);
- q.pop();
- UNIT_ASSERT(q.front() == 101);
- q.pop();
- UNIT_ASSERT(q.front() == 69);
- q.pop();
- UNIT_ASSERT(q.empty());
- }
- Y_UNIT_TEST(queue2) {
- using TQueueType = TQueue<int>;
- TQueueType q;
- {
- TQueueType qq;
- qq.push(42);
- qq.push(101);
- qq.push(69);
- qq.swap(q);
- }
- UNIT_ASSERT(q.front() == 42);
- q.pop();
- UNIT_ASSERT(q.front() == 101);
- q.pop();
- UNIT_ASSERT(q.front() == 69);
- q.pop();
- UNIT_ASSERT(q.empty());
- }
- Y_UNIT_TEST(queue3) {
- using TQueueType = TQueue<int>;
- TQueueType q;
- q.push(42);
- q.push(101);
- q.push(69);
- q.clear();
- UNIT_ASSERT(q.empty());
- }
- } // Y_UNIT_TEST_SUITE(TYQueueTest)
|