compact_flat_map_ut.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include <library/cpp/yt/small_containers/compact_flat_map.h>
  2. #include <library/cpp/testing/gtest/gtest.h>
  3. #include <string>
  4. #include <vector>
  5. namespace NYT {
  6. namespace {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. using TMap = TCompactFlatMap<std::string, std::string, 2>;
  9. TMap CreateMap()
  10. {
  11. std::vector<std::pair<std::string, std::string>> data = {{"I", "met"}, {"a", "traveller"}, {"from", "an"}, {"antique", "land"}};
  12. return {data.begin(), data.end()};
  13. }
  14. TEST(TCompactFlatMapTest, DefaultEmpty)
  15. {
  16. TMap m;
  17. EXPECT_TRUE(m.empty());
  18. EXPECT_EQ(m.begin(), m.end());
  19. }
  20. TEST(TCompactFlatMapTest, Reserve)
  21. {
  22. // No real way to test reserve - just use it and wiggle about.
  23. auto m1 = CreateMap();
  24. TMap m2;
  25. m2.reserve(m1.size());
  26. m2.insert(m1.begin(), m1.end());
  27. EXPECT_EQ(m1.size(), m2.size());
  28. }
  29. TEST(TCompactFlatMapTest, Size)
  30. {
  31. auto m = CreateMap();
  32. EXPECT_EQ(m.size(), 4u);
  33. EXPECT_EQ(m.ssize(), 4);
  34. m.insert({"Who", "said"});
  35. EXPECT_EQ(m.size(), 5u);
  36. EXPECT_EQ(m.ssize(), 5);
  37. m.erase("antique");
  38. EXPECT_EQ(m.size(), 4u);
  39. EXPECT_EQ(m.ssize(), 4);
  40. }
  41. TEST(TCompactFlatMapTest, ClearAndEmpty)
  42. {
  43. auto m = CreateMap();
  44. EXPECT_FALSE(m.empty());
  45. EXPECT_NE(m.begin(), m.end());
  46. m.clear();
  47. EXPECT_TRUE(m.empty());
  48. EXPECT_EQ(m.begin(), m.end());
  49. m.insert({"Who", "said"});
  50. EXPECT_FALSE(m.empty());
  51. EXPECT_NE(m.begin(), m.end());
  52. }
  53. TEST(TCompactFlatMapTest, FindMutable)
  54. {
  55. auto m = CreateMap();
  56. {
  57. auto it = m.find("from");
  58. EXPECT_NE(it, m.end());
  59. EXPECT_EQ(it->second, "an");
  60. it->second = "the";
  61. }
  62. {
  63. auto it = m.find("from");
  64. EXPECT_NE(it, m.end());
  65. EXPECT_EQ(it->second, "the");
  66. }
  67. {
  68. auto it = m.find("Who");
  69. EXPECT_EQ(it, m.end());
  70. }
  71. }
  72. TEST(TCompactFlatMapTest, FindConst)
  73. {
  74. const auto& m = CreateMap();
  75. {
  76. auto it = m.find("from");
  77. EXPECT_NE(it, m.end());
  78. EXPECT_EQ(it->second, "an");
  79. }
  80. {
  81. auto it = m.find("Who");
  82. EXPECT_EQ(it, m.end());
  83. }
  84. }
  85. TEST(TCompactFlatMapTest, Insert)
  86. {
  87. auto m = CreateMap();
  88. auto [it, inserted] = m.insert({"Who", "said"});
  89. EXPECT_TRUE(inserted);
  90. EXPECT_EQ(m.ssize(), 5);
  91. EXPECT_NE(it, m.end());
  92. EXPECT_EQ(it, m.find("Who"));
  93. EXPECT_EQ(it->second, "said");
  94. auto [it2, inserted2] = m.insert({"Who", "told"});
  95. EXPECT_FALSE(inserted2);
  96. EXPECT_EQ(m.ssize(), 5);
  97. EXPECT_EQ(it2, it);
  98. EXPECT_EQ(it->second, "said");
  99. std::vector<std::pair<std::string, std::string>> data = {{"Two", "vast"}, {"and", "trunkless"}, {"legs", "of"}, {"stone", "Stand"}, {"in", "the"}, {"desert", "..."}};
  100. m.insert(data.begin(), data.end());
  101. EXPECT_EQ(m.ssize(), 11);
  102. EXPECT_NE(m.find("and"), m.end());
  103. EXPECT_EQ(m.find("and")->second, "trunkless");
  104. }
  105. TEST(TCompactFlatMapTest, Emplace)
  106. {
  107. auto m = CreateMap();
  108. auto [it, inserted] = m.emplace("Far", "place");
  109. EXPECT_TRUE(inserted);
  110. EXPECT_EQ(m.ssize(), 5);
  111. EXPECT_NE(it, m.end());
  112. EXPECT_EQ(it, m.find("Far"));
  113. EXPECT_EQ(it->second, "place");
  114. auto [it2, inserted2] = m.emplace("Far", "behind");
  115. EXPECT_FALSE(inserted2);
  116. EXPECT_EQ(m.ssize(), 5);
  117. EXPECT_EQ(it2, it);
  118. EXPECT_EQ(it->second, "place");
  119. }
  120. TEST(TCompactFlatMapTest, Subscript)
  121. {
  122. auto m = CreateMap();
  123. EXPECT_EQ(m["antique"], "land");
  124. EXPECT_EQ(m.ssize(), 4);
  125. EXPECT_EQ(m["Who"], "");
  126. EXPECT_EQ(m.ssize(), 5);
  127. }
  128. TEST(TCompactFlatMapTest, Erase)
  129. {
  130. auto m = CreateMap();
  131. m.erase("antique");
  132. EXPECT_EQ(m.ssize(), 3);
  133. m.erase("Who");
  134. EXPECT_EQ(m.ssize(), 3);
  135. m.erase(m.begin(), m.end());
  136. EXPECT_TRUE(m.empty());
  137. }
  138. TEST(TCompactFlatMapTest, GrowShrink)
  139. {
  140. TMap m;
  141. m.insert({"Two", "vast"});
  142. m.insert({"and", "trunkless"});
  143. m.insert({"legs", "of"});
  144. m.insert({"stone", "Stand"});
  145. m.insert({"in", "the"});
  146. m.insert({"desert", "..."});
  147. m.erase("legs");
  148. m.erase("stone");
  149. m.erase("in");
  150. m.erase("desert");
  151. EXPECT_EQ(m.ssize(), 2);
  152. // Must not crash or trigger asan.
  153. }
  154. TEST(TCompactFlatMapTest, GrowShrinkGrow)
  155. {
  156. TMap m;
  157. m.insert({"Two", "vast"});
  158. m.insert({"and", "trunkless"});
  159. m.insert({"legs", "of"});
  160. m.insert({"stone", "Stand"});
  161. m.insert({"in", "the"});
  162. m.insert({"desert", "..."});
  163. m.erase("legs");
  164. m.erase("stone");
  165. m.erase("in");
  166. m.erase("desert");
  167. EXPECT_EQ(m.ssize(), 2);
  168. m.insert({"I", "met"});
  169. m.insert({"a", "traveller"});
  170. m.insert({"from", "an"});
  171. m.insert({"antique", "land"});
  172. EXPECT_EQ(m.ssize(), 6);
  173. // Must not crash or trigger asan.
  174. }
  175. TEST(TCompactFlatMapTest, LowerBound)
  176. {
  177. TMap m;
  178. EXPECT_EQ(m.lower_bound("a"), m.end());
  179. m.emplace("b", "value");
  180. EXPECT_EQ(m.lower_bound("a")->second, "value");
  181. EXPECT_EQ(m.lower_bound("b")->second, "value");
  182. m.emplace("c", "value2");
  183. // Grows here.
  184. m.emplace("d", "value3");
  185. EXPECT_EQ(m.lower_bound("a")->second, "value");
  186. EXPECT_EQ(m.lower_bound("e"), m.end());
  187. }
  188. TEST(TCompactFlatMapTest, UpperBound)
  189. {
  190. using TKeyValue = std::pair<std::string, std::string>;
  191. TMap m;
  192. EXPECT_EQ(m.upper_bound("a"), m.end());
  193. m.emplace("b", "value");
  194. EXPECT_EQ(m.upper_bound("a")->second, "value");
  195. EXPECT_EQ(m.upper_bound("b"), m.end());
  196. m.emplace("c", "value2");
  197. // Grows here.
  198. m.emplace("d", "value3");
  199. EXPECT_EQ(*m.upper_bound("a"), TKeyValue("b", "value"));
  200. EXPECT_EQ(*m.upper_bound("b"), TKeyValue("c", "value2"));
  201. EXPECT_EQ(m.upper_bound("d"), m.end());
  202. }
  203. TEST(TCompactFlatMapTest, EqualRange)
  204. {
  205. TMap m;
  206. EXPECT_EQ(m.equal_range("a"), std::pair(m.end(), m.end()));
  207. m.emplace("b", "value-b");
  208. EXPECT_EQ(m.equal_range("a"), std::pair(m.begin(), m.begin()));
  209. EXPECT_EQ(m.equal_range("b"), std::pair(m.begin(), m.end()));
  210. EXPECT_EQ(m.equal_range("c"), std::pair(m.end(), m.end()));
  211. m.emplace("c", "value-c");
  212. m.emplace("d", "value-d");
  213. auto it = m.begin();
  214. EXPECT_EQ(m.equal_range("a"), std::pair(it, it));
  215. EXPECT_EQ(m.equal_range("b"), std::pair(it, std::next(it)));
  216. ++it;
  217. EXPECT_EQ(m.equal_range("c"), std::pair(it, std::next(it)));
  218. ++it;
  219. EXPECT_EQ(m.equal_range("d"), std::pair(it, std::next(it)));
  220. EXPECT_EQ(m.equal_range("e"), std::pair(m.end(), m.end()));
  221. }
  222. ////////////////////////////////////////////////////////////////////////////////
  223. } // namespace
  224. } // namespace NYT