TwineTest.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===- TwineTest.cpp - Twine unit tests -----------------------------------===//
  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/ADT/Twine.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include "gtest/gtest.h"
  13. using namespace llvm;
  14. namespace {
  15. std::string repr(const Twine &Value) {
  16. std::string res;
  17. llvm::raw_string_ostream OS(res);
  18. Value.printRepr(OS);
  19. return OS.str();
  20. }
  21. TEST(TwineTest, Construction) {
  22. EXPECT_EQ("", Twine().str());
  23. EXPECT_EQ("hi", Twine("hi").str());
  24. EXPECT_EQ("hi", Twine(std::string("hi")).str());
  25. EXPECT_EQ("hi", Twine(StringRef("hi")).str());
  26. EXPECT_EQ("hi", Twine(StringRef(std::string("hi"))).str());
  27. EXPECT_EQ("hi", Twine(StringRef("hithere", 2)).str());
  28. EXPECT_EQ("hi", Twine(SmallString<4>("hi")).str());
  29. }
  30. TEST(TwineTest, Numbers) {
  31. EXPECT_EQ("123", Twine(123U).str());
  32. EXPECT_EQ("123", Twine(123).str());
  33. EXPECT_EQ("-123", Twine(-123).str());
  34. EXPECT_EQ("123", Twine(123).str());
  35. EXPECT_EQ("-123", Twine(-123).str());
  36. EXPECT_EQ("7b", Twine::utohexstr(123).str());
  37. }
  38. TEST(TwineTest, Characters) {
  39. EXPECT_EQ("x", Twine('x').str());
  40. EXPECT_EQ("x", Twine(static_cast<unsigned char>('x')).str());
  41. EXPECT_EQ("x", Twine(static_cast<signed char>('x')).str());
  42. }
  43. TEST(TwineTest, Concat) {
  44. // Check verse repr, since we care about the actual representation not just
  45. // the result.
  46. // Concat with null.
  47. EXPECT_EQ("(Twine null empty)",
  48. repr(Twine("hi").concat(Twine::createNull())));
  49. EXPECT_EQ("(Twine null empty)",
  50. repr(Twine::createNull().concat(Twine("hi"))));
  51. // Concat with empty.
  52. EXPECT_EQ("(Twine cstring:\"hi\" empty)",
  53. repr(Twine("hi").concat(Twine())));
  54. EXPECT_EQ("(Twine cstring:\"hi\" empty)",
  55. repr(Twine().concat(Twine("hi"))));
  56. EXPECT_EQ("(Twine smallstring:\"hi\" empty)",
  57. repr(Twine().concat(Twine(SmallString<5>("hi")))));
  58. EXPECT_EQ("(Twine smallstring:\"hey\" cstring:\"there\")",
  59. repr(Twine(SmallString<7>("hey")).concat(Twine("there"))));
  60. // Concatenation of unary ropes.
  61. EXPECT_EQ("(Twine cstring:\"a\" cstring:\"b\")",
  62. repr(Twine("a").concat(Twine("b"))));
  63. // Concatenation of other ropes.
  64. EXPECT_EQ("(Twine rope:(Twine cstring:\"a\" cstring:\"b\") cstring:\"c\")",
  65. repr(Twine("a").concat(Twine("b")).concat(Twine("c"))));
  66. EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine cstring:\"b\" cstring:\"c\"))",
  67. repr(Twine("a").concat(Twine("b").concat(Twine("c")))));
  68. EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine smallstring:\"b\" cstring:\"c\"))",
  69. repr(Twine("a").concat(Twine(SmallString<3>("b")).concat(Twine("c")))));
  70. }
  71. TEST(TwineTest, toNullTerminatedStringRef) {
  72. SmallString<8> storage;
  73. EXPECT_EQ(0, *Twine("hello").toNullTerminatedStringRef(storage).end());
  74. EXPECT_EQ(0,
  75. *Twine(StringRef("hello")).toNullTerminatedStringRef(storage).end());
  76. EXPECT_EQ(0, *Twine(SmallString<11>("hello"))
  77. .toNullTerminatedStringRef(storage)
  78. .end());
  79. }
  80. // I suppose linking in the entire code generator to add a unit test to check
  81. // the code size of the concat operation is overkill... :)
  82. } // end anonymous namespace