1476a6dabSNick Lewycky //===- unittest/ADT/MapVectorTest.cpp - MapVector unit tests ----*- C++ -*-===//
2476a6dabSNick Lewycky //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6476a6dabSNick Lewycky //
7476a6dabSNick Lewycky //===----------------------------------------------------------------------===//
8476a6dabSNick Lewycky
9476a6dabSNick Lewycky #include "llvm/ADT/MapVector.h"
10d71825c3SMichael Gottesman #include "llvm/ADT/iterator_range.h"
119a67b073SChandler Carruth #include "gtest/gtest.h"
12476a6dabSNick Lewycky #include <utility>
13476a6dabSNick Lewycky
14476a6dabSNick Lewycky using namespace llvm;
15476a6dabSNick Lewycky
TEST(MapVectorTest,swap)16d811ffa5SChandler Carruth TEST(MapVectorTest, swap) {
17d811ffa5SChandler Carruth MapVector<int, int> MV1, MV2;
18d811ffa5SChandler Carruth std::pair<MapVector<int, int>::iterator, bool> R;
19d811ffa5SChandler Carruth
20d811ffa5SChandler Carruth R = MV1.insert(std::make_pair(1, 2));
21d811ffa5SChandler Carruth ASSERT_EQ(R.first, MV1.begin());
22d811ffa5SChandler Carruth EXPECT_EQ(R.first->first, 1);
23d811ffa5SChandler Carruth EXPECT_EQ(R.first->second, 2);
24d811ffa5SChandler Carruth EXPECT_TRUE(R.second);
25d811ffa5SChandler Carruth
26d811ffa5SChandler Carruth EXPECT_FALSE(MV1.empty());
27d811ffa5SChandler Carruth EXPECT_TRUE(MV2.empty());
28d811ffa5SChandler Carruth MV2.swap(MV1);
29d811ffa5SChandler Carruth EXPECT_TRUE(MV1.empty());
30d811ffa5SChandler Carruth EXPECT_FALSE(MV2.empty());
31d811ffa5SChandler Carruth
32d811ffa5SChandler Carruth auto I = MV1.find(1);
33d811ffa5SChandler Carruth ASSERT_EQ(MV1.end(), I);
34d811ffa5SChandler Carruth
35d811ffa5SChandler Carruth I = MV2.find(1);
36d811ffa5SChandler Carruth ASSERT_EQ(I, MV2.begin());
37d811ffa5SChandler Carruth EXPECT_EQ(I->first, 1);
38d811ffa5SChandler Carruth EXPECT_EQ(I->second, 2);
39d811ffa5SChandler Carruth }
40d811ffa5SChandler Carruth
TEST(MapVectorTest,insert_pop)41b1c67569SDouglas Gregor TEST(MapVectorTest, insert_pop) {
42476a6dabSNick Lewycky MapVector<int, int> MV;
43476a6dabSNick Lewycky std::pair<MapVector<int, int>::iterator, bool> R;
44476a6dabSNick Lewycky
45476a6dabSNick Lewycky R = MV.insert(std::make_pair(1, 2));
46476a6dabSNick Lewycky ASSERT_EQ(R.first, MV.begin());
47476a6dabSNick Lewycky EXPECT_EQ(R.first->first, 1);
48476a6dabSNick Lewycky EXPECT_EQ(R.first->second, 2);
49476a6dabSNick Lewycky EXPECT_TRUE(R.second);
50476a6dabSNick Lewycky
51476a6dabSNick Lewycky R = MV.insert(std::make_pair(1, 3));
52476a6dabSNick Lewycky ASSERT_EQ(R.first, MV.begin());
53476a6dabSNick Lewycky EXPECT_EQ(R.first->first, 1);
54476a6dabSNick Lewycky EXPECT_EQ(R.first->second, 2);
55476a6dabSNick Lewycky EXPECT_FALSE(R.second);
56476a6dabSNick Lewycky
57476a6dabSNick Lewycky R = MV.insert(std::make_pair(4, 5));
58476a6dabSNick Lewycky ASSERT_NE(R.first, MV.end());
59476a6dabSNick Lewycky EXPECT_EQ(R.first->first, 4);
60476a6dabSNick Lewycky EXPECT_EQ(R.first->second, 5);
61476a6dabSNick Lewycky EXPECT_TRUE(R.second);
62476a6dabSNick Lewycky
63476a6dabSNick Lewycky EXPECT_EQ(MV.size(), 2u);
64476a6dabSNick Lewycky EXPECT_EQ(MV[1], 2);
65476a6dabSNick Lewycky EXPECT_EQ(MV[4], 5);
66b1c67569SDouglas Gregor
67b1c67569SDouglas Gregor MV.pop_back();
68b1c67569SDouglas Gregor EXPECT_EQ(MV.size(), 1u);
69b1c67569SDouglas Gregor EXPECT_EQ(MV[1], 2);
70b1c67569SDouglas Gregor
71b1c67569SDouglas Gregor R = MV.insert(std::make_pair(4, 7));
72b1c67569SDouglas Gregor ASSERT_NE(R.first, MV.end());
73b1c67569SDouglas Gregor EXPECT_EQ(R.first->first, 4);
74b1c67569SDouglas Gregor EXPECT_EQ(R.first->second, 7);
75b1c67569SDouglas Gregor EXPECT_TRUE(R.second);
76b1c67569SDouglas Gregor
77b1c67569SDouglas Gregor EXPECT_EQ(MV.size(), 2u);
78b1c67569SDouglas Gregor EXPECT_EQ(MV[1], 2);
79b1c67569SDouglas Gregor EXPECT_EQ(MV[4], 7);
80476a6dabSNick Lewycky }
81db88e31eSDuncan P. N. Exon Smith
TEST(MapVectorTest,erase)82db88e31eSDuncan P. N. Exon Smith TEST(MapVectorTest, erase) {
83db88e31eSDuncan P. N. Exon Smith MapVector<int, int> MV;
84db88e31eSDuncan P. N. Exon Smith
85db88e31eSDuncan P. N. Exon Smith MV.insert(std::make_pair(1, 2));
86db88e31eSDuncan P. N. Exon Smith MV.insert(std::make_pair(3, 4));
87db88e31eSDuncan P. N. Exon Smith MV.insert(std::make_pair(5, 6));
88db88e31eSDuncan P. N. Exon Smith ASSERT_EQ(MV.size(), 3u);
89db88e31eSDuncan P. N. Exon Smith
90db88e31eSDuncan P. N. Exon Smith MV.erase(MV.find(1));
91db88e31eSDuncan P. N. Exon Smith ASSERT_EQ(MV.size(), 2u);
92db88e31eSDuncan P. N. Exon Smith ASSERT_EQ(MV.find(1), MV.end());
93db88e31eSDuncan P. N. Exon Smith ASSERT_EQ(MV[3], 4);
94db88e31eSDuncan P. N. Exon Smith ASSERT_EQ(MV[5], 6);
956ad1eb4bSKaelyn Takata
961353fe8bSKaelyn Takata ASSERT_EQ(MV.erase(3), 1u);
976ad1eb4bSKaelyn Takata ASSERT_EQ(MV.size(), 1u);
986ad1eb4bSKaelyn Takata ASSERT_EQ(MV.find(3), MV.end());
996ad1eb4bSKaelyn Takata ASSERT_EQ(MV[5], 6);
1001353fe8bSKaelyn Takata
1011353fe8bSKaelyn Takata ASSERT_EQ(MV.erase(79), 0u);
1021353fe8bSKaelyn Takata ASSERT_EQ(MV.size(), 1u);
103db88e31eSDuncan P. N. Exon Smith }
104f51601c8SDuncan P. N. Exon Smith
TEST(MapVectorTest,remove_if)105f51601c8SDuncan P. N. Exon Smith TEST(MapVectorTest, remove_if) {
106f51601c8SDuncan P. N. Exon Smith MapVector<int, int> MV;
107f51601c8SDuncan P. N. Exon Smith
108f51601c8SDuncan P. N. Exon Smith MV.insert(std::make_pair(1, 11));
109f51601c8SDuncan P. N. Exon Smith MV.insert(std::make_pair(2, 12));
110f51601c8SDuncan P. N. Exon Smith MV.insert(std::make_pair(3, 13));
111f51601c8SDuncan P. N. Exon Smith MV.insert(std::make_pair(4, 14));
112f51601c8SDuncan P. N. Exon Smith MV.insert(std::make_pair(5, 15));
113f51601c8SDuncan P. N. Exon Smith MV.insert(std::make_pair(6, 16));
114f51601c8SDuncan P. N. Exon Smith ASSERT_EQ(MV.size(), 6u);
115f51601c8SDuncan P. N. Exon Smith
116f51601c8SDuncan P. N. Exon Smith MV.remove_if([](const std::pair<int, int> &Val) { return Val.second % 2; });
117f51601c8SDuncan P. N. Exon Smith ASSERT_EQ(MV.size(), 3u);
118f51601c8SDuncan P. N. Exon Smith ASSERT_EQ(MV.find(1), MV.end());
119f51601c8SDuncan P. N. Exon Smith ASSERT_EQ(MV.find(3), MV.end());
120f51601c8SDuncan P. N. Exon Smith ASSERT_EQ(MV.find(5), MV.end());
121f51601c8SDuncan P. N. Exon Smith ASSERT_EQ(MV[2], 12);
122f51601c8SDuncan P. N. Exon Smith ASSERT_EQ(MV[4], 14);
123f51601c8SDuncan P. N. Exon Smith ASSERT_EQ(MV[6], 16);
124f51601c8SDuncan P. N. Exon Smith }
125d71825c3SMichael Gottesman
TEST(MapVectorTest,iteration_test)126d71825c3SMichael Gottesman TEST(MapVectorTest, iteration_test) {
127d71825c3SMichael Gottesman MapVector<int, int> MV;
128d71825c3SMichael Gottesman
129d71825c3SMichael Gottesman MV.insert(std::make_pair(1, 11));
130d71825c3SMichael Gottesman MV.insert(std::make_pair(2, 12));
131d71825c3SMichael Gottesman MV.insert(std::make_pair(3, 13));
132d71825c3SMichael Gottesman MV.insert(std::make_pair(4, 14));
133d71825c3SMichael Gottesman MV.insert(std::make_pair(5, 15));
134d71825c3SMichael Gottesman MV.insert(std::make_pair(6, 16));
135d71825c3SMichael Gottesman ASSERT_EQ(MV.size(), 6u);
136d71825c3SMichael Gottesman
137d71825c3SMichael Gottesman int count = 1;
138d71825c3SMichael Gottesman for (auto P : make_range(MV.begin(), MV.end())) {
139d71825c3SMichael Gottesman ASSERT_EQ(P.first, count);
140d71825c3SMichael Gottesman count++;
141d71825c3SMichael Gottesman }
142d71825c3SMichael Gottesman
143d71825c3SMichael Gottesman count = 6;
144d71825c3SMichael Gottesman for (auto P : make_range(MV.rbegin(), MV.rend())) {
145d71825c3SMichael Gottesman ASSERT_EQ(P.first, count);
146d71825c3SMichael Gottesman count--;
147d71825c3SMichael Gottesman }
148d71825c3SMichael Gottesman }
14964670671SMichael Gottesman
TEST(MapVectorTest,NonCopyable)1501b782176SJustin Lebar TEST(MapVectorTest, NonCopyable) {
1511b782176SJustin Lebar MapVector<int, std::unique_ptr<int>> MV;
1520eaee545SJonas Devlieghere MV.insert(std::make_pair(1, std::make_unique<int>(1)));
1530eaee545SJonas Devlieghere MV.insert(std::make_pair(2, std::make_unique<int>(2)));
1541b782176SJustin Lebar
1551b782176SJustin Lebar ASSERT_EQ(MV.count(1), 1u);
1561b782176SJustin Lebar ASSERT_EQ(*MV.find(2)->second, 2);
1571b782176SJustin Lebar }
1581b782176SJustin Lebar
159360ef6f5SEric Fiselier template <class IntType> struct MapVectorMappedTypeTest : ::testing::Test {
160360ef6f5SEric Fiselier using int_type = IntType;
161360ef6f5SEric Fiselier };
162360ef6f5SEric Fiselier
163360ef6f5SEric Fiselier using MapIntTypes = ::testing::Types<int, long, long long, unsigned,
164360ef6f5SEric Fiselier unsigned long, unsigned long long>;
165*05de4b41SBenjamin Kramer TYPED_TEST_SUITE(MapVectorMappedTypeTest, MapIntTypes, );
166360ef6f5SEric Fiselier
TYPED_TEST(MapVectorMappedTypeTest,DifferentDenseMap)167360ef6f5SEric Fiselier TYPED_TEST(MapVectorMappedTypeTest, DifferentDenseMap) {
168360ef6f5SEric Fiselier // Test that using a map with a mapped type other than 'unsigned' compiles
169360ef6f5SEric Fiselier // and works.
170360ef6f5SEric Fiselier using IntType = typename TestFixture::int_type;
171360ef6f5SEric Fiselier using MapVectorType = MapVector<int, int, DenseMap<int, IntType>>;
172360ef6f5SEric Fiselier
173360ef6f5SEric Fiselier MapVectorType MV;
174360ef6f5SEric Fiselier std::pair<typename MapVectorType::iterator, bool> R;
175360ef6f5SEric Fiselier
176360ef6f5SEric Fiselier R = MV.insert(std::make_pair(1, 2));
177360ef6f5SEric Fiselier ASSERT_EQ(R.first, MV.begin());
178360ef6f5SEric Fiselier EXPECT_EQ(R.first->first, 1);
179360ef6f5SEric Fiselier EXPECT_EQ(R.first->second, 2);
180360ef6f5SEric Fiselier EXPECT_TRUE(R.second);
181360ef6f5SEric Fiselier
182360ef6f5SEric Fiselier const std::pair<int, int> Elem(1, 3);
183360ef6f5SEric Fiselier R = MV.insert(Elem);
184360ef6f5SEric Fiselier ASSERT_EQ(R.first, MV.begin());
185360ef6f5SEric Fiselier EXPECT_EQ(R.first->first, 1);
186360ef6f5SEric Fiselier EXPECT_EQ(R.first->second, 2);
187360ef6f5SEric Fiselier EXPECT_FALSE(R.second);
188360ef6f5SEric Fiselier
189360ef6f5SEric Fiselier int& value = MV[4];
190360ef6f5SEric Fiselier EXPECT_EQ(value, 0);
191360ef6f5SEric Fiselier value = 5;
192360ef6f5SEric Fiselier
193360ef6f5SEric Fiselier EXPECT_EQ(MV.size(), 2u);
194360ef6f5SEric Fiselier EXPECT_EQ(MV[1], 2);
195360ef6f5SEric Fiselier EXPECT_EQ(MV[4], 5);
196360ef6f5SEric Fiselier }
197360ef6f5SEric Fiselier
TEST(SmallMapVectorSmallTest,insert_pop)19864670671SMichael Gottesman TEST(SmallMapVectorSmallTest, insert_pop) {
19964670671SMichael Gottesman SmallMapVector<int, int, 32> MV;
20064670671SMichael Gottesman std::pair<SmallMapVector<int, int, 32>::iterator, bool> R;
20164670671SMichael Gottesman
20264670671SMichael Gottesman R = MV.insert(std::make_pair(1, 2));
20364670671SMichael Gottesman ASSERT_EQ(R.first, MV.begin());
20464670671SMichael Gottesman EXPECT_EQ(R.first->first, 1);
20564670671SMichael Gottesman EXPECT_EQ(R.first->second, 2);
20664670671SMichael Gottesman EXPECT_TRUE(R.second);
20764670671SMichael Gottesman
20864670671SMichael Gottesman R = MV.insert(std::make_pair(1, 3));
20964670671SMichael Gottesman ASSERT_EQ(R.first, MV.begin());
21064670671SMichael Gottesman EXPECT_EQ(R.first->first, 1);
21164670671SMichael Gottesman EXPECT_EQ(R.first->second, 2);
21264670671SMichael Gottesman EXPECT_FALSE(R.second);
21364670671SMichael Gottesman
21464670671SMichael Gottesman R = MV.insert(std::make_pair(4, 5));
21564670671SMichael Gottesman ASSERT_NE(R.first, MV.end());
21664670671SMichael Gottesman EXPECT_EQ(R.first->first, 4);
21764670671SMichael Gottesman EXPECT_EQ(R.first->second, 5);
21864670671SMichael Gottesman EXPECT_TRUE(R.second);
21964670671SMichael Gottesman
22064670671SMichael Gottesman EXPECT_EQ(MV.size(), 2u);
22164670671SMichael Gottesman EXPECT_EQ(MV[1], 2);
22264670671SMichael Gottesman EXPECT_EQ(MV[4], 5);
22364670671SMichael Gottesman
22464670671SMichael Gottesman MV.pop_back();
22564670671SMichael Gottesman EXPECT_EQ(MV.size(), 1u);
22664670671SMichael Gottesman EXPECT_EQ(MV[1], 2);
22764670671SMichael Gottesman
22864670671SMichael Gottesman R = MV.insert(std::make_pair(4, 7));
22964670671SMichael Gottesman ASSERT_NE(R.first, MV.end());
23064670671SMichael Gottesman EXPECT_EQ(R.first->first, 4);
23164670671SMichael Gottesman EXPECT_EQ(R.first->second, 7);
23264670671SMichael Gottesman EXPECT_TRUE(R.second);
23364670671SMichael Gottesman
23464670671SMichael Gottesman EXPECT_EQ(MV.size(), 2u);
23564670671SMichael Gottesman EXPECT_EQ(MV[1], 2);
23664670671SMichael Gottesman EXPECT_EQ(MV[4], 7);
23764670671SMichael Gottesman }
23864670671SMichael Gottesman
TEST(SmallMapVectorSmallTest,erase)23964670671SMichael Gottesman TEST(SmallMapVectorSmallTest, erase) {
24064670671SMichael Gottesman SmallMapVector<int, int, 32> MV;
24164670671SMichael Gottesman
24264670671SMichael Gottesman MV.insert(std::make_pair(1, 2));
24364670671SMichael Gottesman MV.insert(std::make_pair(3, 4));
24464670671SMichael Gottesman MV.insert(std::make_pair(5, 6));
24564670671SMichael Gottesman ASSERT_EQ(MV.size(), 3u);
24664670671SMichael Gottesman
24764670671SMichael Gottesman MV.erase(MV.find(1));
24864670671SMichael Gottesman ASSERT_EQ(MV.size(), 2u);
24964670671SMichael Gottesman ASSERT_EQ(MV.find(1), MV.end());
25064670671SMichael Gottesman ASSERT_EQ(MV[3], 4);
25164670671SMichael Gottesman ASSERT_EQ(MV[5], 6);
25264670671SMichael Gottesman
25364670671SMichael Gottesman ASSERT_EQ(MV.erase(3), 1u);
25464670671SMichael Gottesman ASSERT_EQ(MV.size(), 1u);
25564670671SMichael Gottesman ASSERT_EQ(MV.find(3), MV.end());
25664670671SMichael Gottesman ASSERT_EQ(MV[5], 6);
25764670671SMichael Gottesman
25864670671SMichael Gottesman ASSERT_EQ(MV.erase(79), 0u);
25964670671SMichael Gottesman ASSERT_EQ(MV.size(), 1u);
26064670671SMichael Gottesman }
26164670671SMichael Gottesman
TEST(SmallMapVectorSmallTest,remove_if)26264670671SMichael Gottesman TEST(SmallMapVectorSmallTest, remove_if) {
26364670671SMichael Gottesman SmallMapVector<int, int, 32> MV;
26464670671SMichael Gottesman
26564670671SMichael Gottesman MV.insert(std::make_pair(1, 11));
26664670671SMichael Gottesman MV.insert(std::make_pair(2, 12));
26764670671SMichael Gottesman MV.insert(std::make_pair(3, 13));
26864670671SMichael Gottesman MV.insert(std::make_pair(4, 14));
26964670671SMichael Gottesman MV.insert(std::make_pair(5, 15));
27064670671SMichael Gottesman MV.insert(std::make_pair(6, 16));
27164670671SMichael Gottesman ASSERT_EQ(MV.size(), 6u);
27264670671SMichael Gottesman
27364670671SMichael Gottesman MV.remove_if([](const std::pair<int, int> &Val) { return Val.second % 2; });
27464670671SMichael Gottesman ASSERT_EQ(MV.size(), 3u);
27564670671SMichael Gottesman ASSERT_EQ(MV.find(1), MV.end());
27664670671SMichael Gottesman ASSERT_EQ(MV.find(3), MV.end());
27764670671SMichael Gottesman ASSERT_EQ(MV.find(5), MV.end());
27864670671SMichael Gottesman ASSERT_EQ(MV[2], 12);
27964670671SMichael Gottesman ASSERT_EQ(MV[4], 14);
28064670671SMichael Gottesman ASSERT_EQ(MV[6], 16);
28164670671SMichael Gottesman }
28264670671SMichael Gottesman
TEST(SmallMapVectorSmallTest,iteration_test)28364670671SMichael Gottesman TEST(SmallMapVectorSmallTest, iteration_test) {
28464670671SMichael Gottesman SmallMapVector<int, int, 32> MV;
28564670671SMichael Gottesman
28664670671SMichael Gottesman MV.insert(std::make_pair(1, 11));
28764670671SMichael Gottesman MV.insert(std::make_pair(2, 12));
28864670671SMichael Gottesman MV.insert(std::make_pair(3, 13));
28964670671SMichael Gottesman MV.insert(std::make_pair(4, 14));
29064670671SMichael Gottesman MV.insert(std::make_pair(5, 15));
29164670671SMichael Gottesman MV.insert(std::make_pair(6, 16));
29264670671SMichael Gottesman ASSERT_EQ(MV.size(), 6u);
29364670671SMichael Gottesman
29464670671SMichael Gottesman int count = 1;
29564670671SMichael Gottesman for (auto P : make_range(MV.begin(), MV.end())) {
29664670671SMichael Gottesman ASSERT_EQ(P.first, count);
29764670671SMichael Gottesman count++;
29864670671SMichael Gottesman }
29964670671SMichael Gottesman
30064670671SMichael Gottesman count = 6;
30164670671SMichael Gottesman for (auto P : make_range(MV.rbegin(), MV.rend())) {
30264670671SMichael Gottesman ASSERT_EQ(P.first, count);
30364670671SMichael Gottesman count--;
30464670671SMichael Gottesman }
30564670671SMichael Gottesman }
30664670671SMichael Gottesman
TEST(SmallMapVectorSmallTest,NonCopyable)3071b782176SJustin Lebar TEST(SmallMapVectorSmallTest, NonCopyable) {
3081b782176SJustin Lebar SmallMapVector<int, std::unique_ptr<int>, 8> MV;
3090eaee545SJonas Devlieghere MV.insert(std::make_pair(1, std::make_unique<int>(1)));
3100eaee545SJonas Devlieghere MV.insert(std::make_pair(2, std::make_unique<int>(2)));
3111b782176SJustin Lebar
3121b782176SJustin Lebar ASSERT_EQ(MV.count(1), 1u);
3131b782176SJustin Lebar ASSERT_EQ(*MV.find(2)->second, 2);
3141b782176SJustin Lebar }
3151b782176SJustin Lebar
TEST(SmallMapVectorLargeTest,insert_pop)31664670671SMichael Gottesman TEST(SmallMapVectorLargeTest, insert_pop) {
31764670671SMichael Gottesman SmallMapVector<int, int, 1> MV;
31864670671SMichael Gottesman std::pair<SmallMapVector<int, int, 1>::iterator, bool> R;
31964670671SMichael Gottesman
32064670671SMichael Gottesman R = MV.insert(std::make_pair(1, 2));
32164670671SMichael Gottesman ASSERT_EQ(R.first, MV.begin());
32264670671SMichael Gottesman EXPECT_EQ(R.first->first, 1);
32364670671SMichael Gottesman EXPECT_EQ(R.first->second, 2);
32464670671SMichael Gottesman EXPECT_TRUE(R.second);
32564670671SMichael Gottesman
32664670671SMichael Gottesman R = MV.insert(std::make_pair(1, 3));
32764670671SMichael Gottesman ASSERT_EQ(R.first, MV.begin());
32864670671SMichael Gottesman EXPECT_EQ(R.first->first, 1);
32964670671SMichael Gottesman EXPECT_EQ(R.first->second, 2);
33064670671SMichael Gottesman EXPECT_FALSE(R.second);
33164670671SMichael Gottesman
33264670671SMichael Gottesman R = MV.insert(std::make_pair(4, 5));
33364670671SMichael Gottesman ASSERT_NE(R.first, MV.end());
33464670671SMichael Gottesman EXPECT_EQ(R.first->first, 4);
33564670671SMichael Gottesman EXPECT_EQ(R.first->second, 5);
33664670671SMichael Gottesman EXPECT_TRUE(R.second);
33764670671SMichael Gottesman
33864670671SMichael Gottesman EXPECT_EQ(MV.size(), 2u);
33964670671SMichael Gottesman EXPECT_EQ(MV[1], 2);
34064670671SMichael Gottesman EXPECT_EQ(MV[4], 5);
34164670671SMichael Gottesman
34264670671SMichael Gottesman MV.pop_back();
34364670671SMichael Gottesman EXPECT_EQ(MV.size(), 1u);
34464670671SMichael Gottesman EXPECT_EQ(MV[1], 2);
34564670671SMichael Gottesman
34664670671SMichael Gottesman R = MV.insert(std::make_pair(4, 7));
34764670671SMichael Gottesman ASSERT_NE(R.first, MV.end());
34864670671SMichael Gottesman EXPECT_EQ(R.first->first, 4);
34964670671SMichael Gottesman EXPECT_EQ(R.first->second, 7);
35064670671SMichael Gottesman EXPECT_TRUE(R.second);
35164670671SMichael Gottesman
35264670671SMichael Gottesman EXPECT_EQ(MV.size(), 2u);
35364670671SMichael Gottesman EXPECT_EQ(MV[1], 2);
35464670671SMichael Gottesman EXPECT_EQ(MV[4], 7);
35564670671SMichael Gottesman }
35664670671SMichael Gottesman
TEST(SmallMapVectorLargeTest,erase)35764670671SMichael Gottesman TEST(SmallMapVectorLargeTest, erase) {
35864670671SMichael Gottesman SmallMapVector<int, int, 1> MV;
35964670671SMichael Gottesman
36064670671SMichael Gottesman MV.insert(std::make_pair(1, 2));
36164670671SMichael Gottesman MV.insert(std::make_pair(3, 4));
36264670671SMichael Gottesman MV.insert(std::make_pair(5, 6));
36364670671SMichael Gottesman ASSERT_EQ(MV.size(), 3u);
36464670671SMichael Gottesman
36564670671SMichael Gottesman MV.erase(MV.find(1));
36664670671SMichael Gottesman ASSERT_EQ(MV.size(), 2u);
36764670671SMichael Gottesman ASSERT_EQ(MV.find(1), MV.end());
36864670671SMichael Gottesman ASSERT_EQ(MV[3], 4);
36964670671SMichael Gottesman ASSERT_EQ(MV[5], 6);
37064670671SMichael Gottesman
37164670671SMichael Gottesman ASSERT_EQ(MV.erase(3), 1u);
37264670671SMichael Gottesman ASSERT_EQ(MV.size(), 1u);
37364670671SMichael Gottesman ASSERT_EQ(MV.find(3), MV.end());
37464670671SMichael Gottesman ASSERT_EQ(MV[5], 6);
37564670671SMichael Gottesman
37664670671SMichael Gottesman ASSERT_EQ(MV.erase(79), 0u);
37764670671SMichael Gottesman ASSERT_EQ(MV.size(), 1u);
37864670671SMichael Gottesman }
37964670671SMichael Gottesman
TEST(SmallMapVectorLargeTest,remove_if)38064670671SMichael Gottesman TEST(SmallMapVectorLargeTest, remove_if) {
38164670671SMichael Gottesman SmallMapVector<int, int, 1> MV;
38264670671SMichael Gottesman
38364670671SMichael Gottesman MV.insert(std::make_pair(1, 11));
38464670671SMichael Gottesman MV.insert(std::make_pair(2, 12));
38564670671SMichael Gottesman MV.insert(std::make_pair(3, 13));
38664670671SMichael Gottesman MV.insert(std::make_pair(4, 14));
38764670671SMichael Gottesman MV.insert(std::make_pair(5, 15));
38864670671SMichael Gottesman MV.insert(std::make_pair(6, 16));
38964670671SMichael Gottesman ASSERT_EQ(MV.size(), 6u);
39064670671SMichael Gottesman
39164670671SMichael Gottesman MV.remove_if([](const std::pair<int, int> &Val) { return Val.second % 2; });
39264670671SMichael Gottesman ASSERT_EQ(MV.size(), 3u);
39364670671SMichael Gottesman ASSERT_EQ(MV.find(1), MV.end());
39464670671SMichael Gottesman ASSERT_EQ(MV.find(3), MV.end());
39564670671SMichael Gottesman ASSERT_EQ(MV.find(5), MV.end());
39664670671SMichael Gottesman ASSERT_EQ(MV[2], 12);
39764670671SMichael Gottesman ASSERT_EQ(MV[4], 14);
39864670671SMichael Gottesman ASSERT_EQ(MV[6], 16);
39964670671SMichael Gottesman }
40064670671SMichael Gottesman
TEST(SmallMapVectorLargeTest,iteration_test)40164670671SMichael Gottesman TEST(SmallMapVectorLargeTest, iteration_test) {
40264670671SMichael Gottesman SmallMapVector<int, int, 1> MV;
40364670671SMichael Gottesman
40464670671SMichael Gottesman MV.insert(std::make_pair(1, 11));
40564670671SMichael Gottesman MV.insert(std::make_pair(2, 12));
40664670671SMichael Gottesman MV.insert(std::make_pair(3, 13));
40764670671SMichael Gottesman MV.insert(std::make_pair(4, 14));
40864670671SMichael Gottesman MV.insert(std::make_pair(5, 15));
40964670671SMichael Gottesman MV.insert(std::make_pair(6, 16));
41064670671SMichael Gottesman ASSERT_EQ(MV.size(), 6u);
41164670671SMichael Gottesman
41264670671SMichael Gottesman int count = 1;
41364670671SMichael Gottesman for (auto P : make_range(MV.begin(), MV.end())) {
41464670671SMichael Gottesman ASSERT_EQ(P.first, count);
41564670671SMichael Gottesman count++;
41664670671SMichael Gottesman }
41764670671SMichael Gottesman
41864670671SMichael Gottesman count = 6;
41964670671SMichael Gottesman for (auto P : make_range(MV.rbegin(), MV.rend())) {
42064670671SMichael Gottesman ASSERT_EQ(P.first, count);
42164670671SMichael Gottesman count--;
42264670671SMichael Gottesman }
42364670671SMichael Gottesman }
424