020-generic-map.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2019-2023 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. }
  23. namespace syncspirit::model::details {
  24. template <> inline std::string_view get_lru_key<std::string>(const std::string &key) { return key; }
  25. } // namespace syncspirit::model::details
  26. TEST_CASE("lru cache", "[model]") {
  27. SECTION("string item") {
  28. mru_list_t<std::string> list(3);
  29. list.put("a");
  30. list.put("b");
  31. list.put("c");
  32. CHECK(list.get("a") == "a");
  33. CHECK(list.get("b") == "b");
  34. CHECK(list.get("c") == "c");
  35. CHECK(list.get("d") == "");
  36. list.put("d");
  37. CHECK(list.get("a") == "");
  38. CHECK(list.get("b") == "b");
  39. CHECK(list.get("c") == "c");
  40. CHECK(list.get("d") == "d");
  41. list.get("b");
  42. list.put("e");
  43. CHECK(list.get("a") == "");
  44. CHECK(list.get("b") == "b");
  45. CHECK(list.get("c") == "");
  46. CHECK(list.get("d") == "d");
  47. CHECK(list.get("e") == "e");
  48. list.put("e");
  49. CHECK(list.get("a") == "");
  50. CHECK(list.get("b") == "b");
  51. CHECK(list.get("c") == "");
  52. CHECK(list.get("d") == "d");
  53. CHECK(list.get("e") == "e");
  54. list.remove("d");
  55. CHECK(list.get("a") == "");
  56. CHECK(list.get("b") == "b");
  57. CHECK(list.get("c") == "");
  58. CHECK(list.get("d") == "");
  59. CHECK(list.get("e") == "e");
  60. list.put("c");
  61. CHECK(list.get("a") == "");
  62. CHECK(list.get("b") == "b");
  63. CHECK(list.get("c") == "c");
  64. CHECK(list.get("d") == "");
  65. CHECK(list.get("e") == "e");
  66. }
  67. }
  68. struct item_t {
  69. std::string key;
  70. int value;
  71. };
  72. using item_map_t = syncspirit::model::generic_map_t<item_t, 1>;
  73. namespace syncspirit::model {
  74. template <> SYNCSPIRIT_API inline std::string_view get_index<0>(const item_t &item) noexcept { return item.key; }
  75. } // namespace syncspirit::model
  76. TEST_CASE("generic map ops") {
  77. item_map_t map;
  78. auto i1 = item_t{"k", 1};
  79. auto i2 = item_t{"k", 2};
  80. map.put(i1);
  81. CHECK(map.get("k").value == 1);
  82. map.put(i2);
  83. CHECK(map.get("k").value == 2);
  84. REQUIRE(map.size() == 1);
  85. map.remove(i2);
  86. REQUIRE(map.size() == 0);
  87. }