summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Common/SPSCQueueTest.cpp
diff options
context:
space:
mode:
authorMichael M <mchtly@gmail.com>2017-08-23 16:45:42 -0700
committerMichael M <mchtly@gmail.com>2017-08-23 17:00:52 -0700
commitb58f8d19ab20c2b4511e3516e69a26a28d4d642c (patch)
tree701da56d12829ef7bb5bde25e4c2ea86fedf7cbb /Source/UnitTests/Common/SPSCQueueTest.cpp
parent4ee85a3e076f8f5569809542b8d8a8869b9b47f9 (diff)
Rename Common::FifoQueue to Common::SPSCQueue
Since all queues are FIFO data structures, the name wasn't informative as to why you'd use it over a normal queue. I originally thought it had something to do with the hardware graphics FIFO. This renames it using the common acronym SPSC, which stands for single-producer single-consumer, and is most commonly used to talk about lock-free data structures, both of which this is.
Diffstat (limited to 'Source/UnitTests/Common/SPSCQueueTest.cpp')
-rw-r--r--Source/UnitTests/Common/SPSCQueueTest.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/Source/UnitTests/Common/SPSCQueueTest.cpp b/Source/UnitTests/Common/SPSCQueueTest.cpp
new file mode 100644
index 0000000000..0a4fbfcc1c
--- /dev/null
+++ b/Source/UnitTests/Common/SPSCQueueTest.cpp
@@ -0,0 +1,71 @@
+// Copyright 2014 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#include <gtest/gtest.h>
+#include <thread>
+
+#include "Common/SPSCQueue.h"
+
+TEST(SPSCQueue, Simple)
+{
+ Common::SPSCQueue<u32> q;
+
+ EXPECT_EQ(0u, q.Size());
+ EXPECT_TRUE(q.Empty());
+
+ q.Push(1);
+ EXPECT_EQ(1u, q.Size());
+ EXPECT_FALSE(q.Empty());
+
+ u32 v;
+ q.Pop(v);
+ EXPECT_EQ(1u, v);
+ EXPECT_EQ(0u, q.Size());
+ EXPECT_TRUE(q.Empty());
+
+ // Test the FIFO order.
+ for (u32 i = 0; i < 1000; ++i)
+ q.Push(i);
+ EXPECT_EQ(1000u, q.Size());
+ 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());
+}
+
+TEST(SPSCQueue, MultiThreaded)
+{
+ Common::SPSCQueue<u32> q;
+
+ 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();
+}