fix(SimpleLruCounter): Correctly set bumped node's next (#2346)

This commit is contained in:
Shahar Mike 2023-12-28 12:26:16 +02:00 committed by GitHub
parent 69f269e808
commit c7093c40e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View file

@ -84,7 +84,7 @@ void SimpleLruCounter::BumpToHead(uint32_t index) {
node_arr_[node.prev].next = node.next;
node_arr_[node.next].prev = node.prev;
node.prev = tail;
node.prev = head_;
node.next = head_;
head_ = index;
}
}; // namespace dfly

View file

@ -13,9 +13,10 @@ namespace dfly {
class SimpleLruTest : public ::testing::Test {
protected:
SimpleLruTest() : cache_(4) {
SimpleLruTest() : cache_(kSize) {
}
const size_t kSize = 4;
SimpleLruCounter cache_;
};
@ -45,4 +46,29 @@ TEST_F(SimpleLruTest, Basic) {
ASSERT_EQ(6, cache_.Get("f"));
}
TEST_F(SimpleLruTest, DifferentOrder) {
for (uint32_t i = 0; i < kSize * 2; ++i) {
cache_.Put(absl::StrCat(i), i);
}
for (uint32_t i = 0; i < kSize; ++i) {
EXPECT_EQ(nullopt, cache_.Get(absl::StrCat(i)));
}
for (uint32_t i = kSize; i < kSize * 2; ++i) {
EXPECT_EQ(i, cache_.Get(absl::StrCat(i)));
}
for (uint32_t i = kSize; i > 0; --i) {
cache_.Put(absl::StrCat(i), i);
}
cache_.Put("0", 0);
for (uint32_t i = 0; i < kSize; ++i) {
EXPECT_EQ(i, cache_.Get(absl::StrCat(i)));
}
for (uint32_t i = kSize; i < kSize * 2; ++i) {
EXPECT_EQ(nullopt, cache_.Get(absl::StrCat(i)));
}
}
} // namespace dfly