Adds garbage collection to map test cases
[junction.git] / test / linear_driver.cpp
1 #include <junction/ConcurrentMap_Linear.h>
2 #include <iostream>
3 #include <memory>
4 #include <chrono>
5 #include <cassert>
6
7 namespace {
8
9 const size_t kMapSize = 10000;
10 const size_t kPassCount = 1;
11 const unsigned s_nInsertPercentage = 10;
12 const char* kTestName = "InsDelFind";
13 const char* kBenchmarkName = "JunctionMapLinear";
14
15 } // namespace
16
17 typedef junction::ConcurrentMap_Linear<turf::u64, turf::u64> Map;
18
19 int main() {
20     auto start_time = std::chrono::system_clock::now();
21
22     size_t nInsertedNum = 0;
23     size_t nFindSuccess = 0;
24     std::unique_ptr<Map> map(new Map(kMapSize / 8));
25     for (size_t count = 0; count < kPassCount; count++) {
26         for (size_t i = 1; i <= kMapSize; ++i) {
27             // The number to operate on the map.
28             size_t n = i;
29             // Insert
30             if (i % s_nInsertPercentage == 1) {
31                 auto iter = map->insertOrFind(n);
32                 if (!iter.getValue()) {
33                   iter.assignValue(n + 1);
34                   nInsertedNum++;
35                   std::cout << "Inserted\n";
36                 }
37             }
38             // Find
39             {
40                 auto iter = map->find(n);
41                 if (!iter.getValue()) {
42                     ++nFindSuccess;
43                     std::cout << "Found\n";
44                 }
45             }
46             // Delete
47             if (i % s_nInsertPercentage == 1) {
48                 auto iter = map->find(n);
49                 if (iter.getValue()) {
50                     iter.eraseValue();
51                     std::cout << "Erased\n";
52                 }
53             }
54         }
55     }
56     assert(nFindSuccess == nFindSuccess && "junction::ConcurrentMap_Linear ERROR");
57
58     auto finish_time = std::chrono::system_clock::now();
59     auto dur = finish_time - start_time;
60     auto milisecs = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
61     std::cout << "[       OK ] " << kTestName << "." << kBenchmarkName << "("
62               << milisecs.count() << " ms)\n";
63
64     return 0;
65 }