1 //===- llvm/unittest/Support/raw_ostream_test.cpp - raw_ostream tests -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Format.h"
13 #include "llvm/Support/raw_ostream.h"
19 template<typename T
> std::string
printToString(const T
&Value
) {
21 llvm::raw_string_ostream(res
) << Value
;
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.
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
)
39 return OS
.str().substr(StartIndex
);
42 template<typename T
> std::string
printToStringUnbuffered(const T
&Value
) {
44 llvm::raw_string_ostream
OS(res
);
50 TEST(raw_ostreamTest
, Types_Buffered
) {
52 EXPECT_EQ("c", printToString('c'));
55 EXPECT_EQ("hello", printToString("hello"));
56 EXPECT_EQ("hello", printToString(std::string("hello")));
59 EXPECT_EQ("0", printToString(0));
60 EXPECT_EQ("2425", printToString(2425));
61 EXPECT_EQ("-2425", printToString(-2425));
64 EXPECT_EQ("0", printToString(0LL));
65 EXPECT_EQ("257257257235709", printToString(257257257235709LL));
66 EXPECT_EQ("-257257257235709", printToString(-257257257235709LL));
69 EXPECT_EQ("1.100000e+00", printToString(1.1));
72 EXPECT_EQ("0x0", printToString((void*) 0));
73 EXPECT_EQ("0xbeef", printToString((void*) 0xbeef));
74 EXPECT_EQ("0xdeadbeef", printToString((void*) 0xdeadbeef));
77 EXPECT_EQ("18446744073709551615", printToString(UINT64_MAX
));
78 EXPECT_EQ("-9223372036854775808", printToString(INT64_MIN
));
81 TEST(raw_ostreamTest
, Types_Unbuffered
) {
83 EXPECT_EQ("c", printToStringUnbuffered('c'));
86 EXPECT_EQ("hello", printToStringUnbuffered("hello"));
87 EXPECT_EQ("hello", printToStringUnbuffered(std::string("hello")));
90 EXPECT_EQ("0", printToStringUnbuffered(0));
91 EXPECT_EQ("2425", printToStringUnbuffered(2425));
92 EXPECT_EQ("-2425", printToStringUnbuffered(-2425));
95 EXPECT_EQ("0", printToStringUnbuffered(0LL));
96 EXPECT_EQ("257257257235709", printToStringUnbuffered(257257257235709LL));
97 EXPECT_EQ("-257257257235709", printToStringUnbuffered(-257257257235709LL));
100 EXPECT_EQ("1.100000e+00", printToStringUnbuffered(1.1));
103 EXPECT_EQ("0x0", printToStringUnbuffered((void*) 0));
104 EXPECT_EQ("0xbeef", printToStringUnbuffered((void*) 0xbeef));
105 EXPECT_EQ("0xdeadbeef", printToStringUnbuffered((void*) 0xdeadbeef));
108 EXPECT_EQ("18446744073709551615", printToStringUnbuffered(UINT64_MAX
));
109 EXPECT_EQ("-9223372036854775808", printToStringUnbuffered(INT64_MIN
));
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));