]> git.seodisparate.com - UDPConnection/commitdiff
Add more to unit test for TSQueue
authorStephen Seo <seo.disparate@gmail.com>
Fri, 7 Jun 2019 03:17:35 +0000 (12:17 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Fri, 7 Jun 2019 03:17:35 +0000 (12:17 +0900)
cpp_impl/src/TSQueue.hpp
cpp_impl/src/test/TestTSQueue.cpp

index 1afae60a6f353ce357335c3f3beb93fa1ddc6047..0a5e61612a0e11a7e222a8be8fd7bf88f4a3eb38 100644 (file)
@@ -9,6 +9,8 @@
 
 class TSQueue {
   public:
+    typedef std::unique_ptr<unsigned char[]> TopType;
+
     TSQueue(unsigned int elemSize,
             unsigned int capacity = UDPC_TSQUEUE_DEFAULT_CAPACITY);
     ~TSQueue();
@@ -21,7 +23,7 @@ class TSQueue {
     TSQueue &operator=(TSQueue &&other) = delete;
 
     bool push(void *data);
-    std::unique_ptr<unsigned char[]> top();
+    TopType top();
     bool pop();
     void clear();
     void changeCapacity(unsigned int newCapacity);
index 698a0da1ef2f914f18ec7beabe3ab4aa65a1b712..f1adeea5fddff804a24e88089f0be5600a1a8282 100644 (file)
@@ -1,5 +1,8 @@
 #include <gtest/gtest.h>
 
+#include <cstdio>
+#include <future>
+
 #include "TSQueue.hpp"
 
 TEST(TSQueue, Usage)
@@ -107,3 +110,44 @@ TEST(TSQueue, Usage)
     EXPECT_FALSE(q.pop());
     EXPECT_EQ(0, q.size());
 }
+
+TEST(TSQueue, Concurrent)
+{
+    TSQueue q(sizeof(int), 4);
+
+    auto a0 = std::async(std::launch::async, [&q] () {int i = 0; return q.push(&i); });
+    auto a1 = std::async(std::launch::async, [&q] () {int i = 1; return q.push(&i); });
+    auto a2 = std::async(std::launch::async, [&q] () {int i = 2; return q.push(&i); });
+    auto a3 = std::async(std::launch::async, [&q] () {int i = 3; return q.push(&i); });
+    auto a4 = std::async(std::launch::async, [&q] () {int i = 4; return q.push(&i); });
+
+    bool results[] = {
+        a0.get(),
+        a1.get(),
+        a2.get(),
+        a3.get(),
+        a4.get()
+    };
+
+    int insertCount = 0;
+    for(int i = 0; i < 5; ++i) {
+        if(results[i]) {
+            ++insertCount;
+        }
+    }
+
+    EXPECT_EQ(insertCount, 4);
+    EXPECT_EQ(q.size(), 4);
+
+    TSQueue::TopType top;
+    for(int i = 0; i < 4; ++i) {
+        top = q.top();
+        EXPECT_TRUE(q.pop());
+        EXPECT_EQ(q.size(), 3 - i);
+        printf("%d ", *((int*)top.get()));
+    }
+    printf("\n");
+
+    EXPECT_FALSE(q.pop());
+    EXPECT_EQ(q.size(), 0);
+}