1 //===- StringTableBuilderTest.cpp -----------------------------------------===//
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/DebugInfo/PDB/Native/PDBStringTable.h"
10 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
11 #include "llvm/Support/BinaryByteStream.h"
12 #include "llvm/Support/BinaryStreamReader.h"
13 #include "llvm/Support/BinaryStreamWriter.h"
14 #include "llvm/Testing/Support/Error.h"
15 
16 #include "gtest/gtest.h"
17 
18 using namespace llvm;
19 using namespace llvm::pdb;
20 using namespace llvm::support;
21 
22 namespace {
23 class StringTableBuilderTest : public ::testing::Test {};
24 }
25 
26 TEST_F(StringTableBuilderTest, Simple) {
27   // Create /names table contents.
28   PDBStringTableBuilder Builder;
29 
30   // This test case is carefully constructed to ensure that at least one
31   // string gets bucketed into slot 0, *and* to ensure that at least one
32   // has a hash collision at the end of the bucket list so it has to
33   // wrap around.
34   uint32_t FooID = Builder.insert("foo");
35   uint32_t BarID = Builder.insert("bar");
36   uint32_t BazID = Builder.insert("baz");
37   uint32_t BuzzID = Builder.insert("buzz");
38   uint32_t BazzID = Builder.insert("bazz");
39   uint32_t BarrID = Builder.insert("barr");
40 
41   // Re-inserting the same item should return the same id.
42   EXPECT_EQ(FooID, Builder.insert("foo"));
43   EXPECT_EQ(BarID, Builder.insert("bar"));
44   EXPECT_EQ(BazID, Builder.insert("baz"));
45   EXPECT_EQ(BuzzID, Builder.insert("buzz"));
46   EXPECT_EQ(BazzID, Builder.insert("bazz"));
47   EXPECT_EQ(BarrID, Builder.insert("barr"));
48 
49   // Each ID should be distinct.
50   std::set<uint32_t> Distinct{FooID, BarID, BazID, BuzzID, BazzID, BarrID};
51   EXPECT_EQ(6U, Distinct.size());
52 
53   std::vector<uint8_t> Buffer(Builder.calculateSerializedSize());
54   MutableBinaryByteStream OutStream(Buffer, little);
55   BinaryStreamWriter Writer(OutStream);
56   EXPECT_THAT_ERROR(Builder.commit(Writer), Succeeded());
57 
58   // Reads the contents back.
59   BinaryByteStream InStream(Buffer, little);
60   BinaryStreamReader Reader(InStream);
61   PDBStringTable Table;
62   EXPECT_THAT_ERROR(Table.reload(Reader), Succeeded());
63 
64   EXPECT_EQ(6U, Table.getNameCount());
65   EXPECT_EQ(1U, Table.getHashVersion());
66 
67   EXPECT_THAT_EXPECTED(Table.getStringForID(FooID), HasValue("foo"));
68   EXPECT_THAT_EXPECTED(Table.getStringForID(BarID), HasValue("bar"));
69   EXPECT_THAT_EXPECTED(Table.getStringForID(BazID), HasValue("baz"));
70   EXPECT_THAT_EXPECTED(Table.getStringForID(BuzzID), HasValue("buzz"));
71   EXPECT_THAT_EXPECTED(Table.getStringForID(BazzID), HasValue("bazz"));
72   EXPECT_THAT_EXPECTED(Table.getStringForID(BarrID), HasValue("barr"));
73 
74   EXPECT_THAT_EXPECTED(Table.getIDForString("foo"), HasValue(FooID));
75   EXPECT_THAT_EXPECTED(Table.getIDForString("bar"), HasValue(BarID));
76   EXPECT_THAT_EXPECTED(Table.getIDForString("baz"), HasValue(BazID));
77   EXPECT_THAT_EXPECTED(Table.getIDForString("buzz"), HasValue(BuzzID));
78   EXPECT_THAT_EXPECTED(Table.getIDForString("bazz"), HasValue(BazzID));
79   EXPECT_THAT_EXPECTED(Table.getIDForString("barr"), HasValue(BarrID));
80 }
81