diff options
author | Dawid Jurczak <dawid_jurek@vp.pl> | 2022-08-04 18:26:18 +0200 |
---|---|---|
committer | Dawid Jurczak <dawid_jurek@vp.pl> | 2022-08-05 13:35:41 +0200 |
commit | 1bd31a689844a43c9cde41a067119bef11159539 (patch) | |
tree | b6ac0e18a58072294d40d1f6cb13cdb69cb3389c /llvm/unittests/ADT/SmallVectorTest.cpp | |
parent | b2de84633a0a262b894e7cf87d29b167787aa2d6 (diff) | |
download | llvm-1bd31a689844a43c9cde41a067119bef11159539.zip llvm-1bd31a689844a43c9cde41a067119bef11159539.tar.gz llvm-1bd31a689844a43c9cde41a067119bef11159539.tar.bz2 |
[NFC] Add SmallVector constructor to allow creation of SmallVector<T> from ArrayRef of items convertible to type T
Extracted from https://reviews.llvm.org/D129781 and address comment:
https://reviews.llvm.org/D129781#3655571
Differential Revision: https://reviews.llvm.org/D130268
Diffstat (limited to 'llvm/unittests/ADT/SmallVectorTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/SmallVectorTest.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp index 1ba3e7e..db94929 100644 --- a/llvm/unittests/ADT/SmallVectorTest.cpp +++ b/llvm/unittests/ADT/SmallVectorTest.cpp @@ -246,6 +246,18 @@ TYPED_TEST(SmallVectorTest, ConstructorIterTest) { assertValuesInOrder(V, 3u, 1, 2, 3); } +// Constructor test. +TYPED_TEST(SmallVectorTest, ConstructorFromArrayRefSimpleTest) { + SCOPED_TRACE("ConstructorFromArrayRefSimpleTest"); + std::array<Constructable, 3> StdArray = {Constructable(1), Constructable(2), + Constructable(3)}; + ArrayRef<Constructable> Array = StdArray; + auto &V = this->theVector; + V = SmallVector<Constructable, 4>(Array); + assertValuesInOrder(V, 3u, 1, 2, 3); + ASSERT_EQ(NumBuiltinElts(TypeParam{}), NumBuiltinElts(V)); +} + // New vector test. TYPED_TEST(SmallVectorTest, EmptyVectorTest) { SCOPED_TRACE("EmptyVectorTest"); @@ -1130,6 +1142,44 @@ TEST(SmallVectorTest, InitializerList) { EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2})); } +struct To { + int Content; + friend bool operator==(const To &LHS, const To &RHS) { + return LHS.Content == RHS.Content; + } +}; + +class From { +public: + From() = default; + From(To M) { T = M; } + operator To() const { return T; } + +private: + To T; +}; + +TEST(SmallVectorTest, ConstructFromArrayRefOfConvertibleType) { + To to1{1}, to2{2}, to3{3}; + std::vector<From> StdVector = {From(to1), From(to2), From(to3)}; + ArrayRef<From> Array = StdVector; + { + llvm::SmallVector<To> Vector(Array); + + ASSERT_EQ(Array.size(), Vector.size()); + for (size_t I = 0; I < Array.size(); ++I) + EXPECT_EQ(Array[I], Vector[I]); + } + { + llvm::SmallVector<To, 4> Vector(Array); + + ASSERT_EQ(Array.size(), Vector.size()); + ASSERT_EQ(4u, NumBuiltinElts(Vector)); + for (size_t I = 0; I < Array.size(); ++I) + EXPECT_EQ(Array[I], Vector[I]); + } +} + template <class VectorT> class SmallVectorReferenceInvalidationTest : public SmallVectorTestBase { protected: |