123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- #include "queue.h"
- #include <library/cpp/testing/unittest/registar.h>
- #include <util/generic/set.h>
- namespace NThreading {
- ////////////////////////////////////////////////////////////////////////////////
- Y_UNIT_TEST_SUITE(TOneOneQueueTest){
- Y_UNIT_TEST(ShouldBeEmptyAtStart){
- TOneOneQueue<int> queue;
- int result = 0;
- UNIT_ASSERT(queue.IsEmpty());
- UNIT_ASSERT(!queue.Dequeue(result));
- }
- Y_UNIT_TEST(ShouldReturnEntries) {
- TOneOneQueue<int> queue;
- queue.Enqueue(1);
- queue.Enqueue(2);
- queue.Enqueue(3);
- int result = 0;
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT_EQUAL(result, 1);
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT_EQUAL(result, 2);
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT_EQUAL(result, 3);
- UNIT_ASSERT(queue.IsEmpty());
- UNIT_ASSERT(!queue.Dequeue(result));
- }
- Y_UNIT_TEST(ShouldStoreMultipleChunks) {
- TOneOneQueue<int, 100> queue;
- for (int i = 0; i < 1000; ++i) {
- queue.Enqueue(i);
- }
- for (int i = 0; i < 1000; ++i) {
- int result = 0;
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT_EQUAL(result, i);
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
- Y_UNIT_TEST_SUITE(TManyOneQueueTest){
- Y_UNIT_TEST(ShouldBeEmptyAtStart){
- TManyOneQueue<int> queue;
- int result;
- UNIT_ASSERT(queue.IsEmpty());
- UNIT_ASSERT(!queue.Dequeue(result));
- }
- Y_UNIT_TEST(ShouldReturnEntries) {
- TManyOneQueue<int> queue;
- queue.Enqueue(1);
- queue.Enqueue(2);
- queue.Enqueue(3);
- int result = 0;
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT_EQUAL(result, 1);
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT_EQUAL(result, 2);
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT_EQUAL(result, 3);
- UNIT_ASSERT(queue.IsEmpty());
- UNIT_ASSERT(!queue.Dequeue(result));
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
- Y_UNIT_TEST_SUITE(TManyManyQueueTest){
- Y_UNIT_TEST(ShouldBeEmptyAtStart){
- TManyManyQueue<int> queue;
- int result = 0;
- UNIT_ASSERT(queue.IsEmpty());
- UNIT_ASSERT(!queue.Dequeue(result));
- }
- Y_UNIT_TEST(ShouldReturnEntries) {
- TManyManyQueue<int> queue;
- queue.Enqueue(1);
- queue.Enqueue(2);
- queue.Enqueue(3);
- int result = 0;
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT_EQUAL(result, 1);
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT_EQUAL(result, 2);
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT_EQUAL(result, 3);
- UNIT_ASSERT(queue.IsEmpty());
- UNIT_ASSERT(!queue.Dequeue(result));
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
- Y_UNIT_TEST_SUITE(TRelaxedManyOneQueueTest){
- Y_UNIT_TEST(ShouldBeEmptyAtStart){
- TRelaxedManyOneQueue<int> queue;
- int result;
- UNIT_ASSERT(queue.IsEmpty());
- UNIT_ASSERT(!queue.Dequeue(result));
- }
- Y_UNIT_TEST(ShouldReturnEntries) {
- TSet<int> items = {1, 2, 3};
- TRelaxedManyOneQueue<int> queue;
- for (int item : items) {
- queue.Enqueue(item);
- }
- int result = 0;
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT(items.erase(result));
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT(items.erase(result));
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT(items.erase(result));
- UNIT_ASSERT(queue.IsEmpty());
- UNIT_ASSERT(!queue.Dequeue(result));
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
- Y_UNIT_TEST_SUITE(TRelaxedManyManyQueueTest){
- Y_UNIT_TEST(ShouldBeEmptyAtStart){
- TRelaxedManyManyQueue<int> queue;
- int result = 0;
- UNIT_ASSERT(queue.IsEmpty());
- UNIT_ASSERT(!queue.Dequeue(result));
- }
- Y_UNIT_TEST(ShouldReturnEntries) {
- TSet<int> items = {1, 2, 3};
- TRelaxedManyManyQueue<int> queue;
- for (int item : items) {
- queue.Enqueue(item);
- }
- int result = 0;
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT(items.erase(result));
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT(items.erase(result));
- UNIT_ASSERT(!queue.IsEmpty());
- UNIT_ASSERT(queue.Dequeue(result));
- UNIT_ASSERT(items.erase(result));
- UNIT_ASSERT(queue.IsEmpty());
- UNIT_ASSERT(!queue.Dequeue(result));
- }
- }
- }
|