Misha Brukman | 1757a81 | 2009-04-01 21:36:40 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/raw_ostream_test.cpp - raw_ostream tests -----===// |
Daniel Dunbar | af41991 | 2009-03-17 16:14:59 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/SmallString.h" |
Rafael Espindola | d31f04b | 2017-03-13 20:00:25 +0000 | [diff] [blame] | 11 | #include "llvm/Support/FileSystem.h" |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Format.h" |
Daniel Dunbar | af41991 | 2009-03-17 16:14:59 +0000 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 14 | #include "gtest/gtest.h" |
Daniel Dunbar | af41991 | 2009-03-17 16:14:59 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | template<typename T> std::string printToString(const T &Value) { |
| 21 | std::string res; |
| 22 | llvm::raw_string_ostream(res) << Value; |
| 23 | return res; |
| 24 | } |
| 25 | |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 26 | /// printToString - Print the given value to a stream which only has \arg |
| 27 | /// BytesLeftInBuffer bytes left in the buffer. This is useful for testing edge |
| 28 | /// cases in the buffer handling logic. |
| 29 | template<typename T> std::string printToString(const T &Value, |
| 30 | unsigned BytesLeftInBuffer) { |
| 31 | // FIXME: This is relying on internal knowledge of how raw_ostream works to |
| 32 | // get the buffer position right. |
| 33 | SmallString<256> SVec; |
| 34 | assert(BytesLeftInBuffer < 256 && "Invalid buffer count!"); |
| 35 | llvm::raw_svector_ostream OS(SVec); |
| 36 | unsigned StartIndex = 256 - BytesLeftInBuffer; |
| 37 | for (unsigned i = 0; i != StartIndex; ++i) |
| 38 | OS << '?'; |
| 39 | OS << Value; |
| 40 | return OS.str().substr(StartIndex); |
| 41 | } |
| 42 | |
Daniel Dunbar | af41991 | 2009-03-17 16:14:59 +0000 | [diff] [blame] | 43 | template<typename T> std::string printToStringUnbuffered(const T &Value) { |
| 44 | std::string res; |
| 45 | llvm::raw_string_ostream OS(res); |
| 46 | OS.SetUnbuffered(); |
| 47 | OS << Value; |
| 48 | return res; |
| 49 | } |
| 50 | |
| 51 | TEST(raw_ostreamTest, Types_Buffered) { |
| 52 | // Char |
| 53 | EXPECT_EQ("c", printToString('c')); |
| 54 | |
| 55 | // String |
| 56 | EXPECT_EQ("hello", printToString("hello")); |
| 57 | EXPECT_EQ("hello", printToString(std::string("hello"))); |
| 58 | |
| 59 | // Int |
| 60 | EXPECT_EQ("0", printToString(0)); |
| 61 | EXPECT_EQ("2425", printToString(2425)); |
| 62 | EXPECT_EQ("-2425", printToString(-2425)); |
| 63 | |
| 64 | // Long long |
| 65 | EXPECT_EQ("0", printToString(0LL)); |
| 66 | EXPECT_EQ("257257257235709", printToString(257257257235709LL)); |
| 67 | EXPECT_EQ("-257257257235709", printToString(-257257257235709LL)); |
| 68 | |
| 69 | // Double |
| 70 | EXPECT_EQ("1.100000e+00", printToString(1.1)); |
| 71 | |
| 72 | // void* |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 73 | EXPECT_EQ("0x0", printToString((void*) nullptr)); |
Reid Kleckner | 19d532b | 2016-02-10 19:11:15 +0000 | [diff] [blame] | 74 | EXPECT_EQ("0xbeef", printToString((void*) 0xbeefLL)); |
| 75 | EXPECT_EQ("0xdeadbeef", printToString((void*) 0xdeadbeefLL)); |
Daniel Dunbar | faea971 | 2009-08-19 19:58:19 +0000 | [diff] [blame] | 76 | |
| 77 | // Min and max. |
| 78 | EXPECT_EQ("18446744073709551615", printToString(UINT64_MAX)); |
| 79 | EXPECT_EQ("-9223372036854775808", printToString(INT64_MIN)); |
Daniel Dunbar | af41991 | 2009-03-17 16:14:59 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST(raw_ostreamTest, Types_Unbuffered) { |
| 83 | // Char |
| 84 | EXPECT_EQ("c", printToStringUnbuffered('c')); |
| 85 | |
| 86 | // String |
| 87 | EXPECT_EQ("hello", printToStringUnbuffered("hello")); |
| 88 | EXPECT_EQ("hello", printToStringUnbuffered(std::string("hello"))); |
| 89 | |
| 90 | // Int |
| 91 | EXPECT_EQ("0", printToStringUnbuffered(0)); |
| 92 | EXPECT_EQ("2425", printToStringUnbuffered(2425)); |
| 93 | EXPECT_EQ("-2425", printToStringUnbuffered(-2425)); |
| 94 | |
| 95 | // Long long |
| 96 | EXPECT_EQ("0", printToStringUnbuffered(0LL)); |
| 97 | EXPECT_EQ("257257257235709", printToStringUnbuffered(257257257235709LL)); |
| 98 | EXPECT_EQ("-257257257235709", printToStringUnbuffered(-257257257235709LL)); |
| 99 | |
| 100 | // Double |
| 101 | EXPECT_EQ("1.100000e+00", printToStringUnbuffered(1.1)); |
| 102 | |
| 103 | // void* |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 104 | EXPECT_EQ("0x0", printToStringUnbuffered((void*) nullptr)); |
Reid Kleckner | 19d532b | 2016-02-10 19:11:15 +0000 | [diff] [blame] | 105 | EXPECT_EQ("0xbeef", printToStringUnbuffered((void*) 0xbeefLL)); |
| 106 | EXPECT_EQ("0xdeadbeef", printToStringUnbuffered((void*) 0xdeadbeefLL)); |
Daniel Dunbar | faea971 | 2009-08-19 19:58:19 +0000 | [diff] [blame] | 107 | |
| 108 | // Min and max. |
| 109 | EXPECT_EQ("18446744073709551615", printToStringUnbuffered(UINT64_MAX)); |
| 110 | EXPECT_EQ("-9223372036854775808", printToStringUnbuffered(INT64_MIN)); |
Daniel Dunbar | af41991 | 2009-03-17 16:14:59 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 113 | TEST(raw_ostreamTest, BufferEdge) { |
| 114 | EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 1)); |
| 115 | EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 2)); |
| 116 | EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 3)); |
| 117 | EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 4)); |
| 118 | EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 10)); |
| 119 | } |
| 120 | |
Daniel Dunbar | 316b4a0 | 2009-09-15 20:31:46 +0000 | [diff] [blame] | 121 | TEST(raw_ostreamTest, TinyBuffer) { |
| 122 | std::string Str; |
| 123 | raw_string_ostream OS(Str); |
| 124 | OS.SetBufferSize(1); |
| 125 | OS << "hello"; |
| 126 | OS << 1; |
| 127 | OS << 'w' << 'o' << 'r' << 'l' << 'd'; |
| 128 | EXPECT_EQ("hello1world", OS.str()); |
| 129 | } |
| 130 | |
Daniel Dunbar | 4108c43 | 2009-10-17 20:43:08 +0000 | [diff] [blame] | 131 | TEST(raw_ostreamTest, WriteEscaped) { |
| 132 | std::string Str; |
| 133 | |
| 134 | Str = ""; |
| 135 | raw_string_ostream(Str).write_escaped("hi"); |
| 136 | EXPECT_EQ("hi", Str); |
| 137 | |
| 138 | Str = ""; |
| 139 | raw_string_ostream(Str).write_escaped("\\\t\n\""); |
| 140 | EXPECT_EQ("\\\\\\t\\n\\\"", Str); |
| 141 | |
| 142 | Str = ""; |
| 143 | raw_string_ostream(Str).write_escaped("\1\10\200"); |
| 144 | EXPECT_EQ("\\001\\010\\200", Str); |
| 145 | } |
| 146 | |
Nick Kledzik | e648037 | 2014-09-25 20:30:58 +0000 | [diff] [blame] | 147 | TEST(raw_ostreamTest, Justify) { |
| 148 | EXPECT_EQ("xyz ", printToString(left_justify("xyz", 6), 6)); |
| 149 | EXPECT_EQ("abc", printToString(left_justify("abc", 3), 3)); |
| 150 | EXPECT_EQ("big", printToString(left_justify("big", 1), 3)); |
| 151 | EXPECT_EQ(" xyz", printToString(right_justify("xyz", 6), 6)); |
| 152 | EXPECT_EQ("abc", printToString(right_justify("abc", 3), 3)); |
| 153 | EXPECT_EQ("big", printToString(right_justify("big", 1), 3)); |
Frederich Munch | 5e9d6d0 | 2017-07-13 16:11:08 +0000 | [diff] [blame] | 154 | EXPECT_EQ(" on ", printToString(center_justify("on", 9), 9)); |
| 155 | EXPECT_EQ(" off ", printToString(center_justify("off", 10), 10)); |
| 156 | EXPECT_EQ("single ", printToString(center_justify("single", 7), 7)); |
| 157 | EXPECT_EQ("none", printToString(center_justify("none", 1), 4)); |
| 158 | EXPECT_EQ("none", printToString(center_justify("none", 1), 1)); |
Nick Kledzik | e648037 | 2014-09-25 20:30:58 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | TEST(raw_ostreamTest, FormatHex) { |
| 162 | EXPECT_EQ("0x1234", printToString(format_hex(0x1234, 6), 6)); |
| 163 | EXPECT_EQ("0x001234", printToString(format_hex(0x1234, 8), 8)); |
| 164 | EXPECT_EQ("0x00001234", printToString(format_hex(0x1234, 10), 10)); |
| 165 | EXPECT_EQ("0x1234", printToString(format_hex(0x1234, 4), 6)); |
| 166 | EXPECT_EQ("0xff", printToString(format_hex(255, 4), 4)); |
| 167 | EXPECT_EQ("0xFF", printToString(format_hex(255, 4, true), 4)); |
| 168 | EXPECT_EQ("0x1", printToString(format_hex(1, 3), 3)); |
| 169 | EXPECT_EQ("0x12", printToString(format_hex(0x12, 3), 4)); |
| 170 | EXPECT_EQ("0x123", printToString(format_hex(0x123, 3), 5)); |
Zachary Turner | 39571b3 | 2015-01-26 18:21:33 +0000 | [diff] [blame] | 171 | EXPECT_EQ("FF", printToString(format_hex_no_prefix(0xFF, 2, true), 4)); |
| 172 | EXPECT_EQ("ABCD", printToString(format_hex_no_prefix(0xABCD, 2, true), 4)); |
Nick Kledzik | e648037 | 2014-09-25 20:30:58 +0000 | [diff] [blame] | 173 | EXPECT_EQ("0xffffffffffffffff", |
| 174 | printToString(format_hex(UINT64_MAX, 18), 18)); |
| 175 | EXPECT_EQ("0x8000000000000000", |
| 176 | printToString(format_hex((INT64_MIN), 18), 18)); |
| 177 | } |
| 178 | |
| 179 | TEST(raw_ostreamTest, FormatDecimal) { |
| 180 | EXPECT_EQ(" 0", printToString(format_decimal(0, 4), 4)); |
| 181 | EXPECT_EQ(" -1", printToString(format_decimal(-1, 4), 4)); |
| 182 | EXPECT_EQ(" -1", printToString(format_decimal(-1, 6), 6)); |
| 183 | EXPECT_EQ("1234567890", printToString(format_decimal(1234567890, 10), 10)); |
| 184 | EXPECT_EQ(" 9223372036854775807", |
| 185 | printToString(format_decimal(INT64_MAX, 21), 21)); |
| 186 | EXPECT_EQ(" -9223372036854775808", |
| 187 | printToString(format_decimal(INT64_MIN, 21), 21)); |
| 188 | } |
| 189 | |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 190 | static std::string formatted_bytes_str(ArrayRef<uint8_t> Bytes, |
| 191 | llvm::Optional<uint64_t> Offset = None, |
| 192 | uint32_t NumPerLine = 16, |
| 193 | uint8_t ByteGroupSize = 4) { |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 194 | std::string S; |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 195 | raw_string_ostream Str(S); |
| 196 | Str << format_bytes(Bytes, Offset, NumPerLine, ByteGroupSize); |
| 197 | Str.flush(); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 198 | return S; |
| 199 | } |
Nick Kledzik | e648037 | 2014-09-25 20:30:58 +0000 | [diff] [blame] | 200 | |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 201 | static std::string format_bytes_with_ascii_str(ArrayRef<uint8_t> Bytes, |
| 202 | Optional<uint64_t> Offset = None, |
| 203 | uint32_t NumPerLine = 16, |
| 204 | uint8_t ByteGroupSize = 4) { |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 205 | std::string S; |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 206 | raw_string_ostream Str(S); |
| 207 | Str << format_bytes_with_ascii(Bytes, Offset, NumPerLine, ByteGroupSize); |
| 208 | Str.flush(); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 209 | return S; |
| 210 | } |
| 211 | |
| 212 | TEST(raw_ostreamTest, FormattedHexBytes) { |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 213 | std::vector<uint8_t> Buf = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', |
| 214 | 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', |
| 215 | 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', |
| 216 | '1', '2', '3', '4', '5', '6', '7', '8', '9'}; |
| 217 | ArrayRef<uint8_t> B(Buf); |
| 218 | |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 219 | // Test invalid input. |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 220 | EXPECT_EQ("", formatted_bytes_str(ArrayRef<uint8_t>())); |
| 221 | EXPECT_EQ("", format_bytes_with_ascii_str(ArrayRef<uint8_t>())); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 222 | //---------------------------------------------------------------------- |
| 223 | // Test hex byte output with the default 4 byte groups |
| 224 | //---------------------------------------------------------------------- |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 225 | EXPECT_EQ("61", formatted_bytes_str(B.take_front())); |
| 226 | EXPECT_EQ("61626364 65", formatted_bytes_str(B.take_front(5))); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 227 | // Test that 16 bytes get written to a line correctly. |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 228 | EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70", |
| 229 | formatted_bytes_str(B.take_front(16))); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 230 | // Test raw bytes with default 16 bytes per line wrapping. |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 231 | EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70\n71", |
| 232 | formatted_bytes_str(B.take_front(17))); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 233 | // Test raw bytes with 1 bytes per line wrapping. |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 234 | EXPECT_EQ("61\n62\n63\n64\n65\n66", |
| 235 | formatted_bytes_str(B.take_front(6), None, 1)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 236 | // Test raw bytes with 7 bytes per line wrapping. |
| 237 | EXPECT_EQ("61626364 656667\n68696a6b 6c6d6e\n6f7071", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 238 | formatted_bytes_str(B.take_front(17), None, 7)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 239 | // Test raw bytes with 8 bytes per line wrapping. |
| 240 | EXPECT_EQ("61626364 65666768\n696a6b6c 6d6e6f70\n71", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 241 | formatted_bytes_str(B.take_front(17), None, 8)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 242 | //---------------------------------------------------------------------- |
| 243 | // Test hex byte output with the 1 byte groups |
| 244 | //---------------------------------------------------------------------- |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 245 | EXPECT_EQ("61 62 63 64 65", |
| 246 | formatted_bytes_str(B.take_front(5), None, 16, 1)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 247 | // Test that 16 bytes get written to a line correctly. |
| 248 | EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 249 | formatted_bytes_str(B.take_front(16), None, 16, 1)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 250 | // Test raw bytes with default 16 bytes per line wrapping. |
| 251 | EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70\n71", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 252 | formatted_bytes_str(B.take_front(17), None, 16, 1)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 253 | // Test raw bytes with 7 bytes per line wrapping. |
| 254 | EXPECT_EQ("61 62 63 64 65 66 67\n68 69 6a 6b 6c 6d 6e\n6f 70 71", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 255 | formatted_bytes_str(B.take_front(17), None, 7, 1)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 256 | // Test raw bytes with 8 bytes per line wrapping. |
| 257 | EXPECT_EQ("61 62 63 64 65 66 67 68\n69 6a 6b 6c 6d 6e 6f 70\n71", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 258 | formatted_bytes_str(B.take_front(17), None, 8, 1)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 259 | |
| 260 | //---------------------------------------------------------------------- |
| 261 | // Test hex byte output with the 2 byte groups |
| 262 | //---------------------------------------------------------------------- |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 263 | EXPECT_EQ("6162 6364 65", formatted_bytes_str(B.take_front(5), None, 16, 2)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 264 | // Test that 16 bytes get written to a line correctly. |
| 265 | EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 266 | formatted_bytes_str(B.take_front(16), None, 16, 2)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 267 | // Test raw bytes with default 16 bytes per line wrapping. |
| 268 | EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70\n71", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 269 | formatted_bytes_str(B.take_front(17), None, 16, 2)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 270 | // Test raw bytes with 7 bytes per line wrapping. |
| 271 | EXPECT_EQ("6162 6364 6566 67\n6869 6a6b 6c6d 6e\n6f70 71", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 272 | formatted_bytes_str(B.take_front(17), None, 7, 2)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 273 | // Test raw bytes with 8 bytes per line wrapping. |
| 274 | EXPECT_EQ("6162 6364 6566 6768\n696a 6b6c 6d6e 6f70\n71", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 275 | formatted_bytes_str(B.take_front(17), None, 8, 2)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 276 | |
| 277 | //---------------------------------------------------------------------- |
| 278 | // Test hex bytes with offset with the default 4 byte groups. |
| 279 | //---------------------------------------------------------------------- |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 280 | EXPECT_EQ("0000: 61", formatted_bytes_str(B.take_front(), 0x0)); |
| 281 | EXPECT_EQ("1000: 61", formatted_bytes_str(B.take_front(), 0x1000)); |
| 282 | EXPECT_EQ("1000: 61\n1001: 62", |
| 283 | formatted_bytes_str(B.take_front(2), 0x1000, 1)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 284 | //---------------------------------------------------------------------- |
| 285 | // Test hex bytes with ASCII with the default 4 byte groups. |
| 286 | //---------------------------------------------------------------------- |
| 287 | EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70 |abcdefghijklmnop|", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 288 | format_bytes_with_ascii_str(B.take_front(16))); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 289 | EXPECT_EQ("61626364 65666768 |abcdefgh|\n" |
| 290 | "696a6b6c 6d6e6f70 |ijklmnop|", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 291 | format_bytes_with_ascii_str(B.take_front(16), None, 8)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 292 | EXPECT_EQ("61626364 65666768 |abcdefgh|\n696a6b6c |ijkl|", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 293 | format_bytes_with_ascii_str(B.take_front(12), None, 8)); |
| 294 | std::vector<uint8_t> Unprintable = {'a', '\x1e', 'b', '\x1f'}; |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 295 | // Make sure the ASCII is still lined up correctly when fewer bytes than 16 |
| 296 | // bytes per line are available. The ASCII should still be aligned as if 16 |
| 297 | // bytes of hex might be displayed. |
| 298 | EXPECT_EQ("611e621f |a.b.|", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 299 | format_bytes_with_ascii_str(Unprintable)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 300 | //---------------------------------------------------------------------- |
| 301 | // Test hex bytes with ASCII with offsets with the default 4 byte groups. |
| 302 | //---------------------------------------------------------------------- |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 303 | EXPECT_EQ("0000: 61626364 65666768 " |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 304 | "696a6b6c 6d6e6f70 |abcdefghijklmnop|", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 305 | format_bytes_with_ascii_str(B.take_front(16), 0)); |
| 306 | EXPECT_EQ("0000: 61626364 65666768 |abcdefgh|\n" |
| 307 | "0008: 696a6b6c 6d6e6f70 |ijklmnop|", |
| 308 | format_bytes_with_ascii_str(B.take_front(16), 0, 8)); |
| 309 | EXPECT_EQ("0000: 61626364 656667 |abcdefg|\n" |
| 310 | "0007: 68696a6b 6c |hijkl|", |
| 311 | format_bytes_with_ascii_str(B.take_front(12), 0, 7)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 312 | |
| 313 | //---------------------------------------------------------------------- |
| 314 | // Test hex bytes with ASCII with offsets with the default 2 byte groups. |
| 315 | //---------------------------------------------------------------------- |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 316 | EXPECT_EQ("0000: 6162 6364 6566 6768 " |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 317 | "696a 6b6c 6d6e 6f70 |abcdefghijklmnop|", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 318 | format_bytes_with_ascii_str(B.take_front(16), 0, 16, 2)); |
| 319 | EXPECT_EQ("0000: 6162 6364 6566 6768 |abcdefgh|\n" |
| 320 | "0008: 696a 6b6c 6d6e 6f70 |ijklmnop|", |
| 321 | format_bytes_with_ascii_str(B.take_front(16), 0, 8, 2)); |
| 322 | EXPECT_EQ("0000: 6162 6364 6566 67 |abcdefg|\n" |
| 323 | "0007: 6869 6a6b 6c |hijkl|", |
| 324 | format_bytes_with_ascii_str(B.take_front(12), 0, 7, 2)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 325 | |
| 326 | //---------------------------------------------------------------------- |
| 327 | // Test hex bytes with ASCII with offsets with the default 1 byte groups. |
| 328 | //---------------------------------------------------------------------- |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 329 | EXPECT_EQ("0000: 61 62 63 64 65 66 67 68 " |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 330 | "69 6a 6b 6c 6d 6e 6f 70 |abcdefghijklmnop|", |
Zachary Turner | 4a86af0 | 2016-11-10 20:16:45 +0000 | [diff] [blame] | 331 | format_bytes_with_ascii_str(B.take_front(16), 0, 16, 1)); |
| 332 | EXPECT_EQ("0000: 61 62 63 64 65 66 67 68 |abcdefgh|\n" |
| 333 | "0008: 69 6a 6b 6c 6d 6e 6f 70 |ijklmnop|", |
| 334 | format_bytes_with_ascii_str(B.take_front(16), 0, 8, 1)); |
| 335 | EXPECT_EQ("0000: 61 62 63 64 65 66 67 |abcdefg|\n" |
| 336 | "0007: 68 69 6a 6b 6c |hijkl|", |
| 337 | format_bytes_with_ascii_str(B.take_front(12), 0, 7, 1)); |
Greg Clayton | bde0a16 | 2016-11-09 00:15:54 +0000 | [diff] [blame] | 338 | } |
Rafael Espindola | d31f04b | 2017-03-13 20:00:25 +0000 | [diff] [blame] | 339 | |
| 340 | TEST(raw_fd_ostreamTest, multiple_raw_fd_ostream_to_stdout) { |
| 341 | std::error_code EC; |
| 342 | |
| 343 | { raw_fd_ostream("-", EC, sys::fs::OpenFlags::F_None); } |
| 344 | { raw_fd_ostream("-", EC, sys::fs::OpenFlags::F_None); } |
| 345 | } |
Daniel Dunbar | af41991 | 2009-03-17 16:14:59 +0000 | [diff] [blame] | 346 | } |