020-generic-map.cpp 3.1 KB

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