50fdd2ed3141f26b7e665b9e3329d27a4549a579
[oota-llvm.git] / unittests / ADT / TwineTest.cpp
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
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/raw_ostream.h"
13 using namespace llvm;
14
15 namespace {
16
17 std::string repr(const Twine &Value) {
18   std::string res;
19   llvm::raw_string_ostream OS(res);
20   Value.printRepr(OS);
21   return OS.str();
22 }
23
24 TEST(TwineTest, Construction) {
25   EXPECT_EQ("", Twine().str());
26   EXPECT_EQ("hi", Twine("hi").str());
27   EXPECT_EQ("hi", Twine(std::string("hi")).str());
28   EXPECT_EQ("hi", Twine(StringRef("hi")).str());
29   EXPECT_EQ("hi", Twine(StringRef(std::string("hi"))).str());
30   EXPECT_EQ("hi", Twine(StringRef("hithere", 2)).str());
31 }
32
33 TEST(TwineTest, Concat) {
34   // Check verse repr, since we care about the actual representation not just
35   // the result.
36
37   // Concat with null.
38   EXPECT_EQ("(Twine null empty)", 
39             repr(Twine("hi").concat(Twine::createNull())));
40   EXPECT_EQ("(Twine null empty)", 
41             repr(Twine::createNull().concat(Twine("hi"))));
42   
43   // Concat with empty.
44   EXPECT_EQ("(Twine cstring:\"hi\" empty)", 
45             repr(Twine("hi").concat(Twine())));
46   EXPECT_EQ("(Twine cstring:\"hi\" empty)", 
47             repr(Twine().concat(Twine("hi"))));
48
49   // Concatenation of unary ropes.
50   EXPECT_EQ("(Twine cstring:\"a\" cstring:\"b\")", 
51             repr(Twine("a").concat(Twine("b"))));
52
53   // Concatenation of other ropes.
54   EXPECT_EQ("(Twine rope:(Twine cstring:\"a\" cstring:\"b\") cstring:\"c\")", 
55             repr(Twine("a").concat(Twine("b")).concat(Twine("c"))));
56   EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine cstring:\"b\" cstring:\"c\"))",
57             repr(Twine("a").concat(Twine("b").concat(Twine("c")))));
58 }
59
60   // I suppose linking in the entire code generator to add a unit test to check
61   // the code size of the concat operation is overkill... :)
62
63 } // end anonymous namespace