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