Repository navigation
Expand file tree
/
Copy pathtest_git_base_blob_cache.cpp
More file actions
162 lines (138 loc) · 6.07 KB
/
Copy pathtest_git_base_blob_cache.cpp
File metadata and controls
162 lines (138 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* This file is part of Notepad Next.
* Copyright 2026 NotepadAI contributors
*
* Notepad Next is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Notepad Next is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Notepad Next. If not, see <https://www.gnu.org/licenses/>.
*/
#include "GitBaseBlobCache.h"
#include <QByteArray>
#include <QString>
#include <QTest>
#include <QtTest/QtTest>
class TestGitBaseBlobCache : public QObject
{
Q_OBJECT
private slots:
void init(); // fresh-cache guarantee per test
void putGet_roundTrips();
void miss_returnsEmpty();
void invalidate_removesEntry();
void invalidateRepo_removesAllForThatRepo_keepsOthers();
void lruEviction_dropsLeastRecentlyUsed();
void getPromotesToMostRecentlyUsed();
void shrinkBudget_evictsImmediately();
void replaceSamePath_updatesBytesAccount();
};
namespace {
QByteArray bigBlob(int sizeKiB, char fill)
{
return QByteArray(sizeKiB * 1024, fill);
}
} // namespace
void TestGitBaseBlobCache::init()
{
GitBaseBlobCache::instance().clear();
GitBaseBlobCache::instance().setMaxBytes(16 * 1024 * 1024);
}
void TestGitBaseBlobCache::putGet_roundTrips()
{
auto &c = GitBaseBlobCache::instance();
c.put(QStringLiteral("/repo"), QStringLiteral("a.cpp"),
QByteArrayLiteral("hello world"));
QCOMPARE(c.get(QStringLiteral("/repo"), QStringLiteral("a.cpp")),
QByteArrayLiteral("hello world"));
QCOMPARE(c.byteCount(), qsizetype(11));
}
void TestGitBaseBlobCache::miss_returnsEmpty()
{
auto &c = GitBaseBlobCache::instance();
QVERIFY(c.get(QStringLiteral("/nope"), QStringLiteral("x.cpp")).isEmpty());
}
void TestGitBaseBlobCache::invalidate_removesEntry()
{
auto &c = GitBaseBlobCache::instance();
c.put(QStringLiteral("/repo"), QStringLiteral("a.cpp"), QByteArrayLiteral("body"));
c.invalidate(QStringLiteral("/repo"), QStringLiteral("a.cpp"));
QVERIFY(c.get(QStringLiteral("/repo"), QStringLiteral("a.cpp")).isEmpty());
QCOMPARE(c.byteCount(), qsizetype(0));
}
void TestGitBaseBlobCache::invalidateRepo_removesAllForThatRepo_keepsOthers()
{
auto &c = GitBaseBlobCache::instance();
c.put(QStringLiteral("/r1"), QStringLiteral("a.cpp"), QByteArrayLiteral("A"));
c.put(QStringLiteral("/r1"), QStringLiteral("b.cpp"), QByteArrayLiteral("B"));
c.put(QStringLiteral("/r2"), QStringLiteral("c.cpp"), QByteArrayLiteral("C"));
c.invalidateRepo(QStringLiteral("/r1"));
QVERIFY(c.get(QStringLiteral("/r1"), QStringLiteral("a.cpp")).isEmpty());
QVERIFY(c.get(QStringLiteral("/r1"), QStringLiteral("b.cpp")).isEmpty());
QCOMPARE(c.get(QStringLiteral("/r2"), QStringLiteral("c.cpp")),
QByteArrayLiteral("C"));
QCOMPARE(c.byteCount(), qsizetype(1));
}
void TestGitBaseBlobCache::lruEviction_dropsLeastRecentlyUsed()
{
auto &c = GitBaseBlobCache::instance();
// 4 entries × 4 KiB; budget set to 12 KiB so inserting the fourth must
// evict the first.
c.setMaxBytes(12 * 1024);
c.put(QStringLiteral("/r"), QStringLiteral("1"), bigBlob(4, '1'));
c.put(QStringLiteral("/r"), QStringLiteral("2"), bigBlob(4, '2'));
c.put(QStringLiteral("/r"), QStringLiteral("3"), bigBlob(4, '3'));
// Insert #4 — total would be 16 KiB; LRU (= #1) must go.
c.put(QStringLiteral("/r"), QStringLiteral("4"), bigBlob(4, '4'));
QVERIFY(c.get(QStringLiteral("/r"), QStringLiteral("1")).isEmpty());
QCOMPARE(c.get(QStringLiteral("/r"), QStringLiteral("2")).size(), qsizetype(4096));
QCOMPARE(c.get(QStringLiteral("/r"), QStringLiteral("3")).size(), qsizetype(4096));
QCOMPARE(c.get(QStringLiteral("/r"), QStringLiteral("4")).size(), qsizetype(4096));
QCOMPARE(c.byteCount(), qsizetype(12 * 1024));
}
void TestGitBaseBlobCache::getPromotesToMostRecentlyUsed()
{
auto &c = GitBaseBlobCache::instance();
c.setMaxBytes(12 * 1024);
c.put(QStringLiteral("/r"), QStringLiteral("1"), bigBlob(4, '1'));
c.put(QStringLiteral("/r"), QStringLiteral("2"), bigBlob(4, '2'));
c.put(QStringLiteral("/r"), QStringLiteral("3"), bigBlob(4, '3'));
// Touch #1 — it now becomes MRU; #2 is the new LRU.
QCOMPARE(c.get(QStringLiteral("/r"), QStringLiteral("1")).size(), qsizetype(4096));
c.put(QStringLiteral("/r"), QStringLiteral("4"), bigBlob(4, '4'));
QCOMPARE(c.get(QStringLiteral("/r"), QStringLiteral("1")).size(), qsizetype(4096));
QVERIFY(c.get(QStringLiteral("/r"), QStringLiteral("2")).isEmpty());
}
void TestGitBaseBlobCache::shrinkBudget_evictsImmediately()
{
auto &c = GitBaseBlobCache::instance();
c.setMaxBytes(16 * 1024);
c.put(QStringLiteral("/r"), QStringLiteral("1"), bigBlob(4, '1'));
c.put(QStringLiteral("/r"), QStringLiteral("2"), bigBlob(4, '2'));
c.put(QStringLiteral("/r"), QStringLiteral("3"), bigBlob(4, '3'));
c.put(QStringLiteral("/r"), QStringLiteral("4"), bigBlob(4, '4'));
QCOMPARE(c.byteCount(), qsizetype(16 * 1024));
c.setMaxBytes(8 * 1024);
QCOMPARE(c.byteCount(), qsizetype(8 * 1024));
// Oldest two must be gone; newest two survive.
QVERIFY(c.get(QStringLiteral("/r"), QStringLiteral("1")).isEmpty());
QVERIFY(c.get(QStringLiteral("/r"), QStringLiteral("2")).isEmpty());
}
void TestGitBaseBlobCache::replaceSamePath_updatesBytesAccount()
{
auto &c = GitBaseBlobCache::instance();
c.put(QStringLiteral("/r"), QStringLiteral("a"), bigBlob(4, 'a'));
QCOMPARE(c.byteCount(), qsizetype(4096));
c.put(QStringLiteral("/r"), QStringLiteral("a"), bigBlob(2, 'b'));
QCOMPARE(c.byteCount(), qsizetype(2048));
QCOMPARE(c.get(QStringLiteral("/r"), QStringLiteral("a")).at(0), 'b');
}
QTEST_GUILESS_MAIN(TestGitBaseBlobCache)
#include "test_git_base_blob_cache.moc"