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