Refactors test cases to use gtest framework
[junction.git] / test / junction_driver.cpp
index 62b9d6253a3afb81dc5c094d767ab43ebf8acafb..d0164387950f63a585733dde3ce66a531ec5ceb6 100644 (file)
+#include <cassert>
+#include <chrono>
+#include <iostream>
+#include <junction/ConcurrentMap_Crude.h>
 #include <junction/ConcurrentMap_Grampa.h>
-#include <junction/ConcurrentMap_Linear.h>
 #include <junction/ConcurrentMap_Leapfrog.h>
-#include <junction/ConcurrentMap_Crude.h>
-#include <iostream>
+#include <junction/ConcurrentMap_Linear.h>
 #include <memory>
-#include <chrono>
-#include <cassert>
 
-namespace {
+#include <gtest/gtest.h>
 
-const unsigned s_nInsertPercentage = 10;
-const char* kTestName = "InsDelFind";
-// Run GC after "kGCFrequency" operations.
-const size_t kGCFrequency = 3000;
+namespace junction_test {
 
-const size_t kCrudeMapSize = 10000;
-const size_t kCrudePassCount = 40000;
-const char* kCrudeBenchmarkName = "JunctionMapCrude";
+class JunctionMapInsDelFindTest : public ::testing::Test {
+protected:
+  typedef junction::ConcurrentMap_Grampa<size_t, size_t> GrampaMap;
+  typedef junction::ConcurrentMap_Linear<size_t, size_t> LinearMap;
+  typedef junction::ConcurrentMap_Leapfrog<size_t, size_t> LeapfrogMap;
+  typedef junction::ConcurrentMap_Crude<size_t, size_t> CrudeMap;
 
-const size_t kGrampaMapSize = 20000;
-const size_t kGrampaPassCount = 30000;
-const char* kGrampaBenchmarkName = "JunctionMapGrampa";
+  static const unsigned s_nInsertPercentage = 10;
+  // Run GC after "kGCFrequency" operations.
+  static const size_t kGCFrequency = 3000;
+  static const size_t kLeapfrogGCFrequency = 1500;
 
-const size_t kLinearMapSize = 20000;
-const size_t kLinearPassCount = 70000;
-const char* kLinearBenchmarkName = "JunctionMapLinear";
+  static const size_t kCrudeMapSize = 10000;
+  static const size_t kCrudePassCount = 400000;
 
-const size_t kLeapfrogMapSize = 20000;
-const size_t kLeapfrogPassCount = 75000;
-const char* kLeapfrogBenchmarkName = "JunctionMapLeapfrog";
+  static const size_t kGrampaMapSize = 20000;
+  static const size_t kGrampaPassCount = 60000;
 
-} // namespace
+  static const size_t kLinearMapSize = 20000;
+  static const size_t kLinearPassCount = 70000;
 
-typedef junction::ConcurrentMap_Grampa<size_t, size_t> GrampaMap;
-typedef junction::ConcurrentMap_Linear<size_t, size_t> LinearMap;
-typedef junction::ConcurrentMap_Leapfrog<size_t, size_t> LeapfrogMap;
-typedef junction::ConcurrentMap_Crude<size_t, size_t> CrudeMap;
+  static const size_t kLeapfrogMapSize = 20000;
+  static const size_t kLeapfrogPassCount = 75000;
 
-template <typename Map>
-void run_crude_map(size_t map_size, size_t pass_count, const char* bench_name) {
-    std::cout << "[ RUN      ] " << kTestName << "." << bench_name << "\n";
-    auto start_time = std::chrono::system_clock::now();
+  static void SetUpTestCase() {}
 
+  template <typename Map>
+  static void run_test(size_t map_size, size_t pass_count,
+                       size_t gc_frequency) {
     size_t nInsertedNum = 0;
     size_t nFindSuccess = 0;
     size_t nOperations = 0;
-    // Seems like the crude map won't resize, so better have a large enough
-    // capacity.
-    std::unique_ptr<Map> map(new Map(map_size * 32));
+    std::unique_ptr<Map> map(new Map());
     auto qsbrContext = junction::DefaultQSBR.createContext();
     for (size_t count = 0; count < pass_count; count++) {
-        for (size_t i = 0; i < map_size; ++i) {
-            // The number to operate on the map.
-            size_t n = map_size + i;
-            // Insert
-            if (i % s_nInsertPercentage == 1) {
-                map->assign(i, n);
-                nInsertedNum++;
-//                std::cout << "Inserted" << i << "\n";
-            }
-            // Find
-            {
-                if (map->get(i)) {
-                    ++nFindSuccess;
-//                    std::cout << "Found" << i << "\n";
-                }
-            }
-            // Delete
-            if (i % s_nInsertPercentage == 1) {
-                if (map->get(i)) {
-                    map->assign(n, 0);
-//                    std::cout << "Erased" << i << "\n";
-                }
-            }
-            if (++nOperations > kGCFrequency) {
-              junction::DefaultQSBR.update(qsbrContext);
-              nOperations = 0;
-            }
+      for (size_t i = 0; i < map_size; ++i) {
+        // The number to operate on the map.
+        size_t n = map_size + i;
+        // Insert
+        if (i % s_nInsertPercentage == 1) {
+          auto iter = map->insertOrFind(i);
+          if (!iter.getValue()) {
+            iter.assignValue(n);
+            nInsertedNum++;
+            //                  std::cout << "Inserted" << i << "\n";
+          }
+        }
+        // Find
+        {
+          auto iter = map->find(i);
+          if (iter.getValue()) {
+            ++nFindSuccess;
+            //                    std::cout << "Found" << i << "\n";
+          }
         }
+        // Delete
+        if (i % s_nInsertPercentage == 1) {
+          auto iter = map->find(i);
+          if (iter.getValue()) {
+            iter.eraseValue();
+            //                    std::cout << "Erased" << i << "\n";
+          }
+        }
+        if (++nOperations > gc_frequency) {
+          junction::DefaultQSBR.update(qsbrContext);
+          nOperations = 0;
+        }
+      }
     }
     junction::DefaultQSBR.update(qsbrContext);
-    auto finish_time = std::chrono::system_clock::now();
-    auto dur = finish_time - start_time;
-    auto milisecs = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
-
-    if (nFindSuccess != nInsertedNum) {
-        std::cout << "nFindSuccess=" << nFindSuccess << ", nInsertedNum="
-                  << nInsertedNum << "\n";
-        std::cout << "[       FAILED ] " << kTestName << "." << bench_name
-                  << "(" << milisecs.count() << " ms)\n";
-        assert(false && "ConcurrentMap ERROR");
-    } else {
-        std::cout << "[       OK ] " << kTestName << "." << bench_name
-                  << "(" << milisecs.count() << " ms)\n";
-    }
-}
-
-template <typename Map>
-void run_test(size_t map_size, size_t pass_count, const char* bench_name) {
-    std::cout << "[ RUN      ] " << kTestName << "." << bench_name << "\n";
-    auto start_time = std::chrono::system_clock::now();
+    junction::DefaultQSBR.destroyContext(qsbrContext);
+    EXPECT_EQ(nFindSuccess, nInsertedNum);
+  }
 
+  template <typename Map>
+  void run_crude_map(size_t map_size, size_t pass_count, size_t gc_frequency) {
     size_t nInsertedNum = 0;
     size_t nFindSuccess = 0;
     size_t nOperations = 0;
-    std::unique_ptr<Map> map(new Map());
+    // Seems like the crude map won't resize, so better have a large enough
+    // capacity.
+    std::unique_ptr<Map> map(new Map(map_size * 32));
     auto qsbrContext = junction::DefaultQSBR.createContext();
     for (size_t count = 0; count < pass_count; count++) {
-        for (size_t i = 0; i < map_size; ++i) {
-            // The number to operate on the map.
-            size_t n = map_size + i;
-            // Insert
-            if (i % s_nInsertPercentage == 1) {
-                auto iter = map->insertOrFind(i);
-                if (!iter.getValue()) {
-                  iter.assignValue(n);
-                  nInsertedNum++;
-//                  std::cout << "Inserted" << i << "\n";
-                }
-            }
-            // Find
-            {
-                auto iter = map->find(i);
-                if (iter.getValue()) {
-                    ++nFindSuccess;
-//                    std::cout << "Found" << i << "\n";
-                }
-            }
-            // Delete
-            if (i % s_nInsertPercentage == 1) {
-                auto iter = map->find(i);
-                if (iter.getValue()) {
-                    iter.eraseValue();
-//                    std::cout << "Erased" << i << "\n";
-                }
-            }
-            if (++nOperations > kGCFrequency) {
-                junction::DefaultQSBR.update(qsbrContext);
-                nOperations = 0;
-            }
+      for (size_t i = 0; i < map_size; ++i) {
+        // The number to operate on the map.
+        size_t n = map_size + i;
+        // Insert
+        if (i % s_nInsertPercentage == 1) {
+          map->assign(i, n);
+          nInsertedNum++;
+          //                std::cout << "Inserted" << i << "\n";
         }
+        // Find
+        {
+          if (map->get(i)) {
+            ++nFindSuccess;
+            //                    std::cout << "Found" << i << "\n";
+          }
+        }
+        // Delete
+        if (i % s_nInsertPercentage == 1) {
+          if (map->get(i)) {
+            map->assign(n, 0);
+            //                    std::cout << "Erased" << i << "\n";
+          }
+        }
+        if (++nOperations > gc_frequency) {
+          junction::DefaultQSBR.update(qsbrContext);
+          nOperations = 0;
+        }
+      }
     }
-    auto finish_time = std::chrono::system_clock::now();
-    auto dur = finish_time - start_time;
-    auto milisecs = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
-
-    if (nFindSuccess != nInsertedNum) {
-        std::cout << "nFindSuccess=" << nFindSuccess << ", nInsertedNum="
-                  << nInsertedNum << "\n";
-        std::cout << "[       FAILED ] " << kTestName << "." << bench_name
-                  << "(" << milisecs.count() << " ms)\n";
-        assert(false && "ConcurrentMap ERROR");
-    } else {
-        std::cout << "[       OK ] " << kTestName << "." << bench_name
-                  << "(" << milisecs.count() << " ms)\n";
-    }
+    junction::DefaultQSBR.update(qsbrContext);
+    junction::DefaultQSBR.destroyContext(qsbrContext);
+
+    EXPECT_EQ(nFindSuccess, nInsertedNum);
+  }
+};
+
+TEST_F(JunctionMapInsDelFindTest, JunctionMapCrude) {
+  run_crude_map<CrudeMap>(kCrudeMapSize, kCrudePassCount, kGCFrequency);
 }
 
-int main() {
-    run_test<LinearMap>(kLinearMapSize, kLinearPassCount, kLinearBenchmarkName);
-    run_test<LeapfrogMap>(kLeapfrogMapSize, kLeapfrogPassCount, kLeapfrogBenchmarkName);
-    run_test<GrampaMap>(kGrampaMapSize, kGrampaPassCount, kGrampaBenchmarkName);
-    run_crude_map<CrudeMap>(kCrudeMapSize, kCrudePassCount, kCrudeBenchmarkName);
-    return 0;
+TEST_F(JunctionMapInsDelFindTest, JunctionMapLeapfrog) {
+  run_test<LeapfrogMap>(kLeapfrogMapSize, kLeapfrogPassCount,
+                        kLeapfrogGCFrequency);
+}
+
+TEST_F(JunctionMapInsDelFindTest, JunctionMapLinear) {
+  run_test<LinearMap>(kLinearMapSize, kLinearPassCount, kGCFrequency);
+}
+
+TEST_F(JunctionMapInsDelFindTest, JunctionMapGrampa) {
+  run_test<GrampaMap>(kGrampaMapSize, kGrampaPassCount, kGCFrequency);
+}
+
+} // namespace junction_test
+
+int main(int argc, char** argv) {
+  // Init Google test
+  ::testing::InitGoogleTest(&argc, argv);
+  int result = RUN_ALL_TESTS();
+  return 0;
 }