| 1 |
//===- STLExtrasTest.cpp - Unit tests for STL extras ----------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "llvm/ADT/STLExtras.h" |
| 10 |
#include "llvm/ADT/StringRef.h" |
| 11 |
#include "gmock/gmock.h" |
| 12 |
#include "gtest/gtest.h" |
| 13 |
|
| 14 |
#include <array> |
| 15 |
#include <climits> |
| 16 |
#include <cstddef> |
| 17 |
#include <initializer_list> |
| 18 |
#include <iterator> |
| 19 |
#include <list> |
| 20 |
#include <tuple> |
| 21 |
#include <type_traits> |
| 22 |
#include <unordered_set> |
| 23 |
#include <utility> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
using namespace llvm; |
| 27 |
|
| 28 |
using testing::ElementsAre; |
| 29 |
using testing::UnorderedElementsAre; |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
int f(rank<0>) { return 0; } |
| 34 |
int f(rank<1>) { return 1; } |
| 35 |
int f(rank<2>) { return 2; } |
| 36 |
int f(rank<4>) { return 4; } |
| 37 |
|
| 38 |
TEST(STLExtrasTest, Rank) { |
| 39 |
// We shouldn't get ambiguities and should select the overload of the same |
| 40 |
// rank as the argument. |
| 41 |
EXPECT_EQ(0, f(rank<0>())); |
| 42 |
EXPECT_EQ(1, f(rank<1>())); |
| 43 |
EXPECT_EQ(2, f(rank<2>())); |
| 44 |
|
| 45 |
// This overload is missing so we end up back at 2. |
| 46 |
EXPECT_EQ(2, f(rank<3>())); |
| 47 |
|
| 48 |
// But going past 3 should work fine. |
| 49 |
EXPECT_EQ(4, f(rank<4>())); |
| 50 |
|
| 51 |
// And we can even go higher and just fall back to the last overload. |
| 52 |
EXPECT_EQ(4, f(rank<5>())); |
| 53 |
EXPECT_EQ(4, f(rank<6>())); |
| 54 |
} |
| 55 |
|
| 56 |
TEST(STLExtrasTest, EnumerateLValue) { |
| 57 |
// Test that a simple LValue can be enumerated and gives correct results with |
| 58 |
// multiple types, including the empty container. |
| 59 |
std::vector<char> foo = {'a', 'b', 'c'}; |
| 60 |
typedef std::pair<std::size_t, char> CharPairType; |
| 61 |
std::vector<CharPairType> CharResults; |
| 62 |
|
| 63 |
for (auto [index, value] : llvm::enumerate(foo)) { |
| 64 |
CharResults.emplace_back(index, value); |
| 65 |
} |
| 66 |
|
| 67 |
EXPECT_THAT(CharResults, |
| 68 |
ElementsAre(CharPairType(0u, 'a'), CharPairType(1u, 'b'), |
| 69 |
CharPairType(2u, 'c'))); |
| 70 |
|
| 71 |
// Test a const range of a different type. |
| 72 |
typedef std::pair<std::size_t, int> IntPairType; |
| 73 |
std::vector<IntPairType> IntResults; |
| 74 |
const std::vector<int> bar = {1, 2, 3}; |
| 75 |
for (auto [index, value] : llvm::enumerate(bar)) { |
| 76 |
IntResults.emplace_back(index, value); |
| 77 |
} |
| 78 |
EXPECT_THAT(IntResults, ElementsAre(IntPairType(0u, 1), IntPairType(1u, 2), |
| 79 |
IntPairType(2u, 3))); |
| 80 |
|
| 81 |
// Test an empty range. |
| 82 |
IntResults.clear(); |
| 83 |
const std::vector<int> baz{}; |
| 84 |
for (auto [index, value] : llvm::enumerate(baz)) { |
| 85 |
IntResults.emplace_back(index, value); |
| 86 |
} |
| 87 |
EXPECT_TRUE(IntResults.empty()); |
| 88 |
} |
| 89 |
|
| 90 |
TEST(STLExtrasTest, EnumerateModifyLValue) { |
| 91 |
// Test that you can modify the underlying entries of an lvalue range through |
| 92 |
// the enumeration iterator. |
| 93 |
std::vector<char> foo = {'a', 'b', 'c'}; |
| 94 |
|
| 95 |
for (auto X : llvm::enumerate(foo)) { |
| 96 |
++X.value(); |
| 97 |
} |
| 98 |
EXPECT_THAT(foo, ElementsAre('b', 'c', 'd')); |
| 99 |
|
| 100 |
// Also test if this works with structured bindings. |
| 101 |
foo = {'a', 'b', 'c'}; |
| 102 |
|
| 103 |
for (auto [index, value] : llvm::enumerate(foo)) { |
| 104 |
++value; |
| 105 |
} |
| 106 |
EXPECT_THAT(foo, ElementsAre('b', 'c', 'd')); |
| 107 |
} |
| 108 |
|
| 109 |
TEST(STLExtrasTest, EnumerateRValueRef) { |
| 110 |
// Test that an rvalue can be enumerated. |
| 111 |
typedef std::pair<std::size_t, int> PairType; |
| 112 |
std::vector<PairType> Results; |
| 113 |
|
| 114 |
auto Enumerator = llvm::enumerate(std::vector<int>{1, 2, 3}); |
| 115 |
|
| 116 |
for (auto X : llvm::enumerate(std::vector<int>{1, 2, 3})) { |
| 117 |
Results.emplace_back(X.index(), X.value()); |
| 118 |
} |
| 119 |
|
| 120 |
EXPECT_THAT(Results, |
| 121 |
ElementsAre(PairType(0u, 1), PairType(1u, 2), PairType(2u, 3))); |
| 122 |
|
| 123 |
// Also test if this works with structured bindings. |
| 124 |
Results.clear(); |
| 125 |
|
| 126 |
for (auto [index, value] : llvm::enumerate(std::vector<int>{1, 2, 3})) { |
| 127 |
Results.emplace_back(index, value); |
| 128 |
} |
| 129 |
|
| 130 |
EXPECT_THAT(Results, |
| 131 |
ElementsAre(PairType(0u, 1), PairType(1u, 2), PairType(2u, 3))); |
| 132 |
} |
| 133 |
|
| 134 |
TEST(STLExtrasTest, EnumerateModifyRValue) { |
| 135 |
// Test that when enumerating an rvalue, modification still works (even if |
| 136 |
// this isn't terribly useful, it at least shows that we haven't snuck an |
| 137 |
// extra const in there somewhere. |
| 138 |
typedef std::pair<std::size_t, char> PairType; |
| 139 |
std::vector<PairType> Results; |
| 140 |
|
| 141 |
for (auto X : llvm::enumerate(std::vector<char>{'1', '2', '3'})) { |
| 142 |
++X.value(); |
| 143 |
Results.emplace_back(X.index(), X.value()); |
| 144 |
} |
| 145 |
|
| 146 |
EXPECT_THAT(Results, ElementsAre(PairType(0u, '2'), PairType(1u, '3'), |
| 147 |
PairType(2u, '4'))); |
| 148 |
|
| 149 |
// Also test if this works with structured bindings. |
| 150 |
Results.clear(); |
| 151 |
|
| 152 |
for (auto [index, value] : |
| 153 |
llvm::enumerate(std::vector<char>{'1', '2', '3'})) { |
| 154 |
++value; |
| 155 |
Results.emplace_back(index, value); |
| 156 |
} |
| 157 |
|
| 158 |
EXPECT_THAT(Results, ElementsAre(PairType(0u, '2'), PairType(1u, '3'), |
| 159 |
PairType(2u, '4'))); |
| 160 |
} |
| 161 |
|
| 162 |
TEST(STLExtrasTest, EnumerateTwoRanges) { |
| 163 |
using Tuple = std::tuple<size_t, int, bool>; |
| 164 |
|
| 165 |
std::vector<int> Ints = {1, 2}; |
| 166 |
std::vector<bool> Bools = {true, false}; |
| 167 |
EXPECT_THAT(llvm::enumerate(Ints, Bools), |
| 168 |
ElementsAre(Tuple(0, 1, true), Tuple(1, 2, false))); |
| 169 |
|
| 170 |
// Check that we can modify the values when the temporary is a const |
| 171 |
// reference. |
| 172 |
for (const auto &[Idx, Int, Bool] : llvm::enumerate(Ints, Bools)) { |
| 173 |
(void)Idx; |
| 174 |
Bool = false; |
| 175 |
Int = -1; |
| 176 |
} |
| 177 |
|
| 178 |
EXPECT_THAT(Ints, ElementsAre(-1, -1)); |
| 179 |
EXPECT_THAT(Bools, ElementsAre(false, false)); |
| 180 |
|
| 181 |
// Check that we can modify the values when the result gets copied. |
| 182 |
for (auto [Idx, Bool, Int] : llvm::enumerate(Bools, Ints)) { |
| 183 |
(void)Idx; |
| 184 |
Int = 3; |
| 185 |
Bool = true; |
| 186 |
} |
| 187 |
|
| 188 |
EXPECT_THAT(Ints, ElementsAre(3, 3)); |
| 189 |
EXPECT_THAT(Bools, ElementsAre(true, true)); |
| 190 |
|
| 191 |
// Check that we can modify the values through `.value()`. |
| 192 |
size_t Iters = 0; |
| 193 |
for (auto It : llvm::enumerate(Bools, Ints)) { |
| 194 |
EXPECT_EQ(It.index(), Iters); |
| 195 |
++Iters; |
| 196 |
|
| 197 |
std::get<0>(It.value()) = false; |
| 198 |
std::get<1>(It.value()) = 4; |
| 199 |
} |
| 200 |
|
| 201 |
EXPECT_THAT(Ints, ElementsAre(4, 4)); |
| 202 |
EXPECT_THAT(Bools, ElementsAre(false, false)); |
| 203 |
} |
| 204 |
|
| 205 |
TEST(STLExtrasTest, EnumerateThreeRanges) { |
| 206 |
using Tuple = std::tuple<size_t, int, bool, char>; |
| 207 |
|
| 208 |
std::vector<int> Ints = {1, 2}; |
| 209 |
std::vector<bool> Bools = {true, false}; |
| 210 |
char Chars[] = {'X', 'D'}; |
| 211 |
EXPECT_THAT(llvm::enumerate(Ints, Bools, Chars), |
| 212 |
ElementsAre(Tuple(0, 1, true, 'X'), Tuple(1, 2, false, 'D'))); |
| 213 |
|
| 214 |
for (auto [Idx, Int, Bool, Char] : llvm::enumerate(Ints, Bools, Chars)) { |
| 215 |
(void)Idx; |
| 216 |
Int = 0; |
| 217 |
Bool = true; |
| 218 |
Char = '!'; |
| 219 |
} |
| 220 |
|
| 221 |
EXPECT_THAT(Ints, ElementsAre(0, 0)); |
| 222 |
EXPECT_THAT(Bools, ElementsAre(true, true)); |
| 223 |
EXPECT_THAT(Chars, ElementsAre('!', '!')); |
| 224 |
|
| 225 |
// Check that we can modify the values through `.values()`. |
| 226 |
size_t Iters = 0; |
| 227 |
for (auto It : llvm::enumerate(Ints, Bools, Chars)) { |
| 228 |
EXPECT_EQ(It.index(), Iters); |
| 229 |
++Iters; |
| 230 |
auto [Int, Bool, Char] = It.value(); |
| 231 |
Int = 42; |
| 232 |
Bool = false; |
| 233 |
Char = '$'; |
| 234 |
} |
| 235 |
|
| 236 |
EXPECT_THAT(Ints, ElementsAre(42, 42)); |
| 237 |
EXPECT_THAT(Bools, ElementsAre(false, false)); |
| 238 |
EXPECT_THAT(Chars, ElementsAre('$', '$')); |
| 239 |
} |
| 240 |
|
| 241 |
TEST(STLExtrasTest, EnumerateTemporaries) { |
| 242 |
using Tuple = std::tuple<size_t, int, bool>; |
| 243 |
|
| 244 |
EXPECT_THAT( |
| 245 |
llvm::enumerate(llvm::SmallVector<int>({1, 2, 3}), |
| 246 |
std::vector<bool>({true, false, true})), |
| 247 |
ElementsAre(Tuple(0, 1, true), Tuple(1, 2, false), Tuple(2, 3, true))); |
| 248 |
|
| 249 |
size_t Iters = 0; |
| 250 |
// This is fine from the point of view of range lifetimes because `zippy` will |
| 251 |
// move all temporaries into its storage. No lifetime extension is necessary. |
| 252 |
for (auto [Idx, Int, Bool] : |
| 253 |
llvm::enumerate(llvm::SmallVector<int>({1, 2, 3}), |
| 254 |
std::vector<bool>({true, false, true}))) { |
| 255 |
EXPECT_EQ(Idx, Iters); |
| 256 |
++Iters; |
| 257 |
Int = 0; |
| 258 |
Bool = true; |
| 259 |
} |
| 260 |
|
| 261 |
Iters = 0; |
| 262 |
// The same thing but with the result as a const reference. |
| 263 |
for (const auto &[Idx, Int, Bool] : |
| 264 |
llvm::enumerate(llvm::SmallVector<int>({1, 2, 3}), |
| 265 |
std::vector<bool>({true, false, true}))) { |
| 266 |
EXPECT_EQ(Idx, Iters); |
| 267 |
++Iters; |
| 268 |
Int = 0; |
| 269 |
Bool = true; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
#if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) |
| 274 |
TEST(STLExtrasTest, EnumerateDifferentLengths) { |
| 275 |
std::vector<int> Ints = {0, 1}; |
| 276 |
bool Bools[] = {true, false, true}; |
| 277 |
std::string Chars = "abc"; |
| 278 |
EXPECT_DEATH(llvm::enumerate(Ints, Bools, Chars), |
| 279 |
"Ranges have different length"); |
| 280 |
EXPECT_DEATH(llvm::enumerate(Bools, Ints, Chars), |
| 281 |
"Ranges have different length"); |
| 282 |
EXPECT_DEATH(llvm::enumerate(Bools, Chars, Ints), |
| 283 |
"Ranges have different length"); |
| 284 |
} |
| 285 |
#endif |
| 286 |
|
| 287 |
template <bool B> struct CanMove {}; |
| 288 |
template <> struct CanMove<false> { |
| 289 |
CanMove(CanMove &&) = delete; |
| 290 |
|
| 291 |
CanMove() = default; |
| 292 |
CanMove(const CanMove &) = default; |
| 293 |
}; |
| 294 |
|
| 295 |
template <bool B> struct CanCopy {}; |
| 296 |
template <> struct CanCopy<false> { |
| 297 |
CanCopy(const CanCopy &) = delete; |
| 298 |
|
| 299 |
CanCopy() = default; |
| 300 |
CanCopy(CanCopy &&) = default; |
| 301 |
}; |
| 302 |
|
| 303 |
template <bool Moveable, bool Copyable> |
| 304 |
class Counted : CanMove<Moveable>, CanCopy<Copyable> { |
| 305 |
int &C; |
| 306 |
int &M; |
| 307 |
int &D; |
| 308 |
|
| 309 |
public: |
| 310 |
explicit Counted(int &C, int &M, int &D) : C(C), M(M), D(D) {} |
| 311 |
Counted(const Counted &O) : CanCopy<Copyable>(O), C(O.C), M(O.M), D(O.D) { |
| 312 |
++C; |
| 313 |
} |
| 314 |
Counted(Counted &&O) |
| 315 |
: CanMove<Moveable>(std::move(O)), C(O.C), M(O.M), D(O.D) { |
| 316 |
++M; |
| 317 |
} |
| 318 |
~Counted() { ++D; } |
| 319 |
}; |
| 320 |
|
| 321 |
template <bool Moveable, bool Copyable> |
| 322 |
struct Range : Counted<Moveable, Copyable> { |
| 323 |
using Counted<Moveable, Copyable>::Counted; |
| 324 |
int *begin() const { return nullptr; } |
| 325 |
int *end() const { return nullptr; } |
| 326 |
}; |
| 327 |
|
| 328 |
TEST(STLExtrasTest, EnumerateLifetimeSemanticsPRValue) { |
| 329 |
int Copies = 0; |
| 330 |
int Moves = 0; |
| 331 |
int Destructors = 0; |
| 332 |
{ |
| 333 |
auto E = enumerate(Range<true, false>(Copies, Moves, Destructors)); |
| 334 |
(void)E; |
| 335 |
// Doesn't compile. rvalue ranges must be moveable. |
| 336 |
// auto E2 = enumerate(Range<false, true>(Copies, Moves, Destructors)); |
| 337 |
EXPECT_EQ(0, Copies); |
| 338 |
EXPECT_EQ(1, Moves); |
| 339 |
EXPECT_EQ(1, Destructors); |
| 340 |
} |
| 341 |
EXPECT_EQ(0, Copies); |
| 342 |
EXPECT_EQ(1, Moves); |
| 343 |
EXPECT_EQ(2, Destructors); |
| 344 |
} |
| 345 |
|
| 346 |
TEST(STLExtrasTest, EnumerateLifetimeSemanticsRValue) { |
| 347 |
// With an rvalue, it should not be destroyed until the end of the scope. |
| 348 |
int Copies = 0; |
| 349 |
int Moves = 0; |
| 350 |
int Destructors = 0; |
| 351 |
{ |
| 352 |
Range<true, false> R(Copies, Moves, Destructors); |
| 353 |
{ |
| 354 |
auto E = enumerate(std::move(R)); |
| 355 |
(void)E; |
| 356 |
// Doesn't compile. rvalue ranges must be moveable. |
| 357 |
// auto E2 = enumerate(Range<false, true>(Copies, Moves, Destructors)); |
| 358 |
EXPECT_EQ(0, Copies); |
| 359 |
EXPECT_EQ(1, Moves); |
| 360 |
EXPECT_EQ(0, Destructors); |
| 361 |
} |
| 362 |
EXPECT_EQ(0, Copies); |
| 363 |
EXPECT_EQ(1, Moves); |
| 364 |
EXPECT_EQ(1, Destructors); |
| 365 |
} |
| 366 |
EXPECT_EQ(0, Copies); |
| 367 |
EXPECT_EQ(1, Moves); |
| 368 |
EXPECT_EQ(2, Destructors); |
| 369 |
} |
| 370 |
|
| 371 |
TEST(STLExtrasTest, EnumerateLifetimeSemanticsLValue) { |
| 372 |
// With an lvalue, it should not be destroyed even after the end of the scope. |
| 373 |
// lvalue ranges need be neither copyable nor moveable. |
| 374 |
int Copies = 0; |
| 375 |
int Moves = 0; |
| 376 |
int Destructors = 0; |
| 377 |
{ |
| 378 |
Range<false, false> R(Copies, Moves, Destructors); |
| 379 |
{ |
| 380 |
auto E = enumerate(R); |
| 381 |
(void)E; |
| 382 |
EXPECT_EQ(0, Copies); |
| 383 |
EXPECT_EQ(0, Moves); |
| 384 |
EXPECT_EQ(0, Destructors); |
| 385 |
} |
| 386 |
EXPECT_EQ(0, Copies); |
| 387 |
EXPECT_EQ(0, Moves); |
| 388 |
EXPECT_EQ(0, Destructors); |
| 389 |
} |
| 390 |
EXPECT_EQ(0, Copies); |
| 391 |
EXPECT_EQ(0, Moves); |
| 392 |
EXPECT_EQ(1, Destructors); |
| 393 |
} |
| 394 |
|
| 395 |
namespace some_namespace { |
| 396 |
struct some_struct { |
| 397 |
std::vector<int> data; |
| 398 |
std::string swap_val; |
| 399 |
}; |
| 400 |
|
| 401 |
std::vector<int>::const_iterator begin(const some_struct &s) { |
| 402 |
return s.data.begin(); |
| 403 |
} |
| 404 |
|
| 405 |
std::vector<int>::const_iterator end(const some_struct &s) { |
| 406 |
return s.data.end(); |
| 407 |
} |
| 408 |
|
| 409 |
std::vector<int>::const_reverse_iterator rbegin(const some_struct &s) { |
| 410 |
return s.data.rbegin(); |
| 411 |
} |
| 412 |
|
| 413 |
std::vector<int>::const_reverse_iterator rend(const some_struct &s) { |
| 414 |
return s.data.rend(); |
| 415 |
} |
| 416 |
|
| 417 |
void swap(some_struct &lhs, some_struct &rhs) { |
| 418 |
// make swap visible as non-adl swap would even seem to |
| 419 |
// work with std::swap which defaults to moving |
| 420 |
lhs.swap_val = "lhs"; |
| 421 |
rhs.swap_val = "rhs"; |
| 422 |
} |
| 423 |
|
| 424 |
struct List { |
| 425 |
std::list<int> data; |
| 426 |
}; |
| 427 |
|
| 428 |
std::list<int>::const_iterator begin(const List &list) { |
| 429 |
return list.data.begin(); |
| 430 |
} |
| 431 |
std::list<int>::const_iterator end(const List &list) { return list.data.end(); } |
| 432 |
|
| 433 |
struct Pairs { |
| 434 |
std::vector<std::pair<std::string, int>> data; |
| 435 |
using const_iterator = |
| 436 |
std::vector<std::pair<std::string, int>>::const_iterator; |
| 437 |
}; |
| 438 |
|
| 439 |
Pairs::const_iterator begin(const Pairs &p) { return p.data.begin(); } |
| 440 |
Pairs::const_iterator end(const Pairs &p) { return p.data.end(); } |
| 441 |
|
| 442 |
struct requires_move {}; |
| 443 |
int *begin(requires_move &&) { return nullptr; } |
| 444 |
int *end(requires_move &&) { return nullptr; } |
| 445 |
} // namespace some_namespace |
| 446 |
|
| 447 |
TEST(STLExtrasTest, EnumerateCustomBeginEnd) { |
| 448 |
// Check that `enumerate` uses ADL to find `begin`/`end` iterators |
| 449 |
// of the enumerated type. |
| 450 |
some_namespace::some_struct X{}; |
| 451 |
X.data = {1, 2, 3}; |
| 452 |
|
| 453 |
unsigned Iters = 0; |
| 454 |
for (auto [Idx, Val] : enumerate(X)) { |
| 455 |
EXPECT_EQ(Val, X.data[Idx]); |
| 456 |
++Iters; |
| 457 |
} |
| 458 |
EXPECT_EQ(Iters, 3u); |
| 459 |
} |
| 460 |
|
| 461 |
TEST(STLExtrasTest, CountAdaptor) { |
| 462 |
std::vector<int> v; |
| 463 |
|
| 464 |
v.push_back(1); |
| 465 |
v.push_back(2); |
| 466 |
v.push_back(1); |
| 467 |
v.push_back(4); |
| 468 |
v.push_back(3); |
| 469 |
v.push_back(2); |
| 470 |
v.push_back(1); |
| 471 |
|
| 472 |
EXPECT_EQ(3, count(v, 1)); |
| 473 |
EXPECT_EQ(2, count(v, 2)); |
| 474 |
EXPECT_EQ(1, count(v, 3)); |
| 475 |
EXPECT_EQ(1, count(v, 4)); |
| 476 |
} |
| 477 |
|
| 478 |
TEST(STLExtrasTest, for_each) { |
| 479 |
std::vector<int> v{0, 1, 2, 3, 4}; |
| 480 |
int count = 0; |
| 481 |
|
| 482 |
llvm::for_each(v, [&count](int) { ++count; }); |
| 483 |
EXPECT_EQ(5, count); |
| 484 |
} |
| 485 |
|
| 486 |
TEST(STLExtrasTest, ToVector) { |
| 487 |
std::vector<char> v = {'a', 'b', 'c'}; |
| 488 |
auto Enumerated = to_vector<4>(enumerate(v)); |
| 489 |
ASSERT_EQ(3u, Enumerated.size()); |
| 490 |
for (size_t I = 0; I < v.size(); ++I) { |
| 491 |
EXPECT_EQ(I, Enumerated[I].index()); |
| 492 |
EXPECT_EQ(v[I], Enumerated[I].value()); |
| 493 |
} |
| 494 |
|
| 495 |
auto EnumeratedImplicitSize = to_vector(enumerate(v)); |
| 496 |
ASSERT_EQ(3u, EnumeratedImplicitSize.size()); |
| 497 |
for (size_t I = 0; I < v.size(); ++I) { |
| 498 |
EXPECT_EQ(I, EnumeratedImplicitSize[I].index()); |
| 499 |
EXPECT_EQ(v[I], EnumeratedImplicitSize[I].value()); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
TEST(STLExtrasTest, ConcatRange) { |
| 504 |
std::vector<int> Expected = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 505 |
std::vector<int> Test; |
| 506 |
|
| 507 |
std::vector<int> V1234 = {1, 2, 3, 4}; |
| 508 |
std::list<int> L56 = {5, 6}; |
| 509 |
SmallVector<int, 2> SV78 = {7, 8}; |
| 510 |
|
| 511 |
// Use concat across different sized ranges of different types with different |
| 512 |
// iterators. |
| 513 |
for (int &i : concat<int>(V1234, L56, SV78)) |
| 514 |
Test.push_back(i); |
| 515 |
EXPECT_EQ(Expected, Test); |
| 516 |
|
| 517 |
// Use concat between a temporary, an L-value, and an R-value to make sure |
| 518 |
// complex lifetimes work well. |
| 519 |
Test.clear(); |
| 520 |
for (int &i : concat<int>(std::vector<int>(V1234), L56, std::move(SV78))) |
| 521 |
Test.push_back(i); |
| 522 |
EXPECT_EQ(Expected, Test); |
| 523 |
} |
| 524 |
|
| 525 |
TEST(STLExtrasTest, ConcatRangeADL) { |
| 526 |
// Make sure that we use the `begin`/`end` functions from `some_namespace`, |
| 527 |
// using ADL. |
| 528 |
some_namespace::some_struct S0; |
| 529 |
S0.data = {1, 2}; |
| 530 |
some_namespace::some_struct S1; |
| 531 |
S1.data = {3, 4}; |
| 532 |
EXPECT_THAT(concat<const int>(S0, S1), ElementsAre(1, 2, 3, 4)); |
| 533 |
} |
| 534 |
|
| 535 |
TEST(STLExtrasTest, MakeFirstSecondRangeADL) { |
| 536 |
// Make sure that we use the `begin`/`end` functions from `some_namespace`, |
| 537 |
// using ADL. |
| 538 |
some_namespace::Pairs Pairs; |
| 539 |
Pairs.data = {{"foo", 1}, {"bar", 2}}; |
| 540 |
EXPECT_THAT(make_first_range(Pairs), ElementsAre("foo", "bar")); |
| 541 |
EXPECT_THAT(make_second_range(Pairs), ElementsAre(1, 2)); |
| 542 |
} |
| 543 |
|
| 544 |
template <typename T> struct Iterator { |
| 545 |
int i = 0; |
| 546 |
T operator*() const { return i; } |
| 547 |
Iterator &operator++() { |
| 548 |
++i; |
| 549 |
return *this; |
| 550 |
} |
| 551 |
bool operator==(Iterator RHS) const { return i == RHS.i; } |
| 552 |
}; |
| 553 |
|
| 554 |
template <typename T> struct RangeWithValueType { |
| 555 |
int i; |
| 556 |
RangeWithValueType(int i) : i(i) {} |
| 557 |
Iterator<T> begin() { return Iterator<T>{0}; } |
| 558 |
Iterator<T> end() { return Iterator<T>{i}; } |
| 559 |
}; |
| 560 |
|
| 561 |
TEST(STLExtrasTest, ValueReturn) { |
| 562 |
RangeWithValueType<int> R(1); |
| 563 |
auto C = concat<int>(R, R); |
| 564 |
auto I = C.begin(); |
| 565 |
ASSERT_NE(I, C.end()); |
| 566 |
static_assert(std::is_same_v<decltype((*I)), int>); |
| 567 |
auto V = *I; |
| 568 |
ASSERT_EQ(V, 0); |
| 569 |
} |
| 570 |
|
| 571 |
TEST(STLExtrasTest, ReferenceReturn) { |
| 572 |
RangeWithValueType<const int&> R(1); |
| 573 |
auto C = concat<const int>(R, R); |
| 574 |
auto I = C.begin(); |
| 575 |
ASSERT_NE(I, C.end()); |
| 576 |
static_assert(std::is_same_v<decltype((*I)), const int &>); |
| 577 |
auto V = *I; |
| 578 |
ASSERT_EQ(V, 0); |
| 579 |
} |
| 580 |
|
| 581 |
TEST(STLExtrasTest, PartitionAdaptor) { |
| 582 |
std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 583 |
|
| 584 |
auto I = partition(V, [](int i) { return i % 2 == 0; }); |
| 585 |
ASSERT_EQ(V.begin() + 4, I); |
| 586 |
|
| 587 |
// Sort the two halves as partition may have messed with the order. |
| 588 |
llvm::sort(V.begin(), I); |
| 589 |
llvm::sort(I, V.end()); |
| 590 |
|
| 591 |
EXPECT_EQ(2, V[0]); |
| 592 |
EXPECT_EQ(4, V[1]); |
| 593 |
EXPECT_EQ(6, V[2]); |
| 594 |
EXPECT_EQ(8, V[3]); |
| 595 |
EXPECT_EQ(1, V[4]); |
| 596 |
EXPECT_EQ(3, V[5]); |
| 597 |
EXPECT_EQ(5, V[6]); |
| 598 |
EXPECT_EQ(7, V[7]); |
| 599 |
} |
| 600 |
|
| 601 |
TEST(STLExtrasTest, EraseIf) { |
| 602 |
std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 603 |
|
| 604 |
erase_if(V, [](int i) { return i % 2 == 0; }); |
| 605 |
EXPECT_EQ(4u, V.size()); |
| 606 |
EXPECT_EQ(1, V[0]); |
| 607 |
EXPECT_EQ(3, V[1]); |
| 608 |
EXPECT_EQ(5, V[2]); |
| 609 |
EXPECT_EQ(7, V[3]); |
| 610 |
} |
| 611 |
|
| 612 |
TEST(STLExtrasTest, AppendRange) { |
| 613 |
std::vector<int> V = {1, 2}; |
| 614 |
auto AppendVals1 = {3}; |
| 615 |
append_range(V, AppendVals1); |
| 616 |
EXPECT_THAT(V, ElementsAre(1, 2, 3)); |
| 617 |
|
| 618 |
int AppendVals2[] = {4, 5}; |
| 619 |
append_range(V, AppendVals2); |
| 620 |
EXPECT_THAT(V, ElementsAre(1, 2, 3, 4, 5)); |
| 621 |
|
| 622 |
std::string Str; |
| 623 |
append_range(Str, "abc"); |
| 624 |
EXPECT_THAT(Str, ElementsAre('a', 'b', 'c', '\0')); |
| 625 |
append_range(Str, "def"); |
| 626 |
EXPECT_THAT(Str, ElementsAre('a', 'b', 'c', '\0', 'd', 'e', 'f', '\0')); |
| 627 |
} |
| 628 |
|
| 629 |
TEST(STLExtrasTest, AppendValues) { |
| 630 |
std::vector<int> Vals = {1, 2}; |
| 631 |
append_values(Vals, 3); |
| 632 |
EXPECT_THAT(Vals, ElementsAre(1, 2, 3)); |
| 633 |
|
| 634 |
append_values(Vals, 4, 5); |
| 635 |
EXPECT_THAT(Vals, ElementsAre(1, 2, 3, 4, 5)); |
| 636 |
|
| 637 |
std::vector<StringRef> Strs; |
| 638 |
std::string A = "A"; |
| 639 |
std::string B = "B"; |
| 640 |
std::string C = "C"; |
| 641 |
append_values(Strs, A, B); |
| 642 |
EXPECT_THAT(Strs, ElementsAre(A, B)); |
| 643 |
append_values(Strs, C); |
| 644 |
EXPECT_THAT(Strs, ElementsAre(A, B, C)); |
| 645 |
|
| 646 |
std::unordered_set<int> Set; |
| 647 |
append_values(Set, 1, 2); |
| 648 |
EXPECT_THAT(Set, UnorderedElementsAre(1, 2)); |
| 649 |
append_values(Set, 3, 1); |
| 650 |
EXPECT_THAT(Set, UnorderedElementsAre(1, 2, 3)); |
| 651 |
} |
| 652 |
|
| 653 |
TEST(STLExtrasTest, ADLTest) { |
| 654 |
some_namespace::some_struct s{{1, 2, 3, 4, 5}, ""}; |
| 655 |
some_namespace::some_struct s2{{2, 4, 6, 8, 10}, ""}; |
| 656 |
|
| 657 |
EXPECT_EQ(*adl_begin(s), 1); |
| 658 |
EXPECT_EQ(*(adl_end(s) - 1), 5); |
| 659 |
EXPECT_EQ(*adl_rbegin(s), 5); |
| 660 |
EXPECT_EQ(*(adl_rend(s) - 1), 1); |
| 661 |
|
| 662 |
adl_swap(s, s2); |
| 663 |
EXPECT_EQ(s.swap_val, "lhs"); |
| 664 |
EXPECT_EQ(s2.swap_val, "rhs"); |
| 665 |
|
| 666 |
int count = 0; |
| 667 |
llvm::for_each(s, [&count](int) { ++count; }); |
| 668 |
EXPECT_EQ(count, 5); |
| 669 |
} |
| 670 |
|
| 671 |
TEST(STLExtrasTest, ADLTestTemporaryRange) { |
| 672 |
EXPECT_EQ(adl_begin(some_namespace::requires_move{}), nullptr); |
| 673 |
EXPECT_EQ(adl_end(some_namespace::requires_move{}), nullptr); |
| 674 |
} |
| 675 |
|
| 676 |
TEST(STLExtrasTest, ADLTestConstexpr) { |
| 677 |
// `std::begin`/`std::end` are marked as `constexpr`; check that |
| 678 |
// `adl_begin`/`adl_end` also work in constant-evaluated contexts. |
| 679 |
static constexpr int c_arr[] = {7, 8, 9}; |
| 680 |
static_assert(adl_begin(c_arr) == c_arr); |
| 681 |
static_assert(adl_end(c_arr) == c_arr + 3); |
| 682 |
|
| 683 |
static constexpr std::array<int, 2> std_arr = {1, 2}; |
| 684 |
static_assert(adl_begin(std_arr) == std_arr.begin()); |
| 685 |
static_assert(adl_end(std_arr) == std_arr.end()); |
| 686 |
SUCCEED(); |
| 687 |
} |
| 688 |
|
| 689 |
struct FooWithMemberSize { |
| 690 |
size_t size() const { return 42; } |
| 691 |
auto begin() { return Data.begin(); } |
| 692 |
auto end() { return Data.end(); } |
| 693 |
|
| 694 |
std::set<int> Data; |
| 695 |
}; |
| 696 |
|
| 697 |
namespace some_namespace { |
| 698 |
struct FooWithFreeSize { |
| 699 |
auto begin() { return Data.begin(); } |
| 700 |
auto end() { return Data.end(); } |
| 701 |
|
| 702 |
std::set<int> Data; |
| 703 |
}; |
| 704 |
|
| 705 |
size_t size(const FooWithFreeSize &) { return 13; } |
| 706 |
} // namespace some_namespace |
| 707 |
|
| 708 |
TEST(STLExtrasTest, ADLSizeTest) { |
| 709 |
FooWithMemberSize foo1; |
| 710 |
EXPECT_EQ(adl_size(foo1), 42u); |
| 711 |
|
| 712 |
some_namespace::FooWithFreeSize foo2; |
| 713 |
EXPECT_EQ(adl_size(foo2), 13u); |
| 714 |
|
| 715 |
static constexpr int c_arr[] = {1, 2, 3}; |
| 716 |
static_assert(adl_size(c_arr) == 3u); |
| 717 |
|
| 718 |
static constexpr std::array<int, 4> cpp_arr = {}; |
| 719 |
static_assert(adl_size(cpp_arr) == 4u); |
| 720 |
} |
| 721 |
|
| 722 |
TEST(STLExtrasTest, DropBeginTest) { |
| 723 |
SmallVector<int, 5> vec{0, 1, 2, 3, 4}; |
| 724 |
|
| 725 |
for (int n = 0; n < 5; ++n) { |
| 726 |
int i = n; |
| 727 |
for (auto &v : drop_begin(vec, n)) { |
| 728 |
EXPECT_EQ(v, i); |
| 729 |
i += 1; |
| 730 |
} |
| 731 |
EXPECT_EQ(i, 5); |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
TEST(STLExtrasTest, DropBeginDefaultTest) { |
| 736 |
SmallVector<int, 5> vec{0, 1, 2, 3, 4}; |
| 737 |
|
| 738 |
int i = 1; |
| 739 |
for (auto &v : drop_begin(vec)) { |
| 740 |
EXPECT_EQ(v, i); |
| 741 |
i += 1; |
| 742 |
} |
| 743 |
EXPECT_EQ(i, 5); |
| 744 |
} |
| 745 |
|
| 746 |
TEST(STLExtrasTest, DropEndTest) { |
| 747 |
SmallVector<int, 5> vec{0, 1, 2, 3, 4}; |
| 748 |
|
| 749 |
for (int n = 0; n < 5; ++n) { |
| 750 |
int i = 0; |
| 751 |
for (auto &v : drop_end(vec, n)) { |
| 752 |
EXPECT_EQ(v, i); |
| 753 |
i += 1; |
| 754 |
} |
| 755 |
EXPECT_EQ(i, 5 - n); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
TEST(STLExtrasTest, DropEndDefaultTest) { |
| 760 |
SmallVector<int, 5> vec{0, 1, 2, 3, 4}; |
| 761 |
|
| 762 |
int i = 0; |
| 763 |
for (auto &v : drop_end(vec)) { |
| 764 |
EXPECT_EQ(v, i); |
| 765 |
i += 1; |
| 766 |
} |
| 767 |
EXPECT_EQ(i, 4); |
| 768 |
} |
| 769 |
|
| 770 |
TEST(STLExtrasTest, MapRangeTest) { |
| 771 |
SmallVector<int, 5> Vec{0, 1, 2}; |
| 772 |
EXPECT_THAT(map_range(Vec, [](int V) { return V + 1; }), |
| 773 |
ElementsAre(1, 2, 3)); |
| 774 |
|
| 775 |
// Make sure that we use the `begin`/`end` functions |
| 776 |
// from `some_namespace`, using ADL. |
| 777 |
some_namespace::some_struct S; |
| 778 |
S.data = {3, 4, 5}; |
| 779 |
EXPECT_THAT(map_range(S, [](int V) { return V * 2; }), ElementsAre(6, 8, 10)); |
| 780 |
} |
| 781 |
|
| 782 |
TEST(STLExtrasTest, EarlyIncrementTest) { |
| 783 |
std::list<int> L = {1, 2, 3, 4}; |
| 784 |
|
| 785 |
auto EIR = make_early_inc_range(L); |
| 786 |
|
| 787 |
auto I = EIR.begin(); |
| 788 |
auto EI = EIR.end(); |
| 789 |
EXPECT_NE(I, EI); |
| 790 |
|
| 791 |
EXPECT_EQ(1, *I); |
| 792 |
#if LLVM_ENABLE_ABI_BREAKING_CHECKS |
| 793 |
#ifndef NDEBUG |
| 794 |
// Repeated dereferences are not allowed. |
| 795 |
EXPECT_DEATH(*I, "Cannot dereference"); |
| 796 |
// Comparison after dereference is not allowed. |
| 797 |
EXPECT_DEATH((void)(I == EI), "Cannot compare"); |
| 798 |
EXPECT_DEATH((void)(I != EI), "Cannot compare"); |
| 799 |
#endif |
| 800 |
#endif |
| 801 |
|
| 802 |
++I; |
| 803 |
EXPECT_NE(I, EI); |
| 804 |
#if LLVM_ENABLE_ABI_BREAKING_CHECKS |
| 805 |
#ifndef NDEBUG |
| 806 |
// You cannot increment prior to dereference. |
| 807 |
EXPECT_DEATH(++I, "Cannot increment"); |
| 808 |
#endif |
| 809 |
#endif |
| 810 |
EXPECT_EQ(2, *I); |
| 811 |
#if LLVM_ENABLE_ABI_BREAKING_CHECKS |
| 812 |
#ifndef NDEBUG |
| 813 |
// Repeated dereferences are not allowed. |
| 814 |
EXPECT_DEATH(*I, "Cannot dereference"); |
| 815 |
#endif |
| 816 |
#endif |
| 817 |
|
| 818 |
// Inserting shouldn't break anything. We should be able to keep dereferencing |
| 819 |
// the current iterator and increment. The increment to go to the "next" |
| 820 |
// iterator from before we inserted. |
| 821 |
L.insert(std::next(L.begin(), 2), -1); |
| 822 |
++I; |
| 823 |
EXPECT_EQ(3, *I); |
| 824 |
|
| 825 |
// Erasing the front including the current doesn't break incrementing. |
| 826 |
L.erase(L.begin(), std::prev(L.end())); |
| 827 |
++I; |
| 828 |
EXPECT_EQ(4, *I); |
| 829 |
++I; |
| 830 |
EXPECT_EQ(EIR.end(), I); |
| 831 |
} |
| 832 |
|
| 833 |
TEST(STLExtrasTest, EarlyIncADLTest) { |
| 834 |
// Make sure that we use the `begin`/`end` functions from `some_namespace`, |
| 835 |
// using ADL. |
| 836 |
some_namespace::some_struct S; |
| 837 |
S.data = {1, 2, 3}; |
| 838 |
EXPECT_THAT(make_early_inc_range(S), ElementsAre(1, 2, 3)); |
| 839 |
} |
| 840 |
|
| 841 |
// A custom iterator that returns a pointer when dereferenced. This is used to |
| 842 |
// test make_early_inc_range with iterators that do not return a reference on |
| 843 |
// dereferencing. |
| 844 |
struct CustomPointerIterator |
| 845 |
: public iterator_adaptor_base<CustomPointerIterator, |
| 846 |
std::list<int>::iterator, |
| 847 |
std::forward_iterator_tag> { |
| 848 |
using base_type = |
| 849 |
iterator_adaptor_base<CustomPointerIterator, std::list<int>::iterator, |
| 850 |
std::forward_iterator_tag>; |
| 851 |
|
| 852 |
explicit CustomPointerIterator(std::list<int>::iterator I) : base_type(I) {} |
| 853 |
|
| 854 |
// Retrieve a pointer to the current int. |
| 855 |
int *operator*() const { return &*base_type::wrapped(); } |
| 856 |
}; |
| 857 |
|
| 858 |
// Make sure make_early_inc_range works with iterators that do not return a |
| 859 |
// reference on dereferencing. The test is similar to EarlyIncrementTest, but |
| 860 |
// uses CustomPointerIterator. |
| 861 |
TEST(STLExtrasTest, EarlyIncrementTestCustomPointerIterator) { |
| 862 |
std::list<int> L = {1, 2, 3, 4}; |
| 863 |
|
| 864 |
auto CustomRange = make_range(CustomPointerIterator(L.begin()), |
| 865 |
CustomPointerIterator(L.end())); |
| 866 |
auto EIR = make_early_inc_range(CustomRange); |
| 867 |
|
| 868 |
auto I = EIR.begin(); |
| 869 |
auto EI = EIR.end(); |
| 870 |
EXPECT_NE(I, EI); |
| 871 |
|
| 872 |
EXPECT_EQ(&*L.begin(), *I); |
| 873 |
#if LLVM_ENABLE_ABI_BREAKING_CHECKS |
| 874 |
#ifndef NDEBUG |
| 875 |
// Repeated dereferences are not allowed. |
| 876 |
EXPECT_DEATH(*I, "Cannot dereference"); |
| 877 |
// Comparison after dereference is not allowed. |
| 878 |
EXPECT_DEATH((void)(I == EI), "Cannot compare"); |
| 879 |
EXPECT_DEATH((void)(I != EI), "Cannot compare"); |
| 880 |
#endif |
| 881 |
#endif |
| 882 |
|
| 883 |
++I; |
| 884 |
EXPECT_NE(I, EI); |
| 885 |
#if LLVM_ENABLE_ABI_BREAKING_CHECKS |
| 886 |
#ifndef NDEBUG |
| 887 |
// You cannot increment prior to dereference. |
| 888 |
EXPECT_DEATH(++I, "Cannot increment"); |
| 889 |
#endif |
| 890 |
#endif |
| 891 |
EXPECT_EQ(&*std::next(L.begin()), *I); |
| 892 |
#if LLVM_ENABLE_ABI_BREAKING_CHECKS |
| 893 |
#ifndef NDEBUG |
| 894 |
// Repeated dereferences are not allowed. |
| 895 |
EXPECT_DEATH(*I, "Cannot dereference"); |
| 896 |
#endif |
| 897 |
#endif |
| 898 |
|
| 899 |
// Inserting shouldn't break anything. We should be able to keep dereferencing |
| 900 |
// the currrent iterator and increment. The increment to go to the "next" |
| 901 |
// iterator from before we inserted. |
| 902 |
L.insert(std::next(L.begin(), 2), -1); |
| 903 |
++I; |
| 904 |
EXPECT_EQ(&*std::next(L.begin(), 3), *I); |
| 905 |
|
| 906 |
// Erasing the front including the current doesn't break incrementing. |
| 907 |
L.erase(L.begin(), std::prev(L.end())); |
| 908 |
++I; |
| 909 |
EXPECT_EQ(&*L.begin(), *I); |
| 910 |
++I; |
| 911 |
EXPECT_EQ(EIR.end(), I); |
| 912 |
} |
| 913 |
|
| 914 |
TEST(STLExtrasTest, ReplaceADL) { |
| 915 |
// Make sure that we use the `begin`/`end` functions from `some_namespace`, |
| 916 |
// using ADL. |
| 917 |
std::vector<int> Cont = {0, 1, 2, 3, 4, 5}; |
| 918 |
some_namespace::some_struct S; |
| 919 |
S.data = {42, 43, 44}; |
| 920 |
replace(Cont, Cont.begin() + 2, Cont.begin() + 5, S); |
| 921 |
EXPECT_THAT(Cont, ElementsAre(0, 1, 42, 43, 44, 5)); |
| 922 |
} |
| 923 |
|
| 924 |
TEST(STLExtrasTest, AllEqual) { |
| 925 |
std::vector<int> V; |
| 926 |
EXPECT_TRUE(all_equal(V)); |
| 927 |
|
| 928 |
V.push_back(1); |
| 929 |
EXPECT_TRUE(all_equal(V)); |
| 930 |
|
| 931 |
V.push_back(1); |
| 932 |
V.push_back(1); |
| 933 |
EXPECT_TRUE(all_equal(V)); |
| 934 |
|
| 935 |
V.push_back(2); |
| 936 |
EXPECT_FALSE(all_equal(V)); |
| 937 |
} |
| 938 |
|
| 939 |
// Test to verify that all_equal works with a container that does not |
| 940 |
// model the random access iterator concept. |
| 941 |
TEST(STLExtrasTest, AllEqualNonRandomAccess) { |
| 942 |
std::list<int> V; |
| 943 |
static_assert(!std::is_convertible_v< |
| 944 |
std::iterator_traits<decltype(V)::iterator>::iterator_category, |
| 945 |
std::random_access_iterator_tag>); |
| 946 |
EXPECT_TRUE(all_equal(V)); |
| 947 |
|
| 948 |
V.push_back(1); |
| 949 |
EXPECT_TRUE(all_equal(V)); |
| 950 |
|
| 951 |
V.push_back(1); |
| 952 |
V.push_back(1); |
| 953 |
EXPECT_TRUE(all_equal(V)); |
| 954 |
|
| 955 |
V.push_back(2); |
| 956 |
EXPECT_FALSE(all_equal(V)); |
| 957 |
} |
| 958 |
|
| 959 |
TEST(STLExtrasTest, AllEqualInitializerList) { |
| 960 |
EXPECT_TRUE(all_equal({1})); |
| 961 |
EXPECT_TRUE(all_equal({1, 1})); |
| 962 |
EXPECT_FALSE(all_equal({1, 2})); |
| 963 |
EXPECT_FALSE(all_equal({2, 1})); |
| 964 |
EXPECT_TRUE(all_equal({1, 1, 1})); |
| 965 |
} |
| 966 |
|
| 967 |
TEST(STLExtrasTest, to_address) { |
| 968 |
int *V1 = new int; |
| 969 |
EXPECT_EQ(V1, to_address(V1)); |
| 970 |
|
| 971 |
// Check fancy pointer overload for unique_ptr |
| 972 |
std::unique_ptr<int> V2 = std::make_unique<int>(0); |
| 973 |
EXPECT_EQ(V2.get(), llvm::to_address(V2)); |
| 974 |
|
| 975 |
V2.reset(V1); |
| 976 |
EXPECT_EQ(V1, llvm::to_address(V2)); |
| 977 |
V2.release(); |
| 978 |
|
| 979 |
// Check fancy pointer overload for shared_ptr |
| 980 |
std::shared_ptr<int> V3 = std::make_shared<int>(0); |
| 981 |
std::shared_ptr<int> V4 = V3; |
| 982 |
EXPECT_EQ(V3.get(), V4.get()); |
| 983 |
EXPECT_EQ(V3.get(), llvm::to_address(V3)); |
| 984 |
EXPECT_EQ(V4.get(), llvm::to_address(V4)); |
| 985 |
|
| 986 |
V3.reset(V1); |
| 987 |
EXPECT_EQ(V1, llvm::to_address(V3)); |
| 988 |
} |
| 989 |
|
| 990 |
TEST(STLExtrasTest, partition_point) { |
| 991 |
std::vector<int> V = {1, 3, 5, 7, 9}; |
| 992 |
|
| 993 |
// Range version. |
| 994 |
EXPECT_EQ(V.begin() + 3, |
| 995 |
partition_point(V, [](unsigned X) { return X < 7; })); |
| 996 |
EXPECT_EQ(V.begin(), partition_point(V, [](unsigned X) { return X < 1; })); |
| 997 |
EXPECT_EQ(V.end(), partition_point(V, [](unsigned X) { return X < 50; })); |
| 998 |
} |
| 999 |
|
| 1000 |
TEST(STLExtrasTest, hasSingleElement) { |
| 1001 |
const std::vector<int> V0 = {}, V1 = {1}, V2 = {1, 2}; |
| 1002 |
const std::vector<int> V10(10); |
| 1003 |
|
| 1004 |
EXPECT_FALSE(hasSingleElement(V0)); |
| 1005 |
EXPECT_TRUE(hasSingleElement(V1)); |
| 1006 |
EXPECT_FALSE(hasSingleElement(V2)); |
| 1007 |
EXPECT_FALSE(hasSingleElement(V10)); |
| 1008 |
|
| 1009 |
// Make sure that we use the `begin`/`end` functions |
| 1010 |
// from `some_namespace`, using ADL. |
| 1011 |
some_namespace::some_struct S; |
| 1012 |
EXPECT_FALSE(hasSingleElement(S)); |
| 1013 |
S.data = V1; |
| 1014 |
EXPECT_TRUE(hasSingleElement(S)); |
| 1015 |
S.data = V2; |
| 1016 |
EXPECT_FALSE(hasSingleElement(S)); |
| 1017 |
} |
| 1018 |
|
| 1019 |
TEST(STLExtrasTest, getSingleElement) { |
| 1020 |
// Test const and non-const containers. |
| 1021 |
const std::vector<int> V1 = {7}; |
| 1022 |
EXPECT_EQ(getSingleElement(V1), 7); |
| 1023 |
std::vector<int> V2 = {8}; |
| 1024 |
EXPECT_EQ(getSingleElement(V2), 8); |
| 1025 |
|
| 1026 |
// Test LLVM container. |
| 1027 |
SmallVector<int> V3{9}; |
| 1028 |
EXPECT_EQ(getSingleElement(V3), 9); |
| 1029 |
|
| 1030 |
// Test that the returned element is a reference. |
| 1031 |
getSingleElement(V3) = 11; |
| 1032 |
EXPECT_EQ(V3[0], 11); |
| 1033 |
|
| 1034 |
// Test non-random access container. |
| 1035 |
std::list<int> L1 = {10}; |
| 1036 |
EXPECT_EQ(getSingleElement(L1), 10); |
| 1037 |
|
| 1038 |
// Make sure that we use the `begin`/`end` functions from `some_namespace`, |
| 1039 |
// using ADL. |
| 1040 |
some_namespace::some_struct S; |
| 1041 |
S.data = V2; |
| 1042 |
EXPECT_EQ(getSingleElement(S), 8); |
| 1043 |
|
| 1044 |
#if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) |
| 1045 |
// Make sure that we crash on empty or too many elements. |
| 1046 |
SmallVector<int> V4; |
| 1047 |
EXPECT_DEATH(getSingleElement(V4), "expected container with single element"); |
| 1048 |
SmallVector<int> V5{12, 13, 14}; |
| 1049 |
EXPECT_DEATH(getSingleElement(V5), "expected container with single element"); |
| 1050 |
std::list<int> L2; |
| 1051 |
EXPECT_DEATH(getSingleElement(L2), "expected container with single element"); |
| 1052 |
#endif |
| 1053 |
} |
| 1054 |
|
| 1055 |
TEST(STLExtrasTest, hasNItems) { |
| 1056 |
const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; |
| 1057 |
const std::list<int> V3 = {1, 3, 5}; |
| 1058 |
|
| 1059 |
EXPECT_TRUE(hasNItems(V0, 0)); |
| 1060 |
EXPECT_FALSE(hasNItems(V0, 2)); |
| 1061 |
EXPECT_TRUE(hasNItems(V1, 1)); |
| 1062 |
EXPECT_FALSE(hasNItems(V1, 2)); |
| 1063 |
|
| 1064 |
EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 3, [](int x) { return x < 10; })); |
| 1065 |
EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 0, [](int x) { return x > 10; })); |
| 1066 |
EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); |
| 1067 |
|
| 1068 |
// Make sure that we use the `begin`/`end` functions from `some_namespace`, |
| 1069 |
// using ADL. |
| 1070 |
some_namespace::List L; |
| 1071 |
L.data = {0, 1, 2}; |
| 1072 |
EXPECT_FALSE(hasNItems(L, 2)); |
| 1073 |
EXPECT_TRUE(hasNItems(L, 3)); |
| 1074 |
} |
| 1075 |
|
| 1076 |
TEST(STLExtras, hasNItemsOrMore) { |
| 1077 |
const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; |
| 1078 |
const std::list<int> V3 = {1, 3, 5}; |
| 1079 |
|
| 1080 |
EXPECT_TRUE(hasNItemsOrMore(V1, 1)); |
| 1081 |
EXPECT_FALSE(hasNItemsOrMore(V1, 2)); |
| 1082 |
|
| 1083 |
EXPECT_TRUE(hasNItemsOrMore(V2, 1)); |
| 1084 |
EXPECT_TRUE(hasNItemsOrMore(V2, 2)); |
| 1085 |
EXPECT_FALSE(hasNItemsOrMore(V2, 3)); |
| 1086 |
|
| 1087 |
EXPECT_TRUE(hasNItemsOrMore(V3, 3)); |
| 1088 |
EXPECT_FALSE(hasNItemsOrMore(V3, 4)); |
| 1089 |
|
| 1090 |
EXPECT_TRUE( |
| 1091 |
hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x < 10; })); |
| 1092 |
EXPECT_FALSE( |
| 1093 |
hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x > 10; })); |
| 1094 |
EXPECT_TRUE( |
| 1095 |
hasNItemsOrMore(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); |
| 1096 |
|
| 1097 |
// Make sure that we use the `begin`/`end` functions from `some_namespace`, |
| 1098 |
// using ADL. |
| 1099 |
some_namespace::List L; |
| 1100 |
L.data = {0, 1, 2}; |
| 1101 |
EXPECT_TRUE(hasNItemsOrMore(L, 1)); |
| 1102 |
EXPECT_FALSE(hasNItems(L, 4)); |
| 1103 |
} |
| 1104 |
|
| 1105 |
TEST(STLExtras, hasNItemsOrLess) { |
| 1106 |
const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; |
| 1107 |
const std::list<int> V3 = {1, 3, 5}; |
| 1108 |
|
| 1109 |
EXPECT_TRUE(hasNItemsOrLess(V0, 0)); |
| 1110 |
EXPECT_TRUE(hasNItemsOrLess(V0, 1)); |
| 1111 |
EXPECT_TRUE(hasNItemsOrLess(V0, 2)); |
| 1112 |
|
| 1113 |
EXPECT_FALSE(hasNItemsOrLess(V1, 0)); |
| 1114 |
EXPECT_TRUE(hasNItemsOrLess(V1, 1)); |
| 1115 |
EXPECT_TRUE(hasNItemsOrLess(V1, 2)); |
| 1116 |
|
| 1117 |
EXPECT_FALSE(hasNItemsOrLess(V2, 0)); |
| 1118 |
EXPECT_FALSE(hasNItemsOrLess(V2, 1)); |
| 1119 |
EXPECT_TRUE(hasNItemsOrLess(V2, 2)); |
| 1120 |
EXPECT_TRUE(hasNItemsOrLess(V2, 3)); |
| 1121 |
|
| 1122 |
EXPECT_FALSE(hasNItemsOrLess(V3, 0)); |
| 1123 |
EXPECT_FALSE(hasNItemsOrLess(V3, 1)); |
| 1124 |
EXPECT_FALSE(hasNItemsOrLess(V3, 2)); |
| 1125 |
EXPECT_TRUE(hasNItemsOrLess(V3, 3)); |
| 1126 |
EXPECT_TRUE(hasNItemsOrLess(V3, 4)); |
| 1127 |
|
| 1128 |
EXPECT_TRUE( |
| 1129 |
hasNItemsOrLess(V3.begin(), V3.end(), 1, [](int x) { return x == 1; })); |
| 1130 |
EXPECT_TRUE( |
| 1131 |
hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); |
| 1132 |
EXPECT_TRUE( |
| 1133 |
hasNItemsOrLess(V3.begin(), V3.end(), 5, [](int x) { return x < 5; })); |
| 1134 |
EXPECT_FALSE( |
| 1135 |
hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 10; })); |
| 1136 |
|
| 1137 |
// Make sure that we use the `begin`/`end` functions from `some_namespace`, |
| 1138 |
// using ADL. |
| 1139 |
some_namespace::List L; |
| 1140 |
L.data = {0, 1, 2}; |
| 1141 |
EXPECT_FALSE(hasNItemsOrLess(L, 1)); |
| 1142 |
EXPECT_TRUE(hasNItemsOrLess(L, 4)); |
| 1143 |
} |
| 1144 |
|
| 1145 |
TEST(STLExtras, MoveRange) { |
| 1146 |
class Foo { |
| 1147 |
bool A; |
| 1148 |
|
| 1149 |
public: |
| 1150 |
Foo() : A(true) {} |
| 1151 |
Foo(const Foo &) = delete; |
| 1152 |
Foo(Foo &&Other) : A(Other.A) { Other.A = false; } |
| 1153 |
Foo &operator=(const Foo &) = delete; |
| 1154 |
Foo &operator=(Foo &&Other) { |
| 1155 |
if (this != &Other) { |
| 1156 |
A = Other.A; |
| 1157 |
Other.A = false; |
| 1158 |
} |
| 1159 |
return *this; |
| 1160 |
} |
| 1161 |
operator bool() const { return A; } |
| 1162 |
}; |
| 1163 |
SmallVector<Foo, 4U> V1, V2, V3, V4; |
| 1164 |
auto HasVal = [](const Foo &Item) { return static_cast<bool>(Item); }; |
| 1165 |
auto Build = [&] { |
| 1166 |
SmallVector<Foo, 4U> Foos; |
| 1167 |
Foos.resize(4U); |
| 1168 |
return Foos; |
| 1169 |
}; |
| 1170 |
|
| 1171 |
V1.resize(4U); |
| 1172 |
EXPECT_TRUE(llvm::all_of(V1, HasVal)); |
| 1173 |
|
| 1174 |
llvm::move(V1, std::back_inserter(V2)); |
| 1175 |
|
| 1176 |
// Ensure input container is same size, but its contents were moved out. |
| 1177 |
EXPECT_EQ(V1.size(), 4U); |
| 1178 |
EXPECT_TRUE(llvm::none_of(V1, HasVal)); |
| 1179 |
|
| 1180 |
// Ensure output container has the contents of the input container. |
| 1181 |
EXPECT_EQ(V2.size(), 4U); |
| 1182 |
EXPECT_TRUE(llvm::all_of(V2, HasVal)); |
| 1183 |
|
| 1184 |
llvm::move(std::move(V2), std::back_inserter(V3)); |
| 1185 |
|
| 1186 |
EXPECT_TRUE(llvm::none_of(V2, HasVal)); |
| 1187 |
EXPECT_EQ(V3.size(), 4U); |
| 1188 |
EXPECT_TRUE(llvm::all_of(V3, HasVal)); |
| 1189 |
|
| 1190 |
llvm::move(Build(), std::back_inserter(V4)); |
| 1191 |
EXPECT_EQ(V4.size(), 4U); |
| 1192 |
EXPECT_TRUE(llvm::all_of(V4, HasVal)); |
| 1193 |
} |
| 1194 |
|
| 1195 |
TEST(STLExtras, Unique) { |
| 1196 |
std::vector<int> V = {1, 5, 5, 4, 3, 3, 3}; |
| 1197 |
|
| 1198 |
auto I = llvm::unique(V, [](int a, int b) { return a == b; }); |
| 1199 |
|
| 1200 |
EXPECT_EQ(I, V.begin() + 4); |
| 1201 |
|
| 1202 |
EXPECT_EQ(1, V[0]); |
| 1203 |
EXPECT_EQ(5, V[1]); |
| 1204 |
EXPECT_EQ(4, V[2]); |
| 1205 |
EXPECT_EQ(3, V[3]); |
| 1206 |
} |
| 1207 |
|
| 1208 |
TEST(STLExtras, UniqueNoPred) { |
| 1209 |
std::vector<int> V = {1, 5, 5, 4, 3, 3, 3}; |
| 1210 |
|
| 1211 |
auto I = llvm::unique(V); |
| 1212 |
|
| 1213 |
EXPECT_EQ(I, V.begin() + 4); |
| 1214 |
|
| 1215 |
EXPECT_EQ(1, V[0]); |
| 1216 |
EXPECT_EQ(5, V[1]); |
| 1217 |
EXPECT_EQ(4, V[2]); |
| 1218 |
EXPECT_EQ(3, V[3]); |
| 1219 |
} |
| 1220 |
|
| 1221 |
TEST(STLExtrasTest, MakeVisitorOneCallable) { |
| 1222 |
auto IdentityLambda = [](auto X) { return X; }; |
| 1223 |
auto IdentityVisitor = makeVisitor(IdentityLambda); |
| 1224 |
EXPECT_EQ(IdentityLambda(1), IdentityVisitor(1)); |
| 1225 |
EXPECT_EQ(IdentityLambda(2.0f), IdentityVisitor(2.0f)); |
| 1226 |
EXPECT_TRUE((std::is_same<decltype(IdentityLambda(IdentityLambda)), |
| 1227 |
decltype(IdentityLambda)>::value)); |
| 1228 |
EXPECT_TRUE((std::is_same<decltype(IdentityVisitor(IdentityVisitor)), |
| 1229 |
decltype(IdentityVisitor)>::value)); |
| 1230 |
} |
| 1231 |
|
| 1232 |
TEST(STLExtrasTest, MakeVisitorTwoCallables) { |
| 1233 |
auto Visitor = |
| 1234 |
makeVisitor([](int) { return 0; }, [](std::string) { return 1; }); |
| 1235 |
EXPECT_EQ(Visitor(42), 0); |
| 1236 |
EXPECT_EQ(Visitor("foo"), 1); |
| 1237 |
} |
| 1238 |
|
| 1239 |
TEST(STLExtrasTest, MakeVisitorCallableMultipleOperands) { |
| 1240 |
auto Second = makeVisitor([](int I, float F) { return F; }, |
| 1241 |
[](float F, int I) { return I; }); |
| 1242 |
EXPECT_EQ(Second(1.f, 1), 1); |
| 1243 |
EXPECT_EQ(Second(1, 1.f), 1.f); |
| 1244 |
} |
| 1245 |
|
| 1246 |
TEST(STLExtrasTest, MakeVisitorDefaultCase) { |
| 1247 |
{ |
| 1248 |
auto Visitor = makeVisitor([](int I) { return I + 100; }, |
| 1249 |
[](float F) { return F * 2; }, |
| 1250 |
[](auto) { return -1; }); |
| 1251 |
EXPECT_EQ(Visitor(24), 124); |
| 1252 |
EXPECT_EQ(Visitor(2.f), 4.f); |
| 1253 |
EXPECT_EQ(Visitor(2.), -1); |
| 1254 |
EXPECT_EQ(Visitor(Visitor), -1); |
| 1255 |
} |
| 1256 |
{ |
| 1257 |
auto Visitor = makeVisitor([](auto) { return -1; }, |
| 1258 |
[](int I) { return I + 100; }, |
| 1259 |
[](float F) { return F * 2; }); |
| 1260 |
EXPECT_EQ(Visitor(24), 124); |
| 1261 |
EXPECT_EQ(Visitor(2.f), 4.f); |
| 1262 |
EXPECT_EQ(Visitor(2.), -1); |
| 1263 |
EXPECT_EQ(Visitor(Visitor), -1); |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
template <bool Moveable, bool Copyable> |
| 1268 |
struct Functor : Counted<Moveable, Copyable> { |
| 1269 |
using Counted<Moveable, Copyable>::Counted; |
| 1270 |
void operator()() {} |
| 1271 |
}; |
| 1272 |
|
| 1273 |
TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsPRValue) { |
| 1274 |
int Copies = 0; |
| 1275 |
int Moves = 0; |
| 1276 |
int Destructors = 0; |
| 1277 |
{ |
| 1278 |
auto V = makeVisitor(Functor<true, false>(Copies, Moves, Destructors)); |
| 1279 |
(void)V; |
| 1280 |
EXPECT_EQ(0, Copies); |
| 1281 |
EXPECT_EQ(1, Moves); |
| 1282 |
EXPECT_EQ(1, Destructors); |
| 1283 |
} |
| 1284 |
EXPECT_EQ(0, Copies); |
| 1285 |
EXPECT_EQ(1, Moves); |
| 1286 |
EXPECT_EQ(2, Destructors); |
| 1287 |
} |
| 1288 |
|
| 1289 |
TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsRValue) { |
| 1290 |
int Copies = 0; |
| 1291 |
int Moves = 0; |
| 1292 |
int Destructors = 0; |
| 1293 |
{ |
| 1294 |
Functor<true, false> F(Copies, Moves, Destructors); |
| 1295 |
{ |
| 1296 |
auto V = makeVisitor(std::move(F)); |
| 1297 |
(void)V; |
| 1298 |
EXPECT_EQ(0, Copies); |
| 1299 |
EXPECT_EQ(1, Moves); |
| 1300 |
EXPECT_EQ(0, Destructors); |
| 1301 |
} |
| 1302 |
EXPECT_EQ(0, Copies); |
| 1303 |
EXPECT_EQ(1, Moves); |
| 1304 |
EXPECT_EQ(1, Destructors); |
| 1305 |
} |
| 1306 |
EXPECT_EQ(0, Copies); |
| 1307 |
EXPECT_EQ(1, Moves); |
| 1308 |
EXPECT_EQ(2, Destructors); |
| 1309 |
} |
| 1310 |
|
| 1311 |
TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsLValue) { |
| 1312 |
int Copies = 0; |
| 1313 |
int Moves = 0; |
| 1314 |
int Destructors = 0; |
| 1315 |
{ |
| 1316 |
Functor<true, true> F(Copies, Moves, Destructors); |
| 1317 |
{ |
| 1318 |
auto V = makeVisitor(F); |
| 1319 |
(void)V; |
| 1320 |
EXPECT_EQ(1, Copies); |
| 1321 |
EXPECT_EQ(0, Moves); |
| 1322 |
EXPECT_EQ(0, Destructors); |
| 1323 |
} |
| 1324 |
EXPECT_EQ(1, Copies); |
| 1325 |
EXPECT_EQ(0, Moves); |
| 1326 |
EXPECT_EQ(1, Destructors); |
| 1327 |
} |
| 1328 |
EXPECT_EQ(1, Copies); |
| 1329 |
EXPECT_EQ(0, Moves); |
| 1330 |
EXPECT_EQ(2, Destructors); |
| 1331 |
} |
| 1332 |
|
| 1333 |
TEST(STLExtrasTest, AllOfZip) { |
| 1334 |
std::vector<int> v1 = {0, 4, 2, 1}; |
| 1335 |
std::vector<int> v2 = {1, 4, 3, 6}; |
| 1336 |
EXPECT_TRUE(all_of_zip(v1, v2, [](int v1, int v2) { return v1 <= v2; })); |
| 1337 |
EXPECT_FALSE(all_of_zip(v1, v2, [](int L, int R) { return L < R; })); |
| 1338 |
|
| 1339 |
// Triple vectors |
| 1340 |
std::vector<int> v3 = {1, 6, 5, 7}; |
| 1341 |
EXPECT_EQ(true, all_of_zip(v1, v2, v3, [](int a, int b, int c) { |
| 1342 |
return a <= b && b <= c; |
| 1343 |
})); |
| 1344 |
EXPECT_EQ(false, all_of_zip(v1, v2, v3, [](int a, int b, int c) { |
| 1345 |
return a < b && b < c; |
| 1346 |
})); |
| 1347 |
|
| 1348 |
// Shorter vector should fail even with an always-true predicate. |
| 1349 |
std::vector<int> v_short = {1, 4}; |
| 1350 |
EXPECT_EQ(false, all_of_zip(v1, v_short, [](int, int) { return true; })); |
| 1351 |
EXPECT_EQ(false, |
| 1352 |
all_of_zip(v1, v2, v_short, [](int, int, int) { return true; })); |
| 1353 |
} |
| 1354 |
|
| 1355 |
TEST(STLExtrasTest, TypesAreDistinct) { |
| 1356 |
EXPECT_TRUE((llvm::TypesAreDistinct<>::value)); |
| 1357 |
EXPECT_TRUE((llvm::TypesAreDistinct<int>::value)); |
| 1358 |
EXPECT_FALSE((llvm::TypesAreDistinct<int, int>::value)); |
| 1359 |
EXPECT_TRUE((llvm::TypesAreDistinct<int, float>::value)); |
| 1360 |
EXPECT_FALSE((llvm::TypesAreDistinct<int, float, int>::value)); |
| 1361 |
EXPECT_TRUE((llvm::TypesAreDistinct<int, float, double>::value)); |
| 1362 |
EXPECT_FALSE((llvm::TypesAreDistinct<int, float, double, float>::value)); |
| 1363 |
EXPECT_TRUE((llvm::TypesAreDistinct<int, int *>::value)); |
| 1364 |
EXPECT_TRUE((llvm::TypesAreDistinct<int, int &>::value)); |
| 1365 |
EXPECT_TRUE((llvm::TypesAreDistinct<int, int &&>::value)); |
| 1366 |
EXPECT_TRUE((llvm::TypesAreDistinct<int, const int>::value)); |
| 1367 |
} |
| 1368 |
|
| 1369 |
TEST(STLExtrasTest, FirstIndexOfType) { |
| 1370 |
EXPECT_EQ((llvm::FirstIndexOfType<int, int>::value), 0u); |
| 1371 |
EXPECT_EQ((llvm::FirstIndexOfType<int, int, int>::value), 0u); |
| 1372 |
EXPECT_EQ((llvm::FirstIndexOfType<int, float, int>::value), 1u); |
| 1373 |
EXPECT_EQ((llvm::FirstIndexOfType<int const *, float, int, int const *, |
| 1374 |
const int>::value), |
| 1375 |
2u); |
| 1376 |
} |
| 1377 |
|
| 1378 |
TEST(STLExtrasTest, TypeAtIndex) { |
| 1379 |
EXPECT_TRUE((std::is_same<int, llvm::TypeAtIndex<0, int>>::value)); |
| 1380 |
EXPECT_TRUE((std::is_same<int, llvm::TypeAtIndex<0, int, float>>::value)); |
| 1381 |
EXPECT_TRUE((std::is_same<float, llvm::TypeAtIndex<1, int, float>>::value)); |
| 1382 |
EXPECT_TRUE( |
| 1383 |
(std::is_same<float, llvm::TypeAtIndex<1, int, float, double>>::value)); |
| 1384 |
EXPECT_TRUE( |
| 1385 |
(std::is_same<float, llvm::TypeAtIndex<1, int, float, double>>::value)); |
| 1386 |
EXPECT_TRUE( |
| 1387 |
(std::is_same<double, llvm::TypeAtIndex<2, int, float, double>>::value)); |
| 1388 |
} |
| 1389 |
|
| 1390 |
enum Doggos { |
| 1391 |
Floofer, |
| 1392 |
Woofer, |
| 1393 |
SubWoofer, |
| 1394 |
Pupper, |
| 1395 |
Pupperino, |
| 1396 |
Longboi, |
| 1397 |
}; |
| 1398 |
|
| 1399 |
struct WooferCmp { |
| 1400 |
// Not copyable. |
| 1401 |
WooferCmp() = default; |
| 1402 |
WooferCmp(const WooferCmp &) = delete; |
| 1403 |
WooferCmp &operator=(const WooferCmp &) = delete; |
| 1404 |
|
| 1405 |
friend bool operator==(const Doggos &Doggo, const WooferCmp &) { |
| 1406 |
return Doggo == Doggos::Woofer; |
| 1407 |
} |
| 1408 |
}; |
| 1409 |
|
| 1410 |
TEST(STLExtrasTest, IsContainedInitializerList) { |
| 1411 |
EXPECT_TRUE(is_contained({Woofer, SubWoofer}, Woofer)); |
| 1412 |
EXPECT_TRUE(is_contained({Woofer, SubWoofer}, SubWoofer)); |
| 1413 |
EXPECT_FALSE(is_contained({Woofer, SubWoofer}, Pupper)); |
| 1414 |
|
| 1415 |
// Check that the initializer list type and the element type do not have to |
| 1416 |
// match exactly. |
| 1417 |
EXPECT_TRUE(is_contained({Floofer, Woofer, SubWoofer}, WooferCmp{})); |
| 1418 |
EXPECT_FALSE(is_contained({Floofer, SubWoofer}, WooferCmp{})); |
| 1419 |
|
| 1420 |
EXPECT_TRUE(is_contained({"a", "bb", "ccc", "dddd"}, llvm::StringRef("ccc"))); |
| 1421 |
EXPECT_FALSE(is_contained({"a", "bb", "ccc", "dddd"}, llvm::StringRef("x"))); |
| 1422 |
|
| 1423 |
static_assert(is_contained({Woofer, SubWoofer}, SubWoofer), "SubWoofer!"); |
| 1424 |
static_assert(!is_contained({Woofer, SubWoofer}, Pupper), "Missing Pupper!"); |
| 1425 |
|
| 1426 |
EXPECT_TRUE(is_contained({1, 2, 3, 4}, 3)); |
| 1427 |
EXPECT_FALSE(is_contained({1, 2, 3, 4}, 5)); |
| 1428 |
|
| 1429 |
static_assert(is_contained({1, 2, 3, 4}, 3), "It's there!"); |
| 1430 |
static_assert(!is_contained({1, 2, 3, 4}, 5), "It's not there :("); |
| 1431 |
} |
| 1432 |
|
| 1433 |
TEST(STLExtrasTest, IsContainedMemberContains) { |
| 1434 |
// Check that `llvm::is_contained` uses the member `.contains()` when |
| 1435 |
// available. Check that `.contains()` is preferred over `.find()`. |
| 1436 |
struct Foo { |
| 1437 |
bool contains(int) const { |
| 1438 |
++NumContainsCalls; |
| 1439 |
return ContainsResult; |
| 1440 |
} |
| 1441 |
int *begin() { return nullptr; } |
| 1442 |
int *end() { return nullptr; } |
| 1443 |
int *find(int) { return nullptr; } |
| 1444 |
|
| 1445 |
bool ContainsResult = false; |
| 1446 |
mutable unsigned NumContainsCalls = 0; |
| 1447 |
} Container; |
| 1448 |
|
| 1449 |
EXPECT_EQ(Container.NumContainsCalls, 0u); |
| 1450 |
EXPECT_FALSE(is_contained(Container, 1)); |
| 1451 |
EXPECT_EQ(Container.NumContainsCalls, 1u); |
| 1452 |
|
| 1453 |
Container.ContainsResult = true; |
| 1454 |
EXPECT_TRUE(is_contained(Container, 1)); |
| 1455 |
EXPECT_EQ(Container.NumContainsCalls, 2u); |
| 1456 |
} |
| 1457 |
|
| 1458 |
TEST(STLExtrasTest, IsContainedMemberFind) { |
| 1459 |
// Check that `llvm::is_contained` uses the member `.find(x)` when available. |
| 1460 |
struct Foo { |
| 1461 |
auto begin() { return Data.begin(); } |
| 1462 |
auto end() { return Data.end(); } |
| 1463 |
auto find(int X) { |
| 1464 |
++NumFindCalls; |
| 1465 |
return std::find(begin(), end(), X); |
| 1466 |
} |
| 1467 |
|
| 1468 |
std::vector<int> Data; |
| 1469 |
mutable unsigned NumFindCalls = 0; |
| 1470 |
} Container; |
| 1471 |
|
| 1472 |
Container.Data = {1, 2, 3}; |
| 1473 |
|
| 1474 |
EXPECT_EQ(Container.NumFindCalls, 0u); |
| 1475 |
EXPECT_TRUE(is_contained(Container, 1)); |
| 1476 |
EXPECT_TRUE(is_contained(Container, 3)); |
| 1477 |
EXPECT_EQ(Container.NumFindCalls, 2u); |
| 1478 |
|
| 1479 |
EXPECT_FALSE(is_contained(Container, 4)); |
| 1480 |
EXPECT_EQ(Container.NumFindCalls, 3u); |
| 1481 |
} |
| 1482 |
|
| 1483 |
TEST(STLExtrasTest, addEnumValues) { |
| 1484 |
enum A { Zero = 0, One = 1 }; |
| 1485 |
enum B { IntMax = INT_MAX, ULongLongMax = ULLONG_MAX }; |
| 1486 |
enum class C : unsigned { Two = 2 }; |
| 1487 |
|
| 1488 |
// Non-fixed underlying types, with same underlying types |
| 1489 |
static_assert(addEnumValues(Zero, One) == 1, |
| 1490 |
"addEnumValues(Zero, One) failed."); |
| 1491 |
static_assert(addEnumValues(IntMax, ULongLongMax) == |
| 1492 |
INT_MAX + static_cast<unsigned long long>(ULLONG_MAX), |
| 1493 |
"addEnumValues(IntMax, ULongLongMax) failed."); |
| 1494 |
// Non-fixed underlying types, with different underlying types |
| 1495 |
static_assert(addEnumValues(Zero, IntMax) == INT_MAX, |
| 1496 |
"addEnumValues(Zero, IntMax) failed."); |
| 1497 |
static_assert(addEnumValues(One, ULongLongMax) == |
| 1498 |
1 + static_cast<unsigned long long>(ULLONG_MAX), |
| 1499 |
"addEnumValues(One, ULongLongMax) failed."); |
| 1500 |
// Non-fixed underlying type enum and fixed underlying type enum, with same |
| 1501 |
// underlying types |
| 1502 |
static_assert(addEnumValues(One, C::Two) == 3, |
| 1503 |
"addEnumValues(One, C::Two) failed."); |
| 1504 |
// Non-fixed underlying type enum and fixed underlying type enum, with |
| 1505 |
// different underlying types |
| 1506 |
static_assert(addEnumValues(ULongLongMax, C::Two) == |
| 1507 |
static_cast<unsigned long long>(ULLONG_MAX) + 2, |
| 1508 |
"addEnumValues(ULongLongMax, C::Two) failed."); |
| 1509 |
} |
| 1510 |
|
| 1511 |
TEST(STLExtrasTest, LessFirst) { |
| 1512 |
{ |
| 1513 |
std::pair<int, int> A(0, 1); |
| 1514 |
std::pair<int, int> B(1, 0); |
| 1515 |
EXPECT_TRUE(less_first()(A, B)); |
| 1516 |
EXPECT_FALSE(less_first()(B, A)); |
| 1517 |
} |
| 1518 |
|
| 1519 |
{ |
| 1520 |
std::tuple<int, int> A(0, 1); |
| 1521 |
std::tuple<int, int> B(1, 0); |
| 1522 |
EXPECT_TRUE(less_first()(A, B)); |
| 1523 |
EXPECT_FALSE(less_first()(B, A)); |
| 1524 |
} |
| 1525 |
} |
| 1526 |
|
| 1527 |
TEST(STLExtrasTest, LessSecond) { |
| 1528 |
{ |
| 1529 |
std::pair<int, int> A(0, 1); |
| 1530 |
std::pair<int, int> B(1, 0); |
| 1531 |
EXPECT_FALSE(less_second()(A, B)); |
| 1532 |
EXPECT_TRUE(less_second()(B, A)); |
| 1533 |
} |
| 1534 |
|
| 1535 |
{ |
| 1536 |
std::tuple<int, int> A(0, 1); |
| 1537 |
std::tuple<int, int> B(1, 0); |
| 1538 |
EXPECT_FALSE(less_second()(A, B)); |
| 1539 |
EXPECT_TRUE(less_second()(B, A)); |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
TEST(STLExtrasTest, Mismatch) { |
| 1544 |
{ |
| 1545 |
const int MMIndex = 5; |
| 1546 |
StringRef First = "FooBar"; |
| 1547 |
StringRef Second = "FooBaz"; |
| 1548 |
auto [MMFirst, MMSecond] = mismatch(First, Second); |
| 1549 |
EXPECT_EQ(MMFirst, First.begin() + MMIndex); |
| 1550 |
EXPECT_EQ(MMSecond, Second.begin() + MMIndex); |
| 1551 |
} |
| 1552 |
|
| 1553 |
{ |
| 1554 |
SmallVector<int> First = {0, 1, 2}; |
| 1555 |
SmallVector<int> Second = {0, 1, 2, 3}; |
| 1556 |
auto [MMFirst, MMSecond] = mismatch(First, Second); |
| 1557 |
EXPECT_EQ(MMFirst, First.end()); |
| 1558 |
EXPECT_EQ(MMSecond, Second.begin() + 3); |
| 1559 |
} |
| 1560 |
|
| 1561 |
{ |
| 1562 |
SmallVector<int> First = {0, 1}; |
| 1563 |
SmallVector<int> Empty; |
| 1564 |
auto [MMFirst, MMEmpty] = mismatch(First, Empty); |
| 1565 |
EXPECT_EQ(MMFirst, First.begin()); |
| 1566 |
EXPECT_EQ(MMEmpty, Empty.begin()); |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
TEST(STLExtrasTest, Includes) { |
| 1571 |
{ |
| 1572 |
std::vector<int> V1 = {1, 2}; |
| 1573 |
std::vector<int> V2; |
| 1574 |
EXPECT_TRUE(includes(V1, V2)); |
| 1575 |
EXPECT_FALSE(includes(V2, V1)); |
| 1576 |
V2.push_back(1); |
| 1577 |
EXPECT_TRUE(includes(V1, V2)); |
| 1578 |
V2.push_back(3); |
| 1579 |
EXPECT_FALSE(includes(V1, V2)); |
| 1580 |
} |
| 1581 |
|
| 1582 |
{ |
| 1583 |
std::vector<int> V1 = {3, 2, 1}; |
| 1584 |
std::vector<int> V2; |
| 1585 |
EXPECT_TRUE(includes(V1, V2, std::greater<>())); |
| 1586 |
EXPECT_FALSE(includes(V2, V1, std::greater<>())); |
| 1587 |
V2.push_back(3); |
| 1588 |
EXPECT_TRUE(includes(V1, V2, std::greater<>())); |
| 1589 |
V2.push_back(0); |
| 1590 |
EXPECT_FALSE(includes(V1, V2, std::greater<>())); |
| 1591 |
} |
| 1592 |
} |
| 1593 |
|
| 1594 |
TEST(STLExtrasTest, Fill) { |
| 1595 |
std::vector<int> V1 = {1, 2, 3}; |
| 1596 |
std::vector<int> V2; |
| 1597 |
int Val = 4; |
| 1598 |
fill(V1, Val); |
| 1599 |
EXPECT_THAT(V1, ElementsAre(Val, Val, Val)); |
| 1600 |
V2.resize(4); |
| 1601 |
fill(V2, Val); |
| 1602 |
EXPECT_THAT(V2, ElementsAre(Val, Val, Val, Val)); |
| 1603 |
} |
| 1604 |
|
| 1605 |
struct Foo; |
| 1606 |
struct Bar {}; |
| 1607 |
|
| 1608 |
static_assert(is_incomplete_v<Foo>, "Foo is incomplete"); |
| 1609 |
static_assert(!is_incomplete_v<Bar>, "Bar is defined"); |
| 1610 |
|
| 1611 |
} // namespace |