summaryrefslogtreecommitdiff
path: root/unittests/ADT
diff options
context:
space:
mode:
authorStepan Dyatkovskiy <stpworld@narod.ru>2012-04-25 17:09:38 +0000
committerStepan Dyatkovskiy <stpworld@narod.ru>2012-04-25 17:09:38 +0000
commit76271a3366731d4c372fdebcd8d3437e6e09a61b (patch)
tree753cad255078a61246190aa77b8360ee685af238 /unittests/ADT
parent50e1d84ba8efc1973137c65e0b0e048ecf8cf5d6 (diff)
downloadllvm-76271a3366731d4c372fdebcd8d3437e6e09a61b.tar.gz
llvm-76271a3366731d4c372fdebcd8d3437e6e09a61b.tar.bz2
llvm-76271a3366731d4c372fdebcd8d3437e6e09a61b.tar.xz
First implementation of:
- FlatArrayMap. Very simple map container that uses flat array inside. - MultiImplMap. Map container interface, that has two modes, one for small amount of elements and one for big amount. - SmallMap. SmallMap is DenseMap compatible MultiImplMap. It uses FlatArrayMap for small mode, and DenseMap for big mode. Also added unittests for new classes and update for ProgrammersManual. For more details about new classes see ProgrammersManual and comments in sourcecode. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@155557 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/ADT')
-rw-r--r--unittests/ADT/SmallMapTest.cpp133
1 files changed, 133 insertions, 0 deletions
diff --git a/unittests/ADT/SmallMapTest.cpp b/unittests/ADT/SmallMapTest.cpp
new file mode 100644
index 0000000000..361f0126bf
--- /dev/null
+++ b/unittests/ADT/SmallMapTest.cpp
@@ -0,0 +1,133 @@
+//===- llvm/unittest/ADT/SmallMapTest.cpp ------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// SmallMap unit tests.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "llvm/ADT/SmallMap.h"
+
+using namespace llvm;
+
+// SmallMap test.
+TEST(SmallMapTest, GeneralTest) {
+
+ int buf[10];
+
+ SmallMap<int *, int, 3> a;
+ SmallMap<int *, int, 3> b;
+ SmallMap<int *, int, 3>::iterator found;
+ std::pair<SmallMap<int *, int, 3>::iterator, bool> insRes;
+ SmallMap<int *, int, 3>::const_iterator foundc;
+
+ a.insert(std::make_pair(&buf[0], 0));
+ insRes = a.insert(std::make_pair(&buf[1], 1));
+ EXPECT_TRUE(insRes.second);
+
+ // Check insertion, looking up, and data editing in small mode.
+ insRes = a.insert(std::make_pair(&buf[1], 6));
+ EXPECT_FALSE(insRes.second);
+ EXPECT_EQ(insRes.first->second, 1);
+ insRes.first->second = 5;
+ found = a.find(&buf[1]);
+ EXPECT_NE(found, a.end());
+ EXPECT_EQ(found->second, 5);
+ a[&buf[1]] = 10;
+ EXPECT_EQ(found->second, 10);
+ // Check "not found" case.
+ found = a.find(&buf[8]);
+ EXPECT_EQ(found, a.end());
+
+ // Check increment for small mode.
+ found = a.begin();
+ ++found;
+ EXPECT_EQ(found->second, 10);
+
+ b.insert(std::make_pair(&buf[2], 2));
+
+ std::swap(a, b);
+ a.swap(b);
+ std::swap(a, b);
+
+ EXPECT_EQ(1U, a.size());
+ EXPECT_EQ(2U, b.size());
+ EXPECT_TRUE(a.count(&buf[2]));
+ EXPECT_TRUE(b.count(&buf[0]));
+ EXPECT_TRUE(b.count(&buf[1]));
+
+ insRes = b.insert(std::make_pair(&buf[3], 3));
+ EXPECT_TRUE(insRes.second);
+
+ // Check insertion, looking up, and data editing in big mode.
+ insRes = b.insert(std::make_pair(&buf[3], 6));
+ EXPECT_FALSE(insRes.second);
+ EXPECT_EQ(insRes.first->second, 3);
+ insRes.first->second = 7;
+ found = b.find(&buf[3]);
+ EXPECT_EQ(found->second, 7);
+ b[&buf[3]] = 14;
+ EXPECT_EQ(found->second, 14);
+ // Check constant looking up.
+ foundc = b.find(&buf[3]);
+ EXPECT_EQ(foundc->first, &buf[3]);
+ EXPECT_EQ(foundc->second, 14);
+ // Check not found case.
+ found = b.find(&buf[8]);
+ EXPECT_EQ(found, b.end());
+
+ // Check increment for big mode.
+ found = b.find(&buf[1]);
+ ++found;
+ EXPECT_EQ(found->second, 14);
+
+ std::swap(a, b);
+ a.swap(b);
+ std::swap(a, b);
+
+ EXPECT_EQ(3U, a.size());
+ EXPECT_EQ(1U, b.size());
+ EXPECT_TRUE(a.count(&buf[0]));
+ EXPECT_TRUE(a.count(&buf[1]));
+ EXPECT_TRUE(a.count(&buf[3]));
+ EXPECT_TRUE(b.count(&buf[2]));
+ EXPECT_EQ(b.find(&buf[2])->second, 2);
+
+ std::swap(a, b);
+ a.swap(b);
+ std::swap(a, b);
+
+ EXPECT_EQ(1U, a.size());
+ EXPECT_EQ(3U, b.size());
+ EXPECT_TRUE(a.count(&buf[2]));
+ EXPECT_TRUE(b.count(&buf[0]));
+ EXPECT_TRUE(b.count(&buf[1]));
+ EXPECT_TRUE(b.count(&buf[3]));
+
+ a.insert(std::make_pair(&buf[4], 4));
+ a.insert(std::make_pair(&buf[5], 5));
+ a.insert(std::make_pair(&buf[6], 6));
+
+ std::swap(b, a);
+
+ EXPECT_EQ(3U, a.size());
+ EXPECT_EQ(4U, b.size());
+ EXPECT_TRUE(b.count(&buf[2]));
+ EXPECT_TRUE(b.count(&buf[4]));
+ EXPECT_TRUE(b.count(&buf[5]));
+ EXPECT_TRUE(b.count(&buf[6]));
+ EXPECT_TRUE(a.count(&buf[0]));
+ EXPECT_TRUE(a.count(&buf[1]));
+ EXPECT_TRUE(a.count(&buf[3]));
+
+ // Check findAndConstruct
+ SmallMap<int *, int, 3>::value_type Buf7;
+ Buf7 = a.FindAndConstruct(&buf[7]);
+ EXPECT_EQ(Buf7.second, 0);
+}