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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/containers/flat_map.h"
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "base/ranges/algorithm.h"
#include "base/strings/string_piece.h"
#include "base/test/move_only_int.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
// A flat_map is basically a interface to flat_tree. So several basic
// operations are tested to make sure things are set up properly, but the bulk
// of the tests are in flat_tree_unittests.cc.
using ::testing::ElementsAre;
namespace base {
namespace {
struct Unsortable {
int value;
};
bool operator==(const Unsortable& lhs, const Unsortable& rhs) {
return lhs.value == rhs.value;
}
bool operator<(const Unsortable& lhs, const Unsortable& rhs) = delete;
bool operator<=(const Unsortable& lhs, const Unsortable& rhs) = delete;
bool operator>(const Unsortable& lhs, const Unsortable& rhs) = delete;
bool operator>=(const Unsortable& lhs, const Unsortable& rhs) = delete;
class ImplicitInt {
public:
// NOLINTNEXTLINE(google-explicit-constructor)
ImplicitInt(int data) : data_(data) {}
private:
friend bool operator<(const ImplicitInt& lhs, const ImplicitInt& rhs) {
return lhs.data_ < rhs.data_;
}
int data_;
};
} // namespace
TEST(FlatMap, IncompleteType) {
struct A {
using Map = flat_map<A, A>;
int data;
Map set_with_incomplete_type;
Map::iterator it;
Map::const_iterator cit;
// We do not declare operator< because clang complains that it's unused.
};
A a;
}
TEST(FlatMap, RangeConstructor) {
flat_map<int, int>::value_type input_vals[] = {
{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}};
flat_map<int, int> first(std::begin(input_vals), std::end(input_vals));
EXPECT_THAT(first, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 1),
std::make_pair(3, 1)));
}
TEST(FlatMap, MoveConstructor) {
using pair = std::pair<MoveOnlyInt, MoveOnlyInt>;
flat_map<MoveOnlyInt, MoveOnlyInt> original;
original.insert(pair(MoveOnlyInt(1), MoveOnlyInt(1)));
original.insert(pair(MoveOnlyInt(2), MoveOnlyInt(2)));
original.insert(pair(MoveOnlyInt(3), MoveOnlyInt(3)));
original.insert(pair(MoveOnlyInt(4), MoveOnlyInt(4)));
flat_map<MoveOnlyInt, MoveOnlyInt> moved(std::move(original));
EXPECT_EQ(1U, moved.count(MoveOnlyInt(1)));
EXPECT_EQ(1U, moved.count(MoveOnlyInt(2)));
EXPECT_EQ(1U, moved.count(MoveOnlyInt(3)));
EXPECT_EQ(1U, moved.count(MoveOnlyInt(4)));
}
TEST(FlatMap, VectorConstructor) {
using IntPair = std::pair<int, int>;
using IntMap = flat_map<int, int>;
std::vector<IntPair> vect{{1, 1}, {1, 2}, {2, 1}};
IntMap map(std::move(vect));
EXPECT_THAT(map, ElementsAre(IntPair(1, 1), IntPair(2, 1)));
}
TEST(FlatMap, InitializerListConstructor) {
flat_map<int, int> cont(
{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {1, 2}, {10, 10}, {8, 8}});
EXPECT_THAT(cont, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2),
std::make_pair(3, 3), std::make_pair(4, 4),
std::make_pair(5, 5), std::make_pair(8, 8),
std::make_pair(10, 10)));
}
TEST(FlatMap, SortedRangeConstructor) {
using PairType = std::pair<int, Unsortable>;
using MapType = flat_map<int, Unsortable>;
MapType::value_type input_vals[] = {{1, {1}}, {2, {1}}, {3, {1}}};
MapType map(sorted_unique, std::begin(input_vals), std::end(input_vals));
EXPECT_THAT(
map, ElementsAre(PairType(1, {1}), PairType(2, {1}), PairType(3, {1})));
}
TEST(FlatMap, SortedCopyFromVectorConstructor) {
using PairType = std::pair<int, Unsortable>;
using MapType = flat_map<int, Unsortable>;
std::vector<PairType> vect{{1, {1}}, {2, {1}}};
MapType map(sorted_unique, vect);
EXPECT_THAT(map, ElementsAre(PairType(1, {1}), PairType(2, {1})));
}
TEST(FlatMap, SortedMoveFromVectorConstructor) {
using PairType = std::pair<int, Unsortable>;
using MapType = flat_map<int, Unsortable>;
std::vector<PairType> vect{{1, {1}}, {2, {1}}};
MapType map(sorted_unique, std::move(vect));
EXPECT_THAT(map, ElementsAre(PairType(1, {1}), PairType(2, {1})));
}
TEST(FlatMap, SortedInitializerListConstructor) {
using PairType = std::pair<int, Unsortable>;
flat_map<int, Unsortable> map(
sorted_unique,
{{1, {1}}, {2, {2}}, {3, {3}}, {4, {4}}, {5, {5}}, {8, {8}}, {10, {10}}});
EXPECT_THAT(map,
ElementsAre(PairType(1, {1}), PairType(2, {2}), PairType(3, {3}),
PairType(4, {4}), PairType(5, {5}), PairType(8, {8}),
PairType(10, {10})));
}
TEST(FlatMap, InitializerListAssignment) {
flat_map<int, int> cont;
cont = {{1, 1}, {2, 2}};
EXPECT_THAT(cont, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
}
TEST(FlatMap, InsertFindSize) {
base::flat_map<int, int> s;
s.insert(std::make_pair(1, 1));
s.insert(std::make_pair(1, 1));
s.insert(std::make_pair(2, 2));
EXPECT_EQ(2u, s.size());
EXPECT_EQ(std::make_pair(1, 1), *s.find(1));
EXPECT_EQ(std::make_pair(2, 2), *s.find(2));
EXPECT_EQ(s.end(), s.find(7));
}
TEST(FlatMap, CopySwap) {
base::flat_map<int, int> original;
original.insert({1, 1});
original.insert({2, 2});
EXPECT_THAT(original,
ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
base::flat_map<int, int> copy(original);
EXPECT_THAT(copy, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
copy.erase(copy.begin());
copy.insert({10, 10});
EXPECT_THAT(copy, ElementsAre(std::make_pair(2, 2), std::make_pair(10, 10)));
original.swap(copy);
EXPECT_THAT(original,
ElementsAre(std::make_pair(2, 2), std::make_pair(10, 10)));
EXPECT_THAT(copy, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
}
// operator[](const Key&)
TEST(FlatMap, SubscriptConstKey) {
base::flat_map<std::string, int> m;
// Default construct elements that don't exist yet.
int& s = m["a"];
EXPECT_EQ(0, s);
EXPECT_EQ(1u, m.size());
// The returned mapped reference should refer into the map.
s = 22;
EXPECT_EQ(22, m["a"]);
// Overwrite existing elements.
m["a"] = 44;
EXPECT_EQ(44, m["a"]);
}
// operator[](Key&&)
TEST(FlatMap, SubscriptMoveOnlyKey) {
base::flat_map<MoveOnlyInt, int> m;
// Default construct elements that don't exist yet.
int& s = m[MoveOnlyInt(1)];
EXPECT_EQ(0, s);
EXPECT_EQ(1u, m.size());
// The returned mapped reference should refer into the map.
s = 22;
EXPECT_EQ(22, m[MoveOnlyInt(1)]);
// Overwrite existing elements.
m[MoveOnlyInt(1)] = 44;
EXPECT_EQ(44, m[MoveOnlyInt(1)]);
}
// Mapped& at(const Key&)
// const Mapped& at(const Key&) const
TEST(FlatMap, AtFunction) {
base::flat_map<int, std::string> m = {{1, "a"}, {2, "b"}};
// Basic Usage.
EXPECT_EQ("a", m.at(1));
EXPECT_EQ("b", m.at(2));
// Const reference works.
const std::string& const_ref = std::as_const(m).at(1);
EXPECT_EQ("a", const_ref);
// Reference works, can operate on the string.
m.at(1)[0] = 'x';
EXPECT_EQ("x", m.at(1));
// Out-of-bounds will CHECK.
EXPECT_DEATH_IF_SUPPORTED(m.at(-1), "");
EXPECT_DEATH_IF_SUPPORTED({ m.at(-1)[0] = 'z'; }, "");
// Heterogeneous look-up works.
base::flat_map<std::string, int> m2 = {{"a", 1}, {"b", 2}};
EXPECT_EQ(1, m2.at(base::StringPiece("a")));
EXPECT_EQ(2, std::as_const(m2).at(base::StringPiece("b")));
}
// insert_or_assign(K&&, M&&)
TEST(FlatMap, InsertOrAssignMoveOnlyKey) {
base::flat_map<MoveOnlyInt, MoveOnlyInt> m;
// Initial insertion should return an iterator to the element and set the
// second pair member to |true|. The inserted key and value should be moved
// from.
MoveOnlyInt key(1);
MoveOnlyInt val(22);
auto result = m.insert_or_assign(std::move(key), std::move(val));
EXPECT_EQ(1, result.first->first.data());
EXPECT_EQ(22, result.first->second.data());
EXPECT_TRUE(result.second);
EXPECT_EQ(1u, m.size());
EXPECT_EQ(0, key.data()); // moved from
EXPECT_EQ(0, val.data()); // moved from
// Second call with same key should result in an assignment, overwriting the
// old value. Assignment should be indicated by setting the second pair member
// to |false|. Only the inserted value should be moved from, the key should be
// left intact.
key = MoveOnlyInt(1);
val = MoveOnlyInt(44);
result = m.insert_or_assign(std::move(key), std::move(val));
EXPECT_EQ(1, result.first->first.data());
EXPECT_EQ(44, result.first->second.data());
EXPECT_FALSE(result.second);
EXPECT_EQ(1u, m.size());
EXPECT_EQ(1, key.data()); // not moved from
EXPECT_EQ(0, val.data()); // moved from
// Check that random insertion results in sorted range.
base::flat_map<MoveOnlyInt, int> map;
for (int i : {3, 1, 5, 6, 8, 7, 0, 9, 4, 2}) {
map.insert_or_assign(MoveOnlyInt(i), i);
EXPECT_TRUE(ranges::is_sorted(map));
}
}
// insert_or_assign(const_iterator hint, K&&, M&&)
TEST(FlatMap, InsertOrAssignMoveOnlyKeyWithHint) {
base::flat_map<MoveOnlyInt, MoveOnlyInt> m;
// Initial insertion should return an iterator to the element. The inserted
// key and value should be moved from.
MoveOnlyInt key(1);
MoveOnlyInt val(22);
auto result = m.insert_or_assign(m.end(), std::move(key), std::move(val));
EXPECT_EQ(1, result->first.data());
EXPECT_EQ(22, result->second.data());
EXPECT_EQ(1u, m.size());
EXPECT_EQ(0, key.data()); // moved from
EXPECT_EQ(0, val.data()); // moved from
// Second call with same key should result in an assignment, overwriting the
// old value. Only the inserted value should be moved from, the key should be
// left intact.
key = MoveOnlyInt(1);
val = MoveOnlyInt(44);
result = m.insert_or_assign(m.end(), std::move(key), std::move(val));
EXPECT_EQ(1, result->first.data());
EXPECT_EQ(44, result->second.data());
EXPECT_EQ(1u, m.size());
EXPECT_EQ(1, key.data()); // not moved from
EXPECT_EQ(0, val.data()); // moved from
// Check that random insertion results in sorted range.
base::flat_map<MoveOnlyInt, int> map;
for (int i : {3, 1, 5, 6, 8, 7, 0, 9, 4, 2}) {
map.insert_or_assign(map.end(), MoveOnlyInt(i), i);
EXPECT_TRUE(ranges::is_sorted(map));
}
}
// try_emplace(K&&, Args&&...)
TEST(FlatMap, TryEmplaceMoveOnlyKey) {
base::flat_map<MoveOnlyInt, std::pair<MoveOnlyInt, MoveOnlyInt>> m;
// Trying to emplace into an empty map should succeed. Insertion should return
// an iterator to the element and set the second pair member to |true|. The
// inserted key and value should be moved from.
MoveOnlyInt key(1);
MoveOnlyInt val1(22);
MoveOnlyInt val2(44);
// Test piecewise construction of mapped_type.
auto result = m.try_emplace(std::move(key), std::move(val1), std::move(val2));
EXPECT_EQ(1, result.first->first.data());
EXPECT_EQ(22, result.first->second.first.data());
EXPECT_EQ(44, result.first->second.second.data());
EXPECT_TRUE(result.second);
EXPECT_EQ(1u, m.size());
EXPECT_EQ(0, key.data()); // moved from
EXPECT_EQ(0, val1.data()); // moved from
EXPECT_EQ(0, val2.data()); // moved from
// Second call with same key should result in a no-op, returning an iterator
// to the existing element and returning false as the second pair member.
// Key and values that were attempted to be inserted should be left intact.
key = MoveOnlyInt(1);
auto paired_val = std::make_pair(MoveOnlyInt(33), MoveOnlyInt(55));
// Test construction of mapped_type from pair.
result = m.try_emplace(std::move(key), std::move(paired_val));
EXPECT_EQ(1, result.first->first.data());
EXPECT_EQ(22, result.first->second.first.data());
EXPECT_EQ(44, result.first->second.second.data());
EXPECT_FALSE(result.second);
EXPECT_EQ(1u, m.size());
EXPECT_EQ(1, key.data()); // not moved from
EXPECT_EQ(33, paired_val.first.data()); // not moved from
EXPECT_EQ(55, paired_val.second.data()); // not moved from
// Check that random insertion results in sorted range.
base::flat_map<MoveOnlyInt, int> map;
for (int i : {3, 1, 5, 6, 8, 7, 0, 9, 4, 2}) {
map.try_emplace(MoveOnlyInt(i), i);
EXPECT_TRUE(ranges::is_sorted(map));
}
}
// try_emplace(const_iterator hint, K&&, Args&&...)
TEST(FlatMap, TryEmplaceMoveOnlyKeyWithHint) {
base::flat_map<MoveOnlyInt, std::pair<MoveOnlyInt, MoveOnlyInt>> m;
// Trying to emplace into an empty map should succeed. Insertion should return
// an iterator to the element. The inserted key and value should be moved
// from.
MoveOnlyInt key(1);
MoveOnlyInt val1(22);
MoveOnlyInt val2(44);
// Test piecewise construction of mapped_type.
auto result =
m.try_emplace(m.end(), std::move(key), std::move(val1), std::move(val2));
EXPECT_EQ(1, result->first.data());
EXPECT_EQ(22, result->second.first.data());
EXPECT_EQ(44, result->second.second.data());
EXPECT_EQ(1u, m.size());
EXPECT_EQ(0, key.data()); // moved from
EXPECT_EQ(0, val1.data()); // moved from
EXPECT_EQ(0, val2.data()); // moved from
// Second call with same key should result in a no-op, returning an iterator
// to the existing element. Key and values that were attempted to be inserted
// should be left intact.
key = MoveOnlyInt(1);
val1 = MoveOnlyInt(33);
val2 = MoveOnlyInt(55);
auto paired_val = std::make_pair(MoveOnlyInt(33), MoveOnlyInt(55));
// Test construction of mapped_type from pair.
result = m.try_emplace(m.end(), std::move(key), std::move(paired_val));
EXPECT_EQ(1, result->first.data());
EXPECT_EQ(22, result->second.first.data());
EXPECT_EQ(44, result->second.second.data());
EXPECT_EQ(1u, m.size());
EXPECT_EQ(1, key.data()); // not moved from
EXPECT_EQ(33, paired_val.first.data()); // not moved from
EXPECT_EQ(55, paired_val.second.data()); // not moved from
// Check that random insertion results in sorted range.
base::flat_map<MoveOnlyInt, int> map;
for (int i : {3, 1, 5, 6, 8, 7, 0, 9, 4, 2}) {
map.try_emplace(map.end(), MoveOnlyInt(i), i);
EXPECT_TRUE(ranges::is_sorted(map));
}
}
TEST(FlatMap, UsingTransparentCompare) {
using ExplicitInt = base::MoveOnlyInt;
base::flat_map<ExplicitInt, int> m;
const auto& m1 = m;
int x = 0;
// Check if we can use lookup functions without converting to key_type.
// Correctness is checked in flat_tree tests.
m.count(x);
m1.count(x);
m.find(x);
m1.find(x);
m.contains(x);
m1.contains(x);
m.equal_range(x);
m1.equal_range(x);
m.lower_bound(x);
m1.lower_bound(x);
m.upper_bound(x);
m1.upper_bound(x);
m.erase(x);
// Check if we broke overload resolution.
m.emplace(ExplicitInt(0), 0);
m.emplace(ExplicitInt(1), 0);
m.erase(m.begin());
m.erase(m.cbegin());
}
TEST(FlatMap, UsingInitializerList) {
base::flat_map<ImplicitInt, int> m;
const auto& m1 = m;
// Check if the calls can be resolved. Correctness is checked in flat_tree
// tests.
m.count({1});
m1.count({2});
m.find({3});
m1.find({4});
m.contains({5});
m1.contains({6});
m.equal_range({7});
m1.equal_range({8});
m.lower_bound({9});
m1.lower_bound({10});
m.upper_bound({11});
m1.upper_bound({12});
m.erase({13});
}
TEST(FlatMap, DeductionGuides) {
{
std::vector<std::pair<int, float>> v = {{1, 4.0}, {2, 3.0}};
flat_map map{v};
static_assert(std::is_same_v<decltype(map), flat_map<int, float>>);
}
{
std::vector<std::pair<int, float>> v = {{1, 4.0}, {2, 3.0}};
flat_map map(std::move(v));
static_assert(std::is_same_v<decltype(map), flat_map<int, float>>);
}
}
} // namespace base
|