From d2e15e5f1efa1ef160a2d4f40ab61180477d1f14 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Thu, 21 Sep 2017 11:43:59 +0200 Subject: Clang: Add file cache The database is using file path integer ids to handle file paths because otherwise we would save many redundant data. This patch is improving it further with the introduction of a database based file path cache. The entries are now divided in a directory path and file name. This is quite handy for directory based file watching. Change-Id: I03f2e388e43f3d521d6bf8e39dfb95eb2309dc73 Reviewed-by: Tim Jenssen --- tests/unit/unittest/stringcache-test.cpp | 134 ++++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 2 deletions(-) (limited to 'tests/unit/unittest/stringcache-test.cpp') 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 #include @@ -38,8 +41,15 @@ using uint64 = unsigned long long; using Utils::compare; using Utils::reverseCompare; using ClangBackEnd::findInSorted; +using StorageIdFunction = std::function; +using StorageStringFunction = std::function; -using CacheEntries = ClangBackEnd::FileCacheCacheEntries; +using Cache = ClangBackEnd::StringCache, + 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 mockStorage; + StorageIdFunction mockStorageFetchDirectyId = [&] (Utils::SmallStringView string) { + return mockStorage.fetchDirectoryId(string); + }; + StorageStringFunction mockStorageFetchDirectyPath = [&] (int id) { + return mockStorage.fetchDirectoryPath(id); + }; + Cache cache; + NiceMock &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"))); } } -- cgit v1.2.1