1 //===- TwineTest.cpp - Twine unit tests -----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/Twine.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Support/FormatAdapters.h"
12 #include "llvm/Support/FormatVariadic.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "gtest/gtest.h"
15 using namespace llvm;
16 
17 namespace {
18 
repr(const Twine & Value)19 std::string repr(const Twine &Value) {
20   std::string res;
21   llvm::raw_string_ostream OS(res);
22   Value.printRepr(OS);
23   return OS.str();
24 }
25 
TEST(TwineTest,Construction)26 TEST(TwineTest, Construction) {
27   EXPECT_EQ("", Twine().str());
28   EXPECT_EQ("hi", Twine("hi").str());
29   EXPECT_EQ("hi", Twine(std::string("hi")).str());
30   EXPECT_EQ("hi", Twine(StringRef("hi")).str());
31   EXPECT_EQ("hi", Twine(StringRef(std::string("hi"))).str());
32   EXPECT_EQ("hi", Twine(StringRef("hithere", 2)).str());
33   EXPECT_EQ("hi", Twine(SmallString<4>("hi")).str());
34   EXPECT_EQ("hi", Twine(formatv("{0}", "hi")).str());
35 #if __cplusplus > 201402L
36   EXPECT_EQ("hi", Twine(std::string_view("hi")).str());
37 #endif
38 }
39 
TEST(TwineTest,Numbers)40 TEST(TwineTest, Numbers) {
41   EXPECT_EQ("123", Twine(123U).str());
42   EXPECT_EQ("123", Twine(123).str());
43   EXPECT_EQ("-123", Twine(-123).str());
44   EXPECT_EQ("123", Twine(123).str());
45   EXPECT_EQ("-123", Twine(-123).str());
46 
47   EXPECT_EQ("7b", Twine::utohexstr(123).str());
48 }
49 
TEST(TwineTest,Characters)50 TEST(TwineTest, Characters) {
51   EXPECT_EQ("x", Twine('x').str());
52   EXPECT_EQ("x", Twine(static_cast<unsigned char>('x')).str());
53   EXPECT_EQ("x", Twine(static_cast<signed char>('x')).str());
54 }
55 
TEST(TwineTest,Concat)56 TEST(TwineTest, Concat) {
57   // Check verse repr, since we care about the actual representation not just
58   // the result.
59 
60   // Concat with null.
61   EXPECT_EQ("(Twine null empty)",
62             repr(Twine("hi").concat(Twine::createNull())));
63   EXPECT_EQ("(Twine null empty)",
64             repr(Twine::createNull().concat(Twine("hi"))));
65 
66   // Concat with empty.
67   EXPECT_EQ("(Twine cstring:\"hi\" empty)",
68             repr(Twine("hi").concat(Twine())));
69   EXPECT_EQ("(Twine cstring:\"hi\" empty)",
70             repr(Twine().concat(Twine("hi"))));
71   EXPECT_EQ("(Twine ptrAndLength:\"hi\" empty)",
72             repr(Twine().concat(Twine(SmallString<5>("hi")))));
73   EXPECT_EQ("(Twine formatv:\"howdy\" empty)",
74             repr(Twine(formatv("howdy")).concat(Twine())));
75   EXPECT_EQ("(Twine formatv:\"howdy\" empty)",
76             repr(Twine().concat(Twine(formatv("howdy")))));
77   EXPECT_EQ("(Twine ptrAndLength:\"hey\" cstring:\"there\")",
78             repr(Twine(SmallString<7>("hey")).concat(Twine("there"))));
79 #if __cplusplus > 201402L
80   EXPECT_EQ("(Twine ptrAndLength:\"hey\" cstring:\"there\")",
81             repr(Twine(std::string_view("hey")).concat(Twine("there"))));
82 #endif
83 
84   // Concatenation of unary ropes.
85   EXPECT_EQ("(Twine cstring:\"a\" cstring:\"b\")",
86             repr(Twine("a").concat(Twine("b"))));
87 
88   // Concatenation of other ropes.
89   EXPECT_EQ("(Twine rope:(Twine cstring:\"a\" cstring:\"b\") cstring:\"c\")",
90             repr(Twine("a").concat(Twine("b")).concat(Twine("c"))));
91   EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine cstring:\"b\" cstring:\"c\"))",
92             repr(Twine("a").concat(Twine("b").concat(Twine("c")))));
93   EXPECT_EQ(
94       "(Twine cstring:\"a\" rope:(Twine ptrAndLength:\"b\" cstring:\"c\"))",
95       repr(Twine("a").concat(Twine(SmallString<3>("b")).concat(Twine("c")))));
96 }
97 
TEST(TwineTest,toNullTerminatedStringRef)98 TEST(TwineTest, toNullTerminatedStringRef) {
99   SmallString<8> storage;
100   EXPECT_EQ(0, *Twine("hello").toNullTerminatedStringRef(storage).end());
101   EXPECT_EQ(0,
102            *Twine(StringRef("hello")).toNullTerminatedStringRef(storage).end());
103   EXPECT_EQ(0, *Twine(SmallString<11>("hello"))
104                     .toNullTerminatedStringRef(storage)
105                     .end());
106   EXPECT_EQ(0, *Twine(formatv("{0}{1}", "how", "dy"))
107                     .toNullTerminatedStringRef(storage)
108                     .end());
109 }
110 
TEST(TwineTest,LazyEvaluation)111 TEST(TwineTest, LazyEvaluation) {
112   struct formatter : FormatAdapter<int> {
113     explicit formatter(int &Count) : FormatAdapter(0), Count(Count) {}
114     int &Count;
115 
116     void format(raw_ostream &OS, StringRef Style) override { ++Count; }
117   };
118 
119   int Count = 0;
120   formatter Formatter(Count);
121   (void)Twine(formatv("{0}", Formatter));
122   EXPECT_EQ(0, Count);
123   (void)Twine(formatv("{0}", Formatter)).str();
124   EXPECT_EQ(1, Count);
125 }
126 
127   // I suppose linking in the entire code generator to add a unit test to check
128   // the code size of the concat operation is overkill... :)
129 
130 } // end anonymous namespace
131