1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <xenium/vyukov_bounded_queue.hpp>
#include <gtest/gtest.h>
#include <vector>
#include <thread>
namespace {
struct VyukovBoundedQueue : testing::Test {};
TEST(VyukovBoundedQueue, push_try_pop_returns_pushed_element)
{
xenium::vyukov_bounded_queue<int> queue(2);
static_assert(xenium::vyukov_bounded_queue<int>::default_to_weak == false, "");
EXPECT_TRUE(queue.try_push(42));
int elem;
EXPECT_TRUE(queue.try_pop(elem));
EXPECT_EQ(42, elem);
}
TEST(VyukovBoundedQueue, push_try_pop_weak_returns_pushed_element)
{
xenium::vyukov_bounded_queue<int> queue(2);
EXPECT_TRUE(queue.try_push(42));
int elem;
EXPECT_TRUE(queue.try_pop_weak(elem));
EXPECT_EQ(42, elem);
}
TEST(VyukovBoundedQueue, push_two_items_pop_them_in_FIFO_order)
{
xenium::vyukov_bounded_queue<int> queue(2);
EXPECT_TRUE(queue.try_push(42));
EXPECT_TRUE(queue.try_push(43));
int elem1;
int elem2;
EXPECT_TRUE(queue.try_pop(elem1));
EXPECT_TRUE(queue.try_pop(elem2));
EXPECT_EQ(42, elem1);
EXPECT_EQ(43, elem2);
}
TEST(VyukovBoundedQueue, try_pop_returns_false_when_queue_is_empty)
{
xenium::vyukov_bounded_queue<int> queue(2);
int elem;
EXPECT_FALSE(queue.try_pop(elem));
EXPECT_FALSE(queue.try_pop_weak(elem));
}
TEST(VyukovBoundedQueue, try_push_returns_false_when_queue_is_full)
{
xenium::vyukov_bounded_queue<int> queue(2);
EXPECT_TRUE(queue.try_push(42));
EXPECT_TRUE(queue.try_push(43));
EXPECT_FALSE(queue.try_push(44));
EXPECT_FALSE(queue.try_push_weak(44));
}
TEST(VyukovBoundedQueue, supports_move_only_types)
{
xenium::vyukov_bounded_queue<std::pair<int, std::unique_ptr<int>>> queue(2);
queue.try_push(41, std::unique_ptr<int>(new int(42)));
std::pair<int, std::unique_ptr<int>> elem;
ASSERT_TRUE(queue.try_pop(elem));
EXPECT_EQ(41, elem.first);
ASSERT_NE(nullptr, elem.second);
EXPECT_EQ(42, *elem.second);
}
TEST(VyukovBoundedQueue, parallel_usage)
{
xenium::vyukov_bounded_queue<int> queue(8);
std::vector<std::thread> threads;
for (int i = 0; i < 4; ++i)
{
threads.push_back(std::thread([i, &queue]
{
#ifdef DEBUG
const int MaxIterations = 40000;
#else
const int MaxIterations = 400000;
#endif
for (int j = 0; j < MaxIterations; ++j)
{
EXPECT_TRUE(queue.try_push(i));
int elem = 0;
EXPECT_TRUE(queue.try_pop(elem));
EXPECT_TRUE(elem >= 0 && elem <= 4);
}
}));
}
for (auto& thread : threads)
thread.join();
}
TEST(VyukovBoundedQueue, parallel_usage_of_weak_operations)
{
xenium::vyukov_bounded_queue<int> queue(8);
std::vector<std::thread> threads;
for (int i = 0; i < 4; ++i)
{
threads.push_back(std::thread([i, &queue]
{
#ifdef DEBUG
const int MaxIterations = 40000;
#else
const int MaxIterations = 400000;
#endif
for (int j = 0; j < MaxIterations; ++j)
{
queue.try_push_weak(i);
int elem;
if (queue.try_pop_weak(elem)) {
EXPECT_TRUE(elem >= 0 && elem <= 4);
}
}
}));
}
for (auto& thread : threads)
thread.join();
}
}
|