diff options
author | Zachary Turner <zturner@google.com> | 2018-03-15 17:38:26 +0000 |
---|---|---|
committer | Zachary Turner <zturner@google.com> | 2018-03-15 17:38:26 +0000 |
commit | ebf03f6c4641cb6f73955c6bbeddf04ec5b353d7 (patch) | |
tree | adbf8589d9067f5be3c0a1b4c2035699653a2859 /llvm/unittests/DebugInfo/PDB/HashTableTest.cpp | |
parent | ca587fe0b4749b5ffca8cfc8358ea4602e435bb7 (diff) | |
download | bcm5719-llvm-ebf03f6c4641cb6f73955c6bbeddf04ec5b353d7.tar.gz bcm5719-llvm-ebf03f6c4641cb6f73955c6bbeddf04ec5b353d7.zip |
Refactor the PDB HashTable class.
It previously only worked when the key and value types were
both 4 byte integers. We now have a use case for a non trivial
value type, so we need to extend it to support arbitrary value
types, which means templatizing it.
llvm-svn: 327647
Diffstat (limited to 'llvm/unittests/DebugInfo/PDB/HashTableTest.cpp')
-rw-r--r-- | llvm/unittests/DebugInfo/PDB/HashTableTest.cpp | 145 |
1 files changed, 100 insertions, 45 deletions
diff --git a/llvm/unittests/DebugInfo/PDB/HashTableTest.cpp b/llvm/unittests/DebugInfo/PDB/HashTableTest.cpp index a49134c0a3e..cffb3840bc3 100644 --- a/llvm/unittests/DebugInfo/PDB/HashTableTest.cpp +++ b/llvm/unittests/DebugInfo/PDB/HashTableTest.cpp @@ -8,10 +8,14 @@ //===----------------------------------------------------------------------===// #include "llvm/DebugInfo/PDB/Native/HashTable.h" + +#include "llvm/DebugInfo/PDB/Native/Hash.h" #include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h" +#include "llvm/Support/Allocator.h" #include "llvm/Support/BinaryByteStream.h" #include "llvm/Support/BinaryStreamReader.h" #include "llvm/Support/BinaryStreamWriter.h" +#include "llvm/Support/StringSaver.h" #include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" @@ -23,7 +27,8 @@ using namespace llvm::pdb; using namespace llvm::support; namespace { -class HashTableInternals : public HashTable { + +class HashTableInternals : public HashTable<uint32_t> { public: using HashTable::Buckets; using HashTable::Present; @@ -32,18 +37,18 @@ public: } TEST(HashTableTest, TestSimple) { - HashTable Table; + HashTableInternals Table; EXPECT_EQ(0u, Table.size()); EXPECT_GT(Table.capacity(), 0u); - Table.set(3, 7); + Table.set_as(3, 7); EXPECT_EQ(1u, Table.size()); - ASSERT_NE(Table.end(), Table.find(3)); + ASSERT_NE(Table.end(), Table.find_as(3)); EXPECT_EQ(7u, Table.get(3)); } TEST(HashTableTest, TestCollision) { - HashTable Table; + HashTableInternals Table; EXPECT_EQ(0u, Table.size()); EXPECT_GT(Table.capacity(), 0u); @@ -53,39 +58,33 @@ TEST(HashTableTest, TestCollision) { uint32_t N1 = Table.capacity() + 1; uint32_t N2 = 2 * N1; - Table.set(N1, 7); - Table.set(N2, 12); + Table.set_as(N1, 7); + Table.set_as(N2, 12); EXPECT_EQ(2u, Table.size()); - ASSERT_NE(Table.end(), Table.find(N1)); - ASSERT_NE(Table.end(), Table.find(N2)); + ASSERT_NE(Table.end(), Table.find_as(N1)); + ASSERT_NE(Table.end(), Table.find_as(N2)); EXPECT_EQ(7u, Table.get(N1)); EXPECT_EQ(12u, Table.get(N2)); } TEST(HashTableTest, TestRemove) { - HashTable Table; + HashTableInternals Table; EXPECT_EQ(0u, Table.size()); EXPECT_GT(Table.capacity(), 0u); - Table.set(1, 2); - Table.set(3, 4); + Table.set_as(1, 2); + Table.set_as(3, 4); EXPECT_EQ(2u, Table.size()); - ASSERT_NE(Table.end(), Table.find(1)); - ASSERT_NE(Table.end(), Table.find(3)); + ASSERT_NE(Table.end(), Table.find_as(1)); + ASSERT_NE(Table.end(), Table.find_as(3)); EXPECT_EQ(2u, Table.get(1)); EXPECT_EQ(4u, Table.get(3)); - - Table.remove(1u); - EXPECT_EQ(1u, Table.size()); - EXPECT_EQ(Table.end(), Table.find(1)); - ASSERT_NE(Table.end(), Table.find(3)); - EXPECT_EQ(4u, Table.get(3)); } TEST(HashTableTest, TestCollisionAfterMultipleProbes) { - HashTable Table; + HashTableInternals Table; EXPECT_EQ(0u, Table.size()); EXPECT_GT(Table.capacity(), 0u); @@ -96,31 +95,17 @@ TEST(HashTableTest, TestCollisionAfterMultipleProbes) { uint32_t N2 = N1 + 1; uint32_t N3 = 2 * N1; - Table.set(N1, 7); - Table.set(N2, 11); - Table.set(N3, 13); + Table.set_as(N1, 7); + Table.set_as(N2, 11); + Table.set_as(N3, 13); EXPECT_EQ(3u, Table.size()); - ASSERT_NE(Table.end(), Table.find(N1)); - ASSERT_NE(Table.end(), Table.find(N2)); - ASSERT_NE(Table.end(), Table.find(N3)); + ASSERT_NE(Table.end(), Table.find_as(N1)); + ASSERT_NE(Table.end(), Table.find_as(N2)); + ASSERT_NE(Table.end(), Table.find_as(N3)); EXPECT_EQ(7u, Table.get(N1)); EXPECT_EQ(11u, Table.get(N2)); EXPECT_EQ(13u, Table.get(N3)); - - // Remove the one that had been filled in the middle, then insert another one - // with a collision. It should fill the newly emptied slot. - Table.remove(N2); - uint32_t N4 = N1 * 3; - Table.set(N4, 17); - EXPECT_EQ(3u, Table.size()); - ASSERT_NE(Table.end(), Table.find(N1)); - ASSERT_NE(Table.end(), Table.find(N3)); - ASSERT_NE(Table.end(), Table.find(N4)); - - EXPECT_EQ(7u, Table.get(N1)); - EXPECT_EQ(13u, Table.get(N3)); - EXPECT_EQ(17u, Table.get(N4)); } TEST(HashTableTest, Grow) { @@ -128,15 +113,15 @@ TEST(HashTableTest, Grow) { // guaranteed to trigger a grow. Then verify that the size is the same, the // capacity is larger, and all the original items are still in the table. - HashTable Table; + HashTableInternals Table; uint32_t OldCapacity = Table.capacity(); for (uint32_t I = 0; I < OldCapacity; ++I) { - Table.set(OldCapacity + I * 2 + 1, I * 2 + 3); + Table.set_as(OldCapacity + I * 2 + 1, I * 2 + 3); } EXPECT_EQ(OldCapacity, Table.size()); EXPECT_GT(Table.capacity(), OldCapacity); for (uint32_t I = 0; I < OldCapacity; ++I) { - ASSERT_NE(Table.end(), Table.find(OldCapacity + I * 2 + 1)); + ASSERT_NE(Table.end(), Table.find_as(OldCapacity + I * 2 + 1)); EXPECT_EQ(I * 2 + 3, Table.get(OldCapacity + I * 2 + 1)); } } @@ -145,7 +130,7 @@ TEST(HashTableTest, Serialization) { HashTableInternals Table; uint32_t Cap = Table.capacity(); for (uint32_t I = 0; I < Cap; ++I) { - Table.set(Cap + I * 2 + 1, I * 2 + 3); + Table.set_as(Cap + I * 2 + 1, I * 2 + 3); } std::vector<uint8_t> Buffer(Table.calculateSerializedLength()); @@ -207,3 +192,73 @@ TEST(HashTableTest, NamedStreamMap) { EXPECT_EQ(7U, N); } while (std::next_permutation(Streams.begin(), Streams.end())); } + +namespace { +struct FooBar { + std::string S; + uint32_t X; + uint32_t Y; + double Z; +}; + +} // namespace + +namespace llvm { +namespace pdb { +template <> struct PdbHashTraits<FooBar> { + std::vector<char> Buffer; + + PdbHashTraits() { Buffer.push_back(0); } + + uint32_t hashLookupKey(StringRef S) const { + return llvm::pdb::hashStringV1(S); + } + + StringRef storageKeyToLookupKey(uint32_t N) const { + if (N >= Buffer.size()) + return StringRef(); + + return StringRef(Buffer.data() + N); + } + + uint32_t lookupKeyToStorageKey(StringRef S) { + uint32_t N = Buffer.size(); + Buffer.insert(Buffer.end(), S.begin(), S.end()); + Buffer.push_back('\0'); + return N; + } +}; +} // namespace pdb +} // namespace llvm + +TEST(HashTableTest, NonTrivialValueType) { + HashTable<FooBar> Table; + uint32_t Cap = Table.capacity(); + for (uint32_t I = 0; I < Cap; ++I) { + FooBar F; + F.S = utostr(I); + F.X = I; + F.Y = I + 1; + F.Z = static_cast<double>(I + 2); + Table.set_as(utostr(I), F); + } + + std::vector<uint8_t> Buffer(Table.calculateSerializedLength()); + MutableBinaryByteStream Stream(Buffer, little); + BinaryStreamWriter Writer(Stream); + EXPECT_THAT_ERROR(Table.commit(Writer), Succeeded()); + // We should have written precisely the number of bytes we calculated earlier. + EXPECT_EQ(Buffer.size(), Writer.getOffset()); + + HashTable<FooBar> Table2; + BinaryStreamReader Reader(Stream); + EXPECT_THAT_ERROR(Table2.load(Reader), Succeeded()); + // We should have read precisely the number of bytes we calculated earlier. + EXPECT_EQ(Buffer.size(), Reader.getOffset()); + + EXPECT_EQ(Table.size(), Table2.size()); + EXPECT_EQ(Table.capacity(), Table2.capacity()); + // EXPECT_EQ(Table.Buckets, Table2.Buckets); + // EXPECT_EQ(Table.Present, Table2.Present); + // EXPECT_EQ(Table.Deleted, Table2.Deleted); +} |