summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Common/FifoQueueTest.cpp
diff options
context:
space:
mode:
authorPierre Bourdon <delroth@gmail.com>2014-03-09 14:51:53 +0100
committerPierre Bourdon <delroth@gmail.com>2014-03-09 14:51:53 +0100
commitb003fd79d7bee1780d980465b03b23d0fd266e9e (patch)
treede17b787e5270cc427ccb4325f0c291d58c6fcf1 /Source/UnitTests/Common/FifoQueueTest.cpp
parent0ea58cddf910cef48b1a08303831027b6ad01475 (diff)
parentaabd524142625647fdad84ecf2401ba6cadfcfa6 (diff)
Merge pull request #146 from delroth/tests
Add more tests for Common and Core/MMIO
Diffstat (limited to 'Source/UnitTests/Common/FifoQueueTest.cpp')
-rw-r--r--Source/UnitTests/Common/FifoQueueTest.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/Source/UnitTests/Common/FifoQueueTest.cpp b/Source/UnitTests/Common/FifoQueueTest.cpp
new file mode 100644
index 0000000000..60135e699c
--- /dev/null
+++ b/Source/UnitTests/Common/FifoQueueTest.cpp
@@ -0,0 +1,67 @@
+// Copyright 2014 Dolphin Emulator Project
+// Licensed under GPLv2
+// Refer to the license.txt file included.
+
+#include <gtest/gtest.h>
+#include <thread>
+
+#include "Common/FifoQueue.h"
+
+TEST(FifoQueue, Simple)
+{
+ Common::FifoQueue<u32> q;
+
+ EXPECT_EQ(0, q.Size());
+ EXPECT_TRUE(q.Empty());
+
+ q.Push(1);
+ EXPECT_EQ(1, q.Size());
+ EXPECT_FALSE(q.Empty());
+
+ u32 v; q.Pop(v);
+ EXPECT_EQ(1, v);
+ EXPECT_EQ(0, q.Size());
+ EXPECT_TRUE(q.Empty());
+
+ // Test the FIFO order.
+ for (u32 i = 0; i < 1000; ++i)
+ q.Push(i);
+ EXPECT_EQ(1000, 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(FifoQueue, MultiThreaded)
+{
+ Common::FifoQueue<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();
+}