fixed_flat_map_unittest.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/containers/fixed_flat_map.h"
  5. #include <string>
  6. #include "base/ranges/algorithm.h"
  7. #include "base/strings/string_piece.h"
  8. #include "base/test/gtest_util.h"
  9. #include "testing/gmock/include/gmock/gmock.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. using ::testing::ElementsAre;
  12. using ::testing::Pair;
  13. namespace base {
  14. namespace {
  15. struct Unsortable {
  16. int value;
  17. };
  18. bool operator==(const Unsortable& lhs, const Unsortable& rhs) {
  19. return lhs.value == rhs.value;
  20. }
  21. bool operator<(const Unsortable& lhs, const Unsortable& rhs) = delete;
  22. bool operator<=(const Unsortable& lhs, const Unsortable& rhs) = delete;
  23. bool operator>(const Unsortable& lhs, const Unsortable& rhs) = delete;
  24. bool operator>=(const Unsortable& lhs, const Unsortable& rhs) = delete;
  25. } // namespace
  26. TEST(FixedFlatMapTest, MakeFixedFlatMap_SortedInput) {
  27. constexpr auto kSquares =
  28. MakeFixedFlatMap<int, int>({{1, 1}, {2, 4}, {3, 9}, {4, 16}});
  29. static_assert(ranges::is_sorted(kSquares), "Error: Map is not sorted.");
  30. static_assert(ranges::adjacent_find(kSquares) == kSquares.end(),
  31. "Error: Map contains repeated elements.");
  32. EXPECT_THAT(kSquares,
  33. ElementsAre(Pair(1, 1), Pair(2, 4), Pair(3, 9), Pair(4, 16)));
  34. }
  35. TEST(FixedFlatMapTest, MakeFixedFlatMap_UnsortedInput) {
  36. constexpr auto kMap =
  37. MakeFixedFlatMap<StringPiece, int>({{"foo", 1}, {"bar", 2}, {"baz", 3}});
  38. static_assert(ranges::is_sorted(kMap), "Error: Map is not sorted.");
  39. static_assert(ranges::adjacent_find(kMap) == kMap.end(),
  40. "Error: Map contains repeated elements.");
  41. EXPECT_THAT(kMap,
  42. ElementsAre(Pair("bar", 2), Pair("baz", 3), Pair("foo", 1)));
  43. }
  44. // Tests that even though the keys are immutable, the values of a non-const map
  45. // can still be changed.
  46. TEST(FixedFlatMapTest, MutableValues) {
  47. auto map = MakeFixedFlatMap<std::string, int>({{"bar", 1}, {"foo", 2}});
  48. EXPECT_THAT(map, ElementsAre(Pair("bar", 1), Pair("foo", 2)));
  49. map.at("bar") = 2;
  50. EXPECT_THAT(map, ElementsAre(Pair("bar", 2), Pair("foo", 2)));
  51. }
  52. // Tests that even though the values are unsortable the built in sort still
  53. // correctly orders the keys.
  54. TEST(FixedFlatMapTest, UnsortableValues) {
  55. using PairType = std::pair<int, Unsortable>;
  56. constexpr auto kSquares = MakeFixedFlatMap<int, Unsortable>({
  57. {4, {16}},
  58. {3, {9}},
  59. {2, {4}},
  60. {1, {1}},
  61. });
  62. EXPECT_THAT(kSquares, ElementsAre(PairType(1, {1}), PairType(2, {4}),
  63. PairType(3, {9}), PairType(4, {16})));
  64. }
  65. // Verifies that passing repeated keys to MakeFixedFlatMap results in a CHECK
  66. // failure.
  67. TEST(FixedFlatMapTest, RepeatedKeys) {
  68. // Note: The extra pair of parens is needed to escape the nested commas in the
  69. // type list.
  70. EXPECT_CHECK_DEATH((MakeFixedFlatMap<StringPiece, int>(
  71. {{"foo", 1}, {"bar", 2}, {"foo", 3}})));
  72. }
  73. } // namespace base