Fixes misc test cases
authorPeizhao Ou <peizhaoo@uci.edu>
Sat, 9 Dec 2017 16:57:27 +0000 (08:57 -0800)
committerPeizhao Ou <peizhaoo@uci.edu>
Sat, 9 Dec 2017 16:57:27 +0000 (08:57 -0800)
test/stress/misc/deque_driver.cpp
test/stress/misc/mcslock_driver.cpp
test/stress/misc/rwlock_driver.cpp
test/stress/misc/spinlock_driver.cpp

index da84edb..e49b4a7 100644 (file)
@@ -54,15 +54,15 @@ protected:
         }
       }
     }
-    //    while (true) {
-    //      int res = deque->take();
-    //      if (res != EMPTY) {
-    //        sums[index] += res;
-    //        succ_counts[index]++;
-    //      } else {
-    //        break;
-    //      }
-    //    }
+    while (true) {
+      int res = deque->take();
+      if (res != EMPTY) {
+        sums[index] += res;
+        succ_counts[index]++;
+      } else {
+        break;
+      }
+    }
   }
 };
 
@@ -103,10 +103,13 @@ TEST_F(ChaseLevDequeTest, DequePushPopTake) {
     received_sum += sums[i];
     overall_count += succ_counts[i];
   }
-  cout << "Sum of push: " << push_sum << "\n";
-  cout << "Received sum:" << received_sum << "\n";
-  cout << "overall_count:" << overall_count << "\n";
-  cout << "push_count=" << push_count << "\n";
+  if (overall_count != push_count || received_sum != push_sum) {
+    cout << "Incorrect deque\n";
+    cout << "Push sum: " << push_sum << "\n";
+    cout << "Received sum:" << received_sum << "\n";
+    cout << "Push count=" << push_count << "\n";
+    cout << "Received count:" << overall_count << "\n";
+  }
 }
 
 } // namespace
index 1435e97..2a2091b 100644 (file)
@@ -27,11 +27,10 @@ protected:
 
   static void Thread() {
     cds_others::mcs_mutex::guard g(my_mutex);
-    x = 1;
     my_mutex->unlock(&g);
     for (ullong i = 0; i < s_nMCSLockPassCount; i++) {
       my_mutex->lock(&g);
-      x = i;
+      x++;
       my_mutex->unlock(&g);
     }
     my_mutex->lock(&g);
@@ -43,14 +42,17 @@ cds_others::mcs_mutex *MCSLockTest::my_mutex;
 
 TEST_F(MCSLockTest, BasicLockUnlock) {
   my_mutex = new cds_others::mcs_mutex();
+  x = 0;
   std::thread *threads = new std::thread[s_nMCSLockThreadCount];
   for (int i = 0; i < s_nMCSLockThreadCount; i++) {
     threads[i] = std::thread(Thread);
   }
-
   for (int i = 0; i < s_nMCSLockThreadCount; i++) {
     threads[i].join();
   }
+  if (x != s_nMCSLockPassCount * s_nMCSLockThreadCount) {
+    cout << "MCS lock incorrect\n";
+  }
 }
 
 } // namespace
index 7d0084d..2c61d44 100644 (file)
@@ -46,11 +46,11 @@ protected:
           if (!rwlock->write_trylock()) {
             rwlock->write_lock();
           }
-          x += 1;
+          x++;
           rwlock->write_unlock();
         } else {
           rwlock->write_lock();
-          x += 1;
+          x++;
           rwlock->write_unlock();
         }
       }
@@ -65,7 +65,7 @@ RWLock *RWLockTest::rwlock;
 TEST_F(RWLockTest, BasicLockUnlock) {
   rwlock = new RWLock();
   int num_threads = s_nRWLockThreadCount;
-  for (int write_percentage = 5; write_percentage < 50; write_percentage += 5) {
+  for (int write_percentage = 5; write_percentage < 40; write_percentage += 5) {
     std::thread *threads = new std::thread[num_threads];
     for (size_t i = 0; i < num_threads; i++) {
       threads[i] = std::thread(ReaderWriterThread, write_percentage);
index b1b0e95..a811db2 100644 (file)
@@ -24,17 +24,16 @@ static size_t s_nTicketLockPassCount = 4000000;
 #define TASK(lock_type, lock_ptr, pass_cnt)                                    \
   static void Thread##lock_type() {                                            \
     for (int i = 0; i < pass_cnt; i++) {                                       \
-      for (int j = 0; j < pass_cnt; j++) {                                     \
-        lock_ptr->lock();                                                      \
-        x = i + j;                                                             \
-        lock_ptr->unlock();                                                    \
-      }                                                                        \
+      lock_ptr->lock();                                                        \
+      x++;                                                                     \
+      lock_ptr->unlock();                                                      \
     }                                                                          \
   }
 
-#define LOCK_TEST(lock_type, lock_ptr)                                         \
+#define LOCK_TEST(lock_type, lock_ptr, pass_cnt)                               \
   TEST_F(SpinLockTest, lock_type) {                                            \
     lock_ptr = new lock_type();                                                \
+    x = 0;                                                                     \
     std::thread *threads = new std::thread[s_nSpinLockThreadCount];            \
     for (int i = 0; i < s_nSpinLockThreadCount; i++) {                         \
       threads[i] = std::thread(Thread##lock_type);                             \
@@ -42,6 +41,9 @@ static size_t s_nTicketLockPassCount = 4000000;
     for (int i = 0; i < s_nSpinLockThreadCount; i++) {                         \
       threads[i].join();                                                       \
     }                                                                          \
+    if (x != s_nSpinLockThreadCount * pass_cnt) {                              \
+      cout << "Incorrect " << #lock_type << "\n";                              \
+    }                                                                          \
   }
 
 class SpinLockTest : public cds_test::stress_fixture {
@@ -71,9 +73,9 @@ SpinLock *SpinLockTest::spin_mutex;
 Reentrant32 *SpinLockTest::reentrant_mutex32;
 Reentrant64 *SpinLockTest::reentrant_mutex64;
 
-LOCK_TEST(TicketLock, ticket_mutex)
-LOCK_TEST(SpinLock, spin_mutex)
-LOCK_TEST(Reentrant32, reentrant_mutex32)
-LOCK_TEST(Reentrant64, reentrant_mutex64)
+LOCK_TEST(TicketLock, ticket_mutex, s_nTicketLockPassCount)
+LOCK_TEST(SpinLock, spin_mutex, s_nSpinLockPassCount)
+LOCK_TEST(Reentrant32, reentrant_mutex32, s_nSpinLockPassCount)
+LOCK_TEST(Reentrant64, reentrant_mutex64, s_nSpinLockPassCount)
 
 } // namespace