Refactors Folly map test cases to use cdsstress library
[folly.git] / folly / stress-test / stress-sequential-folly-map.cpp
1 #include "map_test.h"
2
3 namespace folly_test {
4
5 class FollyMapInsDelFindTest_Sequential : public cds_test::stress_fixture {
6 protected:
7   static unsigned s_nInsertDeletePercentage;
8   static size_t s_nMapSize;
9
10   enum actions { do_find, do_insert, do_delete };
11   static const unsigned int kShuffleSize = 100;
12   static actions s_arrShuffle[kShuffleSize];
13
14   static size_t s_nConcurrentHashMapPassCount;
15   static size_t s_nAtomicHashMapPassCount;
16   static size_t s_nAtomicUnorderedInsertMapPassCount;
17
18   static void SetUpTestCase() {
19     const cds_test::config& cfg = get_config("SequentialFollyMap");
20     GetConfigNonZeroExpected(InsertDeletePercentage, 5);
21     GetConfigNonZeroExpected(MapSize, 10000);
22     GetConfigNonZeroExpected(ConcurrentHashMapPassCount, 1000);
23     GetConfigNonZeroExpected(AtomicHashMapPassCount, 1000);
24     GetConfigNonZeroExpected(AtomicUnorderedInsertMapPassCount, 1000);
25   }
26
27   template <typename Map> static void run_test(Map* map, size_t pass_count) {
28     size_t nInsertedNum = 0;
29     size_t nNotInsertedNum = 0;
30     size_t nFindSuccess = 0;
31     size_t nFindFailed = 0;
32     size_t nDeletedNum = 0;
33     size_t nOperations = 0;
34
35     // The number to operate on the map.
36     size_t n = s_nMapSize;
37     for (size_t count = 0; count < pass_count; count++) {
38       // Insert
39       unsigned mod = count % kShuffleSize;
40       if (mod < s_nInsertDeletePercentage) {
41         if (map_insert(map, n, n)) {
42           nInsertedNum++;
43         } else if (map_insert(map, n, n + 1)) {
44           nInsertedNum++;
45         } else {
46           nNotInsertedNum++;
47         }
48       } else {
49         nNotInsertedNum++;
50       }
51       // Find
52       if (map_find(map, n)) {
53         ++nFindSuccess;
54       } else {
55         ++nFindFailed;
56       }
57       // Delete
58       if (mod < s_nInsertDeletePercentage && map_delete(map, n)) {
59         nDeletedNum++;
60       }
61       if (++n == 2 * s_nMapSize) {
62         n = s_nMapSize;
63       }
64     }
65     EXPECT_EQ(nInsertedNum, nDeletedNum);
66     EXPECT_EQ(nInsertedNum, nFindSuccess);
67     EXPECT_EQ(nFindFailed, nNotInsertedNum);
68   }
69 };
70
71 size_t FollyMapInsDelFindTest_Sequential::s_nMapSize;
72 unsigned FollyMapInsDelFindTest_Sequential::s_nInsertDeletePercentage;
73 const unsigned int FollyMapInsDelFindTest_Sequential::kShuffleSize;
74 FollyMapInsDelFindTest_Sequential::actions
75     FollyMapInsDelFindTest_Sequential::s_arrShuffle
76         [FollyMapInsDelFindTest_Sequential::kShuffleSize];
77
78 size_t FollyMapInsDelFindTest_Sequential::s_nConcurrentHashMapPassCount;
79 size_t FollyMapInsDelFindTest_Sequential::s_nAtomicHashMapPassCount;
80 size_t FollyMapInsDelFindTest_Sequential::s_nAtomicUnorderedInsertMapPassCount;
81
82 TEST_F(FollyMapInsDelFindTest_Sequential, FollyConcurrentHashMap) {
83   std::unique_ptr<ConcurrentHashMap> map(
84       new ConcurrentHashMap(s_nMapSize));
85   run_test(map.get(), s_nConcurrentHashMapPassCount);
86 }
87
88 TEST_F(FollyMapInsDelFindTest_Sequential, FollyAtomicHashMap) {
89   std::unique_ptr<AtomicHashMap> map(
90       new AtomicHashMap(s_nMapSize));
91   run_test(map.get(), s_nAtomicHashMapPassCount);
92 }
93
94 TEST_F(FollyMapInsDelFindTest_Sequential, FollyAtomicUnorderedInsertMap) {
95   std::unique_ptr<AtomicUnorderedInsertMap> map(
96       new AtomicUnorderedInsertMap(s_nMapSize));
97   run_test(map.get(), s_nAtomicUnorderedInsertMapPassCount);
98 }
99
100 } // namespace folly_test