2014-03-09 07:27:04 -06:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-03-09 07:27:04 -06:00
|
|
|
|
2015-03-01 06:52:09 -07:00
|
|
|
#include <gtest/gtest.h>
|
2014-03-09 07:27:04 -06:00
|
|
|
#include <thread>
|
|
|
|
|
2017-08-23 17:45:42 -06:00
|
|
|
#include "Common/SPSCQueue.h"
|
2014-03-09 07:27:04 -06:00
|
|
|
|
2017-08-23 17:45:42 -06:00
|
|
|
TEST(SPSCQueue, Simple)
|
2014-03-09 07:27:04 -06:00
|
|
|
{
|
2017-08-23 17:45:42 -06:00
|
|
|
Common::SPSCQueue<u32> q;
|
2014-03-09 07:27:04 -06:00
|
|
|
|
2014-03-14 20:30:59 -06:00
|
|
|
EXPECT_EQ(0u, q.Size());
|
2014-03-09 07:27:04 -06:00
|
|
|
EXPECT_TRUE(q.Empty());
|
|
|
|
|
|
|
|
q.Push(1);
|
2014-03-14 20:30:59 -06:00
|
|
|
EXPECT_EQ(1u, q.Size());
|
2014-03-09 07:27:04 -06:00
|
|
|
EXPECT_FALSE(q.Empty());
|
|
|
|
|
|
|
|
u32 v;
|
|
|
|
q.Pop(v);
|
2014-03-14 20:30:59 -06:00
|
|
|
EXPECT_EQ(1u, v);
|
|
|
|
EXPECT_EQ(0u, q.Size());
|
2014-03-09 07:27:04 -06:00
|
|
|
EXPECT_TRUE(q.Empty());
|
|
|
|
|
|
|
|
// Test the FIFO order.
|
|
|
|
for (u32 i = 0; i < 1000; ++i)
|
|
|
|
q.Push(i);
|
2014-03-14 20:30:59 -06:00
|
|
|
EXPECT_EQ(1000u, q.Size());
|
2014-03-09 07:27:04 -06: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 17:45:42 -06:00
|
|
|
TEST(SPSCQueue, MultiThreaded)
|
2014-03-09 07:27:04 -06:00
|
|
|
{
|
2017-08-23 17:45:42 -06:00
|
|
|
Common::SPSCQueue<u32> q;
|
2014-03-09 07:27:04 -06: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();
|
|
|
|
}
|