map_test.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. namespace Carbon::Testing {
  13. namespace {
  14. using RawHashtable::FixedHashKeyContext;
  15. using RawHashtable::IndexKeyContext;
  16. using RawHashtable::TestData;
  17. using RawHashtable::TestKeyContext;
  18. using ::testing::Pair;
  19. using ::testing::UnorderedElementsAreArray;
  20. template <typename MapT, typename MatcherRangeT>
  21. void ExpectMapElementsAre(MapT&& m, MatcherRangeT element_matchers) {
  22. // Now collect the elements into a container.
  23. using KeyT = typename std::remove_reference<MapT>::type::KeyT;
  24. using ValueT = typename std::remove_reference<MapT>::type::ValueT;
  25. std::vector<std::pair<KeyT, ValueT>> map_entries;
  26. m.ForEach([&map_entries](KeyT& k, ValueT& v) {
  27. map_entries.push_back({k, v});
  28. });
  29. // Use the GoogleMock unordered container matcher to validate and show errors
  30. // on wrong elements.
  31. EXPECT_THAT(map_entries, UnorderedElementsAreArray(element_matchers));
  32. }
  33. // Allow directly using an initializer list.
  34. template <typename MapT, typename MatcherT>
  35. void ExpectMapElementsAre(MapT&& m,
  36. std::initializer_list<MatcherT> element_matchers) {
  37. std::vector<MatcherT> element_matchers_storage = element_matchers;
  38. ExpectMapElementsAre(m, element_matchers_storage);
  39. }
  40. template <typename ValueCB, typename RangeT, typename... RangeTs>
  41. auto MakeKeyValues(ValueCB value_cb, RangeT&& range, RangeTs&&... ranges) {
  42. using KeyT = typename RangeT::value_type;
  43. using ValueT = decltype(value_cb(std::declval<KeyT>()));
  44. std::vector<std::pair<KeyT, ValueT>> elements;
  45. auto add_range = [&](RangeT&& r) {
  46. for (const auto&& e : r) {
  47. elements.push_back({e, value_cb(e)});
  48. }
  49. };
  50. add_range(std::forward<RangeT>(range));
  51. (add_range(std::forward<RangeT>(ranges)), ...);
  52. return elements;
  53. }
  54. template <typename MapT>
  55. class MapTest : public ::testing::Test {};
  56. using Types = ::testing::Types<
  57. Map<int, int>, Map<int, int, 16>, Map<int, int, 64>,
  58. Map<int, int, 0, TestKeyContext>, Map<int, int, 16, TestKeyContext>,
  59. Map<int, int, 64, TestKeyContext>, Map<TestData, TestData>,
  60. Map<TestData, TestData, 16>, Map<TestData, TestData, 0, TestKeyContext>,
  61. Map<TestData, TestData, 16, TestKeyContext>>;
  62. TYPED_TEST_SUITE(MapTest, Types);
  63. TYPED_TEST(MapTest, Basic) {
  64. TypeParam m;
  65. EXPECT_FALSE(m.Contains(42));
  66. EXPECT_EQ(nullptr, m[42]);
  67. EXPECT_TRUE(m.Insert(1, 100).is_inserted());
  68. ASSERT_TRUE(m.Contains(1));
  69. auto result = m.Lookup(1);
  70. EXPECT_TRUE(result);
  71. EXPECT_EQ(1, result.key());
  72. EXPECT_EQ(100, result.value());
  73. EXPECT_EQ(100, *m[1]);
  74. // Reinsertion doesn't change the value.
  75. auto i_result = m.Insert(1, 101);
  76. EXPECT_FALSE(i_result.is_inserted());
  77. EXPECT_EQ(100, i_result.value());
  78. EXPECT_EQ(100, *m[1]);
  79. // Update does change the value.
  80. i_result = m.Update(1, 101);
  81. EXPECT_FALSE(i_result.is_inserted());
  82. EXPECT_EQ(101, i_result.value());
  83. EXPECT_EQ(101, *m[1]);
  84. // Verify all the elements.
  85. ExpectMapElementsAre(m, {Pair(1, 101)});
  86. // Fill up a bunch to ensure we trigger growth a few times.
  87. for (int i : llvm::seq(2, 512)) {
  88. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  89. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  90. // Immediately do a basic check of all elements to pin down when an
  91. // insertion corrupts the rest of the table.
  92. ExpectMapElementsAre(
  93. m,
  94. MakeKeyValues([](int k) { return k * 100 + static_cast<int>(k == 1); },
  95. llvm::seq_inclusive(1, i)));
  96. }
  97. for (int i : llvm::seq(1, 512)) {
  98. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  99. EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
  100. EXPECT_EQ(i * 100 + static_cast<int>(i == 1), *m[i]);
  101. EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
  102. EXPECT_EQ(i * 100 + 1, *m[i]);
  103. }
  104. EXPECT_FALSE(m.Contains(513));
  105. // Verify all the elements.
  106. ExpectMapElementsAre(
  107. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(1, 512)));
  108. }
  109. TYPED_TEST(MapTest, FactoryAPI) {
  110. TypeParam m;
  111. EXPECT_TRUE(m.Insert(1, [] { return 100; }).is_inserted());
  112. ASSERT_TRUE(m.Contains(1));
  113. EXPECT_EQ(100, *m[1]);
  114. // Reinsertion doesn't invoke the callback.
  115. EXPECT_FALSE(m.Insert(1, []() -> int {
  116. llvm_unreachable("Should never be called!");
  117. }).is_inserted());
  118. // Update does invoke the callback.
  119. auto i_result = m.Update(1, [] { return 101; });
  120. EXPECT_FALSE(i_result.is_inserted());
  121. EXPECT_EQ(101, i_result.value());
  122. EXPECT_EQ(101, *m[1]);
  123. }
  124. TYPED_TEST(MapTest, Copy) {
  125. using MapT = TypeParam;
  126. MapT m;
  127. // Make sure we exceed the small size for some of the map types, but not all
  128. // of them, so we cover all the combinations of copying between small and
  129. // large.
  130. for (int i : llvm::seq(1, 24)) {
  131. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  132. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  133. }
  134. MapT other_m1 = m;
  135. ExpectMapElementsAre(
  136. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
  137. // Add some more elements to the original.
  138. for (int i : llvm::seq(24, 32)) {
  139. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  140. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  141. }
  142. // The first copy doesn't change.
  143. ExpectMapElementsAre(
  144. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
  145. // A new copy does.
  146. MapT other_m2 = m;
  147. ExpectMapElementsAre(
  148. other_m2, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  149. }
  150. TYPED_TEST(MapTest, Move) {
  151. using MapT = TypeParam;
  152. MapT m;
  153. // Make sure we exceed the small size for some of the map types, but not all
  154. // of them, so we cover all the combinations of moving between small and
  155. // large.
  156. for (int i : llvm::seq(1, 24)) {
  157. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  158. ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
  159. }
  160. MapT other_m1 = std::move(m);
  161. ExpectMapElementsAre(
  162. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
  163. // Add some more elements.
  164. for (int i : llvm::seq(24, 32)) {
  165. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  166. ASSERT_TRUE(other_m1.Insert(i, i * 100).is_inserted());
  167. }
  168. ExpectMapElementsAre(
  169. other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
  170. }
  171. TYPED_TEST(MapTest, Conversions) {
  172. using MapT = TypeParam;
  173. using KeyT = MapT::KeyT;
  174. using ValueT = MapT::ValueT;
  175. using KeyContextT = MapT::KeyContextT;
  176. MapT m;
  177. ASSERT_TRUE(m.Insert(1, 101).is_inserted());
  178. ASSERT_TRUE(m.Insert(2, 102).is_inserted());
  179. ASSERT_TRUE(m.Insert(3, 103).is_inserted());
  180. ASSERT_TRUE(m.Insert(4, 104).is_inserted());
  181. MapView<KeyT, ValueT, KeyContextT> mv = m;
  182. MapView<const KeyT, ValueT, KeyContextT> cmv = m;
  183. MapView<KeyT, const ValueT, KeyContextT> cmv2 = m;
  184. MapView<const KeyT, const ValueT, KeyContextT> cmv3 = m;
  185. EXPECT_TRUE(mv.Contains(1));
  186. EXPECT_EQ(101, *mv[1]);
  187. EXPECT_TRUE(cmv.Contains(2));
  188. EXPECT_EQ(102, *cmv[2]);
  189. EXPECT_TRUE(cmv2.Contains(3));
  190. EXPECT_EQ(103, *cmv2[3]);
  191. EXPECT_TRUE(cmv3.Contains(4));
  192. EXPECT_EQ(104, *cmv3[4]);
  193. }
  194. // This test is largely exercising the underlying `RawHashtable` implementation
  195. // with complex growth, erasure, and re-growth.
  196. TYPED_TEST(MapTest, ComplexOpSequence) {
  197. // Use a small size as well to cover more growth scenarios.
  198. TypeParam m;
  199. EXPECT_FALSE(m.Contains(42));
  200. EXPECT_EQ(nullptr, m[42]);
  201. EXPECT_TRUE(m.Insert(1, 100).is_inserted());
  202. ASSERT_TRUE(m.Contains(1));
  203. auto result = m.Lookup(1);
  204. EXPECT_TRUE(result);
  205. EXPECT_EQ(1, result.key());
  206. EXPECT_EQ(100, result.value());
  207. EXPECT_EQ(100, *m[1]);
  208. // Reinsertion doesn't change the value.
  209. auto i_result = m.Insert(1, 101);
  210. EXPECT_FALSE(i_result.is_inserted());
  211. EXPECT_EQ(100, i_result.value());
  212. EXPECT_EQ(100, *m[1]);
  213. // Update does change the value.
  214. i_result = m.Update(1, 101);
  215. EXPECT_FALSE(i_result.is_inserted());
  216. EXPECT_EQ(101, i_result.value());
  217. EXPECT_EQ(101, *m[1]);
  218. // Verify all the elements.
  219. ExpectMapElementsAre(m, {Pair(1, 101)});
  220. // Fill up the small buffer but don't overflow it.
  221. for (int i : llvm::seq(2, 5)) {
  222. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  223. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  224. }
  225. for (int i : llvm::seq(1, 5)) {
  226. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  227. ASSERT_TRUE(m.Contains(i));
  228. EXPECT_EQ(i * 100 + static_cast<int>(i == 1), *m[i]);
  229. EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
  230. EXPECT_EQ(i * 100 + static_cast<int>(i == 1), *m[i]);
  231. EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
  232. EXPECT_EQ(i * 100 + 1, *m[i]);
  233. }
  234. EXPECT_FALSE(m.Contains(5));
  235. // Verify all the elements.
  236. ExpectMapElementsAre(
  237. m, {Pair(1, 101), Pair(2, 201), Pair(3, 301), Pair(4, 401)});
  238. // Erase some entries from the small buffer.
  239. EXPECT_FALSE(m.Erase(42));
  240. EXPECT_TRUE(m.Erase(2));
  241. EXPECT_EQ(101, *m[1]);
  242. EXPECT_EQ(nullptr, m[2]);
  243. EXPECT_EQ(301, *m[3]);
  244. EXPECT_EQ(401, *m[4]);
  245. EXPECT_TRUE(m.Erase(1));
  246. EXPECT_EQ(nullptr, m[1]);
  247. EXPECT_EQ(nullptr, m[2]);
  248. EXPECT_EQ(301, *m[3]);
  249. EXPECT_EQ(401, *m[4]);
  250. EXPECT_TRUE(m.Erase(4));
  251. EXPECT_EQ(nullptr, m[1]);
  252. EXPECT_EQ(nullptr, m[2]);
  253. EXPECT_EQ(301, *m[3]);
  254. EXPECT_EQ(nullptr, m[4]);
  255. // Fill them back in, but with a different order and going back to the
  256. // original value.
  257. EXPECT_TRUE(m.Insert(1, 100).is_inserted());
  258. EXPECT_TRUE(m.Insert(2, 200).is_inserted());
  259. EXPECT_TRUE(m.Insert(4, 400).is_inserted());
  260. EXPECT_EQ(100, *m[1]);
  261. EXPECT_EQ(200, *m[2]);
  262. EXPECT_EQ(301, *m[3]);
  263. EXPECT_EQ(400, *m[4]);
  264. // Then update their values to match.
  265. EXPECT_FALSE(m.Update(1, 101).is_inserted());
  266. EXPECT_FALSE(m.Update(2, 201).is_inserted());
  267. EXPECT_FALSE(m.Update(4, 401).is_inserted());
  268. // Now fill up the first metadata group.
  269. for (int i : llvm::seq(5, 14)) {
  270. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  271. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  272. }
  273. for (int i : llvm::seq(1, 14)) {
  274. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  275. ASSERT_TRUE(m.Contains(i));
  276. EXPECT_EQ(i * 100 + static_cast<int>(i < 5), *m[i]);
  277. EXPECT_FALSE(m.Insert(i, i * 100 + 2).is_inserted());
  278. EXPECT_EQ(i * 100 + static_cast<int>(i < 5), *m[i]);
  279. EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
  280. EXPECT_EQ(i * 100 + 2, *m[i]);
  281. }
  282. EXPECT_FALSE(m.Contains(42));
  283. // Verify all the elements by walking the entire map.
  284. ExpectMapElementsAre(
  285. m, {Pair(1, 102), Pair(2, 202), Pair(3, 302), Pair(4, 402), Pair(5, 502),
  286. Pair(6, 602), Pair(7, 702), Pair(8, 802), Pair(9, 902),
  287. Pair(10, 1002), Pair(11, 1102), Pair(12, 1202), Pair(13, 1302)});
  288. // Now fill up several more groups.
  289. for (int i : llvm::seq(14, 100)) {
  290. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  291. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  292. }
  293. for (int i : llvm::seq(1, 100)) {
  294. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  295. ASSERT_TRUE(m.Contains(i));
  296. EXPECT_EQ(i * 100 + 2 * static_cast<int>(i < 14), *m[i]);
  297. EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
  298. EXPECT_EQ(i * 100 + 2 * static_cast<int>(i < 14), *m[i]);
  299. EXPECT_FALSE(m.Update(i, i * 100 + 3).is_inserted());
  300. EXPECT_EQ(i * 100 + 3, *m[i]);
  301. }
  302. EXPECT_FALSE(m.Contains(420));
  303. // Check walking the entire container.
  304. ExpectMapElementsAre(
  305. m, MakeKeyValues([](int k) { return k * 100 + 3; }, llvm::seq(1, 100)));
  306. // Clear back to empty.
  307. m.Clear();
  308. EXPECT_FALSE(m.Contains(42));
  309. EXPECT_EQ(nullptr, m[42]);
  310. // Refill but with both overlapping and different values.
  311. for (int i : llvm::seq(50, 150)) {
  312. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  313. }
  314. for (int i : llvm::seq(50, 150)) {
  315. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  316. ASSERT_TRUE(m.Contains(i));
  317. EXPECT_EQ(i * 100, *m[i]);
  318. EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
  319. EXPECT_EQ(i * 100, *m[i]);
  320. EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
  321. EXPECT_EQ(i * 100 + 1, *m[i]);
  322. }
  323. EXPECT_FALSE(m.Contains(42));
  324. EXPECT_FALSE(m.Contains(420));
  325. ExpectMapElementsAre(
  326. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(50, 150)));
  327. EXPECT_FALSE(m.Erase(42));
  328. EXPECT_TRUE(m.Contains(73));
  329. EXPECT_TRUE(m.Erase(73));
  330. EXPECT_FALSE(m.Contains(73));
  331. for (int i : llvm::seq(102, 136)) {
  332. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  333. EXPECT_TRUE(m.Contains(i));
  334. EXPECT_TRUE(m.Erase(i));
  335. EXPECT_FALSE(m.Contains(i));
  336. }
  337. for (int i : llvm::seq(50, 150)) {
  338. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  339. if (i == 73 || (i >= 102 && i < 136)) {
  340. continue;
  341. }
  342. ASSERT_TRUE(m.Contains(i));
  343. EXPECT_EQ(i * 100 + 1, *m[i]);
  344. EXPECT_FALSE(m.Insert(i, i * 100 + 2).is_inserted());
  345. EXPECT_EQ(i * 100 + 1, *m[i]);
  346. EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
  347. EXPECT_EQ(i * 100 + 2, *m[i]);
  348. }
  349. EXPECT_TRUE(m.Insert(73, 73 * 100 + 3).is_inserted());
  350. EXPECT_EQ(73 * 100 + 3, *m[73]);
  351. ExpectMapElementsAre(
  352. m, MakeKeyValues([](int k) { return k * 100 + 2 + (k == 73); },
  353. llvm::seq(50, 102), llvm::seq(136, 150)));
  354. // Reset back to empty and small.
  355. m.Reset();
  356. EXPECT_FALSE(m.Contains(42));
  357. EXPECT_EQ(nullptr, m[42]);
  358. // Refill but with both overlapping and different values, now triggering
  359. // growth too. Also, use update instead of insert.
  360. for (int i : llvm::seq(75, 175)) {
  361. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  362. EXPECT_TRUE(m.Update(i, i * 100).is_inserted());
  363. }
  364. for (int i : llvm::seq(75, 175)) {
  365. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  366. ASSERT_TRUE(m.Contains(i));
  367. EXPECT_EQ(i * 100, *m[i]);
  368. EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
  369. EXPECT_EQ(i * 100, *m[i]);
  370. EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
  371. EXPECT_EQ(i * 100 + 1, *m[i]);
  372. }
  373. EXPECT_FALSE(m.Contains(42));
  374. EXPECT_FALSE(m.Contains(420));
  375. ExpectMapElementsAre(
  376. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(75, 175)));
  377. EXPECT_FALSE(m.Erase(42));
  378. EXPECT_TRUE(m.Contains(93));
  379. EXPECT_TRUE(m.Erase(93));
  380. EXPECT_FALSE(m.Contains(93));
  381. for (int i : llvm::seq(102, 136)) {
  382. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  383. EXPECT_TRUE(m.Contains(i));
  384. EXPECT_TRUE(m.Erase(i));
  385. EXPECT_FALSE(m.Contains(i));
  386. }
  387. for (int i : llvm::seq(75, 175)) {
  388. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  389. if (i == 93 || (i >= 102 && i < 136)) {
  390. continue;
  391. }
  392. ASSERT_TRUE(m.Contains(i));
  393. EXPECT_EQ(i * 100 + 1, *m[i]);
  394. EXPECT_FALSE(m.Insert(i, i * 100 + 2).is_inserted());
  395. EXPECT_EQ(i * 100 + 1, *m[i]);
  396. EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
  397. EXPECT_EQ(i * 100 + 2, *m[i]);
  398. }
  399. EXPECT_TRUE(m.Insert(93, 93 * 100 + 3).is_inserted());
  400. EXPECT_EQ(93 * 100 + 3, *m[93]);
  401. ExpectMapElementsAre(
  402. m, MakeKeyValues([](int k) { return k * 100 + 2 + (k == 93); },
  403. llvm::seq(75, 102), llvm::seq(136, 175)));
  404. }
  405. template <typename MapT>
  406. class MapCollisionTest : public ::testing::Test {};
  407. using CollisionTypes = ::testing::Types<
  408. Map<int, int, 16,
  409. FixedHashKeyContext<7, /*FixIndexBits*/ true, /*FixTagBits*/ false, 0>>,
  410. Map<int, int, 16,
  411. FixedHashKeyContext<7, /*FixIndexBits*/ false, /*FixTagBits*/ true, 0>>,
  412. Map<int, int, 16,
  413. FixedHashKeyContext<7, /*FixIndexBits*/ true, /*FixTagBits*/ true, 0>>,
  414. Map<int, int, 16,
  415. FixedHashKeyContext<7, /*FixIndexBits*/ true, /*FixTagBits*/ true,
  416. ~static_cast<uint64_t>(0)>>>;
  417. TYPED_TEST_SUITE(MapCollisionTest, CollisionTypes);
  418. TYPED_TEST(MapCollisionTest, Basic) {
  419. TypeParam m;
  420. // Fill the map through a couple of growth steps, verifying at each step. Note
  421. // that because this is a collision test, we synthesize actively harmful
  422. // hashes in terms of collisions and so this test is essentially quadratic. We
  423. // need to keep it relatively small.
  424. for (int i : llvm::seq(1, 256)) {
  425. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  426. EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
  427. // Immediately do a basic check of all elements to pin down when an
  428. // insertion corrupts the rest of the table.
  429. ExpectMapElementsAre(m, MakeKeyValues([](int k) { return k * 100; },
  430. llvm::seq_inclusive(1, i)));
  431. }
  432. EXPECT_FALSE(m.Contains(257));
  433. // Erase and re-fill from the back.
  434. for (int i : llvm::seq(192, 256)) {
  435. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  436. EXPECT_TRUE(m.Erase(i));
  437. }
  438. ExpectMapElementsAre(
  439. m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 192)));
  440. for (int i : llvm::seq(192, 256)) {
  441. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  442. EXPECT_TRUE(m.Insert(i, i * 100 + 1).is_inserted());
  443. }
  444. ExpectMapElementsAre(m,
  445. MakeKeyValues([](int k) { return k * 100 + (k >= 192); },
  446. llvm::seq(1, 256)));
  447. // Erase and re-fill from the front.
  448. for (int i : llvm::seq(1, 64)) {
  449. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  450. EXPECT_TRUE(m.Erase(i));
  451. }
  452. ExpectMapElementsAre(m,
  453. MakeKeyValues([](int k) { return k * 100 + (k >= 192); },
  454. llvm::seq(64, 256)));
  455. for (int i : llvm::seq(1, 64)) {
  456. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  457. EXPECT_TRUE(m.Insert(i, i * 100 + 1).is_inserted());
  458. }
  459. ExpectMapElementsAre(
  460. m, MakeKeyValues([](int k) { return k * 100 + (k < 64) + (k >= 192); },
  461. llvm::seq(1, 256)));
  462. // Erase and re-fill from the middle.
  463. for (int i : llvm::seq(64, 192)) {
  464. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  465. EXPECT_TRUE(m.Erase(i));
  466. }
  467. ExpectMapElementsAre(m, MakeKeyValues([](int k) { return k * 100 + 1; },
  468. llvm::seq(1, 64), llvm::seq(192, 256)));
  469. for (int i : llvm::seq(64, 192)) {
  470. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  471. EXPECT_TRUE(m.Insert(i, i * 100 + 1).is_inserted());
  472. }
  473. ExpectMapElementsAre(
  474. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(1, 256)));
  475. // Erase and re-fill from both the back and front.
  476. for (auto s : {llvm::seq(192, 256), llvm::seq(1, 64)}) {
  477. for (int i : s) {
  478. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  479. EXPECT_TRUE(m.Erase(i));
  480. }
  481. }
  482. ExpectMapElementsAre(
  483. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(64, 192)));
  484. for (auto s : {llvm::seq(192, 256), llvm::seq(1, 64)}) {
  485. for (int i : s) {
  486. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  487. EXPECT_TRUE(m.Insert(i, i * 100 + 2).is_inserted());
  488. }
  489. }
  490. ExpectMapElementsAre(
  491. m,
  492. MakeKeyValues([](int k) { return k * 100 + 1 + (k < 64) + (k >= 192); },
  493. llvm::seq(1, 256)));
  494. // And update the middle elements in place.
  495. for (int i : llvm::seq(64, 192)) {
  496. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  497. EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
  498. }
  499. ExpectMapElementsAre(
  500. m, MakeKeyValues([](int k) { return k * 100 + 2; }, llvm::seq(1, 256)));
  501. }
  502. TEST(MapContextTest, Basic) {
  503. llvm::SmallVector<TestData> keys;
  504. for (int i : llvm::seq(0, 513)) {
  505. keys.push_back(i * 100000);
  506. }
  507. IndexKeyContext<TestData> key_context(keys);
  508. Map<ssize_t, int, 0, IndexKeyContext<TestData>> m;
  509. EXPECT_FALSE(m.Contains(42, key_context));
  510. EXPECT_TRUE(m.Insert(1, 100, key_context).is_inserted());
  511. ASSERT_TRUE(m.Contains(1, key_context));
  512. auto result = m.Lookup(TestData(100000), key_context);
  513. EXPECT_TRUE(result);
  514. EXPECT_EQ(1, result.key());
  515. EXPECT_EQ(100, result.value());
  516. // Reinsertion doesn't change the value. Also, double check a temporary
  517. // context.
  518. auto i_result = m.Insert(1, 101, IndexKeyContext<TestData>(keys));
  519. EXPECT_FALSE(i_result.is_inserted());
  520. EXPECT_EQ(100, i_result.value());
  521. // Update does change the value.
  522. i_result = m.Update(1, 101, key_context);
  523. EXPECT_FALSE(i_result.is_inserted());
  524. EXPECT_EQ(101, i_result.value());
  525. // Verify all the elements.
  526. ExpectMapElementsAre(m, {Pair(1, 101)});
  527. // Fill up a bunch to ensure we trigger growth a few times.
  528. for (int i : llvm::seq(2, 512)) {
  529. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  530. EXPECT_TRUE(m.Insert(i, i * 100, key_context).is_inserted());
  531. // Immediately do a basic check of all elements to pin down when an
  532. // insertion corrupts the rest of the table.
  533. for (int j : llvm::seq(1, i)) {
  534. SCOPED_TRACE(llvm::formatv("Assert key: {0}", j).str());
  535. ASSERT_EQ(j * 100 + static_cast<int>(j == 1),
  536. m.Lookup(j, key_context).value());
  537. ASSERT_EQ(j * 100 + static_cast<int>(j == 1),
  538. m.Lookup(TestData(j * 100000), key_context).value());
  539. }
  540. }
  541. for (int i : llvm::seq(1, 512)) {
  542. SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
  543. EXPECT_FALSE(m.Insert(i, i * 100 + 1, key_context).is_inserted());
  544. EXPECT_EQ(i * 100 + static_cast<int>(i == 1),
  545. m.Lookup(i, key_context).value());
  546. EXPECT_FALSE(m.Update(i, i * 100 + 1, key_context).is_inserted());
  547. EXPECT_EQ(i * 100 + 1, m.Lookup(i, key_context).value());
  548. }
  549. EXPECT_FALSE(m.Contains(0, key_context));
  550. EXPECT_FALSE(m.Contains(512, key_context));
  551. // Verify all the elements.
  552. ExpectMapElementsAre(
  553. m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(1, 512)));
  554. }
  555. } // namespace
  556. } // namespace Carbon::Testing