166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===- llvm/unittest/Support/raw_ostream_test.cpp - raw_ostream tests -----===//
266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//                     The LLVM Compiler Infrastructure
466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// This file is distributed under the University of Illinois Open Source
666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// License. See LICENSE.TXT for details.
766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===----------------------------------------------------------------------===//
966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "gtest/gtest.h"
1166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/ADT/SmallString.h"
1266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/Format.h"
1366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/raw_ostream.h"
1466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanusing namespace llvm;
1666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumannamespace {
1866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumantemplate<typename T> std::string printToString(const T &Value) {
2066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  std::string res;
2166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  llvm::raw_string_ostream(res) << Value;
2266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  return res;
2366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
2466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman/// printToString - Print the given value to a stream which only has \arg
2666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman/// BytesLeftInBuffer bytes left in the buffer. This is useful for testing edge
2766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman/// cases in the buffer handling logic.
2866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumantemplate<typename T> std::string printToString(const T &Value,
2966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman                                               unsigned BytesLeftInBuffer) {
3066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // FIXME: This is relying on internal knowledge of how raw_ostream works to
3166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // get the buffer position right.
3266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  SmallString<256> SVec;
3366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  assert(BytesLeftInBuffer < 256 && "Invalid buffer count!");
3466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  llvm::raw_svector_ostream OS(SVec);
3566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  unsigned StartIndex = 256 - BytesLeftInBuffer;
3666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  for (unsigned i = 0; i != StartIndex; ++i)
3766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman    OS << '?';
3866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  OS << Value;
3966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  return OS.str().substr(StartIndex);
4066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
4166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
4266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumantemplate<typename T> std::string printToStringUnbuffered(const T &Value) {
4366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  std::string res;
4466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  llvm::raw_string_ostream OS(res);
4566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  OS.SetUnbuffered();
4666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  OS << Value;
4766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  return res;
4866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
4966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5066b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(raw_ostreamTest, Types_Buffered) {
5166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Char
5266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("c", printToString('c'));
5366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // String
5566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("hello", printToString("hello"));
5666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("hello", printToString(std::string("hello")));
5766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Int
5966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("0", printToString(0));
6066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("2425", printToString(2425));
6166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("-2425", printToString(-2425));
6266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
6366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Long long
6466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("0", printToString(0LL));
6566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("257257257235709", printToString(257257257235709LL));
6666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("-257257257235709", printToString(-257257257235709LL));
6766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
6866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Double
6966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("1.100000e+00", printToString(1.1));
7066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
7166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // void*
7266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("0x0", printToString((void*) 0));
7366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("0xbeef", printToString((void*) 0xbeef));
7466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("0xdeadbeef", printToString((void*) 0xdeadbeef));
7566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
7666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Min and max.
7766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("18446744073709551615", printToString(UINT64_MAX));
7866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("-9223372036854775808", printToString(INT64_MIN));
7966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
8066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
8166b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(raw_ostreamTest, Types_Unbuffered) {
8266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Char
8366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("c", printToStringUnbuffered('c'));
8466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
8566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // String
8666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("hello", printToStringUnbuffered("hello"));
8766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("hello", printToStringUnbuffered(std::string("hello")));
8866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
8966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Int
9066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("0", printToStringUnbuffered(0));
9166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("2425", printToStringUnbuffered(2425));
9266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("-2425", printToStringUnbuffered(-2425));
9366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
9466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Long long
9566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("0", printToStringUnbuffered(0LL));
9666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("257257257235709", printToStringUnbuffered(257257257235709LL));
9766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("-257257257235709", printToStringUnbuffered(-257257257235709LL));
9866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
9966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Double
10066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("1.100000e+00", printToStringUnbuffered(1.1));
10166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
10266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // void*
10366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("0x0", printToStringUnbuffered((void*) 0));
10466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("0xbeef", printToStringUnbuffered((void*) 0xbeef));
10566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("0xdeadbeef", printToStringUnbuffered((void*) 0xdeadbeef));
10666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
10766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Min and max.
10866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("18446744073709551615", printToStringUnbuffered(UINT64_MAX));
10966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("-9223372036854775808", printToStringUnbuffered(INT64_MIN));
11066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
11166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
11266b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(raw_ostreamTest, BufferEdge) {
11366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 1));
11466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 2));
11566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 3));
11666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 4));
11766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 10));
11866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
11966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
12066b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(raw_ostreamTest, TinyBuffer) {
12166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  std::string Str;
12266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  raw_string_ostream OS(Str);
12366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  OS.SetBufferSize(1);
12466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  OS << "hello";
12566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  OS << 1;
12666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  OS << 'w' << 'o' << 'r' << 'l' << 'd';
12766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("hello1world", OS.str());
12866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
12966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
13066b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(raw_ostreamTest, WriteEscaped) {
13166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  std::string Str;
13266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
13366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  Str = "";
13466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  raw_string_ostream(Str).write_escaped("hi");
13566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("hi", Str);
13666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
13766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  Str = "";
13866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  raw_string_ostream(Str).write_escaped("\\\t\n\"");
13966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("\\\\\\t\\n\\\"", Str);
14066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
14166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  Str = "";
14266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  raw_string_ostream(Str).write_escaped("\1\10\200");
14366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ("\\001\\010\\200", Str);
14466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
14566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
14666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
147