020-generic-map.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2019-2024 Ivan Baidakou
  3. #include "test-utils.h"
  4. #include "access.h"
  5. #include "model/device.h"
  6. #include "model/misc/lru_cache.hpp"
  7. using namespace syncspirit;
  8. using namespace syncspirit::model;
  9. using namespace syncspirit::proto;
  10. using namespace syncspirit::test;
  11. namespace bfs = boost::filesystem;
  12. TEST_CASE("generic map", "[model]") {
  13. auto my_id = device_id_t::from_string("KHQNO2S-5QSILRK-YX4JZZ4-7L77APM-QNVGZJT-EKU7IFI-PNEPBMY-4MXFMQD").value();
  14. auto my_device = device_t::create(my_id, "my-device").value();
  15. auto map = devices_map_t();
  16. map.put(my_device);
  17. REQUIRE(map.by_sha256(my_id.get_sha256()));
  18. REQUIRE(map.get(my_device->get_key()));
  19. map.remove(my_device);
  20. REQUIRE(!map.by_sha256(my_id.get_sha256()));
  21. REQUIRE(!map.get(my_device->get_key()));
  22. REQUIRE(map == map);
  23. }
  24. namespace syncspirit::model::details {
  25. template <> inline std::string_view get_lru_key<std::string>(const std::string &key) { return key; }
  26. } // namespace syncspirit::model::details
  27. TEST_CASE("lru cache", "[model]") {
  28. SECTION("string item") {
  29. mru_list_t<std::string> list(3);
  30. list.put("a");
  31. list.put("b");
  32. list.put("c");
  33. CHECK(list.get("a") == "a");
  34. CHECK(list.get("b") == "b");
  35. CHECK(list.get("c") == "c");
  36. CHECK(list.get("d") == "");
  37. list.put("d");
  38. CHECK(list.get("a") == "");
  39. CHECK(list.get("b") == "b");
  40. CHECK(list.get("c") == "c");
  41. CHECK(list.get("d") == "d");
  42. list.get("b");
  43. list.put("e");
  44. CHECK(list.get("a") == "");
  45. CHECK(list.get("b") == "b");
  46. CHECK(list.get("c") == "");
  47. CHECK(list.get("d") == "d");
  48. CHECK(list.get("e") == "e");
  49. list.put("e");
  50. CHECK(list.get("a") == "");
  51. CHECK(list.get("b") == "b");
  52. CHECK(list.get("c") == "");
  53. CHECK(list.get("d") == "d");
  54. CHECK(list.get("e") == "e");
  55. list.remove("d");
  56. CHECK(list.get("a") == "");
  57. CHECK(list.get("b") == "b");
  58. CHECK(list.get("c") == "");
  59. CHECK(list.get("d") == "");
  60. CHECK(list.get("e") == "e");
  61. list.put("c");
  62. CHECK(list.get("a") == "");
  63. CHECK(list.get("b") == "b");
  64. CHECK(list.get("c") == "c");
  65. CHECK(list.get("d") == "");
  66. CHECK(list.get("e") == "e");
  67. }
  68. }
  69. struct item_t {
  70. std::string key;
  71. int value;
  72. };
  73. using item_map_t = syncspirit::model::generic_map_t<item_t, 1>;
  74. namespace syncspirit::model {
  75. template <> SYNCSPIRIT_API inline std::string_view get_index<0>(const item_t &item) noexcept { return item.key; }
  76. } // namespace syncspirit::model
  77. TEST_CASE("generic map ops") {
  78. item_map_t map;
  79. auto i1 = item_t{"k", 1};
  80. auto i2 = item_t{"k", 2};
  81. map.put(i1);
  82. CHECK(map.get("k").value == 1);
  83. map.put(i2);
  84. CHECK(map.get("k").value == 2);
  85. REQUIRE(map.size() == 1);
  86. map.remove(i2);
  87. REQUIRE(map.size() == 0);
  88. }