Refactors test cases to use gtest
[junction.git] / test / junction_sequential_driver.cpp
1 #include <junction/ConcurrentMap_Crude.h>
2 #include <junction/ConcurrentMap_Grampa.h>
3 #include <junction/ConcurrentMap_Leapfrog.h>
4 #include <junction/ConcurrentMap_Linear.h>
5
6 #include <gtest/gtest.h>
7
8 #include <memory>
9
10 namespace junction_test {
11
12 class JunctionMapInsDelFindTest : public ::testing::Test {
13 protected:
14   typedef junction::ConcurrentMap_Grampa<size_t, size_t> GrampaMap;
15   typedef junction::ConcurrentMap_Linear<size_t, size_t> LinearMap;
16   typedef junction::ConcurrentMap_Leapfrog<size_t, size_t> LeapfrogMap;
17   typedef junction::ConcurrentMap_Crude<size_t, size_t> CrudeMap;
18
19   static const unsigned s_nInsertPercentage = 10;
20   // Run GC after "kGCFrequency" operations.
21   static const size_t kGCFrequency = 3000;
22   static const size_t kLeapfrogGCFrequency = 1500;
23
24   static const size_t kCrudeMapSize = 10000;
25   static const size_t kCrudePassCount = 400000;
26
27   static const size_t kGrampaMapSize = 20000;
28   static const size_t kGrampaPassCount = 60000;
29
30   static const size_t kLinearMapSize = 20000;
31   static const size_t kLinearPassCount = 70000;
32
33   static const size_t kLeapfrogMapSize = 20000;
34   static const size_t kLeapfrogPassCount = 75000;
35
36   static void SetUpTestCase() {}
37
38   template <typename Map>
39   static void run_test(size_t map_size, size_t pass_count,
40                        size_t gc_frequency) {
41     size_t nInsertedNum = 0;
42     size_t nFindSuccess = 0;
43     size_t nOperations = 0;
44     std::unique_ptr<Map> map(new Map());
45     auto qsbrContext = junction::DefaultQSBR.createContext();
46     for (size_t count = 0; count < pass_count; count++) {
47       for (size_t i = 0; i < map_size; ++i) {
48         // The number to operate on the map.
49         size_t n = map_size + i;
50         // Insert
51         if (i % s_nInsertPercentage == 1) {
52           auto iter = map->insertOrFind(i);
53           if (!iter.getValue()) {
54             iter.assignValue(n);
55             nInsertedNum++;
56             //                  std::cout << "Inserted" << i << "\n";
57           }
58         }
59         // Find
60         {
61           auto iter = map->find(i);
62           if (iter.getValue()) {
63             ++nFindSuccess;
64             //                    std::cout << "Found" << i << "\n";
65           }
66         }
67         // Delete
68         if (i % s_nInsertPercentage == 1) {
69           auto iter = map->find(i);
70           if (iter.getValue()) {
71             iter.eraseValue();
72             //                    std::cout << "Erased" << i << "\n";
73           }
74         }
75         if (++nOperations > gc_frequency) {
76           junction::DefaultQSBR.update(qsbrContext);
77           nOperations = 0;
78         }
79       }
80     }
81     junction::DefaultQSBR.update(qsbrContext);
82     junction::DefaultQSBR.destroyContext(qsbrContext);
83     EXPECT_EQ(nFindSuccess, nInsertedNum);
84   }
85
86   template <typename Map>
87   void run_crude_map(size_t map_size, size_t pass_count, size_t gc_frequency) {
88     size_t nInsertedNum = 0;
89     size_t nFindSuccess = 0;
90     size_t nOperations = 0;
91     // Seems like the crude map won't resize, so better have a large enough
92     // capacity.
93     std::unique_ptr<Map> map(new Map(map_size * 32));
94     auto qsbrContext = junction::DefaultQSBR.createContext();
95     for (size_t count = 0; count < pass_count; count++) {
96       for (size_t i = 0; i < map_size; ++i) {
97         // The number to operate on the map.
98         size_t n = map_size + i;
99         // Insert
100         if (i % s_nInsertPercentage == 1) {
101           map->assign(i, n);
102           nInsertedNum++;
103           //                std::cout << "Inserted" << i << "\n";
104         }
105         // Find
106         {
107           if (map->get(i)) {
108             ++nFindSuccess;
109             //                    std::cout << "Found" << i << "\n";
110           }
111         }
112         // Delete
113         if (i % s_nInsertPercentage == 1) {
114           if (map->get(i)) {
115             map->assign(n, 0);
116             //                    std::cout << "Erased" << i << "\n";
117           }
118         }
119         if (++nOperations > gc_frequency) {
120           junction::DefaultQSBR.update(qsbrContext);
121           nOperations = 0;
122         }
123       }
124     }
125     junction::DefaultQSBR.update(qsbrContext);
126     junction::DefaultQSBR.destroyContext(qsbrContext);
127
128     EXPECT_EQ(nFindSuccess, nInsertedNum);
129   }
130 };
131
132 TEST_F(JunctionMapInsDelFindTest, JunctionMapCrude) {
133   run_crude_map<CrudeMap>(kCrudeMapSize, kCrudePassCount, kGCFrequency);
134 }
135
136 TEST_F(JunctionMapInsDelFindTest, JunctionMapLeapfrog) {
137   run_test<LeapfrogMap>(kLeapfrogMapSize, kLeapfrogPassCount,
138                         kLeapfrogGCFrequency);
139 }
140
141 TEST_F(JunctionMapInsDelFindTest, JunctionMapLinear) {
142   run_test<LinearMap>(kLinearMapSize, kLinearPassCount, kGCFrequency);
143 }
144
145 TEST_F(JunctionMapInsDelFindTest, JunctionMapGrampa) {
146   run_test<GrampaMap>(kGrampaMapSize, kGrampaPassCount, kGCFrequency);
147 }
148
149 } // namespace junction_test
150
151 int main(int argc, char** argv) {
152   // Init Google test
153   ::testing::InitGoogleTest(&argc, argv);
154   int result = RUN_ALL_TESTS();
155   return 0;
156 }