Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4ab4065b authored by Jack He's avatar Jack He
Browse files

LruCache: Allow Get to take nullptr

* When nullptr is given, copy of item is omitted

Bug: 143515989
Test: atest --host bluetooth_test_common
Change-Id: If5405611e8a32709bf0bf18f416d17adbd07b5ea
parent c1e49cbe
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -83,7 +83,9 @@ class LruCache {
    node_list_.erase(list_iterator);
    node_list_.push_front(node);
    map_iterator->second = node_list_.begin();
    if (value != nullptr) {
      *value = node.second;
    }
    return true;
  }

+14 −0
Original line number Diff line number Diff line
@@ -139,6 +139,20 @@ TEST(BluetoothLruCacheTest, LruCacheMainTest2) {
  EXPECT_EQ(*value, 50);
}

TEST(BluetoothLruCacheTest, LruCacheGetTest) {
  LruCache<int, int> cache(10, "testing", [](int a, int b) {});
  cache.Put(1, 10);
  cache.Put(2, 20);
  int value = 0;
  EXPECT_TRUE(cache.Get(1, &value));
  EXPECT_EQ(value, 10);
  EXPECT_TRUE(cache.Get(1, nullptr));
  EXPECT_TRUE(cache.Get(2, nullptr));
  EXPECT_FALSE(cache.Get(3, nullptr));
  EXPECT_FALSE(cache.Get(3, &value));
  EXPECT_EQ(value, 10);
}

TEST(BluetoothLruCacheTest, LruCacheRemoveTest) {
  LruCache<int, int> cache(10, "testing", [](int a, int b) {});
  for (int key = 0; key <= 30; key++) {