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