File: TwineTest.cpp

package info (click to toggle)
llvm 2.6-9.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 57,604 kB
  • ctags: 44,336
  • sloc: cpp: 344,766; sh: 12,407; ansic: 10,617; ada: 3,070; ml: 2,505; perl: 2,496; makefile: 1,426; pascal: 1,163; exp: 389; asm: 307; python: 298; objc: 260; lisp: 182; csh: 117; xml: 38; f90: 36; tcl: 20
file content (75 lines) | stat: -rw-r--r-- 2,496 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//===- 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(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());
  EXPECT_EQ("123", Twine((char) 123).str());
  EXPECT_EQ("-123", Twine((signed char) -123).str());

  EXPECT_EQ("7b", Twine::utohexstr(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