/3rd_party/llvm/unittests/ADT/TwineTest.cpp

https://code.google.com/p/softart/ · C++ · 87 lines · 56 code · 15 blank · 16 comment · 0 complexity · 840f17c066dd8c04fb71b954b33f2504 MD5 · raw file

  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. }
  29. TEST(TwineTest, Numbers) {
  30. EXPECT_EQ("123", Twine(123U).str());
  31. EXPECT_EQ("123", Twine(123).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("7b", Twine::utohexstr(123).str());
  36. }
  37. TEST(TwineTest, Characters) {
  38. EXPECT_EQ("x", Twine('x').str());
  39. EXPECT_EQ("x", Twine(static_cast<unsigned char>('x')).str());
  40. EXPECT_EQ("x", Twine(static_cast<signed char>('x')).str());
  41. }
  42. TEST(TwineTest, Concat) {
  43. // Check verse repr, since we care about the actual representation not just
  44. // the result.
  45. // Concat with null.
  46. EXPECT_EQ("(Twine null empty)",
  47. repr(Twine("hi").concat(Twine::createNull())));
  48. EXPECT_EQ("(Twine null empty)",
  49. repr(Twine::createNull().concat(Twine("hi"))));
  50. // Concat with empty.
  51. EXPECT_EQ("(Twine cstring:\"hi\" empty)",
  52. repr(Twine("hi").concat(Twine())));
  53. EXPECT_EQ("(Twine cstring:\"hi\" empty)",
  54. repr(Twine().concat(Twine("hi"))));
  55. // Concatenation of unary ropes.
  56. EXPECT_EQ("(Twine cstring:\"a\" cstring:\"b\")",
  57. repr(Twine("a").concat(Twine("b"))));
  58. // Concatenation of other ropes.
  59. EXPECT_EQ("(Twine rope:(Twine cstring:\"a\" cstring:\"b\") cstring:\"c\")",
  60. repr(Twine("a").concat(Twine("b")).concat(Twine("c"))));
  61. EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine cstring:\"b\" cstring:\"c\"))",
  62. repr(Twine("a").concat(Twine("b").concat(Twine("c")))));
  63. }
  64. TEST(TwineTest, toNullTerminatedStringRef) {
  65. SmallString<8> storage;
  66. EXPECT_EQ(0, *Twine("hello").toNullTerminatedStringRef(storage).end());
  67. EXPECT_EQ(0,
  68. *Twine(StringRef("hello")).toNullTerminatedStringRef(storage).end());
  69. }
  70. // I suppose linking in the entire code generator to add a unit test to check
  71. // the code size of the concat operation is overkill... :)
  72. } // end anonymous namespace