StringTableBuilderTest.cpp 3.5 KB

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