2009-07-24 09:04:27 +02:00
|
|
|
//===- TwineTest.cpp - Twine unit tests -----------------------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-07-24 09:04:27 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-12-17 01:38:15 +01:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-06 13:06:56 +02:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2016-12-17 01:38:15 +01:00
|
|
|
#include "llvm/Support/FormatAdapters.h"
|
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2009-07-24 09:04:27 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 11:23:08 +01:00
|
|
|
#include "gtest/gtest.h"
|
2009-07-24 09:04:27 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::string repr(const Twine &Value) {
|
|
|
|
std::string res;
|
|
|
|
llvm::raw_string_ostream OS(res);
|
|
|
|
Value.printRepr(OS);
|
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TwineTest, Construction) {
|
|
|
|
EXPECT_EQ("", Twine().str());
|
|
|
|
EXPECT_EQ("hi", Twine("hi").str());
|
|
|
|
EXPECT_EQ("hi", Twine(std::string("hi")).str());
|
|
|
|
EXPECT_EQ("hi", Twine(StringRef("hi")).str());
|
|
|
|
EXPECT_EQ("hi", Twine(StringRef(std::string("hi"))).str());
|
|
|
|
EXPECT_EQ("hi", Twine(StringRef("hithere", 2)).str());
|
2015-03-17 10:51:17 +01:00
|
|
|
EXPECT_EQ("hi", Twine(SmallString<4>("hi")).str());
|
2016-12-17 01:38:15 +01:00
|
|
|
EXPECT_EQ("hi", Twine(formatv("{0}", "hi")).str());
|
2021-06-09 01:57:26 +02:00
|
|
|
#if __cplusplus > 201402L
|
|
|
|
EXPECT_EQ("hi", Twine(std::string_view("hi")).str());
|
|
|
|
#endif
|
2009-07-24 09:04:27 +02:00
|
|
|
}
|
|
|
|
|
2009-07-29 09:08:44 +02:00
|
|
|
TEST(TwineTest, Numbers) {
|
2009-07-30 05:47:15 +02:00
|
|
|
EXPECT_EQ("123", Twine(123U).str());
|
|
|
|
EXPECT_EQ("123", Twine(123).str());
|
|
|
|
EXPECT_EQ("-123", Twine(-123).str());
|
|
|
|
EXPECT_EQ("123", Twine(123).str());
|
|
|
|
EXPECT_EQ("-123", Twine(-123).str());
|
2009-07-29 09:08:44 +02:00
|
|
|
|
2009-07-30 20:30:19 +02:00
|
|
|
EXPECT_EQ("7b", Twine::utohexstr(123).str());
|
2009-07-29 09:08:44 +02:00
|
|
|
}
|
|
|
|
|
2011-07-24 22:44:30 +02:00
|
|
|
TEST(TwineTest, Characters) {
|
|
|
|
EXPECT_EQ("x", Twine('x').str());
|
|
|
|
EXPECT_EQ("x", Twine(static_cast<unsigned char>('x')).str());
|
|
|
|
EXPECT_EQ("x", Twine(static_cast<signed char>('x')).str());
|
|
|
|
}
|
|
|
|
|
2009-07-24 09:04:27 +02:00
|
|
|
TEST(TwineTest, Concat) {
|
|
|
|
// Check verse repr, since we care about the actual representation not just
|
|
|
|
// the result.
|
|
|
|
|
|
|
|
// Concat with null.
|
|
|
|
EXPECT_EQ("(Twine null empty)",
|
|
|
|
repr(Twine("hi").concat(Twine::createNull())));
|
|
|
|
EXPECT_EQ("(Twine null empty)",
|
|
|
|
repr(Twine::createNull().concat(Twine("hi"))));
|
|
|
|
|
|
|
|
// Concat with empty.
|
|
|
|
EXPECT_EQ("(Twine cstring:\"hi\" empty)",
|
|
|
|
repr(Twine("hi").concat(Twine())));
|
|
|
|
EXPECT_EQ("(Twine cstring:\"hi\" empty)",
|
|
|
|
repr(Twine().concat(Twine("hi"))));
|
2021-07-20 19:29:22 +02:00
|
|
|
EXPECT_EQ("(Twine ptrAndLength:\"hi\" empty)",
|
2015-03-17 10:51:17 +01:00
|
|
|
repr(Twine().concat(Twine(SmallString<5>("hi")))));
|
2016-12-17 01:38:15 +01:00
|
|
|
EXPECT_EQ("(Twine formatv:\"howdy\" empty)",
|
|
|
|
repr(Twine(formatv("howdy")).concat(Twine())));
|
|
|
|
EXPECT_EQ("(Twine formatv:\"howdy\" empty)",
|
|
|
|
repr(Twine().concat(Twine(formatv("howdy")))));
|
2021-07-20 19:29:22 +02:00
|
|
|
EXPECT_EQ("(Twine ptrAndLength:\"hey\" cstring:\"there\")",
|
2015-03-17 10:51:17 +01:00
|
|
|
repr(Twine(SmallString<7>("hey")).concat(Twine("there"))));
|
2021-06-09 01:57:26 +02:00
|
|
|
#if __cplusplus > 201402L
|
2021-07-16 22:17:16 +02:00
|
|
|
EXPECT_EQ("(Twine ptrAndLength:\"hey\" cstring:\"there\")",
|
2021-06-09 01:57:26 +02:00
|
|
|
repr(Twine(std::string_view("hey")).concat(Twine("there"))));
|
|
|
|
#endif
|
2009-07-24 09:04:27 +02:00
|
|
|
|
|
|
|
// Concatenation of unary ropes.
|
|
|
|
EXPECT_EQ("(Twine cstring:\"a\" cstring:\"b\")",
|
|
|
|
repr(Twine("a").concat(Twine("b"))));
|
|
|
|
|
|
|
|
// Concatenation of other ropes.
|
|
|
|
EXPECT_EQ("(Twine rope:(Twine cstring:\"a\" cstring:\"b\") cstring:\"c\")",
|
|
|
|
repr(Twine("a").concat(Twine("b")).concat(Twine("c"))));
|
|
|
|
EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine cstring:\"b\" cstring:\"c\"))",
|
|
|
|
repr(Twine("a").concat(Twine("b").concat(Twine("c")))));
|
2021-07-20 19:29:22 +02:00
|
|
|
EXPECT_EQ(
|
|
|
|
"(Twine cstring:\"a\" rope:(Twine ptrAndLength:\"b\" cstring:\"c\"))",
|
|
|
|
repr(Twine("a").concat(Twine(SmallString<3>("b")).concat(Twine("c")))));
|
2009-07-24 09:04:27 +02:00
|
|
|
}
|
|
|
|
|
2010-12-01 21:37:30 +01:00
|
|
|
TEST(TwineTest, toNullTerminatedStringRef) {
|
|
|
|
SmallString<8> storage;
|
|
|
|
EXPECT_EQ(0, *Twine("hello").toNullTerminatedStringRef(storage).end());
|
|
|
|
EXPECT_EQ(0,
|
|
|
|
*Twine(StringRef("hello")).toNullTerminatedStringRef(storage).end());
|
2015-03-17 10:51:17 +01:00
|
|
|
EXPECT_EQ(0, *Twine(SmallString<11>("hello"))
|
|
|
|
.toNullTerminatedStringRef(storage)
|
|
|
|
.end());
|
2016-12-17 01:38:15 +01:00
|
|
|
EXPECT_EQ(0, *Twine(formatv("{0}{1}", "how", "dy"))
|
|
|
|
.toNullTerminatedStringRef(storage)
|
|
|
|
.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TwineTest, LazyEvaluation) {
|
|
|
|
struct formatter : FormatAdapter<int> {
|
2016-12-17 02:31:46 +01:00
|
|
|
explicit formatter(int &Count) : FormatAdapter(0), Count(Count) {}
|
2016-12-17 01:38:15 +01:00
|
|
|
int &Count;
|
|
|
|
|
2020-07-17 05:36:46 +02:00
|
|
|
void format(raw_ostream &OS, StringRef Style) override { ++Count; }
|
2016-12-17 01:38:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
int Count = 0;
|
|
|
|
formatter Formatter(Count);
|
|
|
|
(void)Twine(formatv("{0}", Formatter));
|
|
|
|
EXPECT_EQ(0, Count);
|
|
|
|
(void)Twine(formatv("{0}", Formatter)).str();
|
|
|
|
EXPECT_EQ(1, Count);
|
2010-12-01 21:37:30 +01:00
|
|
|
}
|
|
|
|
|
2009-07-24 09:04:27 +02:00
|
|
|
// I suppose linking in the entire code generator to add a unit test to check
|
|
|
|
// the code size of the concat operation is overkill... :)
|
|
|
|
|
|
|
|
} // end anonymous namespace
|