blob: 39d3b561b66897684a5a5e64353a4475806b0537 [file] [log] [blame]
Daniel Dunbar2538f7a2009-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 Dunbar2538f7a2009-07-24 07:04:27 +000010#include "llvm/ADT/Twine.h"
Michael J. Spencer7dc7ac32010-12-01 20:37:30 +000011#include "llvm/ADT/SmallString.h"
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000012#include "llvm/Support/raw_ostream.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000013#include "gtest/gtest.h"
Daniel Dunbar2538f7a2009-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());
32}
33
Daniel Dunbar763457e2009-07-29 07:08:44 +000034TEST(TwineTest, Numbers) {
Daniel Dunbar0165a2c2009-07-30 03:47:15 +000035 EXPECT_EQ("123", Twine(123U).str());
36 EXPECT_EQ("123", Twine(123).str());
37 EXPECT_EQ("-123", Twine(-123).str());
38 EXPECT_EQ("123", Twine(123).str());
39 EXPECT_EQ("-123", Twine(-123).str());
Daniel Dunbar763457e2009-07-29 07:08:44 +000040
Daniel Dunbar0fffbaf2009-07-30 18:30:19 +000041 EXPECT_EQ("7b", Twine::utohexstr(123).str());
Daniel Dunbar763457e2009-07-29 07:08:44 +000042}
43
Chris Lattner3f25ee02011-07-24 20:44:30 +000044TEST(TwineTest, Characters) {
45 EXPECT_EQ("x", Twine('x').str());
46 EXPECT_EQ("x", Twine(static_cast<unsigned char>('x')).str());
47 EXPECT_EQ("x", Twine(static_cast<signed char>('x')).str());
48}
49
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000050TEST(TwineTest, Concat) {
51 // Check verse repr, since we care about the actual representation not just
52 // the result.
53
54 // Concat with null.
55 EXPECT_EQ("(Twine null empty)",
56 repr(Twine("hi").concat(Twine::createNull())));
57 EXPECT_EQ("(Twine null empty)",
58 repr(Twine::createNull().concat(Twine("hi"))));
59
60 // Concat with empty.
61 EXPECT_EQ("(Twine cstring:\"hi\" empty)",
62 repr(Twine("hi").concat(Twine())));
63 EXPECT_EQ("(Twine cstring:\"hi\" empty)",
64 repr(Twine().concat(Twine("hi"))));
65
66 // Concatenation of unary ropes.
67 EXPECT_EQ("(Twine cstring:\"a\" cstring:\"b\")",
68 repr(Twine("a").concat(Twine("b"))));
69
70 // Concatenation of other ropes.
71 EXPECT_EQ("(Twine rope:(Twine cstring:\"a\" cstring:\"b\") cstring:\"c\")",
72 repr(Twine("a").concat(Twine("b")).concat(Twine("c"))));
73 EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine cstring:\"b\" cstring:\"c\"))",
74 repr(Twine("a").concat(Twine("b").concat(Twine("c")))));
75}
76
Michael J. Spencer7dc7ac32010-12-01 20:37:30 +000077TEST(TwineTest, toNullTerminatedStringRef) {
78 SmallString<8> storage;
79 EXPECT_EQ(0, *Twine("hello").toNullTerminatedStringRef(storage).end());
80 EXPECT_EQ(0,
81 *Twine(StringRef("hello")).toNullTerminatedStringRef(storage).end());
82}
83
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000084 // I suppose linking in the entire code generator to add a unit test to check
85 // the code size of the concat operation is overkill... :)
86
87} // end anonymous namespace