76 for (
auto j = startWith; count; ++j, --count)
77 CPPUNIT_ASSERT(m.
add(std::to_string(j), j, ttl));
89 const auto key = std::to_string(value);
90 CPPUNIT_ASSERT(m.
add(key, value));
91 CPPUNIT_ASSERT(m.
get(key));
92 CPPUNIT_ASSERT_EQUAL(value, *m.
get(key));
98 const auto key = std::to_string(value);
99 CPPUNIT_ASSERT(m.
add(key, value, ttl));
100 CPPUNIT_ASSERT(m.
get(key));
101 CPPUNIT_ASSERT_EQUAL(value, *m.
get(key));
109 CPPUNIT_ASSERT(m.
get(
"1"));
110 CPPUNIT_ASSERT_EQUAL(1, *(m.
get(
"1")));
111 CPPUNIT_ASSERT(m.
get(
"9"));
112 CPPUNIT_ASSERT_EQUAL(9, *(m.
get(
"9")));
114 CPPUNIT_ASSERT(m.
get(
"1"));
115 CPPUNIT_ASSERT_EQUAL(99, *(m.
get(
"1")));
117 CPPUNIT_ASSERT(!m.
get(
"1"));
125 const auto entriesBefore = m.
entries();
126 CPPUNIT_ASSERT(!m.
get(
"not-there"));
128 CPPUNIT_ASSERT_EQUAL(entriesBefore, m.
entries());
135 Map m(10*1024*1024, 10);
136 CPPUNIT_ASSERT_EQUAL(
static_cast<size_t>(0), m.
entries());
138 CPPUNIT_ASSERT_EQUAL(
static_cast<size_t>(10), m.
entries());
140 CPPUNIT_ASSERT_EQUAL(
static_cast<size_t>(11), m.
entries());
145 CPPUNIT_ASSERT(m.
entries() < 1000);
153 CPPUNIT_ASSERT_EQUAL(
sizeof(int32_t),
static_cast<size_t>(
DefaultMemoryUsage(int32_t{})));
154 CPPUNIT_ASSERT_EQUAL(
sizeof(int64_t),
static_cast<size_t>(
DefaultMemoryUsage(int64_t{})));
156 using Str =
char[10];
164 CPPUNIT_ASSERT_EQUAL(uint64_t(0), nilA.
memLimit());
165 CPPUNIT_ASSERT_EQUAL(uint64_t(0), nilA.
freeMem());
166 CPPUNIT_ASSERT_EQUAL(uint64_t(0), nilA.
memoryUsed());
167 CPPUNIT_ASSERT_EQUAL(
size_t(0), nilA.
entries());
169 const Map nilB(0, 0);
170 CPPUNIT_ASSERT_EQUAL(uint64_t(0), nilB.
memLimit());
171 CPPUNIT_ASSERT_EQUAL(uint64_t(0), nilB.
freeMem());
172 CPPUNIT_ASSERT_EQUAL(uint64_t(0), nilB.
memoryUsed());
173 CPPUNIT_ASSERT_EQUAL(
size_t(0), nilB.
entries());
176 CPPUNIT_ASSERT_EQUAL(uint64_t(1), emptyC.
memLimit());
177 CPPUNIT_ASSERT_EQUAL(uint64_t(1), emptyC.
freeMem());
178 CPPUNIT_ASSERT_EQUAL(uint64_t(0), emptyC.
memoryUsed());
179 CPPUNIT_ASSERT_EQUAL(
size_t(0), emptyC.
entries());
181 const Map emptyD(1024);
182 CPPUNIT_ASSERT_EQUAL(uint64_t(1024), emptyD.
memLimit());
183 CPPUNIT_ASSERT_EQUAL(uint64_t(1024), emptyD.
freeMem());
184 CPPUNIT_ASSERT_EQUAL(uint64_t(0), emptyD.
memoryUsed());
185 CPPUNIT_ASSERT_EQUAL(
size_t(0), emptyD.
entries());
191 const size_t initialCapacity = 1024;
192 Map m(initialCapacity);
194 const auto entriesAtInitialCapacity = m.
entries();
198 CPPUNIT_ASSERT_EQUAL(
size_t(0), m.
entries());
201 const auto increasedCapacity = initialCapacity * 2;
204 CPPUNIT_ASSERT(m.
entries() > entriesAtInitialCapacity);
208 auto iterationsLeft = m.
entries();
209 CPPUNIT_ASSERT(0 < iterationsLeft && iterationsLeft <= increasedCapacity);
214 const auto entriesBefore = m.
entries();
216 const auto newMemoryLimit = memoryUsedBefore/2;
219 CPPUNIT_ASSERT(m.
memoryUsed() <= newMemoryLimit);
220 CPPUNIT_ASSERT(m.
entries() < entriesBefore);
223 CPPUNIT_ASSERT(iterationsLeft > 0);
230 CPPUNIT_ASSERT(m.
entries() > entriesAtInitialCapacity);
240 CPPUNIT_ASSERT(m.
get(
"0"));
242 CPPUNIT_ASSERT(!m.
get(
"0"));
250 CPPUNIT_ASSERT(m.
get(
"0"));
252 CPPUNIT_ASSERT(!m.
get(
"0"));
260 CPPUNIT_ASSERT(m.
get(
"0"));
262 CPPUNIT_ASSERT(!m.
get(
"0"));
273 CPPUNIT_ASSERT(!m.
get(
"0"));
279 CPPUNIT_ASSERT(m.
get(
"0"));
281 CPPUNIT_ASSERT(!m.
get(
"0"));
291 CPPUNIT_ASSERT(!m.
get(
"0"));
299 CPPUNIT_ASSERT(!m.
get(
"0"));
307 CPPUNIT_ASSERT(!m.
get(
"0"));
320 CPPUNIT_ASSERT(!m.
add(
"0", 0, -1));
324 CPPUNIT_ASSERT(!m.
get(
"0"));
334 for (
int j = 0; j < 10; ++j)
337 for (
int j = 100; j < 1000; ++j) {
339 CPPUNIT_ASSERT(m.
get(
"0"));
342 CPPUNIT_ASSERT(!m.
get(
"1"));
343 CPPUNIT_ASSERT(!m.
get(
"2"));
344 CPPUNIT_ASSERT(!m.
get(
"3"));
345 CPPUNIT_ASSERT(!m.
get(
"4"));
348 CPPUNIT_ASSERT(!m.
get(
"0"));
355 const size_t expectedEntryCount = 10;
357 size_t iterations = 0;
358 for (
auto i = m.
cbegin(); i != m.
cend(); ++i) {
360 const auto expectedValue =
static_cast<Map::mapped_type>(expectedEntryCount - iterations);
361 CPPUNIT_ASSERT_EQUAL(expectedValue, i->value);
363 CPPUNIT_ASSERT_EQUAL(expectedEntryCount, iterations);
370 const size_t expectedEntryCount = 10;
372 size_t iterations = 0;
373 for (
const auto &entry: m) {
375 const auto expectedValue =
static_cast<Map::mapped_type>(expectedEntryCount - iterations);
376 CPPUNIT_ASSERT_EQUAL(expectedValue, entry.value);
378 CPPUNIT_ASSERT_EQUAL(expectedEntryCount, iterations);
uint64_t DefaultMemoryUsage(const Value &e)
ConstEntriesIterator cbegin() const
const Value * get(const Key &)
void del(const Key &)
Remove the corresponding entry (if any)
uint64_t memLimit() const
The memory capacity for the map.
uint64_t freeMem() const
The free space of the map.
int Ttl
maximum desired entry caching duration (a.k.a. TTL), in seconds
void setMemLimit(uint64_t newLimit)
Reset the memory capacity for this map, purging if needed.
size_t entries() const
The number of currently stored entries, including expired ones.
bool add(const Key &, const Value &, Ttl)
ConstEntriesIterator cend() const
uint64_t memoryUsed() const
The current (approximate) memory usage of the map.
customizes our test setup
the representation of the configuration. POD.
void testClassicLoopTraversal()
CPPUNIT_TEST_SUITE(TestClpMap)
void testRangeLoopTraversal()
void fillMapWithEntries(Map &)
add (more than) enough entries to make the map full
CPPUNIT_TEST(testPurgeIsLru)
CPPUNIT_TEST(testClassicLoopTraversal)
void addOneEntry(Map &, Map::mapped_type)
void testReplaceEntryWithShorterTtl()
void addSequenceOfEntriesToMap(Map &, size_t count, Map::mapped_type startWith, Map::Ttl)
CPPUNIT_TEST(testNegativeTtl)
CPPUNIT_TEST(testEntryCounter)
CPPUNIT_TEST(testPutGetDelete)
CPPUNIT_TEST(testMemoryLimit)
CPPUNIT_TEST(testConstructor)
CPPUNIT_TEST(testTtlExpiration)
CPPUNIT_TEST(testMemoryCounter)
CPPUNIT_TEST(testReplaceEntryWithShorterTtl)
CPPUNIT_TEST(testZeroTtl)
CPPUNIT_TEST(testRangeLoopTraversal)
implements test program's main() function while enabling customization
int run(int argc, char *argv[])
int main(int argc, char *argv[])
CPPUNIT_TEST_SUITE_REGISTRATION(TestClpMap)