Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 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 | |
Zachary Turner | d69dfb1 | 2016-12-17 00:38:15 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | 3c0d607 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallString.h" |
Zachary Turner | d69dfb1 | 2016-12-17 00:38:15 +0000 | [diff] [blame] | 12 | #include "llvm/Support/FormatAdapters.h" |
| 13 | #include "llvm/Support/FormatVariadic.h" |
Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 5a88dda | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | std::string repr(const Twine &Value) { |
| 21 | std::string res; |
| 22 | llvm::raw_string_ostream OS(res); |
| 23 | Value.printRepr(OS); |
| 24 | return OS.str(); |
| 25 | } |
| 26 | |
| 27 | TEST(TwineTest, Construction) { |
| 28 | EXPECT_EQ("", Twine().str()); |
| 29 | EXPECT_EQ("hi", Twine("hi").str()); |
| 30 | EXPECT_EQ("hi", Twine(std::string("hi")).str()); |
| 31 | EXPECT_EQ("hi", Twine(StringRef("hi")).str()); |
| 32 | EXPECT_EQ("hi", Twine(StringRef(std::string("hi"))).str()); |
| 33 | EXPECT_EQ("hi", Twine(StringRef("hithere", 2)).str()); |
Yaron Keren | 0401f79 | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 34 | EXPECT_EQ("hi", Twine(SmallString<4>("hi")).str()); |
Zachary Turner | d69dfb1 | 2016-12-17 00:38:15 +0000 | [diff] [blame] | 35 | EXPECT_EQ("hi", Twine(formatv("{0}", "hi")).str()); |
Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Daniel Dunbar | 763457e | 2009-07-29 07:08:44 +0000 | [diff] [blame] | 38 | TEST(TwineTest, Numbers) { |
Daniel Dunbar | 0165a2c | 2009-07-30 03:47:15 +0000 | [diff] [blame] | 39 | EXPECT_EQ("123", Twine(123U).str()); |
| 40 | EXPECT_EQ("123", Twine(123).str()); |
| 41 | EXPECT_EQ("-123", Twine(-123).str()); |
| 42 | EXPECT_EQ("123", Twine(123).str()); |
| 43 | EXPECT_EQ("-123", Twine(-123).str()); |
Daniel Dunbar | 763457e | 2009-07-29 07:08:44 +0000 | [diff] [blame] | 44 | |
Daniel Dunbar | 0fffbaf | 2009-07-30 18:30:19 +0000 | [diff] [blame] | 45 | EXPECT_EQ("7b", Twine::utohexstr(123).str()); |
Daniel Dunbar | 763457e | 2009-07-29 07:08:44 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | 3f25ee0 | 2011-07-24 20:44:30 +0000 | [diff] [blame] | 48 | TEST(TwineTest, Characters) { |
| 49 | EXPECT_EQ("x", Twine('x').str()); |
| 50 | EXPECT_EQ("x", Twine(static_cast<unsigned char>('x')).str()); |
| 51 | EXPECT_EQ("x", Twine(static_cast<signed char>('x')).str()); |
| 52 | } |
| 53 | |
Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 54 | TEST(TwineTest, Concat) { |
| 55 | // Check verse repr, since we care about the actual representation not just |
| 56 | // the result. |
| 57 | |
| 58 | // Concat with null. |
| 59 | EXPECT_EQ("(Twine null empty)", |
| 60 | repr(Twine("hi").concat(Twine::createNull()))); |
| 61 | EXPECT_EQ("(Twine null empty)", |
| 62 | repr(Twine::createNull().concat(Twine("hi")))); |
| 63 | |
| 64 | // Concat with empty. |
| 65 | EXPECT_EQ("(Twine cstring:\"hi\" empty)", |
| 66 | repr(Twine("hi").concat(Twine()))); |
| 67 | EXPECT_EQ("(Twine cstring:\"hi\" empty)", |
| 68 | repr(Twine().concat(Twine("hi")))); |
Yaron Keren | 0401f79 | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 69 | EXPECT_EQ("(Twine smallstring:\"hi\" empty)", |
| 70 | repr(Twine().concat(Twine(SmallString<5>("hi"))))); |
Zachary Turner | d69dfb1 | 2016-12-17 00:38:15 +0000 | [diff] [blame] | 71 | EXPECT_EQ("(Twine formatv:\"howdy\" empty)", |
| 72 | repr(Twine(formatv("howdy")).concat(Twine()))); |
| 73 | EXPECT_EQ("(Twine formatv:\"howdy\" empty)", |
| 74 | repr(Twine().concat(Twine(formatv("howdy"))))); |
Yaron Keren | 0401f79 | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 75 | EXPECT_EQ("(Twine smallstring:\"hey\" cstring:\"there\")", |
| 76 | repr(Twine(SmallString<7>("hey")).concat(Twine("there")))); |
Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 77 | |
| 78 | // Concatenation of unary ropes. |
| 79 | EXPECT_EQ("(Twine cstring:\"a\" cstring:\"b\")", |
| 80 | repr(Twine("a").concat(Twine("b")))); |
| 81 | |
| 82 | // Concatenation of other ropes. |
| 83 | EXPECT_EQ("(Twine rope:(Twine cstring:\"a\" cstring:\"b\") cstring:\"c\")", |
| 84 | repr(Twine("a").concat(Twine("b")).concat(Twine("c")))); |
| 85 | EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine cstring:\"b\" cstring:\"c\"))", |
| 86 | repr(Twine("a").concat(Twine("b").concat(Twine("c"))))); |
Yaron Keren | 0401f79 | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 87 | EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine smallstring:\"b\" cstring:\"c\"))", |
| 88 | repr(Twine("a").concat(Twine(SmallString<3>("b")).concat(Twine("c"))))); |
Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Michael J. Spencer | 7dc7ac3 | 2010-12-01 20:37:30 +0000 | [diff] [blame] | 91 | TEST(TwineTest, toNullTerminatedStringRef) { |
| 92 | SmallString<8> storage; |
| 93 | EXPECT_EQ(0, *Twine("hello").toNullTerminatedStringRef(storage).end()); |
| 94 | EXPECT_EQ(0, |
| 95 | *Twine(StringRef("hello")).toNullTerminatedStringRef(storage).end()); |
Yaron Keren | 0401f79 | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 96 | EXPECT_EQ(0, *Twine(SmallString<11>("hello")) |
| 97 | .toNullTerminatedStringRef(storage) |
| 98 | .end()); |
Zachary Turner | d69dfb1 | 2016-12-17 00:38:15 +0000 | [diff] [blame] | 99 | EXPECT_EQ(0, *Twine(formatv("{0}{1}", "how", "dy")) |
| 100 | .toNullTerminatedStringRef(storage) |
| 101 | .end()); |
| 102 | } |
| 103 | |
| 104 | TEST(TwineTest, LazyEvaluation) { |
| 105 | struct formatter : FormatAdapter<int> { |
Evgeniy Stepanov | af2bc9b | 2016-12-17 01:31:46 +0000 | [diff] [blame] | 106 | explicit formatter(int &Count) : FormatAdapter(0), Count(Count) {} |
Zachary Turner | d69dfb1 | 2016-12-17 00:38:15 +0000 | [diff] [blame] | 107 | int &Count; |
| 108 | |
| 109 | void format(raw_ostream &OS, StringRef Style) { ++Count; } |
| 110 | }; |
| 111 | |
| 112 | int Count = 0; |
| 113 | formatter Formatter(Count); |
| 114 | (void)Twine(formatv("{0}", Formatter)); |
| 115 | EXPECT_EQ(0, Count); |
| 116 | (void)Twine(formatv("{0}", Formatter)).str(); |
| 117 | EXPECT_EQ(1, Count); |
Michael J. Spencer | 7dc7ac3 | 2010-12-01 20:37:30 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 120 | // I suppose linking in the entire code generator to add a unit test to check |
| 121 | // the code size of the concat operation is overkill... :) |
| 122 | |
| 123 | } // end anonymous namespace |