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