Refactors parallel barrier test
[libcds.git] / test / stress / misc / barrier_driver.cpp
index 438d033a88798b1949ebe1d4f9525ffa4de4093c..272e3c43d3eef3253d5fa3fb469432923479b577 100644 (file)
@@ -2,9 +2,11 @@
 #include <atomic>
 #include <cds/gc/dhp.h>
 #include <cds/gc/hp.h>
-#include <cds/sync/barrier.h>
+#include <cds/misc/barrier.h>
 #include <cds_test/stress_test.h>
 #include <iostream>
+#include <memory>
+#include <vector>
 #include <thread>
 
 using namespace std;
@@ -13,13 +15,13 @@ namespace {
 
 typedef cds_others::SpinBarrier Barrier;
 
-static size_t s_nBarrierThreadCount = 6;
+static size_t s_nBarrierThreadCount = 2;
 static size_t s_nBarrierPassCount = 100000000;
 
 class BarrierTest : public cds_test::stress_fixture {
 protected:
   static Barrier *barrier;
-  static int count;
+  static size_t count;
 
   static void SetUpTestCase() {
     cds_test::config const &cfg = get_config("Misc");
@@ -27,14 +29,8 @@ protected:
     GetConfig(BarrierThreadCount);
   }
 
-  static void TearDownTestCase() {
-    if (count != s_nBarrierPassCount) {
-      cout << "Incorrect" << endl;
-    }
-  }
-
   static void Thread() {
-    for (ullong i = 0; i < s_nBarrierPassCount; i++) {
+    for (size_t i = 0; i < s_nBarrierPassCount; i++) {
       if (barrier->wait()) {
         count++;
       }
@@ -43,18 +39,20 @@ protected:
 };
 
 Barrier *BarrierTest::barrier;
-int BarrierTest::count;
+size_t BarrierTest::count;
 
 TEST_F(BarrierTest, Wait) {
   barrier = new Barrier(s_nBarrierThreadCount);
-  std::thread *threads = new std::thread[s_nBarrierThreadCount];
-  for (int i = 0; i < s_nBarrierThreadCount; i++) {
-    threads[i] = std::thread(Thread);
+  std::vector<std::thread> threads;
+  for (size_t i = 0; i < s_nBarrierThreadCount; i++) {
+    threads.push_back(std::thread(Thread));
   }
 
-  for (int i = 0; i < s_nBarrierThreadCount; i++) {
-    threads[i].join();
+  for (auto& thrd : threads) {
+    thrd.join();
   }
+  EXPECT_EQ(count, s_nBarrierPassCount);
+  delete barrier;
 }
 
 } // namespace