StringTableBuilderTest.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "llvm/MC/StringTableBuilder.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;
  17. B.add("foo");
  18. B.add("bar");
  19. B.add("foobar");
  20. B.finalize(StringTableBuilder::ELF);
  21. std::string Expected;
  22. Expected += '\x00';
  23. Expected += "foobar";
  24. Expected += '\x00';
  25. Expected += "foo";
  26. Expected += '\x00';
  27. EXPECT_EQ(Expected, B.data());
  28. EXPECT_EQ(1U, B.getOffset("foobar"));
  29. EXPECT_EQ(4U, B.getOffset("bar"));
  30. EXPECT_EQ(8U, B.getOffset("foo"));
  31. }
  32. TEST(StringTableBuilderTest, BasicWinCOFF) {
  33. StringTableBuilder B;
  34. // Strings must be 9 chars or longer to go in the table.
  35. B.add("hippopotamus");
  36. B.add("pygmy hippopotamus");
  37. B.add("river horse");
  38. B.finalize(StringTableBuilder::WinCOFF);
  39. // size_field + "pygmy hippopotamus\0" + "river horse\0"
  40. uint32_t ExpectedSize = 4 + 19 + 12;
  41. EXPECT_EQ(ExpectedSize, B.data().size());
  42. std::string Expected;
  43. ExpectedSize =
  44. support::endian::byte_swap<uint32_t, support::little>(ExpectedSize);
  45. Expected.append((const char*)&ExpectedSize, 4);
  46. Expected += "pygmy hippopotamus";
  47. Expected += '\x00';
  48. Expected += "river horse";
  49. Expected += '\x00';
  50. EXPECT_EQ(Expected, B.data());
  51. EXPECT_EQ(4U, B.getOffset("pygmy hippopotamus"));
  52. EXPECT_EQ(10U, B.getOffset("hippopotamus"));
  53. EXPECT_EQ(23U, B.getOffset("river horse"));
  54. }
  55. }