1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_CONTAINERS_MAP_UTIL_UNITTEST_CC_
#define BASE_CONTAINERS_MAP_UTIL_UNITTEST_CC_
#include "base/containers/map_util.h"
#include <memory>
#include <string>
#include "base/containers/flat_map.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
using testing::AllOf;
using testing::Eq;
using testing::Pointee;
constexpr char kKey[] = "key";
constexpr char kValue[] = "value";
constexpr char kMissingKey[] = "missing_key";
using StringToStringMap = base::flat_map<std::string, std::string>;
using StringToStringPtrMap = base::flat_map<std::string, std::string*>;
using StringToStringUniquePtrMap =
base::flat_map<std::string, std::unique_ptr<std::string>>;
TEST(MapUtilTest, FindOrNull) {
StringToStringMap mapping({{kKey, kValue}});
EXPECT_THAT(FindOrNull(mapping, kKey), Pointee(Eq(kValue)));
EXPECT_EQ(FindOrNull(mapping, kMissingKey), nullptr);
// The following should be able to infer the type of the key from the map's
// type.
base::flat_map<std::pair<int, std::string>, std::string> pair_mapping;
EXPECT_EQ(FindOrNull(pair_mapping, {3, "foo"}), nullptr);
// Homogeneous keys are supported.
std::pair<int, std::string> homogeneous_key(3, "bar");
EXPECT_EQ(FindOrNull(pair_mapping, homogeneous_key), nullptr);
// Heterogenous keys are supported.
std::pair<int, const char*> heterogenous_key(3, "bar");
EXPECT_EQ(FindOrNull(pair_mapping, heterogenous_key), nullptr);
}
TEST(MapUtilTest, FindPtrOrNullForPointers) {
auto val = std::make_unique<std::string>(kValue);
StringToStringPtrMap mapping({{kKey, val.get()}});
EXPECT_THAT(FindPtrOrNull(mapping, kKey),
AllOf(Eq(val.get()), Pointee(Eq(kValue))));
EXPECT_EQ(FindPtrOrNull(mapping, kMissingKey), nullptr);
// The following should be able to infer the type of the key from the map's
// type.
base::flat_map<std::pair<int, std::string>, std::string*> pair_mapping;
EXPECT_EQ(FindPtrOrNull(pair_mapping, {3, "foo"}), nullptr);
// Homogeneous keys are supported.
std::pair<int, std::string> homogeneous_key(3, "bar");
EXPECT_EQ(FindPtrOrNull(pair_mapping, homogeneous_key), nullptr);
// Heterogenous keys are supported.
std::pair<int, const char*> heterogenous_key(3, "bar");
EXPECT_EQ(FindPtrOrNull(pair_mapping, heterogenous_key), nullptr);
}
TEST(MapUtilTest, FindPtrOrNullForPointerLikeValues) {
StringToStringUniquePtrMap mapping;
mapping.insert({kKey, std::make_unique<std::string>(kValue)});
EXPECT_THAT(FindPtrOrNull(mapping, kKey), Pointee(Eq(kValue)));
EXPECT_EQ(FindPtrOrNull(mapping, kMissingKey), nullptr);
}
struct LeftVsRightValue {
enum RefType {
UNKNOWN,
EMPLACED,
LVALUE,
RVALUE,
};
explicit LeftVsRightValue(int n) : ref_type(EMPLACED), value(n) {}
LeftVsRightValue(LeftVsRightValue&& n) : ref_type(RVALUE), value(n.value) {}
LeftVsRightValue(const LeftVsRightValue& n)
: ref_type(LVALUE), value(n.value) {}
LeftVsRightValue& operator=(LeftVsRightValue&& n) {
ref_type = RVALUE;
value = n.value;
return *this;
}
LeftVsRightValue& operator=(const LeftVsRightValue& n) {
ref_type = LVALUE;
value = n.value;
return *this;
}
RefType ref_type = UNKNOWN;
int value = 0;
};
TEST(MapUtilTest, InsertOrAssign) {
using StringToValueMap = std::map<std::string, LeftVsRightValue, std::less<>>;
StringToValueMap map;
// Heterogenous keys - all of types comparable with std::string.
const char key1[] = "This is key 1. It is very long";
std::string_view key2 = "This is key 2. It is also very long";
std::string key3 = "This is key 3. It, like keys 1 and 2, is long";
// Insert a new key with a value that is an implicit rvalue.
auto it = InsertOrAssign(map, key1, LeftVsRightValue(1));
EXPECT_EQ(it->second.ref_type, LeftVsRightValue::RVALUE);
EXPECT_EQ(it->second.value, 1);
// Update that key with a value that is an implicit rvalue.
it = InsertOrAssign(map, key1, LeftVsRightValue(2));
EXPECT_EQ(it->second.ref_type, LeftVsRightValue::RVALUE);
EXPECT_EQ(it->second.value, 2);
// Insert new key with a value that is an explicit rvalue.
LeftVsRightValue v3(3);
it = InsertOrAssign(map, key2, std::move(v3));
EXPECT_EQ(it->second.ref_type, LeftVsRightValue::RVALUE);
EXPECT_EQ(it->second.value, 3);
// Update that key with a value that is an explicit rvalue.
LeftVsRightValue v4(4);
it = InsertOrAssign(map, key2, std::move(v4));
EXPECT_EQ(it->second.ref_type, LeftVsRightValue::RVALUE);
EXPECT_EQ(it->second.value, 4);
// Insert new key with a value that is an lvalue.
LeftVsRightValue v5(5);
it = InsertOrAssign(map, key3, v5);
EXPECT_EQ(it->second.ref_type, LeftVsRightValue::LVALUE);
EXPECT_EQ(it->second.value, 5);
// Update that key with a value that is an lvalue.
LeftVsRightValue v6(6);
it = InsertOrAssign(map, key3, v6);
EXPECT_EQ(it->second.ref_type, LeftVsRightValue::LVALUE);
EXPECT_EQ(it->second.value, 6);
}
} // namespace
} // namespace base
#endif // BASE_CONTAINERS_MAP_UTIL_UNITTEST_CC_
|