2014-03-09 13:27:04 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-03-09 13:27:04 +00:00
|
|
|
|
2015-03-01 13:52:09 +00:00
|
|
|
#include <gtest/gtest.h>
|
2014-03-09 13:27:04 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2017-08-23 23:45:42 +00:00
|
|
|
#include "Common/SPSCQueue.h"
|
2014-03-09 13:27:04 +00:00
|
|
|
|
2017-08-23 23:45:42 +00:00
|
|
|
TEST(SPSCQueue, Simple)
|
2014-03-09 13:27:04 +00:00
|
|
|
{
|
2017-08-23 23:45:42 +00:00
|
|
|
Common::SPSCQueue<u32> q;
|
2014-03-09 13:27:04 +00:00
|
|
|
|
2014-03-15 02:30:59 +00:00
|
|
|
EXPECT_EQ(0u, q.Size());
|
2014-03-09 13:27:04 +00:00
|
|
|
EXPECT_TRUE(q.Empty());
|
|
|
|
|
|
|
|
q.Push(1);
|
2014-03-15 02:30:59 +00:00
|
|
|
EXPECT_EQ(1u, q.Size());
|
2014-03-09 13:27:04 +00:00
|
|
|
EXPECT_FALSE(q.Empty());
|
|
|
|
|
|
|
|
u32 v;
|
|
|
|
q.Pop(v);
|
2014-03-15 02:30:59 +00:00
|
|
|
EXPECT_EQ(1u, v);
|
|
|
|
EXPECT_EQ(0u, q.Size());
|
2014-03-09 13:27:04 +00:00
|
|
|
EXPECT_TRUE(q.Empty());
|
|
|
|
|
|
|
|
// Test the FIFO order.
|
|
|
|
for (u32 i = 0; i < 1000; ++i)
|
|
|
|
q.Push(i);
|
2014-03-15 02:30:59 +00:00
|
|
|
EXPECT_EQ(1000u, q.Size());
|
2014-03-09 13:27:04 +00:00
|
|
|
for (u32 i = 0; i < 1000; ++i)
|
|
|
|
{
|
|
|
|
u32 v2;
|
|
|
|
q.Pop(v2);
|
|
|
|
EXPECT_EQ(i, v2);
|
|
|
|
}
|
|
|
|
EXPECT_TRUE(q.Empty());
|
|
|
|
|
|
|
|
for (u32 i = 0; i < 1000; ++i)
|
|
|
|
q.Push(i);
|
|
|
|
EXPECT_FALSE(q.Empty());
|
|
|
|
q.Clear();
|
|
|
|
EXPECT_TRUE(q.Empty());
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:45:42 +00:00
|
|
|
TEST(SPSCQueue, MultiThreaded)
|
2014-03-09 13:27:04 +00:00
|
|
|
{
|
2017-08-23 23:45:42 +00:00
|
|
|
Common::SPSCQueue<u32> q;
|
2014-03-09 13:27:04 +00:00
|
|
|
|
|
|
|
auto inserter = [&q]() {
|
|
|
|
for (u32 i = 0; i < 100000; ++i)
|
|
|
|
q.Push(i);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto popper = [&q]() {
|
|
|
|
for (u32 i = 0; i < 100000; ++i)
|
|
|
|
{
|
|
|
|
while (q.Empty())
|
|
|
|
;
|
|
|
|
u32 v;
|
|
|
|
q.Pop(v);
|
|
|
|
EXPECT_EQ(i, v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::thread popper_thread(popper);
|
|
|
|
std::thread inserter_thread(inserter);
|
|
|
|
|
|
|
|
popper_thread.join();
|
|
|
|
inserter_thread.join();
|
|
|
|
}
|