• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/unittest/Support/raw_ostream_test.cpp - raw_ostream 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/SmallString.h"
12 #include "llvm/Support/Format.h"
13 #include "llvm/Support/raw_ostream.h"
14 
15 using namespace llvm;
16 
17 namespace {
18 
printToString(const T & Value)19 template<typename T> std::string printToString(const T &Value) {
20   std::string res;
21   llvm::raw_string_ostream(res) << Value;
22   return res;
23 }
24 
25 /// printToString - Print the given value to a stream which only has \arg
26 /// BytesLeftInBuffer bytes left in the buffer. This is useful for testing edge
27 /// cases in the buffer handling logic.
printToString(const T & Value,unsigned BytesLeftInBuffer)28 template<typename T> std::string printToString(const T &Value,
29                                                unsigned BytesLeftInBuffer) {
30   // FIXME: This is relying on internal knowledge of how raw_ostream works to
31   // get the buffer position right.
32   SmallString<256> SVec;
33   assert(BytesLeftInBuffer < 256 && "Invalid buffer count!");
34   llvm::raw_svector_ostream OS(SVec);
35   unsigned StartIndex = 256 - BytesLeftInBuffer;
36   for (unsigned i = 0; i != StartIndex; ++i)
37     OS << '?';
38   OS << Value;
39   return OS.str().substr(StartIndex);
40 }
41 
printToStringUnbuffered(const T & Value)42 template<typename T> std::string printToStringUnbuffered(const T &Value) {
43   std::string res;
44   llvm::raw_string_ostream OS(res);
45   OS.SetUnbuffered();
46   OS << Value;
47   return res;
48 }
49 
TEST(raw_ostreamTest,Types_Buffered)50 TEST(raw_ostreamTest, Types_Buffered) {
51   // Char
52   EXPECT_EQ("c", printToString('c'));
53 
54   // String
55   EXPECT_EQ("hello", printToString("hello"));
56   EXPECT_EQ("hello", printToString(std::string("hello")));
57 
58   // Int
59   EXPECT_EQ("0", printToString(0));
60   EXPECT_EQ("2425", printToString(2425));
61   EXPECT_EQ("-2425", printToString(-2425));
62 
63   // Long long
64   EXPECT_EQ("0", printToString(0LL));
65   EXPECT_EQ("257257257235709", printToString(257257257235709LL));
66   EXPECT_EQ("-257257257235709", printToString(-257257257235709LL));
67 
68   // Double
69   EXPECT_EQ("1.100000e+00", printToString(1.1));
70 
71   // void*
72   EXPECT_EQ("0x0", printToString((void*) nullptr));
73   EXPECT_EQ("0xbeef", printToString((void*) 0xbeef));
74   EXPECT_EQ("0xdeadbeef", printToString((void*) 0xdeadbeef));
75 
76   // Min and max.
77   EXPECT_EQ("18446744073709551615", printToString(UINT64_MAX));
78   EXPECT_EQ("-9223372036854775808", printToString(INT64_MIN));
79 }
80 
TEST(raw_ostreamTest,Types_Unbuffered)81 TEST(raw_ostreamTest, Types_Unbuffered) {
82   // Char
83   EXPECT_EQ("c", printToStringUnbuffered('c'));
84 
85   // String
86   EXPECT_EQ("hello", printToStringUnbuffered("hello"));
87   EXPECT_EQ("hello", printToStringUnbuffered(std::string("hello")));
88 
89   // Int
90   EXPECT_EQ("0", printToStringUnbuffered(0));
91   EXPECT_EQ("2425", printToStringUnbuffered(2425));
92   EXPECT_EQ("-2425", printToStringUnbuffered(-2425));
93 
94   // Long long
95   EXPECT_EQ("0", printToStringUnbuffered(0LL));
96   EXPECT_EQ("257257257235709", printToStringUnbuffered(257257257235709LL));
97   EXPECT_EQ("-257257257235709", printToStringUnbuffered(-257257257235709LL));
98 
99   // Double
100   EXPECT_EQ("1.100000e+00", printToStringUnbuffered(1.1));
101 
102   // void*
103   EXPECT_EQ("0x0", printToStringUnbuffered((void*) nullptr));
104   EXPECT_EQ("0xbeef", printToStringUnbuffered((void*) 0xbeef));
105   EXPECT_EQ("0xdeadbeef", printToStringUnbuffered((void*) 0xdeadbeef));
106 
107   // Min and max.
108   EXPECT_EQ("18446744073709551615", printToStringUnbuffered(UINT64_MAX));
109   EXPECT_EQ("-9223372036854775808", printToStringUnbuffered(INT64_MIN));
110 }
111 
TEST(raw_ostreamTest,BufferEdge)112 TEST(raw_ostreamTest, BufferEdge) {
113   EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 1));
114   EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 2));
115   EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 3));
116   EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 4));
117   EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 10));
118 }
119 
TEST(raw_ostreamTest,TinyBuffer)120 TEST(raw_ostreamTest, TinyBuffer) {
121   std::string Str;
122   raw_string_ostream OS(Str);
123   OS.SetBufferSize(1);
124   OS << "hello";
125   OS << 1;
126   OS << 'w' << 'o' << 'r' << 'l' << 'd';
127   EXPECT_EQ("hello1world", OS.str());
128 }
129 
TEST(raw_ostreamTest,WriteEscaped)130 TEST(raw_ostreamTest, WriteEscaped) {
131   std::string Str;
132 
133   Str = "";
134   raw_string_ostream(Str).write_escaped("hi");
135   EXPECT_EQ("hi", Str);
136 
137   Str = "";
138   raw_string_ostream(Str).write_escaped("\\\t\n\"");
139   EXPECT_EQ("\\\\\\t\\n\\\"", Str);
140 
141   Str = "";
142   raw_string_ostream(Str).write_escaped("\1\10\200");
143   EXPECT_EQ("\\001\\010\\200", Str);
144 }
145 
TEST(raw_ostreamTest,Justify)146 TEST(raw_ostreamTest, Justify) {
147   EXPECT_EQ("xyz   ", printToString(left_justify("xyz", 6), 6));
148   EXPECT_EQ("abc",    printToString(left_justify("abc", 3), 3));
149   EXPECT_EQ("big",    printToString(left_justify("big", 1), 3));
150   EXPECT_EQ("   xyz", printToString(right_justify("xyz", 6), 6));
151   EXPECT_EQ("abc",    printToString(right_justify("abc", 3), 3));
152   EXPECT_EQ("big",    printToString(right_justify("big", 1), 3));
153 }
154 
TEST(raw_ostreamTest,FormatHex)155 TEST(raw_ostreamTest, FormatHex) {
156   EXPECT_EQ("0x1234",     printToString(format_hex(0x1234, 6), 6));
157   EXPECT_EQ("0x001234",   printToString(format_hex(0x1234, 8), 8));
158   EXPECT_EQ("0x00001234", printToString(format_hex(0x1234, 10), 10));
159   EXPECT_EQ("0x1234",     printToString(format_hex(0x1234, 4), 6));
160   EXPECT_EQ("0xff",       printToString(format_hex(255, 4), 4));
161   EXPECT_EQ("0xFF",       printToString(format_hex(255, 4, true), 4));
162   EXPECT_EQ("0x1",        printToString(format_hex(1, 3), 3));
163   EXPECT_EQ("0x12",       printToString(format_hex(0x12, 3), 4));
164   EXPECT_EQ("0x123",      printToString(format_hex(0x123, 3), 5));
165   EXPECT_EQ("FF",         printToString(format_hex_no_prefix(0xFF, 2, true), 4));
166   EXPECT_EQ("ABCD",       printToString(format_hex_no_prefix(0xABCD, 2, true), 4));
167   EXPECT_EQ("0xffffffffffffffff",
168                           printToString(format_hex(UINT64_MAX, 18), 18));
169   EXPECT_EQ("0x8000000000000000",
170                           printToString(format_hex((INT64_MIN), 18), 18));
171 }
172 
TEST(raw_ostreamTest,FormatDecimal)173 TEST(raw_ostreamTest, FormatDecimal) {
174   EXPECT_EQ("   0",        printToString(format_decimal(0, 4), 4));
175   EXPECT_EQ("  -1",        printToString(format_decimal(-1, 4), 4));
176   EXPECT_EQ("    -1",      printToString(format_decimal(-1, 6), 6));
177   EXPECT_EQ("1234567890",  printToString(format_decimal(1234567890, 10), 10));
178   EXPECT_EQ("  9223372036854775807",
179                           printToString(format_decimal(INT64_MAX, 21), 21));
180   EXPECT_EQ(" -9223372036854775808",
181                           printToString(format_decimal(INT64_MIN, 21), 21));
182 }
183 
184 
185 }
186