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