blob: 9683e97511b6cb583e9e4f977f2fe2a410371837 [file] [log] [blame]
Daniel Dunbarafcf5b32009-07-24 07:04:27 +00001//===- 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
Daniel Dunbarafcf5b32009-07-24 07:04:27 +000010#include "llvm/ADT/Twine.h"
Michael J. Spencerf2cc8282010-12-01 20:37:30 +000011#include "llvm/ADT/SmallString.h"
Daniel Dunbarafcf5b32009-07-24 07:04:27 +000012#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000013#include "gtest/gtest.h"
Daniel Dunbarafcf5b32009-07-24 07:04:27 +000014using namespace llvm;
15
16namespace {
17
18std::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
25TEST(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());
Yaron Keren1ee89fc2015-03-17 09:51:17 +000032 EXPECT_EQ("hi", Twine(SmallString<4>("hi")).str());
Daniel Dunbarafcf5b32009-07-24 07:04:27 +000033}
34
Daniel Dunbarb49994a2009-07-29 07:08:44 +000035TEST(TwineTest, Numbers) {
Daniel Dunbare8b32362009-07-30 03:47:15 +000036 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());
Daniel Dunbarb49994a2009-07-29 07:08:44 +000041
Daniel Dunbarbd8556e2009-07-30 18:30:19 +000042 EXPECT_EQ("7b", Twine::utohexstr(123).str());
Daniel Dunbarb49994a2009-07-29 07:08:44 +000043}
44
Chris Lattner9650f062011-07-24 20:44:30 +000045TEST(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
Daniel Dunbarafcf5b32009-07-24 07:04:27 +000051TEST(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"))));
Yaron Keren1ee89fc2015-03-17 09:51:17 +000066 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"))));
Daniel Dunbarafcf5b32009-07-24 07:04:27 +000070
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")))));
Yaron Keren1ee89fc2015-03-17 09:51:17 +000080 EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine smallstring:\"b\" cstring:\"c\"))",
81 repr(Twine("a").concat(Twine(SmallString<3>("b")).concat(Twine("c")))));
Daniel Dunbarafcf5b32009-07-24 07:04:27 +000082}
83
Michael J. Spencerf2cc8282010-12-01 20:37:30 +000084TEST(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());
Yaron Keren1ee89fc2015-03-17 09:51:17 +000089 EXPECT_EQ(0, *Twine(SmallString<11>("hello"))
90 .toNullTerminatedStringRef(storage)
91 .end());
Michael J. Spencerf2cc8282010-12-01 20:37:30 +000092}
93
Daniel Dunbarafcf5b32009-07-24 07:04:27 +000094 // 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