memgraph/tests/unit/concurrent_map.cpp

75 lines
2.0 KiB
C++
Raw Normal View History

2016-06-05 20:30:40 +08:00
#include <iostream>
2016-08-14 19:43:34 +08:00
#include "logging/default.hpp"
#include "logging/streams/stdout.hpp"
#include "data_structures/concurrent/concurrent_map.hpp"
2016-06-15 06:06:21 +08:00
#include "utils/assert.hpp"
2016-06-05 20:30:40 +08:00
using std::cout;
using std::endl;
using skiplist_t = ConcurrentMap<int, int>;
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
void print_skiplist(const skiplist_t::Accessor &skiplist)
{
cout << "---- skiplist now has: ";
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
for (auto &kv : skiplist)
cout << "(" << kv.first << ", " << kv.second << ") ";
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
cout << "----" << endl;
2016-06-05 20:30:40 +08:00
}
2016-08-14 19:43:34 +08:00
int main(void)
{
logging::init_async();
logging::log->pipe(std::make_unique<Stdout>());
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
skiplist_t skiplist;
auto accessor = skiplist.access();
2016-06-15 06:06:21 +08:00
2016-08-14 19:43:34 +08:00
// insert 10
permanent_assert(accessor.insert(1, 10).second == true,
"add first element");
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
// try insert 10 again (should fail)
permanent_assert(accessor.insert(1, 10).second == false,
"add the same element, should fail");
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
// insert 20
permanent_assert(accessor.insert(2, 20).second == true,
"insert new unique element");
2016-06-15 06:06:21 +08:00
2016-08-14 19:43:34 +08:00
print_skiplist(accessor);
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
// value at key 3 shouldn't exist
permanent_assert((accessor.find(3) == accessor.end()) == true,
"try to find element which doesn't exist");
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
// value at key 2 should exist
permanent_assert((accessor.find(2) != accessor.end()) == true,
"find iterator");
2016-06-15 06:06:21 +08:00
2016-08-14 19:43:34 +08:00
// at key 2 is 20 (true)
permanent_assert(accessor.find(2)->second == 20, "find element");
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
// removed existing (1)
permanent_assert(accessor.remove(1) == true, "try to remove element");
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
// removed non-existing (3)
permanent_assert(accessor.remove(3) == false,
"try to remove element which doesn't exist");
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
// insert (1, 10)
permanent_assert(accessor.insert(1, 10).second == true,
"insert unique element");
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
// insert (4, 40)
permanent_assert(accessor.insert(4, 40).second == true,
"insert unique element");
2016-06-05 20:30:40 +08:00
2016-08-14 19:43:34 +08:00
print_skiplist(accessor);
return 0;
2016-06-05 20:30:40 +08:00
}