map_test.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "common/map.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <initializer_list>
  8. #include <type_traits>
  9. #include <utility>
  10. #include <vector>
  11. #include "common/raw_hashtable_test_helpers.h"
  12. // Workaround for std::pair comparison deficiency in libc++ 16.
  13. #if defined(_LIBCPP_VERSION) && _LIBCPP_VERSION < 170000
  14. namespace std {
  15. template <typename T, typename U, typename V, typename W>
  16. requires(convertible_to<V, T> && convertible_to<W, U>)
  17. inline auto operator==(
  18. pair<std::reference_wrapper<T>, std::reference_wrapper<U>> lhs,
  19. pair<V, W> rhs) -> bool {
  20. return lhs.first == static_cast<T>(rhs.first) &&
  21. lhs.second == static_cast<U>(rhs.second);
  22. }
  23. } // namespace std
  24. #endif
  25. namespace Carbon::Testing {
  26. namespace {
  27. using RawHashtable::FixedHashKeyContext;
  28. using RawHashtable::IndexKeyContext;
  29. using RawHashtable::MoveOnlyTestData;
  30. using RawHashtable::TestData;
  31. using RawHashtable::TestKeyContext;
  32. using ::testing::Pair;
  33. using ::testing::UnorderedElementsAreArray;
  34. template <typename MapT, typename MatcherRangeT>
  35. auto ExpectMapElementsAre(MapT&& m, MatcherRangeT element_matchers) -> void {
  36. // Now collect the elements into a container.
  37. using KeyT = typename std::remove_reference<MapT>::type::KeyT;
  38. using ValueT = typename std::remove_reference<MapT>::type::ValueT;
  39. std::vector<
  40. std::pair<std::reference_wrapper<KeyT>, std::reference_wrapper<ValueT>>>
  41. map_entries;
  42. m.ForEach([&map_entries](KeyT& k, ValueT& v) {
  43. map_entries.push_back({std::ref(k), std::ref(v)});
  44. });
  45. // Use the GoogleMock unordered container matcher to validate and show errors
  46. // on wrong elements.
  47. EXPECT_THAT(map_entries, UnorderedElementsAreArray(element_matchers));
  48. }
  49. // Allow directly using an initializer list.
  50. template <typename MapT, typename MatcherT>
  51. auto ExpectMapElementsAre(MapT&& m,
  52. std::initializer_list<MatcherT> element_matchers)
  53. -> void {
  54. std::vector<MatcherT> element_matchers_storage = element_matchers;
  55. ExpectMapElementsAre(m, element_matchers_storage);
  56. }
  57. template <typename ValueCB, typename RangeT, typename... RangeTs>
  58. auto MakeKeyValues(ValueCB value_cb, RangeT&& range, RangeTs&&... ranges)
  59. -> auto {
  60. using KeyT = typename RangeT::value_type;
  61. using ValueT = decltype(value_cb(std::declval<KeyT>()));
  62. std::vector<std::pair<KeyT, ValueT>> elements;
  63. auto add_range = [&](RangeT&& r) {
  64. for (const auto&& e : r) {
  65. elements.push_back({e, value_cb(e)});
  66. }
  67. };
  68. add_range(std::forward<RangeT>(range));
  69. (add_range(std::forward<RangeTs>(ranges)), ...);
  70. return elements;
  71. }
  72. template <typename MapT>
  73. class MapTest : public ::testing::Test {};
  74. template <typename MapT>
  75. class MoveOnlyMapTest : public ::testing::Test {};
  76. using Types = ::testing::Types<
  77. Map<int, int>, Map<int, int, 16>, Map<int, int, 64>,
  78. Map<int, int, 0, TestKeyContext>, Map<int, int, 16, TestKeyContext>,
  79. Map<int, int, 64, TestKeyContext>, Map<TestData, TestData>,
  80. Map<TestData, TestData, 16>, Map<TestData, TestData, 0, TestKeyContext>,
  81. Map<TestData, TestData, 16, TestKeyContext>>;
  82. TYPED_TEST_SUITE(MapTest, Types);
  83. using MoveOnlyTypes = ::testing::Types<
  84. Map<MoveOnlyTestData, MoveOnlyTestData>,
  85. Map<MoveOnlyTestData, MoveOnlyTestData, 16>,
  86. Map<MoveOnlyTestData, MoveOnlyTestData, 64>,
  87. Map<MoveOnlyTestData, MoveOnlyTestData, 0, TestKeyContext>,
  88. Map<MoveOnlyTestData, MoveOnlyTestData, 16, TestKeyContext>>;
  89. TYPED_TEST_SUITE(MoveOnlyMapTest, MoveOnlyTypes);
  90. TYPED_TEST(MapTest, Basic) {
  91. TypeParam m;
  92. EXPECT_FALSE(m.Contains(42));
  93. EXPECT_EQ(nullptr, m[42]);
  94. EXPECT_TRUE(m.Insert(1, 100).is_inserted());
  95. ASSERT_TRUE(m.Contains(1));
  96. auto result = m.Lookup(1);
  97. EXPECT_TRUE(result);
  98. EXPECT_EQ(1, result.key());
  99. EXPECT_EQ(100, result.value());
  100. EXPECT_EQ(100, *m[1]);
  101. // Reinsertion doesn't change the value.
  102. auto i_result = m.Insert(1, 101);
  103. EXPECT_FALSE(i_result.is_inserted());
  104. EXPECT_EQ(100, i_result.value());
  105. EXPECT_EQ(100, *m[1]);
  106. // Update does change the value.
  107. i_result = m.Update(1, 101);
  108. EXPECT_FALSE(i_result.is_inserted());
  109. EXPECT_EQ(101, i_result.value());
  110. EXPECT_EQ(101, *m[1]);
  111. // Verify all the elements.
  112. ExpectMapElementsAre(m, {Pair(1, 101)});
  113. // Fill up a bunch to ensure we trigger growth a few times.
  114. for (int i : llvm::seq(2, 512)) {
  115. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  116. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  117. }
  118. ExpectMapElementsAre(
  119. m, MakeKeyValues([](int k) { return k * 100 + static_cast<int>(k == 1); },
  120. llvm::seq(1, 512)));
  121. for (int i : llvm::seq(1, 512)) {
  122. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  123. EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
  124. EXPECT_EQ(i * 100 + static_cast<int>(i == 1), *m[i]);
  125. EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
  126. EXPECT_EQ(i * 100 + 1, *m[i]);
  127. }
  128. EXPECT_FALSE(m.Contains(513));
  129. // Verify all the elements.
  130. ExpectMapElementsAre(
  131. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(1, 512)));
  132. }
  133. TYPED_TEST(MapTest, FactoryApi) {
  134. TypeParam m;
  135. EXPECT_TRUE(m.Insert(1, [] { return 100; }).is_inserted());
  136. ASSERT_TRUE(m.Contains(1));
  137. EXPECT_EQ(100, *m[1]);
  138. // Reinsertion doesn't invoke the callback.
  139. EXPECT_FALSE(m.Insert(1, []() -> int {
  140. llvm_unreachable("Should never be called!");
  141. }).is_inserted());
  142. // Update does invoke the callback.
  143. auto i_result = m.Update(1, [] { return 101; });
  144. EXPECT_FALSE(i_result.is_inserted());
  145. EXPECT_EQ(101, i_result.value());
  146. EXPECT_EQ(101, *m[1]);
  147. }
  148. TYPED_TEST(MapTest, Copy) {
  149. using MapT = TypeParam;
  150. MapT m;
  151. // Make sure we exceed the small size for some of the map types, but not all
  152. // of them, so we cover all the combinations of copying between small and
  153. // large.
  154. for (int i : llvm::seq(1, 24)) {
  155. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  156. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  157. }
  158. MapT other_m1 = m;
  159. ExpectMapElementsAre(
  160. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
  161. // Add some more elements to the original.
  162. for (int i : llvm::seq(24, 32)) {
  163. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  164. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  165. }
  166. // The first copy doesn't change.
  167. ExpectMapElementsAre(
  168. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
  169. // A new copy does.
  170. MapT other_m2 = m;
  171. ExpectMapElementsAre(
  172. other_m2, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  173. // Copy-assign updates.
  174. other_m1 = m;
  175. ExpectMapElementsAre(
  176. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  177. // Self-assign is a no-op.
  178. other_m1 = const_cast<const MapT&>(other_m1);
  179. ExpectMapElementsAre(
  180. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  181. // But mutating original still doesn't change copies.
  182. for (int i : llvm::seq(32, 48)) {
  183. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  184. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  185. }
  186. ExpectMapElementsAre(
  187. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  188. ExpectMapElementsAre(
  189. other_m2, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  190. }
  191. TYPED_TEST(MapTest, Move) {
  192. using MapT = TypeParam;
  193. MapT m;
  194. // Make sure we exceed the small size for some of the map types, but not all
  195. // of them, so we cover all the combinations of moving between small and
  196. // large.
  197. for (int i : llvm::seq(1, 24)) {
  198. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  199. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  200. }
  201. MapT other_m1 = std::move(m);
  202. ExpectMapElementsAre(
  203. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
  204. // Add some more elements.
  205. for (int i : llvm::seq(24, 32)) {
  206. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  207. ASSERT_TRUE(other_m1.Insert(i, i * 100).is_inserted());
  208. }
  209. ExpectMapElementsAre(
  210. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  211. // Move back over a moved-from.
  212. m = std::move(other_m1);
  213. ExpectMapElementsAre(
  214. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  215. // Copy over moved-from state also works.
  216. other_m1 = m;
  217. ExpectMapElementsAre(
  218. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  219. // Now add still more elements.
  220. for (int i : llvm::seq(32, 48)) {
  221. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  222. ASSERT_TRUE(other_m1.Insert(i, i * 100).is_inserted());
  223. }
  224. ExpectMapElementsAre(
  225. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 48)));
  226. // And move-assign over the copy looks like the moved-from table not the copy.
  227. other_m1 = std::move(m);
  228. ExpectMapElementsAre(
  229. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  230. // Self-swap (which does a self-move) works and is a no-op.
  231. std::swap(other_m1, other_m1);
  232. ExpectMapElementsAre(
  233. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  234. // Test copying of a moved-from table over a valid table and
  235. // self-move-assign. The former is required to be valid, and the latter is
  236. // in at least the case of self-move-assign-when-moved-from, but the result
  237. // can be in any state so just do them and ensure we don't crash.
  238. MapT other_m2 = other_m1;
  239. // NOLINTNEXTLINE(bugprone-use-after-move): Testing required use-after-move.
  240. other_m2 = m;
  241. other_m1 = std::move(other_m1);
  242. m = std::move(m);
  243. }
  244. TYPED_TEST(MoveOnlyMapTest, MoveOnlyTypes) {
  245. using MapT = TypeParam;
  246. static_assert(!std::is_copy_assignable_v<MapT>);
  247. static_assert(!std::is_copy_constructible_v<MapT>);
  248. static_assert(std::is_move_assignable_v<MapT>);
  249. static_assert(std::is_move_constructible_v<MapT>);
  250. auto make_map = [] {
  251. MapT m;
  252. // Make sure we exceed the small size for some of the map types, but not all
  253. // of them, so we cover all the combinations of moving between small and
  254. // large.
  255. for (int i : llvm::seq(1, 24)) {
  256. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  257. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  258. }
  259. return m;
  260. };
  261. MapT m = make_map();
  262. MapT other_m1 = std::move(m);
  263. ExpectMapElementsAre(
  264. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
  265. // Add some more elements.
  266. for (int i : llvm::seq(24, 32)) {
  267. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  268. ASSERT_TRUE(other_m1.Insert(i, i * 100).is_inserted());
  269. }
  270. ExpectMapElementsAre(
  271. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  272. // Move back over a moved-from.
  273. m = std::move(other_m1);
  274. ExpectMapElementsAre(
  275. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  276. // Now add still more elements, crossing the small size limit for all tested
  277. // map types.
  278. for (int i : llvm::seq(32, 72)) {
  279. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  280. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  281. }
  282. ExpectMapElementsAre(
  283. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 72)));
  284. // Assignment replaces the contents.
  285. m = make_map();
  286. ExpectMapElementsAre(
  287. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
  288. // Self-swap (which does a self-move) works and is a no-op.
  289. std::swap(m, m);
  290. ExpectMapElementsAre(
  291. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
  292. }
  293. TYPED_TEST(MapTest, Conversions) {
  294. using MapT = TypeParam;
  295. using KeyT = MapT::KeyT;
  296. using ValueT = MapT::ValueT;
  297. using KeyContextT = MapT::KeyContextT;
  298. MapT m;
  299. ASSERT_TRUE(m.Insert(1, 101).is_inserted());
  300. ASSERT_TRUE(m.Insert(2, 102).is_inserted());
  301. ASSERT_TRUE(m.Insert(3, 103).is_inserted());
  302. ASSERT_TRUE(m.Insert(4, 104).is_inserted());
  303. MapView<KeyT, ValueT, KeyContextT> mv = m;
  304. MapView<const KeyT, ValueT, KeyContextT> cmv = m;
  305. MapView<KeyT, const ValueT, KeyContextT> cmv2 = m;
  306. MapView<const KeyT, const ValueT, KeyContextT> cmv3 = m;
  307. EXPECT_TRUE(mv.Contains(1));
  308. EXPECT_EQ(101, *mv[1]);
  309. EXPECT_TRUE(cmv.Contains(2));
  310. EXPECT_EQ(102, *cmv[2]);
  311. EXPECT_TRUE(cmv2.Contains(3));
  312. EXPECT_EQ(103, *cmv2[3]);
  313. EXPECT_TRUE(cmv3.Contains(4));
  314. EXPECT_EQ(104, *cmv3[4]);
  315. }
  316. TYPED_TEST(MapTest, GrowToAllocSize) {
  317. using MapT = TypeParam;
  318. MapT m;
  319. // Grow when empty. May be a no-op for some small sizes.
  320. m.GrowToAllocSize(32);
  321. // Add some elements that will need to be propagated through subsequent
  322. // growths. Also delete some.
  323. ssize_t storage_bytes = m.ComputeMetrics().storage_bytes;
  324. for (int i : llvm::seq(1, 24)) {
  325. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  326. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  327. }
  328. for (int i : llvm::seq(1, 8)) {
  329. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  330. ASSERT_TRUE(m.Erase(i));
  331. }
  332. // No further growth triggered.
  333. EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
  334. // No-op.
  335. m.GrowToAllocSize(16);
  336. ExpectMapElementsAre(
  337. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(8, 24)));
  338. // No further growth triggered.
  339. EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
  340. // Get a few doubling based growths, and at least one beyond the largest small
  341. // size.
  342. m.GrowToAllocSize(64);
  343. ExpectMapElementsAre(
  344. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(8, 24)));
  345. m.GrowToAllocSize(128);
  346. ExpectMapElementsAre(
  347. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(8, 24)));
  348. // Update the storage bytes after growth.
  349. EXPECT_LT(storage_bytes, m.ComputeMetrics().storage_bytes);
  350. storage_bytes = m.ComputeMetrics().storage_bytes;
  351. // Add some more, but not enough to trigger further growth, and then grow by
  352. // several more multiples of two to test handling large growth.
  353. for (int i : llvm::seq(24, 48)) {
  354. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  355. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  356. }
  357. for (int i : llvm::seq(8, 16)) {
  358. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  359. ASSERT_TRUE(m.Erase(i));
  360. }
  361. // No growth from insertions.
  362. EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
  363. m.GrowToAllocSize(1024);
  364. ExpectMapElementsAre(
  365. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(16, 48)));
  366. // Storage should have grown.
  367. EXPECT_LT(storage_bytes, m.ComputeMetrics().storage_bytes);
  368. }
  369. TYPED_TEST(MapTest, GrowForInsert) {
  370. using MapT = TypeParam;
  371. MapT m;
  372. m.GrowForInsertCount(42);
  373. ssize_t storage_bytes = m.ComputeMetrics().storage_bytes;
  374. for (int i : llvm::seq(1, 42)) {
  375. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  376. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  377. }
  378. ExpectMapElementsAre(
  379. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 42)));
  380. EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
  381. // Erase many elements and grow again for another insert.
  382. for (int i : llvm::seq(1, 32)) {
  383. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  384. ASSERT_TRUE(m.Erase(i));
  385. }
  386. m.GrowForInsertCount(42);
  387. storage_bytes = m.ComputeMetrics().storage_bytes;
  388. for (int i : llvm::seq(42, 84)) {
  389. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  390. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  391. }
  392. ExpectMapElementsAre(
  393. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(32, 84)));
  394. EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
  395. // Erase all the elements, then grow for a much larger insertion and insert
  396. // again.
  397. for (int i : llvm::seq(32, 84)) {
  398. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  399. ASSERT_TRUE(m.Erase(i));
  400. }
  401. m.GrowForInsertCount(321);
  402. storage_bytes = m.ComputeMetrics().storage_bytes;
  403. for (int i : llvm::seq(128, 321 + 128)) {
  404. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  405. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  406. }
  407. ExpectMapElementsAre(m, MakeKeyValues([](int k) { return k * 100; },
  408. llvm::seq(128, 321 + 128)));
  409. EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
  410. }
  411. // This test is largely exercising the underlying `RawHashtable` implementation
  412. // with complex growth, erasure, and re-growth.
  413. TYPED_TEST(MapTest, ComplexOpSequence) {
  414. // Use a small size as well to cover more growth scenarios.
  415. TypeParam m;
  416. EXPECT_FALSE(m.Contains(42));
  417. EXPECT_EQ(nullptr, m[42]);
  418. EXPECT_TRUE(m.Insert(1, 100).is_inserted());
  419. ASSERT_TRUE(m.Contains(1));
  420. auto result = m.Lookup(1);
  421. EXPECT_TRUE(result);
  422. EXPECT_EQ(1, result.key());
  423. EXPECT_EQ(100, result.value());
  424. EXPECT_EQ(100, *m[1]);
  425. // Reinsertion doesn't change the value.
  426. auto i_result = m.Insert(1, 101);
  427. EXPECT_FALSE(i_result.is_inserted());
  428. EXPECT_EQ(100, i_result.value());
  429. EXPECT_EQ(100, *m[1]);
  430. // Update does change the value.
  431. i_result = m.Update(1, 101);
  432. EXPECT_FALSE(i_result.is_inserted());
  433. EXPECT_EQ(101, i_result.value());
  434. EXPECT_EQ(101, *m[1]);
  435. // Verify all the elements.
  436. ExpectMapElementsAre(m, {Pair(1, 101)});
  437. // Fill up the small buffer but don't overflow it.
  438. for (int i : llvm::seq(2, 5)) {
  439. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  440. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  441. }
  442. for (int i : llvm::seq(1, 5)) {
  443. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  444. ASSERT_TRUE(m.Contains(i));
  445. EXPECT_EQ(i * 100 + static_cast<int>(i == 1), *m[i]);
  446. EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
  447. EXPECT_EQ(i * 100 + static_cast<int>(i == 1), *m[i]);
  448. EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
  449. EXPECT_EQ(i * 100 + 1, *m[i]);
  450. }
  451. EXPECT_FALSE(m.Contains(5));
  452. // Verify all the elements.
  453. ExpectMapElementsAre(
  454. m, {Pair(1, 101), Pair(2, 201), Pair(3, 301), Pair(4, 401)});
  455. // Erase some entries from the small buffer.
  456. EXPECT_FALSE(m.Erase(42));
  457. EXPECT_TRUE(m.Erase(2));
  458. EXPECT_EQ(101, *m[1]);
  459. EXPECT_EQ(nullptr, m[2]);
  460. EXPECT_EQ(301, *m[3]);
  461. EXPECT_EQ(401, *m[4]);
  462. EXPECT_TRUE(m.Erase(1));
  463. EXPECT_EQ(nullptr, m[1]);
  464. EXPECT_EQ(nullptr, m[2]);
  465. EXPECT_EQ(301, *m[3]);
  466. EXPECT_EQ(401, *m[4]);
  467. EXPECT_TRUE(m.Erase(4));
  468. EXPECT_EQ(nullptr, m[1]);
  469. EXPECT_EQ(nullptr, m[2]);
  470. EXPECT_EQ(301, *m[3]);
  471. EXPECT_EQ(nullptr, m[4]);
  472. // Fill them back in, but with a different order and going back to the
  473. // original value.
  474. EXPECT_TRUE(m.Insert(1, 100).is_inserted());
  475. EXPECT_TRUE(m.Insert(2, 200).is_inserted());
  476. EXPECT_TRUE(m.Insert(4, 400).is_inserted());
  477. EXPECT_EQ(100, *m[1]);
  478. EXPECT_EQ(200, *m[2]);
  479. EXPECT_EQ(301, *m[3]);
  480. EXPECT_EQ(400, *m[4]);
  481. // Then update their values to match.
  482. EXPECT_FALSE(m.Update(1, 101).is_inserted());
  483. EXPECT_FALSE(m.Update(2, 201).is_inserted());
  484. EXPECT_FALSE(m.Update(4, 401).is_inserted());
  485. // Now fill up the first metadata group.
  486. for (int i : llvm::seq(5, 14)) {
  487. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  488. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  489. }
  490. for (int i : llvm::seq(1, 14)) {
  491. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  492. ASSERT_TRUE(m.Contains(i));
  493. EXPECT_EQ(i * 100 + static_cast<int>(i < 5), *m[i]);
  494. EXPECT_FALSE(m.Insert(i, i * 100 + 2).is_inserted());
  495. EXPECT_EQ(i * 100 + static_cast<int>(i < 5), *m[i]);
  496. EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
  497. EXPECT_EQ(i * 100 + 2, *m[i]);
  498. }
  499. EXPECT_FALSE(m.Contains(42));
  500. // Verify all the elements by walking the entire map.
  501. ExpectMapElementsAre(
  502. m, {Pair(1, 102), Pair(2, 202), Pair(3, 302), Pair(4, 402), Pair(5, 502),
  503. Pair(6, 602), Pair(7, 702), Pair(8, 802), Pair(9, 902),
  504. Pair(10, 1002), Pair(11, 1102), Pair(12, 1202), Pair(13, 1302)});
  505. // Now fill up several more groups.
  506. for (int i : llvm::seq(14, 100)) {
  507. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  508. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  509. }
  510. for (int i : llvm::seq(1, 100)) {
  511. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  512. ASSERT_TRUE(m.Contains(i));
  513. EXPECT_EQ(i * 100 + 2 * static_cast<int>(i < 14), *m[i]);
  514. EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
  515. EXPECT_EQ(i * 100 + 2 * static_cast<int>(i < 14), *m[i]);
  516. EXPECT_FALSE(m.Update(i, i * 100 + 3).is_inserted());
  517. EXPECT_EQ(i * 100 + 3, *m[i]);
  518. }
  519. EXPECT_FALSE(m.Contains(420));
  520. // Check walking the entire container.
  521. ExpectMapElementsAre(
  522. m, MakeKeyValues([](int k) { return k * 100 + 3; }, llvm::seq(1, 100)));
  523. // Clear back to empty.
  524. m.Clear();
  525. EXPECT_FALSE(m.Contains(42));
  526. EXPECT_EQ(nullptr, m[42]);
  527. // Refill but with both overlapping and different values.
  528. for (int i : llvm::seq(50, 150)) {
  529. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  530. }
  531. for (int i : llvm::seq(50, 150)) {
  532. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  533. ASSERT_TRUE(m.Contains(i));
  534. EXPECT_EQ(i * 100, *m[i]);
  535. EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
  536. EXPECT_EQ(i * 100, *m[i]);
  537. EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
  538. EXPECT_EQ(i * 100 + 1, *m[i]);
  539. }
  540. EXPECT_FALSE(m.Contains(42));
  541. EXPECT_FALSE(m.Contains(420));
  542. ExpectMapElementsAre(
  543. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(50, 150)));
  544. EXPECT_FALSE(m.Erase(42));
  545. EXPECT_TRUE(m.Contains(73));
  546. EXPECT_TRUE(m.Erase(73));
  547. EXPECT_FALSE(m.Contains(73));
  548. for (int i : llvm::seq(102, 136)) {
  549. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  550. EXPECT_TRUE(m.Contains(i));
  551. EXPECT_TRUE(m.Erase(i));
  552. EXPECT_FALSE(m.Contains(i));
  553. }
  554. for (int i : llvm::seq(50, 150)) {
  555. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  556. if (i == 73 || (i >= 102 && i < 136)) {
  557. continue;
  558. }
  559. ASSERT_TRUE(m.Contains(i));
  560. EXPECT_EQ(i * 100 + 1, *m[i]);
  561. EXPECT_FALSE(m.Insert(i, i * 100 + 2).is_inserted());
  562. EXPECT_EQ(i * 100 + 1, *m[i]);
  563. EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
  564. EXPECT_EQ(i * 100 + 2, *m[i]);
  565. }
  566. EXPECT_TRUE(m.Insert(73, 73 * 100 + 3).is_inserted());
  567. EXPECT_EQ(73 * 100 + 3, *m[73]);
  568. ExpectMapElementsAre(
  569. m, MakeKeyValues([](int k) { return k * 100 + 2 + (k == 73); },
  570. llvm::seq(50, 102), llvm::seq(136, 150)));
  571. // Reset back to empty and small.
  572. m.Reset();
  573. EXPECT_FALSE(m.Contains(42));
  574. EXPECT_EQ(nullptr, m[42]);
  575. // Refill but with both overlapping and different values, now triggering
  576. // growth too. Also, use update instead of insert.
  577. for (int i : llvm::seq(75, 175)) {
  578. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  579. EXPECT_TRUE(m.Update(i, i * 100).is_inserted());
  580. }
  581. for (int i : llvm::seq(75, 175)) {
  582. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  583. ASSERT_TRUE(m.Contains(i));
  584. EXPECT_EQ(i * 100, *m[i]);
  585. EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
  586. EXPECT_EQ(i * 100, *m[i]);
  587. EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
  588. EXPECT_EQ(i * 100 + 1, *m[i]);
  589. }
  590. EXPECT_FALSE(m.Contains(42));
  591. EXPECT_FALSE(m.Contains(420));
  592. ExpectMapElementsAre(
  593. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(75, 175)));
  594. EXPECT_FALSE(m.Erase(42));
  595. EXPECT_TRUE(m.Contains(93));
  596. EXPECT_TRUE(m.Erase(93));
  597. EXPECT_FALSE(m.Contains(93));
  598. for (int i : llvm::seq(102, 136)) {
  599. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  600. EXPECT_TRUE(m.Contains(i));
  601. EXPECT_TRUE(m.Erase(i));
  602. EXPECT_FALSE(m.Contains(i));
  603. }
  604. for (int i : llvm::seq(75, 175)) {
  605. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  606. if (i == 93 || (i >= 102 && i < 136)) {
  607. continue;
  608. }
  609. ASSERT_TRUE(m.Contains(i));
  610. EXPECT_EQ(i * 100 + 1, *m[i]);
  611. EXPECT_FALSE(m.Insert(i, i * 100 + 2).is_inserted());
  612. EXPECT_EQ(i * 100 + 1, *m[i]);
  613. EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
  614. EXPECT_EQ(i * 100 + 2, *m[i]);
  615. }
  616. EXPECT_TRUE(m.Insert(93, 93 * 100 + 3).is_inserted());
  617. EXPECT_EQ(93 * 100 + 3, *m[93]);
  618. ExpectMapElementsAre(
  619. m, MakeKeyValues([](int k) { return k * 100 + 2 + (k == 93); },
  620. llvm::seq(75, 102), llvm::seq(136, 175)));
  621. }
  622. template <typename MapT>
  623. class MapCollisionTest : public ::testing::Test {};
  624. using CollisionTypes = ::testing::Types<
  625. Map<int, int, 16,
  626. FixedHashKeyContext<7, /*FixIndexBits*/ true, /*FixTagBits*/ false, 0>>,
  627. Map<int, int, 16,
  628. FixedHashKeyContext<7, /*FixIndexBits*/ false, /*FixTagBits*/ true, 0>>,
  629. Map<int, int, 16,
  630. FixedHashKeyContext<7, /*FixIndexBits*/ true, /*FixTagBits*/ true, 0>>,
  631. Map<int, int, 16,
  632. FixedHashKeyContext<7, /*FixIndexBits*/ true, /*FixTagBits*/ true,
  633. ~static_cast<uint64_t>(0)>>>;
  634. TYPED_TEST_SUITE(MapCollisionTest, CollisionTypes);
  635. TYPED_TEST(MapCollisionTest, Basic) {
  636. TypeParam m;
  637. // Fill the map through a couple of growth steps, verifying at each step. Note
  638. // that because this is a collision test, we synthesize actively harmful
  639. // hashes in terms of collisions and so this test is essentially quadratic. We
  640. // need to keep it relatively small.
  641. for (int i : llvm::seq(1, 256)) {
  642. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  643. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  644. }
  645. ExpectMapElementsAre(
  646. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 256)));
  647. // Erase and re-fill from the back.
  648. for (int i : llvm::seq(192, 256)) {
  649. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  650. EXPECT_TRUE(m.Erase(i));
  651. }
  652. ExpectMapElementsAre(
  653. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 192)));
  654. for (int i : llvm::seq(192, 256)) {
  655. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  656. EXPECT_TRUE(m.Insert(i, i * 100 + 1).is_inserted());
  657. }
  658. ExpectMapElementsAre(m,
  659. MakeKeyValues([](int k) { return k * 100 + (k >= 192); },
  660. llvm::seq(1, 256)));
  661. // Erase and re-fill from the front.
  662. for (int i : llvm::seq(1, 64)) {
  663. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  664. EXPECT_TRUE(m.Erase(i));
  665. }
  666. ExpectMapElementsAre(m,
  667. MakeKeyValues([](int k) { return k * 100 + (k >= 192); },
  668. llvm::seq(64, 256)));
  669. for (int i : llvm::seq(1, 64)) {
  670. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  671. EXPECT_TRUE(m.Insert(i, i * 100 + 1).is_inserted());
  672. }
  673. ExpectMapElementsAre(
  674. m, MakeKeyValues([](int k) { return k * 100 + (k < 64) + (k >= 192); },
  675. llvm::seq(1, 256)));
  676. // Erase and re-fill from the middle.
  677. for (int i : llvm::seq(64, 192)) {
  678. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  679. EXPECT_TRUE(m.Erase(i));
  680. }
  681. ExpectMapElementsAre(m, MakeKeyValues([](int k) { return k * 100 + 1; },
  682. llvm::seq(1, 64), llvm::seq(192, 256)));
  683. for (int i : llvm::seq(64, 192)) {
  684. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  685. EXPECT_TRUE(m.Insert(i, i * 100 + 1).is_inserted());
  686. }
  687. ExpectMapElementsAre(
  688. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(1, 256)));
  689. // Erase and re-fill from both the back and front.
  690. for (auto s : {llvm::seq(192, 256), llvm::seq(1, 64)}) {
  691. for (int i : s) {
  692. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  693. EXPECT_TRUE(m.Erase(i));
  694. }
  695. }
  696. ExpectMapElementsAre(
  697. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(64, 192)));
  698. for (auto s : {llvm::seq(192, 256), llvm::seq(1, 64)}) {
  699. for (int i : s) {
  700. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  701. EXPECT_TRUE(m.Insert(i, i * 100 + 2).is_inserted());
  702. }
  703. }
  704. ExpectMapElementsAre(
  705. m,
  706. MakeKeyValues([](int k) { return k * 100 + 1 + (k < 64) + (k >= 192); },
  707. llvm::seq(1, 256)));
  708. // And update the middle elements in place.
  709. for (int i : llvm::seq(64, 192)) {
  710. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  711. EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
  712. }
  713. ExpectMapElementsAre(
  714. m, MakeKeyValues([](int k) { return k * 100 + 2; }, llvm::seq(1, 256)));
  715. }
  716. TEST(MapContextTest, Basic) {
  717. llvm::SmallVector<TestData> keys;
  718. for (int i : llvm::seq(0, 513)) {
  719. keys.push_back(i * 100000);
  720. }
  721. IndexKeyContext<TestData> key_context(keys);
  722. Map<ssize_t, int, 0, IndexKeyContext<TestData>> m;
  723. EXPECT_FALSE(m.Contains(42, key_context));
  724. EXPECT_TRUE(m.Insert(1, 100, key_context).is_inserted());
  725. ASSERT_TRUE(m.Contains(1, key_context));
  726. auto result = m.Lookup(TestData(100000), key_context);
  727. EXPECT_TRUE(result);
  728. EXPECT_EQ(1, result.key());
  729. EXPECT_EQ(100, result.value());
  730. // Reinsertion doesn't change the value. Also, double check a temporary
  731. // context.
  732. auto i_result = m.Insert(1, 101, IndexKeyContext<TestData>(keys));
  733. EXPECT_FALSE(i_result.is_inserted());
  734. EXPECT_EQ(100, i_result.value());
  735. // Update does change the value.
  736. i_result = m.Update(1, 101, key_context);
  737. EXPECT_FALSE(i_result.is_inserted());
  738. EXPECT_EQ(101, i_result.value());
  739. // Verify all the elements.
  740. ExpectMapElementsAre(m, {Pair(1, 101)});
  741. // Fill up a bunch to ensure we trigger growth a few times.
  742. for (int i : llvm::seq(2, 512)) {
  743. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  744. EXPECT_TRUE(m.Insert(i, i * 100, key_context).is_inserted());
  745. }
  746. // Check all the elements, including using the context.
  747. for (int j : llvm::seq(1, 512)) {
  748. SCOPED_TRACE(llvm::formatv("Assert key: {0}", j).str());
  749. ASSERT_EQ(j * 100 + static_cast<int>(j == 1),
  750. m.Lookup(j, key_context).value());
  751. ASSERT_EQ(j * 100 + static_cast<int>(j == 1),
  752. m.Lookup(TestData(j * 100000), key_context).value());
  753. }
  754. for (int i : llvm::seq(1, 512)) {
  755. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  756. EXPECT_FALSE(m.Insert(i, i * 100 + 1, key_context).is_inserted());
  757. EXPECT_EQ(i * 100 + static_cast<int>(i == 1),
  758. m.Lookup(i, key_context).value());
  759. EXPECT_FALSE(m.Update(i, i * 100 + 1, key_context).is_inserted());
  760. EXPECT_EQ(i * 100 + 1, m.Lookup(i, key_context).value());
  761. }
  762. EXPECT_FALSE(m.Contains(0, key_context));
  763. EXPECT_FALSE(m.Contains(512, key_context));
  764. // Verify all the elements.
  765. ExpectMapElementsAre(
  766. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(1, 512)));
  767. }
  768. } // namespace
  769. } // namespace Carbon::Testing