diff options
author | Geir Storli <geirst@verizonmedia.com> | 2019-08-26 13:35:04 +0000 |
---|---|---|
committer | Geir Storli <geirst@verizonmedia.com> | 2019-08-27 09:11:15 +0000 |
commit | b9afbb46aaa9c5260cae554f797eb6151062a085 (patch) | |
tree | 29f6b5927e4cc629d3d5d79e2134f40384859538 /vespalib/src/tests/datastore | |
parent | 41bc53fe2e4db4d21e5d7051bdd77a7b5eb73830 (diff) |
Move count functions from enum store dictionary to unique store dictionary.
Diffstat (limited to 'vespalib/src/tests/datastore')
-rw-r--r-- | vespalib/src/tests/datastore/unique_store_dictionary/CMakeLists.txt | 9 | ||||
-rw-r--r-- | vespalib/src/tests/datastore/unique_store_dictionary/unique_store_dictionary_test.cpp | 84 |
2 files changed, 93 insertions, 0 deletions
diff --git a/vespalib/src/tests/datastore/unique_store_dictionary/CMakeLists.txt b/vespalib/src/tests/datastore/unique_store_dictionary/CMakeLists.txt new file mode 100644 index 00000000000..b1478dea22c --- /dev/null +++ b/vespalib/src/tests/datastore/unique_store_dictionary/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_unique_store_dictionary_test_app TEST + SOURCES + unique_store_dictionary_test.cpp + DEPENDS + vespalib + gtest +) +vespa_add_test(NAME vespalib_unique_store_dictionary_test_app COMMAND vespalib_unique_store_dictionary_test_app) diff --git a/vespalib/src/tests/datastore/unique_store_dictionary/unique_store_dictionary_test.cpp b/vespalib/src/tests/datastore/unique_store_dictionary/unique_store_dictionary_test.cpp new file mode 100644 index 00000000000..8b963dbf007 --- /dev/null +++ b/vespalib/src/tests/datastore/unique_store_dictionary/unique_store_dictionary_test.cpp @@ -0,0 +1,84 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/vespalib/datastore/unique_store.hpp> +#include <vespa/vespalib/datastore/unique_store_dictionary.hpp> +#include <vespa/vespalib/gtest/gtest.h> + +#include <vespa/log/log.h> +LOG_SETUP("unique_store_dictionary_test"); + +using namespace search::datastore; +using namespace search::datastore::uniquestore; + +class Comparator : public EntryComparator { +private: + EntryRef _to_find; + + EntryRef resolve(EntryRef ref) const { + if (ref == EntryRef()) { + return _to_find; + } + return ref; + } + +public: + Comparator(uint32_t to_find) + : _to_find(to_find) + {} + bool operator()(const EntryRef lhs, const EntryRef rhs) const override { + return resolve(lhs).ref() < resolve(rhs).ref(); + } +}; + +struct DictionaryReadTest : public ::testing::Test { + DefaultUniqueStoreDictionary dict; + UniqueStoreDictionaryBase::ReadSnapshot::UP snapshot; + + DictionaryReadTest() + : dict(), + snapshot() + { + } + DictionaryReadTest& add(uint32_t value) { + auto result = dict.add(Comparator(value), [=]() { return EntryRef(value); }); + assert(result.inserted()); + return *this; + } + void take_snapshot() { + dict.freeze(); + snapshot = dict.get_read_snapshot(); + } +}; + +TEST_F(DictionaryReadTest, can_count_occurrences_of_a_key) +{ + add(3).add(5).take_snapshot(); + EXPECT_EQ(0, snapshot->count(Comparator(2))); + EXPECT_EQ(1, snapshot->count(Comparator(3))); + EXPECT_EQ(0, snapshot->count(Comparator(4))); + EXPECT_EQ(1, snapshot->count(Comparator(5))); +} + +TEST_F(DictionaryReadTest, can_count_occurrences_of_keys_in_a_range) +{ + add(3).add(5).add(7).add(9).take_snapshot(); + EXPECT_EQ(1, snapshot->count_in_range(Comparator(3), Comparator(3))); + EXPECT_EQ(1, snapshot->count_in_range(Comparator(3), Comparator(4))); + EXPECT_EQ(2, snapshot->count_in_range(Comparator(3), Comparator(5))); + EXPECT_EQ(3, snapshot->count_in_range(Comparator(3), Comparator(7))); + EXPECT_EQ(4, snapshot->count_in_range(Comparator(3), Comparator(9))); + EXPECT_EQ(4, snapshot->count_in_range(Comparator(3), Comparator(10))); + + EXPECT_EQ(0, snapshot->count_in_range(Comparator(5), Comparator(3))); +} + +TEST_F(DictionaryReadTest, can_iterate_all_keys) +{ + using EntryRefVector = std::vector<EntryRef>; + add(3).add(5).add(7).take_snapshot(); + EntryRefVector refs; + snapshot->foreach_key([&](EntryRef ref){ refs.emplace_back(ref); }); + EXPECT_EQ(EntryRefVector({EntryRef(3), EntryRef(5), EntryRef(7)}), refs); +} + +GTEST_MAIN_RUN_ALL_TESTS() |