1 #include <junction/ConcurrentMap_Crude.h>
2 #include <junction/ConcurrentMap_Grampa.h>
3 #include <junction/ConcurrentMap_Leapfrog.h>
4 #include <junction/ConcurrentMap_Linear.h>
6 #include <gtest/gtest.h>
10 namespace junction_test {
12 class JunctionMapInsDelFindTest : public ::testing::Test {
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;
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;
24 static const size_t kCrudeMapSize = 10000;
25 static const size_t kCrudePassCount = 400000;
27 static const size_t kGrampaMapSize = 20000;
28 static const size_t kGrampaPassCount = 60000;
30 static const size_t kLinearMapSize = 20000;
31 static const size_t kLinearPassCount = 70000;
33 static const size_t kLeapfrogMapSize = 20000;
34 static const size_t kLeapfrogPassCount = 75000;
36 static void SetUpTestCase() {}
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;
51 if (i % s_nInsertPercentage == 1) {
52 auto iter = map->insertOrFind(i);
53 if (!iter.getValue()) {
56 // std::cout << "Inserted" << i << "\n";
61 auto iter = map->find(i);
62 if (iter.getValue()) {
64 // std::cout << "Found" << i << "\n";
68 if (i % s_nInsertPercentage == 1) {
69 auto iter = map->find(i);
70 if (iter.getValue()) {
72 // std::cout << "Erased" << i << "\n";
75 if (++nOperations > gc_frequency) {
76 junction::DefaultQSBR.update(qsbrContext);
81 junction::DefaultQSBR.update(qsbrContext);
82 junction::DefaultQSBR.destroyContext(qsbrContext);
83 EXPECT_EQ(nFindSuccess, nInsertedNum);
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
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;
100 if (i % s_nInsertPercentage == 1) {
103 // std::cout << "Inserted" << i << "\n";
109 // std::cout << "Found" << i << "\n";
113 if (i % s_nInsertPercentage == 1) {
116 // std::cout << "Erased" << i << "\n";
119 if (++nOperations > gc_frequency) {
120 junction::DefaultQSBR.update(qsbrContext);
125 junction::DefaultQSBR.update(qsbrContext);
126 junction::DefaultQSBR.destroyContext(qsbrContext);
128 EXPECT_EQ(nFindSuccess, nInsertedNum);
132 TEST_F(JunctionMapInsDelFindTest, JunctionMapCrude) {
133 run_crude_map<CrudeMap>(kCrudeMapSize, kCrudePassCount, kGCFrequency);
136 TEST_F(JunctionMapInsDelFindTest, JunctionMapLeapfrog) {
137 run_test<LeapfrogMap>(kLeapfrogMapSize, kLeapfrogPassCount,
138 kLeapfrogGCFrequency);
141 TEST_F(JunctionMapInsDelFindTest, JunctionMapLinear) {
142 run_test<LinearMap>(kLinearMapSize, kLinearPassCount, kGCFrequency);
145 TEST_F(JunctionMapInsDelFindTest, JunctionMapGrampa) {
146 run_test<GrampaMap>(kGrampaMapSize, kGrampaPassCount, kGCFrequency);
149 } // namespace junction_test
151 int main(int argc, char** argv) {
153 ::testing::InitGoogleTest(&argc, argv);
154 int result = RUN_ALL_TESTS();