Support/ADT/Twine: Add toNullTerminatedStringRef.
[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/ADT/SmallString.h"
13 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
15
16 namespace {
17
18 std::string repr(const Twine &Value) {
19   std::string res;
20   llvm::raw_string_ostream OS(res);
21   Value.printRepr(OS);
22   return OS.str();
23 }
24
25 TEST(TwineTest, Construction) {
26   EXPECT_EQ("", Twine().str());
27   EXPECT_EQ("hi", Twine("hi").str());
28   EXPECT_EQ("hi", Twine(std::string("hi")).str());
29   EXPECT_EQ("hi", Twine(StringRef("hi")).str());
30   EXPECT_EQ("hi", Twine(StringRef(std::string("hi"))).str());
31   EXPECT_EQ("hi", Twine(StringRef("hithere", 2)).str());
32 }
33
34 TEST(TwineTest, Numbers) {
35   EXPECT_EQ("123", Twine(123U).str());
36   EXPECT_EQ("123", Twine(123).str());
37   EXPECT_EQ("-123", Twine(-123).str());
38   EXPECT_EQ("123", Twine(123).str());
39   EXPECT_EQ("-123", Twine(-123).str());
40   EXPECT_EQ("123", Twine((char) 123).str());
41   EXPECT_EQ("-123", Twine((signed char) -123).str());
42
43   EXPECT_EQ("7b", Twine::utohexstr(123).str());
44 }
45
46 TEST(TwineTest, Concat) {
47   // Check verse repr, since we care about the actual representation not just
48   // the result.
49
50   // Concat with null.
51   EXPECT_EQ("(Twine null empty)", 
52             repr(Twine("hi").concat(Twine::createNull())));
53   EXPECT_EQ("(Twine null empty)", 
54             repr(Twine::createNull().concat(Twine("hi"))));
55   
56   // Concat with empty.
57   EXPECT_EQ("(Twine cstring:\"hi\" empty)", 
58             repr(Twine("hi").concat(Twine())));
59   EXPECT_EQ("(Twine cstring:\"hi\" empty)", 
60             repr(Twine().concat(Twine("hi"))));
61
62   // Concatenation of unary ropes.
63   EXPECT_EQ("(Twine cstring:\"a\" cstring:\"b\")", 
64             repr(Twine("a").concat(Twine("b"))));
65
66   // Concatenation of other ropes.
67   EXPECT_EQ("(Twine rope:(Twine cstring:\"a\" cstring:\"b\") cstring:\"c\")", 
68             repr(Twine("a").concat(Twine("b")).concat(Twine("c"))));
69   EXPECT_EQ("(Twine cstring:\"a\" rope:(Twine cstring:\"b\" cstring:\"c\"))",
70             repr(Twine("a").concat(Twine("b").concat(Twine("c")))));
71 }
72
73 TEST(TwineTest, toNullTerminatedStringRef) {
74   SmallString<8> storage;
75   EXPECT_EQ(0, *Twine("hello").toNullTerminatedStringRef(storage).end());
76   EXPECT_EQ(0,
77            *Twine(StringRef("hello")).toNullTerminatedStringRef(storage).end());
78 }
79
80   // I suppose linking in the entire code generator to add a unit test to check
81   // the code size of the concat operation is overkill... :)
82
83 } // end anonymous namespace