2016-06-05 20:30:40 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2017-10-12 20:22:44 +08:00
|
|
|
#include <fmt/format.h>
|
2017-06-21 17:29:13 +08:00
|
|
|
#include <glog/logging.h>
|
|
|
|
#include <gtest/gtest.h>
|
2016-12-23 02:28:21 +08:00
|
|
|
|
2016-07-05 11:01:22 +08:00
|
|
|
#include "data_structures/concurrent/concurrent_map.hpp"
|
2016-06-05 20:30:40 +08:00
|
|
|
|
2017-02-19 01:03:48 +08:00
|
|
|
using concurrent_map_t = ConcurrentMap<int, int>;
|
2016-06-05 20:30:40 +08:00
|
|
|
|
2018-04-09 15:54:32 +08:00
|
|
|
template <typename TAccessor>
|
|
|
|
void print_skiplist(const TAccessor &access) {
|
2017-06-21 17:29:13 +08:00
|
|
|
DLOG(INFO) << "Map now has: ";
|
2018-04-09 15:54:32 +08:00
|
|
|
for (auto &kv : access)
|
2017-06-21 17:29:13 +08:00
|
|
|
DLOG(INFO) << fmt::format(" ({}, {})", kv.first, kv.second);
|
2016-06-05 20:30:40 +08:00
|
|
|
}
|
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
TEST(ConcurrentMapSkiplist, Mix) {
|
2017-02-19 01:03:48 +08:00
|
|
|
concurrent_map_t skiplist;
|
2017-02-18 18:54:37 +08:00
|
|
|
auto accessor = skiplist.access();
|
2016-06-15 06:06:21 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
// insert 10
|
2017-10-11 19:19:10 +08:00
|
|
|
EXPECT_TRUE(accessor.insert(1, 10).second);
|
2016-06-05 20:30:40 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
// try insert 10 again (should fail)
|
2017-10-11 19:19:10 +08:00
|
|
|
EXPECT_FALSE(accessor.insert(1, 10).second);
|
2016-06-05 20:30:40 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
// insert 20
|
2017-10-11 19:19:10 +08:00
|
|
|
EXPECT_TRUE(accessor.insert(2, 20).second);
|
2016-06-15 06:06:21 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
print_skiplist(accessor);
|
2016-06-05 20:30:40 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
// value at key 3 shouldn't exist
|
2017-10-11 19:19:10 +08:00
|
|
|
EXPECT_TRUE(accessor.find(3) == accessor.end());
|
2016-06-05 20:30:40 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
// value at key 2 should exist
|
2017-10-11 19:19:10 +08:00
|
|
|
EXPECT_TRUE(accessor.find(2) != accessor.end());
|
2016-06-15 06:06:21 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
// at key 2 is 20 (true)
|
2017-10-11 19:19:10 +08:00
|
|
|
EXPECT_EQ(accessor.find(2)->second, 20);
|
2016-06-05 20:30:40 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
// removed existing (1)
|
2017-10-11 19:19:10 +08:00
|
|
|
EXPECT_TRUE(accessor.remove(1));
|
2016-06-05 20:30:40 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
// removed non-existing (3)
|
2017-10-11 19:19:10 +08:00
|
|
|
EXPECT_FALSE(accessor.remove(3));
|
2016-06-05 20:30:40 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
// insert (1, 10)
|
2017-10-11 19:19:10 +08:00
|
|
|
EXPECT_TRUE(accessor.insert(1, 10).second);
|
2016-06-05 20:30:40 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
// insert (4, 40)
|
2017-10-11 19:19:10 +08:00
|
|
|
EXPECT_TRUE(accessor.insert(4, 40).second);
|
2016-06-05 20:30:40 +08:00
|
|
|
|
2017-02-18 18:54:37 +08:00
|
|
|
print_skiplist(accessor);
|
2016-12-23 02:28:21 +08:00
|
|
|
}
|
|
|
|
|
2018-04-09 15:54:32 +08:00
|
|
|
TEST(ConcurrentMapSkiplist, ConstFind) {
|
|
|
|
ConcurrentMap<int, int> map;
|
|
|
|
{
|
|
|
|
auto access = map.access();
|
|
|
|
for (int i = 0; i < 10; ++i) access.insert(i, i);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto &const_map = map;
|
|
|
|
auto access = const_map.access();
|
|
|
|
auto it = access.find(4);
|
|
|
|
EXPECT_NE(it, access.end());
|
|
|
|
it = access.find(12);
|
|
|
|
EXPECT_EQ(it, access.end());
|
|
|
|
}
|
2016-06-05 20:30:40 +08:00
|
|
|
}
|