llvm-6502/unittests/ADT/TwineTest.cpp
Daniel Dunbar 763457e70b Twines: Support numeric conversion directly (uitostr, etc).
- Provides static constructors for doing number to string conversions without
   using temporaries.

 - There are several ways to do this, I think given the Twine constraints this
   is the simplest one.

 - One FIXME for fast number -> hex conversion.

 - Added another comment on one last major bit of perf work Twines need, which
   is to make raw_svector_ostream more efficient.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77445 91177308-0d34-0410-b5e6-96231b3b80d8
2009-07-29 07:08:44 +00:00

76 lines
2.5 KiB
C++

//===- TwineTest.cpp - Twine unit tests -----------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/raw_ostream.h"
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());
}
TEST(TwineTest, Numbers) {
EXPECT_EQ("123", Twine::utostr(123).str());
EXPECT_EQ("-123", Twine::itostr(-123).str());
EXPECT_EQ("123", Twine::utostr(123).str());
EXPECT_EQ("-123", Twine::itostr(-123).str());
EXPECT_EQ("123", Twine::utostr((char) 123).str());
EXPECT_EQ("-123", Twine::itostr((char) -123).str());
EXPECT_EQ("7B", Twine::utohexstr(123).str());
EXPECT_EQ("FFFFFFFFFFFFFF85", Twine::itohexstr(-123).str());
}
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"))));
// 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")))));
}
// 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