diff options
author | Tor Egge <Tor.Egge@online.no> | 2021-04-07 10:43:16 +0200 |
---|---|---|
committer | Tor Egge <Tor.Egge@online.no> | 2021-04-07 10:43:16 +0200 |
commit | 961ac4cd3af767f55ef9f8b4c84e0632fa71a958 (patch) | |
tree | b222c9719f1c349adda89a9bc22ef66d10fb4efb /vespalib | |
parent | 57de422e4b61f95a630c00d93cfddd1db772fc4a (diff) |
Extend unit test for ShardedHashMap.
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/datastore/sharded_hash_map/sharded_hash_map_test.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/vespalib/src/tests/datastore/sharded_hash_map/sharded_hash_map_test.cpp b/vespalib/src/tests/datastore/sharded_hash_map/sharded_hash_map_test.cpp index 9f11b96e672..7aef34be55d 100644 --- a/vespalib/src/tests/datastore/sharded_hash_map/sharded_hash_map_test.cpp +++ b/vespalib/src/tests/datastore/sharded_hash_map/sharded_hash_map_test.cpp @@ -50,6 +50,7 @@ struct DataStoreShardedHashTest : public ::testing::Test void read_work(uint32_t cnt); void read_work(); void write_work(uint32_t cnt); + void populate_sample_data(); }; @@ -173,6 +174,13 @@ DataStoreShardedHashTest::write_work(uint32_t cnt) LOG(info, "done %u write work", cnt); } +void +DataStoreShardedHashTest::populate_sample_data() +{ + for (uint32_t i = 0; i < 50; ++i) { + insert(i); + } +} TEST_F(DataStoreShardedHashTest, single_threaded_reader_without_updates) { @@ -216,4 +224,57 @@ TEST_F(DataStoreShardedHashTest, memory_usage_is_reported) EXPECT_LT(0, usage.allocatedBytesOnHold()); } +TEST_F(DataStoreShardedHashTest, foreach_key_works) +{ + populate_sample_data(); + std::vector<uint32_t> keys; + _hash_map.foreach_key([this, &keys](EntryRef ref) { keys.emplace_back(_allocator.get_wrapped(ref).value()); }); + std::sort(keys.begin(), keys.end()); + EXPECT_EQ(50, keys.size()); + for (uint32_t i = 0; i < 50; ++i) { + EXPECT_EQ(i, keys[i]); + } +} + +TEST_F(DataStoreShardedHashTest, move_keys_works) +{ + populate_sample_data(); + std::vector<EntryRef> refs; + _hash_map.foreach_key([&refs](EntryRef ref) { refs.emplace_back(ref); }); + std::vector<EntryRef> new_refs; + _hash_map.move_keys([this, &new_refs](EntryRef ref) { auto new_ref = _allocator.move(ref); _allocator.hold(ref); new_refs.emplace_back(new_ref); return new_ref; }); + std::vector<EntryRef> verify_new_refs; + _hash_map.foreach_key([&verify_new_refs](EntryRef ref) { verify_new_refs.emplace_back(ref); }); + EXPECT_EQ(50u, refs.size()); + EXPECT_NE(refs, new_refs); + EXPECT_EQ(new_refs, verify_new_refs); + for (uint32_t i = 0; i < 50; ++i) { + EXPECT_NE(refs[i], new_refs[i]); + auto value = _allocator.get_wrapped(refs[i]).value(); + auto new_value = _allocator.get_wrapped(refs[i]).value(); + EXPECT_EQ(value, new_value); + } +} + +TEST_F(DataStoreShardedHashTest, normalize_values_works) +{ + populate_sample_data(); + for (uint32_t i = 0; i < 50; ++i) { + MyCompare comp(_store, i); + auto result = _hash_map.find(comp, EntryRef()); + ASSERT_NE(result, nullptr); + EXPECT_EQ(i, _allocator.get_wrapped(result->first.load_relaxed()).value()); + result->second.store_relaxed(EntryRef(i + 200)); + } + _hash_map.normalize_values([](EntryRef ref) { return EntryRef(ref.ref() + 300); }); + for (uint32_t i = 0; i < 50; ++i) { + MyCompare comp(_store, i); + auto result = _hash_map.find(comp, EntryRef()); + ASSERT_NE(result, nullptr); + EXPECT_EQ(i, _allocator.get_wrapped(result->first.load_relaxed()).value()); + ASSERT_EQ(i + 500, result->second.load_relaxed().ref()); + result->second.store_relaxed(EntryRef()); + } +} + GTEST_MAIN_RUN_ALL_TESTS() |