1 //===- TwineTest.cpp - Twine unit tests -----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "gtest/gtest.h"
18 std::string repr(const Twine &Value) {
20 llvm::raw_string_ostream OS(res);
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());
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());
42 EXPECT_EQ("7b", Twine::utohexstr(123).str());
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());
51 TEST(TwineTest, Concat) {
52 // Check verse repr, since we care about the actual representation not just
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"))));
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"))));
71 // Concatenation of unary ropes.
72 EXPECT_EQ("(Twine cstring:\"a\" cstring:\"b\")",
73 repr(Twine("a").concat(Twine("b"))));
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")))));
84 TEST(TwineTest, toNullTerminatedStringRef) {
85 SmallString<8> storage;
86 EXPECT_EQ(0, *Twine("hello").toNullTerminatedStringRef(storage).end());
88 *Twine(StringRef("hello")).toNullTerminatedStringRef(storage).end());
89 EXPECT_EQ(0, *Twine(SmallString<11>("hello"))
90 .toNullTerminatedStringRef(storage)
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... :)
97 } // end anonymous namespace