StringTableBuilderTest.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/MC/StringTableBuilder.h"
  9. #include "llvm/ADT/SmallString.h"
  10. #include "llvm/Support/Endian.h"
  11. #include "gtest/gtest.h"
  12. #include <string>
  13. using namespace llvm;
  14. namespace {
  15. TEST(StringTableBuilderTest, BasicELF) {
  16. StringTableBuilder B(StringTableBuilder::ELF);
  17. B.add("foo");
  18. B.add("bar");
  19. B.add("foobar");
  20. B.finalize();
  21. std::string Expected;
  22. Expected += '\x00';
  23. Expected += "foobar";
  24. Expected += '\x00';
  25. Expected += "foo";
  26. Expected += '\x00';
  27. SmallString<64> Data;
  28. raw_svector_ostream OS(Data);
  29. B.write(OS);
  30. EXPECT_EQ(Expected, Data);
  31. EXPECT_EQ(1U, B.getOffset("foobar"));
  32. EXPECT_EQ(4U, B.getOffset("bar"));
  33. EXPECT_EQ(8U, B.getOffset("foo"));
  34. }
  35. TEST(StringTableBuilderTest, BasicWinCOFF) {
  36. StringTableBuilder B(StringTableBuilder::WinCOFF);
  37. // Strings must be 9 chars or longer to go in the table.
  38. B.add("hippopotamus");
  39. B.add("pygmy hippopotamus");
  40. B.add("river horse");
  41. B.finalize();
  42. // size_field + "pygmy hippopotamus\0" + "river horse\0"
  43. uint32_t ExpectedSize = 4 + 19 + 12;
  44. EXPECT_EQ(ExpectedSize, B.getSize());
  45. std::string Expected;
  46. ExpectedSize =
  47. support::endian::byte_swap<uint32_t, support::little>(ExpectedSize);
  48. Expected.append((const char*)&ExpectedSize, 4);
  49. Expected += "pygmy hippopotamus";
  50. Expected += '\x00';
  51. Expected += "river horse";
  52. Expected += '\x00';
  53. SmallString<64> Data;
  54. raw_svector_ostream OS(Data);
  55. B.write(OS);
  56. EXPECT_EQ(Expected, Data);
  57. EXPECT_EQ(4U, B.getOffset("pygmy hippopotamus"));
  58. EXPECT_EQ(10U, B.getOffset("hippopotamus"));
  59. EXPECT_EQ(23U, B.getOffset("river horse"));
  60. }
  61. TEST(StringTableBuilderTest, ELFInOrder) {
  62. StringTableBuilder B(StringTableBuilder::ELF);
  63. EXPECT_EQ(1U, B.add("foo"));
  64. EXPECT_EQ(5U, B.add("bar"));
  65. EXPECT_EQ(9U, B.add("foobar"));
  66. B.finalizeInOrder();
  67. std::string Expected;
  68. Expected += '\x00';
  69. Expected += "foo";
  70. Expected += '\x00';
  71. Expected += "bar";
  72. Expected += '\x00';
  73. Expected += "foobar";
  74. Expected += '\x00';
  75. SmallString<64> Data;
  76. raw_svector_ostream OS(Data);
  77. B.write(OS);
  78. EXPECT_EQ(Expected, Data);
  79. EXPECT_EQ(1U, B.getOffset("foo"));
  80. EXPECT_EQ(5U, B.getOffset("bar"));
  81. EXPECT_EQ(9U, B.getOffset("foobar"));
  82. }
  83. }