diff options
Diffstat (limited to 'tests/unit/unittest/stringcache-test.cpp')
-rw-r--r-- | tests/unit/unittest/stringcache-test.cpp | 134 |
1 files changed, 132 insertions, 2 deletions
diff --git a/tests/unit/unittest/stringcache-test.cpp b/tests/unit/unittest/stringcache-test.cpp index e023ae7ff2..1a20e473de 100644 --- a/tests/unit/unittest/stringcache-test.cpp +++ b/tests/unit/unittest/stringcache-test.cpp @@ -25,6 +25,9 @@ #include "googletest.h" +#include "mockmutex.h" +#include "mockfilepathstorage.h" + #include <stringcache.h> #include <utils/smallstringio.h> @@ -38,8 +41,15 @@ using uint64 = unsigned long long; using Utils::compare; using Utils::reverseCompare; using ClangBackEnd::findInSorted; +using StorageIdFunction = std::function<int(Utils::SmallStringView)>; +using StorageStringFunction = std::function<Utils::PathString(int)>; -using CacheEntries = ClangBackEnd::FileCacheCacheEntries; +using Cache = ClangBackEnd::StringCache<Utils::PathString, + int, + NiceMock<MockMutex>, + decltype(&Utils::reverseCompare), + Utils::reverseCompare>; +using CacheEntries = Cache::CacheEntries; class StringCache : public testing::Test { @@ -47,7 +57,15 @@ protected: void SetUp(); protected: - ClangBackEnd::FilePathCache<> cache; + NiceMock<MockFilePathStorage> mockStorage; + StorageIdFunction mockStorageFetchDirectyId = [&] (Utils::SmallStringView string) { + return mockStorage.fetchDirectoryId(string); + }; + StorageStringFunction mockStorageFetchDirectyPath = [&] (int id) { + return mockStorage.fetchDirectoryPath(id); + }; + Cache cache; + NiceMock<MockMutex> &mockMutex = cache.mutex(); Utils::PathString filePath1{"/file/pathOne"}; Utils::PathString filePath2{"/file/pathTwo"}; Utils::PathString filePath3{"/file/pathThree"}; @@ -311,11 +329,123 @@ TEST_F(StringCache, FindInSortedFifeReverse) ASSERT_TRUE(found.wasFound); } +TEST_F(StringCache, StringIdIsLocked) +{ + EXPECT_CALL(mockMutex, lock()); + EXPECT_CALL(mockMutex, unlock()); + + cache.stringId("foo"); +} + +TEST_F(StringCache, StringIdsIsLocked) +{ + EXPECT_CALL(mockMutex, lock()); + EXPECT_CALL(mockMutex, unlock()); + + cache.stringIds({"foo"}); +} + +TEST_F(StringCache, StringIsLocked) +{ + cache.stringId("foo"); + + EXPECT_CALL(mockMutex, lock()); + EXPECT_CALL(mockMutex, unlock()); + + cache.string(0); +} + +TEST_F(StringCache, StringsIsLocked) +{ + cache.stringId("foo"); + + EXPECT_CALL(mockMutex, lock()); + EXPECT_CALL(mockMutex, unlock()); + + cache.strings({0}); +} + +TEST_F(StringCache, StringIdWithStorageFunctionIsLocked) +{ + EXPECT_CALL(mockMutex, lock()); + EXPECT_CALL(mockMutex, unlock()); + + cache.stringId("foo", mockStorageFetchDirectyId); +} + +TEST_F(StringCache, StringIdWithStorageFunctionWhichHasNoEntryIsCallingStorageFunction) +{ + EXPECT_CALL(mockStorage, fetchDirectoryId(Eq("foo"))); + + cache.stringId("foo", mockStorageFetchDirectyId); +} + +TEST_F(StringCache, StringIdWithStorageFunctionWhichHasEntryIsNotCallingStorageFunction) +{ + cache.stringId("foo", mockStorageFetchDirectyId); + + EXPECT_CALL(mockStorage, fetchDirectoryId(Eq("foo"))).Times(0); + + cache.stringId("foo", mockStorageFetchDirectyId); +} + +TEST_F(StringCache, IndexOfStringIdWithStorageFunctionWhichHasEntry) +{ + cache.stringId("foo", mockStorageFetchDirectyId); + + auto index = cache.stringId("foo", mockStorageFetchDirectyId); + + ASSERT_THAT(index, 42); +} + +TEST_F(StringCache, IndexOfStringIdWithStorageFunctionWhichHasNoEntry) +{ + auto index = cache.stringId("foo", mockStorageFetchDirectyId); + + ASSERT_THAT(index, 42); +} + +TEST_F(StringCache, GetEntryByIndexAfterInsertingByCustomIndex) +{ + auto index = cache.stringId("foo", mockStorageFetchDirectyId); + + auto string = cache.string(index, mockStorageFetchDirectyPath); + + ASSERT_THAT(string, Eq("foo")); +} + +TEST_F(StringCache, CallFetchDirectoryPathForLowerIndex) +{ + auto index = cache.stringId("foo", mockStorageFetchDirectyId); + + EXPECT_CALL(mockStorage, fetchDirectoryPath(Eq(index - 1))); + + cache.string(index - 1, mockStorageFetchDirectyPath); +} + +TEST_F(StringCache, CallFetchDirectoryPathForUnknownIndex) +{ + EXPECT_CALL(mockStorage, fetchDirectoryPath(Eq(0))); + + cache.string(0, mockStorageFetchDirectyPath); +} + +TEST_F(StringCache, FetchDirectoryPathForUnknownIndex) +{ + auto string = cache.string(41, mockStorageFetchDirectyPath); + + ASSERT_THAT(string, Eq("bar")); +} void StringCache::SetUp() { std::sort(filePaths.begin(), filePaths.end(), [] (auto &f, auto &l) { return compare(f, l) < 0;}); std::sort(reverseFilePaths.begin(), reverseFilePaths.end(), [] (auto &f, auto &l) { return reverseCompare(f, l) < 0;}); + + ON_CALL(mockStorage, fetchDirectoryId(Eq("foo"))) + .WillByDefault(Return(42)); + ON_CALL(mockStorage, fetchDirectoryPath(41)) + .WillByDefault(Return(Utils::PathString("bar"))); } } |