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

Commit 76c93b6d authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "GD-storage: Add comparison function to ListMap, LruCache, and ConfigCache"

parents 95e663e7 25b6a5d7
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -25,8 +25,6 @@
#include <type_traits>
#include <unordered_map>

#include "os/log.h"

namespace bluetooth {
namespace common {

@@ -81,6 +79,14 @@ class ListMap {
    return *this;
  }

  // comparison operators
  bool operator==(const ListMap& rhs) const {
    return node_list_ == rhs.node_list_;
  }
  bool operator!=(const ListMap& rhs) const {
    return !(*this == rhs);
  }

  ~ListMap() {
    clear();
  }
+24 −0
Original line number Diff line number Diff line
@@ -36,6 +36,30 @@ TEST(ListMapTest, empty_test) {
  EXPECT_FALSE(list_map.extract(42));
}

TEST(ListMapTest, comparison_test) {
  ListMap<int, int> list_map_1;
  list_map_1.insert_or_assign(1, 10);
  list_map_1.insert_or_assign(2, 20);
  ListMap<int, int> list_map_2;
  list_map_2.insert_or_assign(1, 10);
  list_map_2.insert_or_assign(2, 20);
  EXPECT_EQ(list_map_1, list_map_2);
  // List map with different value should be different
  list_map_2.insert_or_assign(1, 11);
  EXPECT_NE(list_map_1, list_map_2);
  // List maps with different order should not be equal
  ListMap<int, int> list_map_3;
  list_map_3.insert_or_assign(2, 20);
  list_map_3.insert_or_assign(1, 10);
  EXPECT_NE(list_map_1, list_map_3);
  // Empty list map should not be equal to non-empty ones
  ListMap<int, int> list_map_4;
  EXPECT_NE(list_map_1, list_map_4);
  // Empty list maps should be equal
  ListMap<int, int> list_map_5;
  EXPECT_EQ(list_map_4, list_map_5);
}

TEST(ListMapTest, copy_test) {
  ListMap<int, std::shared_ptr<int>> list_map;
  list_map.insert_or_assign(1, std::make_shared<int>(100));
+9 −2
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

#pragma once

#include <os/log.h>

#include <functional>
#include <iterator>
#include <list>
@@ -27,6 +25,7 @@
#include <unordered_map>

#include "common/list_map.h"
#include "os/log.h"

namespace bluetooth {
namespace common {
@@ -83,6 +82,14 @@ class LruCache {
    return *this;
  }

  // comparison operators
  bool operator==(const LruCache& rhs) const {
    return capacity_ == rhs.capacity_ && list_map_ == rhs.list_map_;
  }
  bool operator!=(const LruCache& rhs) const {
    return !(*this == rhs);
  }

  ~LruCache() {
    clear();
  }
+32 −0
Original line number Diff line number Diff line
@@ -36,6 +36,38 @@ TEST(LruCacheTest, empty_test) {
  EXPECT_FALSE(cache.extract(42));
}

TEST(LruCacheTest, comparison_test) {
  LruCache<int, int> cache_1(2);
  cache_1.insert_or_assign(1, 10);
  cache_1.insert_or_assign(2, 20);
  LruCache<int, int> cache_2(2);
  cache_2.insert_or_assign(1, 10);
  cache_2.insert_or_assign(2, 20);
  EXPECT_EQ(cache_1, cache_2);
  // Cache with different order should not be equal
  cache_2.find(1);
  EXPECT_NE(cache_1, cache_2);
  cache_1.find(1);
  EXPECT_EQ(cache_1, cache_2);
  // Cache with different value should be different
  cache_2.insert_or_assign(1, 11);
  EXPECT_NE(cache_1, cache_2);
  // Cache with different capacity should not be equal
  LruCache<int, int> cache_3(3);
  cache_3.insert_or_assign(1, 10);
  cache_3.insert_or_assign(2, 20);
  EXPECT_NE(cache_1, cache_3);
  // Empty cache should not be equal to non-empty ones
  LruCache<int, int> cache_4(2);
  EXPECT_NE(cache_1, cache_4);
  // Empty caches should be equal
  LruCache<int, int> cache_5(2);
  EXPECT_EQ(cache_4, cache_5);
  // Empty caches with different capacity should not be equal
  LruCache<int, int> cache_6(3);
  EXPECT_NE(cache_4, cache_6);
}

TEST(LruCacheTest, try_emplace_test) {
  LruCache<int, int> cache(2);
  cache.insert_or_assign(1, 10);
+9 −6
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
#include "storage/config_cache.h"

#include <ios>
#include <sstream>

#include "storage/mutation.h"

@@ -128,6 +127,15 @@ void ConfigCache::SetProperty(std::string section, std::string property, std::st
    return;
  }
  auto section_iter = persistent_devices_.find(section);
  if (section_iter == persistent_devices_.end() && IsLinkKeyProperty(property)) {
    // move paired devices or create new paired device when a link key is set
    auto section_properties = temporary_devices_.extract(section);
    if (section_properties) {
      section_iter = persistent_devices_.try_emplace_back(section, std::move(section_properties->second)).first;
    } else {
      section_iter = persistent_devices_.try_emplace_back(section, common::ListMap<std::string, std::string>{}).first;
    }
  }
  if (section_iter != persistent_devices_.end()) {
    section_iter->second.insert_or_assign(property, std::move(value));
    return;
@@ -138,11 +146,6 @@ void ConfigCache::SetProperty(std::string section, std::string property, std::st
    section_iter = std::get<0>(triple);
  }
  section_iter->second.insert_or_assign(property, std::move(value));
  if (IsLinkKeyProperty(property)) {
    // move paired devices
    auto section_properties = temporary_devices_.extract(section);
    persistent_devices_.insert_or_assign(section, std::move(section_properties->second));
  }
}

bool ConfigCache::RemoveSection(const std::string& section) {
Loading