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>
15 namespace junction_test {
17 class JunctionMapInsDelFindTest : public ::testing::Test {
19 typedef junction::ConcurrentMap_Grampa<size_t, size_t> GrampaMap;
20 typedef junction::ConcurrentMap_Linear<size_t, size_t> LinearMap;
21 typedef junction::ConcurrentMap_Leapfrog<size_t, size_t> LeapfrogMap;
22 typedef junction::ConcurrentMap_Crude<size_t, size_t> CrudeMap;
24 static const unsigned s_nInsertPercentage = 5;
25 static const unsigned s_nDeletePercentage = 5;
27 // Run GC after "kGCFrequency" operations.
28 const size_t kGCFrequency = 1500;
29 const size_t kMapSize = 20000;
31 enum actions { do_find, do_insert, do_delete };
32 static const unsigned int kShuffleSize = 100;
33 static actions s_arrShuffle[kShuffleSize];
35 const size_t kCrudePassCount = 400000;
36 const size_t kGrampaPassCount = 60000;
37 const size_t kLinearPassCount = 70000;
38 const size_t kLeapfrogPassCount = 75000;
40 static void SetUpTestCase() {
41 // Build an array of shuffled actions.
42 EXPECT_LE(s_nInsertPercentage + s_nDeletePercentage, 100);
43 actions* pFirst = s_arrShuffle;
44 actions* pLast = s_arrShuffle + s_nInsertPercentage;
45 std::fill(pFirst, pLast, do_insert);
47 pLast += s_nDeletePercentage;
48 std::fill(pFirst, pLast, do_delete);
50 pLast = s_arrShuffle + sizeof(s_arrShuffle) / sizeof(s_arrShuffle[0]);
52 std::fill(pFirst, pLast, do_find);
54 std::random_device rd;
56 std::shuffle(s_arrShuffle, pLast, g);
59 template <typename Map, typename Key, typename Value>
60 static bool map_insert(Map* map, Key key, Value value) {
61 auto iter = map->insertOrFind(key);
62 if (!iter.getValue()) {
63 iter.assignValue(value);
70 template <typename Map, typename Key>
71 static bool map_delete(Map* map, Key key) {
72 auto iter = map->find(key);
73 if (iter.getValue()) {
81 template <typename Map, typename Key>
82 static bool map_find(Map* map, Key key) {
83 auto iter = map->find(key);
84 if (iter.getValue()) {
91 // Specialization for CrudeMap
92 template <typename Key, typename Value>
93 static bool map_insert(CrudeMap* map, Key key, Value value) {
95 map->assign(key, value);
102 template <typename Key> static bool map_delete(CrudeMap* map, Key key) {
103 if (!map->get(key)) {
104 map->assign(key, ((Key)0));
111 template <typename Key> static bool map_find(CrudeMap* map, Key key) {
112 return map->get(key) != ((Key)0);
115 template <typename Map>
116 void run_test(Map* map, size_t pass_count) {
117 auto qsbrContext = junction::DefaultQSBR.createContext();
119 std::random_device rd;
120 std::mt19937 gen(rd());
121 std::uniform_int_distribution<size_t> dis(kMapSize, 2 * kMapSize);
123 unsigned action_index = 0;
124 size_t nInsertedNum = 0;
125 size_t nFindSuccess = 0;
126 size_t nOperations = 0;
128 for (size_t count = 0; count < pass_count; count++) {
129 for (size_t i = 0; i < kMapSize; ++i) {
130 // The number to operate on the map.
132 switch (s_arrShuffle[action_index]) {
134 if (map_insert(map, n, n)) {
144 if (map_find(map, n)) {
151 if (++action_index >= kShuffleSize) {
154 if (++nOperations > kGCFrequency) {
155 junction::DefaultQSBR.update(qsbrContext);
160 junction::DefaultQSBR.update(qsbrContext);
161 junction::DefaultQSBR.destroyContext(qsbrContext);
165 const unsigned JunctionMapInsDelFindTest::s_nInsertPercentage;
166 const unsigned JunctionMapInsDelFindTest::s_nDeletePercentage;
167 const unsigned int JunctionMapInsDelFindTest::kShuffleSize;
168 JunctionMapInsDelFindTest::actions JunctionMapInsDelFindTest::s_arrShuffle
169 [JunctionMapInsDelFindTest::kShuffleSize];
171 TEST_F(JunctionMapInsDelFindTest, JunctionMapCrude) {
172 std::unique_ptr<CrudeMap> map(new CrudeMap(kMapSize * 32));
173 run_test(map.get(), kCrudePassCount);
176 TEST_F(JunctionMapInsDelFindTest, JunctionMapLeapfrog) {
177 std::unique_ptr<LeapfrogMap> map(new LeapfrogMap());
178 run_test(map.get(), kLeapfrogPassCount);
181 TEST_F(JunctionMapInsDelFindTest, JunctionMapLinear) {
182 std::unique_ptr<LinearMap> map(new LinearMap());
183 run_test(map.get(), kLinearPassCount);
186 TEST_F(JunctionMapInsDelFindTest, JunctionMapGrampa) {
187 std::unique_ptr<GrampaMap> map(new GrampaMap());
188 run_test(map.get(), kGrampaPassCount);
191 } // namespace junction_test
193 int main(int argc, char** argv) {
195 ::testing::InitGoogleTest(&argc, argv);
196 int result = RUN_ALL_TESTS();