summaryrefslogtreecommitdiff
path: root/tests/unit/unittest/stringcache-test.cpp
diff options
context:
space:
mode:
authorMarco Bubke <marco.bubke@qt.io>2017-09-21 11:43:59 +0200
committerTim Jenssen <tim.jenssen@qt.io>2017-10-05 17:36:37 +0000
commitd2e15e5f1efa1ef160a2d4f40ab61180477d1f14 (patch)
treeb35eeec94b895a48e9c7ce6ea00c245935d1ea38 /tests/unit/unittest/stringcache-test.cpp
parent0be824000262882023b2b48b5eab0786f3db379b (diff)
downloadqt-creator-d2e15e5f1efa1ef160a2d4f40ab61180477d1f14.tar.gz
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 <tim.jenssen@qt.io>
Diffstat (limited to 'tests/unit/unittest/stringcache-test.cpp')
-rw-r--r--tests/unit/unittest/stringcache-test.cpp134
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")));
}
}