memgraph/tests/unit/concurrent_map.cpp

62 lines
1.4 KiB
C++
Raw Normal View History

2016-06-05 20:30:40 +08:00
#include <iostream>
#include <fmt/format.h>
#include <glog/logging.h>
#include <gtest/gtest.h>
2016-12-23 02:28:21 +08:00
#include "data_structures/concurrent/concurrent_map.hpp"
2016-06-05 20:30:40 +08:00
using concurrent_map_t = ConcurrentMap<int, int>;
2016-06-05 20:30:40 +08:00
void print_skiplist(const concurrent_map_t::Accessor<false> &map) {
DLOG(INFO) << "Map now has: ";
for (auto &kv : map)
DLOG(INFO) << fmt::format(" ({}, {})", kv.first, kv.second);
2016-06-05 20:30:40 +08:00
}
TEST(ConcurrentMapSkiplist, Mix) {
concurrent_map_t skiplist;
auto accessor = skiplist.access();
2016-06-15 06:06:21 +08:00
// insert 10
EXPECT_TRUE(accessor.insert(1, 10).second);
2016-06-05 20:30:40 +08:00
// try insert 10 again (should fail)
EXPECT_FALSE(accessor.insert(1, 10).second);
2016-06-05 20:30:40 +08:00
// insert 20
EXPECT_TRUE(accessor.insert(2, 20).second);
2016-06-15 06:06:21 +08:00
print_skiplist(accessor);
2016-06-05 20:30:40 +08:00
// value at key 3 shouldn't exist
EXPECT_TRUE(accessor.find(3) == accessor.end());
2016-06-05 20:30:40 +08:00
// value at key 2 should exist
EXPECT_TRUE(accessor.find(2) != accessor.end());
2016-06-15 06:06:21 +08:00
// at key 2 is 20 (true)
EXPECT_EQ(accessor.find(2)->second, 20);
2016-06-05 20:30:40 +08:00
// removed existing (1)
EXPECT_TRUE(accessor.remove(1));
2016-06-05 20:30:40 +08:00
// removed non-existing (3)
EXPECT_FALSE(accessor.remove(3));
2016-06-05 20:30:40 +08:00
// insert (1, 10)
EXPECT_TRUE(accessor.insert(1, 10).second);
2016-06-05 20:30:40 +08:00
// insert (4, 40)
EXPECT_TRUE(accessor.insert(4, 40).second);
2016-06-05 20:30:40 +08:00
print_skiplist(accessor);
2016-12-23 02:28:21 +08:00
}
int main(int argc, char **argv) {
google::InitGoogleLogging(argv[0]);
2016-08-14 19:43:34 +08:00
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
2016-06-05 20:30:40 +08:00
}