166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===- TwineTest.cpp - Twine unit tests -----------------------------------===//
266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//                     The LLVM Compiler Infrastructure
466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// This file is distributed under the University of Illinois Open Source
666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// License. See LICENSE.TXT for details.
766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===----------------------------------------------------------------------===//
966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "gtest/gtest.h"
1166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/ADT/Twine.h"
1266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/ADT/SmallString.h"
1366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/raw_ostream.h"
1466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanusing namespace llvm;
1566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumannamespace {
1766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanstd::string repr(const Twine &Value) {
1966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  std::string res;
2066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  llvm::raw_string_ostream OS(res);
2166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  Value.printRepr(OS);
2266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  return OS.str();
2366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
2466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2566b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(TwineTest, Construction) {
2666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("", Twine().str());
2766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("hi", Twine("hi").str());
2866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("hi", Twine(std::string("hi")).str());
2966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("hi", Twine(StringRef("hi")).str());
3066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("hi", Twine(StringRef(std::string("hi"))).str());
3166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("hi", Twine(StringRef("hithere", 2)).str());
3266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
3366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
3466b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(TwineTest, Numbers) {
3566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("123", Twine(123U).str());
3666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("123", Twine(123).str());
3766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("-123", Twine(-123).str());
3866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("123", Twine(123).str());
3966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("-123", Twine(-123).str());
4066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
4166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("7b", Twine::utohexstr(123).str());
4266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
4366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
4466b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(TwineTest, Characters) {
4566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("x", Twine('x').str());
4666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("x", Twine(static_cast<unsigned char>('x')).str());
4766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("x", Twine(static_cast<signed char>('x')).str());
4866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
4966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5066b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(TwineTest, Concat) {
5166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Check verse repr, since we care about the actual representation not just
5266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // the result.
5366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Concat with null.
5566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("(Twine null empty)",
5666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman            repr(Twine("hi").concat(Twine::createNull())));
5766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("(Twine null empty)",
5866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman            repr(Twine::createNull().concat(Twine("hi"))));
5966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
6066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Concat with empty.
6166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("(Twine cstring:\"hi\" empty)",
6266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman            repr(Twine("hi").concat(Twine())));
6366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("(Twine cstring:\"hi\" empty)",
6466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman            repr(Twine().concat(Twine("hi"))));
6566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
6666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Concatenation of unary ropes.
6766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("(Twine cstring:\"a\" cstring:\"b\")",
6866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman            repr(Twine("a").concat(Twine("b"))));
6966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
7066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Concatenation of other ropes.
7166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("(Twine rope:(Twine cstring:\"a\" cstring:\"b\") cstring:\"c\")",
7266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman            repr(Twine("a").concat(Twine("b")).concat(Twine("c"))));
7366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine cstring:\"b\" cstring:\"c\"))",
7466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman            repr(Twine("a").concat(Twine("b").concat(Twine("c")))));
7566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
7666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
7766b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(TwineTest, toNullTerminatedStringRef) {
7866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  SmallString<8> storage;
7966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ(0, *Twine("hello").toNullTerminatedStringRef(storage).end());
8066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ(0,
8166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman           *Twine(StringRef("hello")).toNullTerminatedStringRef(storage).end());
8266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
8366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
8466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // I suppose linking in the entire code generator to add a unit test to check
8566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // the code size of the concat operation is overkill... :)
8666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
8766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman} // end anonymous namespace
88