Skip to content

Commit

Permalink
A small improvement for the tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Kalmbach <[email protected]>
  • Loading branch information
joka921 committed Dec 12, 2024
1 parent e294b1d commit 4064a85
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions test/ConcurrentCacheTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,30 +533,36 @@ TEST(ConcurrentCache, testTryInsertIfNotPresentDoesWorkCorrectly) {

TEST(ConcurrentCache, computeButDontStore) {
SimpleConcurrentLruCache cache{};

// The last argument of `computeOnce...`: For the sake of this test, all
// results are suitable for the cache. Note: In the `computeButDontStore`
// function this argument is ignored, because the results are never stored in
// the cache.
auto alwaysSuitable = [](auto&&) { return true; };
// Store the element in the cache.
cache.computeOnce(
42, []() { return "42"; }, false, [](auto&&) { return true; });
42, []() { return "42"; }, false, alwaysSuitable);

// The result is read from the cache, so we get "42", not "blubb".
auto res = cache.computeButDontStore(
42, []() { return "blubb"; }, false, [](auto&&) { return true; });
42, []() { return "blubb"; }, false, alwaysSuitable);
EXPECT_EQ(*res._resultPointer, "42");

// The same with `onlyReadFromCache` == true;
res = cache.computeButDontStore(
42, []() { return "blubb"; }, true, [](auto&&) { return true; });
42, []() { return "blubb"; }, true, alwaysSuitable);
EXPECT_EQ(*res._resultPointer, "42");

cache.clearAll();

// Compute, but don't store.
res = cache.computeButDontStore(
42, []() { return "blubb"; }, false, [](auto&&) { return true; });
42, []() { return "blubb"; }, false, alwaysSuitable);
EXPECT_EQ(*res._resultPointer, "blubb");

// Nothing is stored in the cache, so we cannot read it.
EXPECT_FALSE(cache.getIfContained(42).has_value());
res = cache.computeButDontStore(
42, []() { return "blubb"; }, true, [](auto&&) { return true; });
42, []() { return "blubb"; }, true, alwaysSuitable);
EXPECT_EQ(res._resultPointer, nullptr);
}

0 comments on commit 4064a85

Please sign in to comment.