From 6f05bd035c5b23d24f9e9a72311053019bbb7d17 Mon Sep 17 00:00:00 2001 From: JCW Date: Mon, 4 Aug 2025 13:47:04 +0100 Subject: [PATCH] Add test case and improve test coverage Signed-off-by: JCW --- src/test/basics/TaggedCache_test.cpp | 114 ++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 13 deletions(-) diff --git a/src/test/basics/TaggedCache_test.cpp b/src/test/basics/TaggedCache_test.cpp index 3ea2bd4c77..7decb081fe 100644 --- a/src/test/basics/TaggedCache_test.cpp +++ b/src/test/basics/TaggedCache_test.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -153,24 +154,111 @@ public: BEAST_EXPECT(c.getCacheSize() == 1); BEAST_EXPECT(c.getTrackSize() == 1); - auto const p1 = c.fetch(5); - BEAST_EXPECT(p1 != nullptr); - BEAST_EXPECT(c.getCacheSize() == 1); - BEAST_EXPECT(c.getTrackSize() == 1); + { + auto const p1 = c.fetch(5); + BEAST_EXPECT(p1 != nullptr); + BEAST_EXPECT(c.getCacheSize() == 1); + BEAST_EXPECT(c.getTrackSize() == 1); + + // Advance the clock a lot + ++clock; + c.sweep(); + BEAST_EXPECT(c.getCacheSize() == 0); + BEAST_EXPECT(c.getTrackSize() == 1); + + auto p2 = std::make_shared("five_2"); + BEAST_EXPECT(c.canonicalize_replace_cache(5, p2)); + BEAST_EXPECT(c.getCacheSize() == 1); + BEAST_EXPECT(c.getTrackSize() == 1); + // Make sure we get the original object + BEAST_EXPECT(p1.get() != p2.get()); + BEAST_EXPECT(*p2 == "five_2"); + } - // Advance the clock a lot ++clock; c.sweep(); BEAST_EXPECT(c.getCacheSize() == 0); - BEAST_EXPECT(c.getTrackSize() == 1); + BEAST_EXPECT(c.getTrackSize() == 0); + } - auto p2 = std::make_shared("five_2"); - BEAST_EXPECT(c.canonicalize_replace_cache(5, p2)); - BEAST_EXPECT(c.getCacheSize() == 1); - BEAST_EXPECT(c.getTrackSize() == 1); - // Make sure we get the original object - BEAST_EXPECT(p1.get() != p2.get()); - BEAST_EXPECT(*p2 == "five_2"); + { + testcase("intrptr"); + + struct MyRefCountObject : IntrusiveRefCounts + { + std::string _data; + + // Needed to support weak intrusive pointers + virtual void + partialDestructor() {}; + + MyRefCountObject() = default; + explicit MyRefCountObject(std::string data) + : _data(std::move(data)) {} + explicit MyRefCountObject(char const* data) + : _data(data) {} + + MyRefCountObject& operator=(MyRefCountObject const& other) + { + _data = other._data; + return *this; + } + + bool operator==(MyRefCountObject const& other) const + { + return _data == other._data; + } + + bool operator==(std::string const& other) const + { + return _data == other; + } + }; + + using IntrPtrCache = TaggedCache< + Key, + MyRefCountObject, + /*IsKeyCache*/ false, + intr_ptr::SharedWeakUnionPtr, + intr_ptr::SharedPtr + >; + + IntrPtrCache intrPtrCache("IntrPtrTest", 1, 1s, clock, journal); + + intrPtrCache.canonicalize_replace_cache(1, intr_ptr::make_shared("one")); + BEAST_EXPECT(intrPtrCache.getCacheSize() == 1); + BEAST_EXPECT(intrPtrCache.getTrackSize() == 1); + + { + { + intrPtrCache.canonicalize_replace_cache(1, intr_ptr::make_shared("one_replaced")); + + auto p = intrPtrCache.fetch(1); + BEAST_EXPECT(*p == "one_replaced"); + + // Advance the clock a lot + ++clock; + intrPtrCache.sweep(); + BEAST_EXPECT(intrPtrCache.getCacheSize() == 0); + BEAST_EXPECT(intrPtrCache.getTrackSize() == 1); + + intrPtrCache.canonicalize_replace_cache(1, intr_ptr::make_shared("one_replaced_2")); + + auto p2 = intrPtrCache.fetch(1); + BEAST_EXPECT(*p2 == "one_replaced_2"); + + intrPtrCache.del(1, true); + } + + intrPtrCache.canonicalize_replace_cache(1, intr_ptr::make_shared("one_replaced_3")); + auto p3 = intrPtrCache.fetch(1); + BEAST_EXPECT(*p3 == "one_replaced_3"); + } + + ++clock; + intrPtrCache.sweep(); + BEAST_EXPECT(intrPtrCache.getCacheSize() == 0); + BEAST_EXPECT(intrPtrCache.getTrackSize() == 0); } } };