OLD | NEW |
| (Empty) |
1 // Copyright 2007, Google Inc. | |
2 // All rights reserved. | |
3 // | |
4 // Redistribution and use in source and binary forms, with or without | |
5 // modification, are permitted provided that the following conditions are | |
6 // met: | |
7 // | |
8 // * Redistributions of source code must retain the above copyright | |
9 // notice, this list of conditions and the following disclaimer. | |
10 // * Redistributions in binary form must reproduce the above | |
11 // copyright notice, this list of conditions and the following disclaimer | |
12 // in the documentation and/or other materials provided with the | |
13 // distribution. | |
14 // * Neither the name of Google Inc. nor the names of its | |
15 // contributors may be used to endorse or promote products derived from | |
16 // this software without specific prior written permission. | |
17 // | |
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 // | |
30 // Author: wan@google.com (Zhanyong Wan) | |
31 | |
32 // Google Test - The Google C++ Testing Framework | |
33 // | |
34 // This file tests the universal value printer. | |
35 | |
36 #include "gtest/gtest-printers.h" | |
37 | |
38 #include <ctype.h> | |
39 #include <limits.h> | |
40 #include <string.h> | |
41 #include <algorithm> | |
42 #include <deque> | |
43 #include <list> | |
44 #include <map> | |
45 #include <set> | |
46 #include <sstream> | |
47 #include <string> | |
48 #include <utility> | |
49 #include <vector> | |
50 | |
51 #include "gtest/gtest.h" | |
52 | |
53 // hash_map and hash_set are available under Visual C++. | |
54 #if _MSC_VER | |
55 # define GTEST_HAS_HASH_MAP_ 1 // Indicates that hash_map is available. | |
56 # include <hash_map> // NOLINT | |
57 # define GTEST_HAS_HASH_SET_ 1 // Indicates that hash_set is available. | |
58 # include <hash_set> // NOLINT | |
59 #endif // GTEST_OS_WINDOWS | |
60 | |
61 // Some user-defined types for testing the universal value printer. | |
62 | |
63 // An anonymous enum type. | |
64 enum AnonymousEnum { | |
65 kAE1 = -1, | |
66 kAE2 = 1 | |
67 }; | |
68 | |
69 // An enum without a user-defined printer. | |
70 enum EnumWithoutPrinter { | |
71 kEWP1 = -2, | |
72 kEWP2 = 42 | |
73 }; | |
74 | |
75 // An enum with a << operator. | |
76 enum EnumWithStreaming { | |
77 kEWS1 = 10 | |
78 }; | |
79 | |
80 std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) { | |
81 return os << (e == kEWS1 ? "kEWS1" : "invalid"); | |
82 } | |
83 | |
84 // An enum with a PrintTo() function. | |
85 enum EnumWithPrintTo { | |
86 kEWPT1 = 1 | |
87 }; | |
88 | |
89 void PrintTo(EnumWithPrintTo e, std::ostream* os) { | |
90 *os << (e == kEWPT1 ? "kEWPT1" : "invalid"); | |
91 } | |
92 | |
93 // A class implicitly convertible to BiggestInt. | |
94 class BiggestIntConvertible { | |
95 public: | |
96 operator ::testing::internal::BiggestInt() const { return 42; } | |
97 }; | |
98 | |
99 // A user-defined unprintable class template in the global namespace. | |
100 template <typename T> | |
101 class UnprintableTemplateInGlobal { | |
102 public: | |
103 UnprintableTemplateInGlobal() : value_() {} | |
104 private: | |
105 T value_; | |
106 }; | |
107 | |
108 // A user-defined streamable type in the global namespace. | |
109 class StreamableInGlobal { | |
110 public: | |
111 virtual ~StreamableInGlobal() {} | |
112 }; | |
113 | |
114 inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) { | |
115 os << "StreamableInGlobal"; | |
116 } | |
117 | |
118 void operator<<(::std::ostream& os, const StreamableInGlobal* /* x */) { | |
119 os << "StreamableInGlobal*"; | |
120 } | |
121 | |
122 namespace foo { | |
123 | |
124 // A user-defined unprintable type in a user namespace. | |
125 class UnprintableInFoo { | |
126 public: | |
127 UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); } | |
128 private: | |
129 char xy_[8]; | |
130 double z_; | |
131 }; | |
132 | |
133 // A user-defined printable type in a user-chosen namespace. | |
134 struct PrintableViaPrintTo { | |
135 PrintableViaPrintTo() : value() {} | |
136 int value; | |
137 }; | |
138 | |
139 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) { | |
140 *os << "PrintableViaPrintTo: " << x.value; | |
141 } | |
142 | |
143 // A type with a user-defined << for printing its pointer. | |
144 struct PointerPrintable { | |
145 }; | |
146 | |
147 ::std::ostream& operator<<(::std::ostream& os, | |
148 const PointerPrintable* /* x */) { | |
149 return os << "PointerPrintable*"; | |
150 } | |
151 | |
152 // A user-defined printable class template in a user-chosen namespace. | |
153 template <typename T> | |
154 class PrintableViaPrintToTemplate { | |
155 public: | |
156 explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {} | |
157 | |
158 const T& value() const { return value_; } | |
159 private: | |
160 T value_; | |
161 }; | |
162 | |
163 template <typename T> | |
164 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) { | |
165 *os << "PrintableViaPrintToTemplate: " << x.value(); | |
166 } | |
167 | |
168 // A user-defined streamable class template in a user namespace. | |
169 template <typename T> | |
170 class StreamableTemplateInFoo { | |
171 public: | |
172 StreamableTemplateInFoo() : value_() {} | |
173 | |
174 const T& value() const { return value_; } | |
175 private: | |
176 T value_; | |
177 }; | |
178 | |
179 template <typename T> | |
180 inline ::std::ostream& operator<<(::std::ostream& os, | |
181 const StreamableTemplateInFoo<T>& x) { | |
182 return os << "StreamableTemplateInFoo: " << x.value(); | |
183 } | |
184 | |
185 } // namespace foo | |
186 | |
187 namespace testing { | |
188 namespace gtest_printers_test { | |
189 | |
190 using ::std::deque; | |
191 using ::std::list; | |
192 using ::std::make_pair; | |
193 using ::std::map; | |
194 using ::std::multimap; | |
195 using ::std::multiset; | |
196 using ::std::pair; | |
197 using ::std::set; | |
198 using ::std::vector; | |
199 using ::testing::PrintToString; | |
200 using ::testing::internal::NativeArray; | |
201 using ::testing::internal::RE; | |
202 using ::testing::internal::Strings; | |
203 using ::testing::internal::UniversalTersePrint; | |
204 using ::testing::internal::UniversalPrint; | |
205 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings; | |
206 using ::testing::internal::UniversalPrinter; | |
207 using ::testing::internal::kReference; | |
208 using ::testing::internal::string; | |
209 | |
210 #if GTEST_HAS_TR1_TUPLE | |
211 using ::std::tr1::make_tuple; | |
212 using ::std::tr1::tuple; | |
213 #endif | |
214 | |
215 #if _MSC_VER | |
216 // MSVC defines the following classes in the ::stdext namespace while | |
217 // gcc defines them in the :: namespace. Note that they are not part | |
218 // of the C++ standard. | |
219 using ::stdext::hash_map; | |
220 using ::stdext::hash_set; | |
221 using ::stdext::hash_multimap; | |
222 using ::stdext::hash_multiset; | |
223 #endif | |
224 | |
225 // Prints a value to a string using the universal value printer. This | |
226 // is a helper for testing UniversalPrinter<T>::Print() for various types. | |
227 template <typename T> | |
228 string Print(const T& value) { | |
229 ::std::stringstream ss; | |
230 UniversalPrinter<T>::Print(value, &ss); | |
231 return ss.str(); | |
232 } | |
233 | |
234 // Prints a value passed by reference to a string, using the universal | |
235 // value printer. This is a helper for testing | |
236 // UniversalPrinter<T&>::Print() for various types. | |
237 template <typename T> | |
238 string PrintByRef(const T& value) { | |
239 ::std::stringstream ss; | |
240 UniversalPrinter<T&>::Print(value, &ss); | |
241 return ss.str(); | |
242 } | |
243 | |
244 // Tests printing various enum types. | |
245 | |
246 TEST(PrintEnumTest, AnonymousEnum) { | |
247 EXPECT_EQ("-1", Print(kAE1)); | |
248 EXPECT_EQ("1", Print(kAE2)); | |
249 } | |
250 | |
251 TEST(PrintEnumTest, EnumWithoutPrinter) { | |
252 EXPECT_EQ("-2", Print(kEWP1)); | |
253 EXPECT_EQ("42", Print(kEWP2)); | |
254 } | |
255 | |
256 TEST(PrintEnumTest, EnumWithStreaming) { | |
257 EXPECT_EQ("kEWS1", Print(kEWS1)); | |
258 EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0))); | |
259 } | |
260 | |
261 TEST(PrintEnumTest, EnumWithPrintTo) { | |
262 EXPECT_EQ("kEWPT1", Print(kEWPT1)); | |
263 EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0))); | |
264 } | |
265 | |
266 // Tests printing a class implicitly convertible to BiggestInt. | |
267 | |
268 TEST(PrintClassTest, BiggestIntConvertible) { | |
269 EXPECT_EQ("42", Print(BiggestIntConvertible())); | |
270 } | |
271 | |
272 // Tests printing various char types. | |
273 | |
274 // char. | |
275 TEST(PrintCharTest, PlainChar) { | |
276 EXPECT_EQ("'\\0'", Print('\0')); | |
277 EXPECT_EQ("'\\'' (39, 0x27)", Print('\'')); | |
278 EXPECT_EQ("'\"' (34, 0x22)", Print('"')); | |
279 EXPECT_EQ("'?' (63, 0x3F)", Print('?')); | |
280 EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\')); | |
281 EXPECT_EQ("'\\a' (7)", Print('\a')); | |
282 EXPECT_EQ("'\\b' (8)", Print('\b')); | |
283 EXPECT_EQ("'\\f' (12, 0xC)", Print('\f')); | |
284 EXPECT_EQ("'\\n' (10, 0xA)", Print('\n')); | |
285 EXPECT_EQ("'\\r' (13, 0xD)", Print('\r')); | |
286 EXPECT_EQ("'\\t' (9)", Print('\t')); | |
287 EXPECT_EQ("'\\v' (11, 0xB)", Print('\v')); | |
288 EXPECT_EQ("'\\x7F' (127)", Print('\x7F')); | |
289 EXPECT_EQ("'\\xFF' (255)", Print('\xFF')); | |
290 EXPECT_EQ("' ' (32, 0x20)", Print(' ')); | |
291 EXPECT_EQ("'a' (97, 0x61)", Print('a')); | |
292 } | |
293 | |
294 // signed char. | |
295 TEST(PrintCharTest, SignedChar) { | |
296 EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0'))); | |
297 EXPECT_EQ("'\\xCE' (-50)", | |
298 Print(static_cast<signed char>(-50))); | |
299 } | |
300 | |
301 // unsigned char. | |
302 TEST(PrintCharTest, UnsignedChar) { | |
303 EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0'))); | |
304 EXPECT_EQ("'b' (98, 0x62)", | |
305 Print(static_cast<unsigned char>('b'))); | |
306 } | |
307 | |
308 // Tests printing other simple, built-in types. | |
309 | |
310 // bool. | |
311 TEST(PrintBuiltInTypeTest, Bool) { | |
312 EXPECT_EQ("false", Print(false)); | |
313 EXPECT_EQ("true", Print(true)); | |
314 } | |
315 | |
316 // wchar_t. | |
317 TEST(PrintBuiltInTypeTest, Wchar_t) { | |
318 EXPECT_EQ("L'\\0'", Print(L'\0')); | |
319 EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\'')); | |
320 EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"')); | |
321 EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?')); | |
322 EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\')); | |
323 EXPECT_EQ("L'\\a' (7)", Print(L'\a')); | |
324 EXPECT_EQ("L'\\b' (8)", Print(L'\b')); | |
325 EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f')); | |
326 EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n')); | |
327 EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r')); | |
328 EXPECT_EQ("L'\\t' (9)", Print(L'\t')); | |
329 EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v')); | |
330 EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F')); | |
331 EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF')); | |
332 EXPECT_EQ("L' ' (32, 0x20)", Print(L' ')); | |
333 EXPECT_EQ("L'a' (97, 0x61)", Print(L'a')); | |
334 EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576))); | |
335 EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D))); | |
336 } | |
337 | |
338 // Test that Int64 provides more storage than wchar_t. | |
339 TEST(PrintTypeSizeTest, Wchar_t) { | |
340 EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64)); | |
341 } | |
342 | |
343 // Various integer types. | |
344 TEST(PrintBuiltInTypeTest, Integer) { | |
345 EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8 | |
346 EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8 | |
347 EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16 | |
348 EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16 | |
349 EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32 | |
350 EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32 | |
351 EXPECT_EQ("18446744073709551615", | |
352 Print(static_cast<testing::internal::UInt64>(-1))); // uint64 | |
353 EXPECT_EQ("-9223372036854775808", | |
354 Print(static_cast<testing::internal::Int64>(1) << 63)); // int64 | |
355 } | |
356 | |
357 // Size types. | |
358 TEST(PrintBuiltInTypeTest, Size_t) { | |
359 EXPECT_EQ("1", Print(sizeof('a'))); // size_t. | |
360 #if !GTEST_OS_WINDOWS | |
361 // Windows has no ssize_t type. | |
362 EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t. | |
363 #endif // !GTEST_OS_WINDOWS | |
364 } | |
365 | |
366 // Floating-points. | |
367 TEST(PrintBuiltInTypeTest, FloatingPoints) { | |
368 EXPECT_EQ("1.5", Print(1.5f)); // float | |
369 EXPECT_EQ("-2.5", Print(-2.5)); // double | |
370 } | |
371 | |
372 // Since ::std::stringstream::operator<<(const void *) formats the pointer | |
373 // output differently with different compilers, we have to create the expected | |
374 // output first and use it as our expectation. | |
375 static string PrintPointer(const void *p) { | |
376 ::std::stringstream expected_result_stream; | |
377 expected_result_stream << p; | |
378 return expected_result_stream.str(); | |
379 } | |
380 | |
381 // Tests printing C strings. | |
382 | |
383 // const char*. | |
384 TEST(PrintCStringTest, Const) { | |
385 const char* p = "World"; | |
386 EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p)); | |
387 } | |
388 | |
389 // char*. | |
390 TEST(PrintCStringTest, NonConst) { | |
391 char p[] = "Hi"; | |
392 EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"", | |
393 Print(static_cast<char*>(p))); | |
394 } | |
395 | |
396 // NULL C string. | |
397 TEST(PrintCStringTest, Null) { | |
398 const char* p = NULL; | |
399 EXPECT_EQ("NULL", Print(p)); | |
400 } | |
401 | |
402 // Tests that C strings are escaped properly. | |
403 TEST(PrintCStringTest, EscapesProperly) { | |
404 const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a"; | |
405 EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f" | |
406 "\\n\\r\\t\\v\\x7F\\xFF a\"", | |
407 Print(p)); | |
408 } | |
409 | |
410 | |
411 | |
412 // MSVC compiler can be configured to define whar_t as a typedef | |
413 // of unsigned short. Defining an overload for const wchar_t* in that case | |
414 // would cause pointers to unsigned shorts be printed as wide strings, | |
415 // possibly accessing more memory than intended and causing invalid | |
416 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when | |
417 // wchar_t is implemented as a native type. | |
418 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) | |
419 | |
420 // const wchar_t*. | |
421 TEST(PrintWideCStringTest, Const) { | |
422 const wchar_t* p = L"World"; | |
423 EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p)); | |
424 } | |
425 | |
426 // wchar_t*. | |
427 TEST(PrintWideCStringTest, NonConst) { | |
428 wchar_t p[] = L"Hi"; | |
429 EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"", | |
430 Print(static_cast<wchar_t*>(p))); | |
431 } | |
432 | |
433 // NULL wide C string. | |
434 TEST(PrintWideCStringTest, Null) { | |
435 const wchar_t* p = NULL; | |
436 EXPECT_EQ("NULL", Print(p)); | |
437 } | |
438 | |
439 // Tests that wide C strings are escaped properly. | |
440 TEST(PrintWideCStringTest, EscapesProperly) { | |
441 const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r', | |
442 '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'}; | |
443 EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f" | |
444 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"", | |
445 Print(static_cast<const wchar_t*>(s))); | |
446 } | |
447 #endif // native wchar_t | |
448 | |
449 // Tests printing pointers to other char types. | |
450 | |
451 // signed char*. | |
452 TEST(PrintCharPointerTest, SignedChar) { | |
453 signed char* p = reinterpret_cast<signed char*>(0x1234); | |
454 EXPECT_EQ(PrintPointer(p), Print(p)); | |
455 p = NULL; | |
456 EXPECT_EQ("NULL", Print(p)); | |
457 } | |
458 | |
459 // const signed char*. | |
460 TEST(PrintCharPointerTest, ConstSignedChar) { | |
461 signed char* p = reinterpret_cast<signed char*>(0x1234); | |
462 EXPECT_EQ(PrintPointer(p), Print(p)); | |
463 p = NULL; | |
464 EXPECT_EQ("NULL", Print(p)); | |
465 } | |
466 | |
467 // unsigned char*. | |
468 TEST(PrintCharPointerTest, UnsignedChar) { | |
469 unsigned char* p = reinterpret_cast<unsigned char*>(0x1234); | |
470 EXPECT_EQ(PrintPointer(p), Print(p)); | |
471 p = NULL; | |
472 EXPECT_EQ("NULL", Print(p)); | |
473 } | |
474 | |
475 // const unsigned char*. | |
476 TEST(PrintCharPointerTest, ConstUnsignedChar) { | |
477 const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234); | |
478 EXPECT_EQ(PrintPointer(p), Print(p)); | |
479 p = NULL; | |
480 EXPECT_EQ("NULL", Print(p)); | |
481 } | |
482 | |
483 // Tests printing pointers to simple, built-in types. | |
484 | |
485 // bool*. | |
486 TEST(PrintPointerToBuiltInTypeTest, Bool) { | |
487 bool* p = reinterpret_cast<bool*>(0xABCD); | |
488 EXPECT_EQ(PrintPointer(p), Print(p)); | |
489 p = NULL; | |
490 EXPECT_EQ("NULL", Print(p)); | |
491 } | |
492 | |
493 // void*. | |
494 TEST(PrintPointerToBuiltInTypeTest, Void) { | |
495 void* p = reinterpret_cast<void*>(0xABCD); | |
496 EXPECT_EQ(PrintPointer(p), Print(p)); | |
497 p = NULL; | |
498 EXPECT_EQ("NULL", Print(p)); | |
499 } | |
500 | |
501 // const void*. | |
502 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) { | |
503 const void* p = reinterpret_cast<const void*>(0xABCD); | |
504 EXPECT_EQ(PrintPointer(p), Print(p)); | |
505 p = NULL; | |
506 EXPECT_EQ("NULL", Print(p)); | |
507 } | |
508 | |
509 // Tests printing pointers to pointers. | |
510 TEST(PrintPointerToPointerTest, IntPointerPointer) { | |
511 int** p = reinterpret_cast<int**>(0xABCD); | |
512 EXPECT_EQ(PrintPointer(p), Print(p)); | |
513 p = NULL; | |
514 EXPECT_EQ("NULL", Print(p)); | |
515 } | |
516 | |
517 // Tests printing (non-member) function pointers. | |
518 | |
519 void MyFunction(int /* n */) {} | |
520 | |
521 TEST(PrintPointerTest, NonMemberFunctionPointer) { | |
522 // We cannot directly cast &MyFunction to const void* because the | |
523 // standard disallows casting between pointers to functions and | |
524 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce | |
525 // this limitation. | |
526 EXPECT_EQ( | |
527 PrintPointer(reinterpret_cast<const void*>( | |
528 reinterpret_cast<internal::BiggestInt>(&MyFunction))), | |
529 Print(&MyFunction)); | |
530 int (*p)(bool) = NULL; // NOLINT | |
531 EXPECT_EQ("NULL", Print(p)); | |
532 } | |
533 | |
534 // An assertion predicate determining whether a one string is a prefix for | |
535 // another. | |
536 template <typename StringType> | |
537 AssertionResult HasPrefix(const StringType& str, const StringType& prefix) { | |
538 if (str.find(prefix, 0) == 0) | |
539 return AssertionSuccess(); | |
540 | |
541 const bool is_wide_string = sizeof(prefix[0]) > 1; | |
542 const char* const begin_string_quote = is_wide_string ? "L\"" : "\""; | |
543 return AssertionFailure() | |
544 << begin_string_quote << prefix << "\" is not a prefix of " | |
545 << begin_string_quote << str << "\"\n"; | |
546 } | |
547 | |
548 // Tests printing member variable pointers. Although they are called | |
549 // pointers, they don't point to a location in the address space. | |
550 // Their representation is implementation-defined. Thus they will be | |
551 // printed as raw bytes. | |
552 | |
553 struct Foo { | |
554 public: | |
555 virtual ~Foo() {} | |
556 int MyMethod(char x) { return x + 1; } | |
557 virtual char MyVirtualMethod(int /* n */) { return 'a'; } | |
558 | |
559 int value; | |
560 }; | |
561 | |
562 TEST(PrintPointerTest, MemberVariablePointer) { | |
563 EXPECT_TRUE(HasPrefix(Print(&Foo::value), | |
564 Print(sizeof(&Foo::value)) + "-byte object ")); | |
565 int (Foo::*p) = NULL; // NOLINT | |
566 EXPECT_TRUE(HasPrefix(Print(p), | |
567 Print(sizeof(p)) + "-byte object ")); | |
568 } | |
569 | |
570 // Tests printing member function pointers. Although they are called | |
571 // pointers, they don't point to a location in the address space. | |
572 // Their representation is implementation-defined. Thus they will be | |
573 // printed as raw bytes. | |
574 TEST(PrintPointerTest, MemberFunctionPointer) { | |
575 EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod), | |
576 Print(sizeof(&Foo::MyMethod)) + "-byte object ")); | |
577 EXPECT_TRUE( | |
578 HasPrefix(Print(&Foo::MyVirtualMethod), | |
579 Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object ")); | |
580 int (Foo::*p)(char) = NULL; // NOLINT | |
581 EXPECT_TRUE(HasPrefix(Print(p), | |
582 Print(sizeof(p)) + "-byte object ")); | |
583 } | |
584 | |
585 // Tests printing C arrays. | |
586 | |
587 // The difference between this and Print() is that it ensures that the | |
588 // argument is a reference to an array. | |
589 template <typename T, size_t N> | |
590 string PrintArrayHelper(T (&a)[N]) { | |
591 return Print(a); | |
592 } | |
593 | |
594 // One-dimensional array. | |
595 TEST(PrintArrayTest, OneDimensionalArray) { | |
596 int a[5] = { 1, 2, 3, 4, 5 }; | |
597 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a)); | |
598 } | |
599 | |
600 // Two-dimensional array. | |
601 TEST(PrintArrayTest, TwoDimensionalArray) { | |
602 int a[2][5] = { | |
603 { 1, 2, 3, 4, 5 }, | |
604 { 6, 7, 8, 9, 0 } | |
605 }; | |
606 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a)); | |
607 } | |
608 | |
609 // Array of const elements. | |
610 TEST(PrintArrayTest, ConstArray) { | |
611 const bool a[1] = { false }; | |
612 EXPECT_EQ("{ false }", PrintArrayHelper(a)); | |
613 } | |
614 | |
615 // Char array. | |
616 TEST(PrintArrayTest, CharArray) { | |
617 // Array a contains '\0' in the middle and doesn't end with '\0'. | |
618 char a[3] = { 'H', '\0', 'i' }; | |
619 EXPECT_EQ("\"H\\0i\"", PrintArrayHelper(a)); | |
620 } | |
621 | |
622 // Const char array. | |
623 TEST(PrintArrayTest, ConstCharArray) { | |
624 const char a[4] = "\0Hi"; | |
625 EXPECT_EQ("\"\\0Hi\\0\"", PrintArrayHelper(a)); | |
626 } | |
627 | |
628 // Array of objects. | |
629 TEST(PrintArrayTest, ObjectArray) { | |
630 string a[3] = { "Hi", "Hello", "Ni hao" }; | |
631 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a)); | |
632 } | |
633 | |
634 // Array with many elements. | |
635 TEST(PrintArrayTest, BigArray) { | |
636 int a[100] = { 1, 2, 3 }; | |
637 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }", | |
638 PrintArrayHelper(a)); | |
639 } | |
640 | |
641 // Tests printing ::string and ::std::string. | |
642 | |
643 #if GTEST_HAS_GLOBAL_STRING | |
644 // ::string. | |
645 TEST(PrintStringTest, StringInGlobalNamespace) { | |
646 const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; | |
647 const ::string str(s, sizeof(s)); | |
648 EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", | |
649 Print(str)); | |
650 } | |
651 #endif // GTEST_HAS_GLOBAL_STRING | |
652 | |
653 // ::std::string. | |
654 TEST(PrintStringTest, StringInStdNamespace) { | |
655 const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; | |
656 const ::std::string str(s, sizeof(s)); | |
657 EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", | |
658 Print(str)); | |
659 } | |
660 | |
661 TEST(PrintStringTest, StringAmbiguousHex) { | |
662 // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of: | |
663 // '\x6', '\x6B', or '\x6BA'. | |
664 | |
665 // a hex escaping sequence following by a decimal digit | |
666 EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3"))); | |
667 // a hex escaping sequence following by a hex digit (lower-case) | |
668 EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas"))); | |
669 // a hex escaping sequence following by a hex digit (upper-case) | |
670 EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA"))); | |
671 // a hex escaping sequence following by a non-xdigit | |
672 EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!"))); | |
673 } | |
674 | |
675 // Tests printing ::wstring and ::std::wstring. | |
676 | |
677 #if GTEST_HAS_GLOBAL_WSTRING | |
678 // ::wstring. | |
679 TEST(PrintWideStringTest, StringInGlobalNamespace) { | |
680 const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; | |
681 const ::wstring str(s, sizeof(s)/sizeof(wchar_t)); | |
682 EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" | |
683 "\\xD3\\x576\\x8D3\\xC74D a\\0\"", | |
684 Print(str)); | |
685 } | |
686 #endif // GTEST_HAS_GLOBAL_WSTRING | |
687 | |
688 #if GTEST_HAS_STD_WSTRING | |
689 // ::std::wstring. | |
690 TEST(PrintWideStringTest, StringInStdNamespace) { | |
691 const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; | |
692 const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t)); | |
693 EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" | |
694 "\\xD3\\x576\\x8D3\\xC74D a\\0\"", | |
695 Print(str)); | |
696 } | |
697 | |
698 TEST(PrintWideStringTest, StringAmbiguousHex) { | |
699 // same for wide strings. | |
700 EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3"))); | |
701 EXPECT_EQ("L\"mm\\x6\" L\"bananas\"", | |
702 Print(::std::wstring(L"mm\x6" L"bananas"))); | |
703 EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"", | |
704 Print(::std::wstring(L"NOM\x6" L"BANANA"))); | |
705 EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!"))); | |
706 } | |
707 #endif // GTEST_HAS_STD_WSTRING | |
708 | |
709 // Tests printing types that support generic streaming (i.e. streaming | |
710 // to std::basic_ostream<Char, CharTraits> for any valid Char and | |
711 // CharTraits types). | |
712 | |
713 // Tests printing a non-template type that supports generic streaming. | |
714 | |
715 class AllowsGenericStreaming {}; | |
716 | |
717 template <typename Char, typename CharTraits> | |
718 std::basic_ostream<Char, CharTraits>& operator<<( | |
719 std::basic_ostream<Char, CharTraits>& os, | |
720 const AllowsGenericStreaming& /* a */) { | |
721 return os << "AllowsGenericStreaming"; | |
722 } | |
723 | |
724 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) { | |
725 AllowsGenericStreaming a; | |
726 EXPECT_EQ("AllowsGenericStreaming", Print(a)); | |
727 } | |
728 | |
729 // Tests printing a template type that supports generic streaming. | |
730 | |
731 template <typename T> | |
732 class AllowsGenericStreamingTemplate {}; | |
733 | |
734 template <typename Char, typename CharTraits, typename T> | |
735 std::basic_ostream<Char, CharTraits>& operator<<( | |
736 std::basic_ostream<Char, CharTraits>& os, | |
737 const AllowsGenericStreamingTemplate<T>& /* a */) { | |
738 return os << "AllowsGenericStreamingTemplate"; | |
739 } | |
740 | |
741 TEST(PrintTypeWithGenericStreamingTest, TemplateType) { | |
742 AllowsGenericStreamingTemplate<int> a; | |
743 EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a)); | |
744 } | |
745 | |
746 // Tests printing a type that supports generic streaming and can be | |
747 // implicitly converted to another printable type. | |
748 | |
749 template <typename T> | |
750 class AllowsGenericStreamingAndImplicitConversionTemplate { | |
751 public: | |
752 operator bool() const { return false; } | |
753 }; | |
754 | |
755 template <typename Char, typename CharTraits, typename T> | |
756 std::basic_ostream<Char, CharTraits>& operator<<( | |
757 std::basic_ostream<Char, CharTraits>& os, | |
758 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) { | |
759 return os << "AllowsGenericStreamingAndImplicitConversionTemplate"; | |
760 } | |
761 | |
762 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) { | |
763 AllowsGenericStreamingAndImplicitConversionTemplate<int> a; | |
764 EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a)); | |
765 } | |
766 | |
767 #if GTEST_HAS_STRING_PIECE_ | |
768 | |
769 // Tests printing StringPiece. | |
770 | |
771 TEST(PrintStringPieceTest, SimpleStringPiece) { | |
772 const StringPiece sp = "Hello"; | |
773 EXPECT_EQ("\"Hello\"", Print(sp)); | |
774 } | |
775 | |
776 TEST(PrintStringPieceTest, UnprintableCharacters) { | |
777 const char str[] = "NUL (\0) and \r\t"; | |
778 const StringPiece sp(str, sizeof(str) - 1); | |
779 EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp)); | |
780 } | |
781 | |
782 #endif // GTEST_HAS_STRING_PIECE_ | |
783 | |
784 // Tests printing STL containers. | |
785 | |
786 TEST(PrintStlContainerTest, EmptyDeque) { | |
787 deque<char> empty; | |
788 EXPECT_EQ("{}", Print(empty)); | |
789 } | |
790 | |
791 TEST(PrintStlContainerTest, NonEmptyDeque) { | |
792 deque<int> non_empty; | |
793 non_empty.push_back(1); | |
794 non_empty.push_back(3); | |
795 EXPECT_EQ("{ 1, 3 }", Print(non_empty)); | |
796 } | |
797 | |
798 #if GTEST_HAS_HASH_MAP_ | |
799 | |
800 TEST(PrintStlContainerTest, OneElementHashMap) { | |
801 hash_map<int, char> map1; | |
802 map1[1] = 'a'; | |
803 EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1)); | |
804 } | |
805 | |
806 TEST(PrintStlContainerTest, HashMultiMap) { | |
807 hash_multimap<int, bool> map1; | |
808 map1.insert(make_pair(5, true)); | |
809 map1.insert(make_pair(5, false)); | |
810 | |
811 // Elements of hash_multimap can be printed in any order. | |
812 const string result = Print(map1); | |
813 EXPECT_TRUE(result == "{ (5, true), (5, false) }" || | |
814 result == "{ (5, false), (5, true) }") | |
815 << " where Print(map1) returns \"" << result << "\"."; | |
816 } | |
817 | |
818 #endif // GTEST_HAS_HASH_MAP_ | |
819 | |
820 #if GTEST_HAS_HASH_SET_ | |
821 | |
822 TEST(PrintStlContainerTest, HashSet) { | |
823 hash_set<string> set1; | |
824 set1.insert("hello"); | |
825 EXPECT_EQ("{ \"hello\" }", Print(set1)); | |
826 } | |
827 | |
828 TEST(PrintStlContainerTest, HashMultiSet) { | |
829 const int kSize = 5; | |
830 int a[kSize] = { 1, 1, 2, 5, 1 }; | |
831 hash_multiset<int> set1(a, a + kSize); | |
832 | |
833 // Elements of hash_multiset can be printed in any order. | |
834 const string result = Print(set1); | |
835 const string expected_pattern = "{ d, d, d, d, d }"; // d means a digit. | |
836 | |
837 // Verifies the result matches the expected pattern; also extracts | |
838 // the numbers in the result. | |
839 ASSERT_EQ(expected_pattern.length(), result.length()); | |
840 std::vector<int> numbers; | |
841 for (size_t i = 0; i != result.length(); i++) { | |
842 if (expected_pattern[i] == 'd') { | |
843 ASSERT_TRUE(isdigit(static_cast<unsigned char>(result[i])) != 0); | |
844 numbers.push_back(result[i] - '0'); | |
845 } else { | |
846 EXPECT_EQ(expected_pattern[i], result[i]) << " where result is " | |
847 << result; | |
848 } | |
849 } | |
850 | |
851 // Makes sure the result contains the right numbers. | |
852 std::sort(numbers.begin(), numbers.end()); | |
853 std::sort(a, a + kSize); | |
854 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin())); | |
855 } | |
856 | |
857 #endif // GTEST_HAS_HASH_SET_ | |
858 | |
859 TEST(PrintStlContainerTest, List) { | |
860 const string a[] = { | |
861 "hello", | |
862 "world" | |
863 }; | |
864 const list<string> strings(a, a + 2); | |
865 EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings)); | |
866 } | |
867 | |
868 TEST(PrintStlContainerTest, Map) { | |
869 map<int, bool> map1; | |
870 map1[1] = true; | |
871 map1[5] = false; | |
872 map1[3] = true; | |
873 EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1)); | |
874 } | |
875 | |
876 TEST(PrintStlContainerTest, MultiMap) { | |
877 multimap<bool, int> map1; | |
878 // The make_pair template function would deduce the type as | |
879 // pair<bool, int> here, and since the key part in a multimap has to | |
880 // be constant, without a templated ctor in the pair class (as in | |
881 // libCstd on Solaris), make_pair call would fail to compile as no | |
882 // implicit conversion is found. Thus explicit typename is used | |
883 // here instead. | |
884 map1.insert(pair<const bool, int>(true, 0)); | |
885 map1.insert(pair<const bool, int>(true, 1)); | |
886 map1.insert(pair<const bool, int>(false, 2)); | |
887 EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1)); | |
888 } | |
889 | |
890 TEST(PrintStlContainerTest, Set) { | |
891 const unsigned int a[] = { 3, 0, 5 }; | |
892 set<unsigned int> set1(a, a + 3); | |
893 EXPECT_EQ("{ 0, 3, 5 }", Print(set1)); | |
894 } | |
895 | |
896 TEST(PrintStlContainerTest, MultiSet) { | |
897 const int a[] = { 1, 1, 2, 5, 1 }; | |
898 multiset<int> set1(a, a + 5); | |
899 EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1)); | |
900 } | |
901 | |
902 TEST(PrintStlContainerTest, Pair) { | |
903 pair<const bool, int> p(true, 5); | |
904 EXPECT_EQ("(true, 5)", Print(p)); | |
905 } | |
906 | |
907 TEST(PrintStlContainerTest, Vector) { | |
908 vector<int> v; | |
909 v.push_back(1); | |
910 v.push_back(2); | |
911 EXPECT_EQ("{ 1, 2 }", Print(v)); | |
912 } | |
913 | |
914 TEST(PrintStlContainerTest, LongSequence) { | |
915 const int a[100] = { 1, 2, 3 }; | |
916 const vector<int> v(a, a + 100); | |
917 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " | |
918 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v)); | |
919 } | |
920 | |
921 TEST(PrintStlContainerTest, NestedContainer) { | |
922 const int a1[] = { 1, 2 }; | |
923 const int a2[] = { 3, 4, 5 }; | |
924 const list<int> l1(a1, a1 + 2); | |
925 const list<int> l2(a2, a2 + 3); | |
926 | |
927 vector<list<int> > v; | |
928 v.push_back(l1); | |
929 v.push_back(l2); | |
930 EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v)); | |
931 } | |
932 | |
933 TEST(PrintStlContainerTest, OneDimensionalNativeArray) { | |
934 const int a[3] = { 1, 2, 3 }; | |
935 NativeArray<int> b(a, 3, kReference); | |
936 EXPECT_EQ("{ 1, 2, 3 }", Print(b)); | |
937 } | |
938 | |
939 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) { | |
940 const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; | |
941 NativeArray<int[3]> b(a, 2, kReference); | |
942 EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b)); | |
943 } | |
944 | |
945 // Tests that a class named iterator isn't treated as a container. | |
946 | |
947 struct iterator { | |
948 char x; | |
949 }; | |
950 | |
951 TEST(PrintStlContainerTest, Iterator) { | |
952 iterator it = {}; | |
953 EXPECT_EQ("1-byte object <00>", Print(it)); | |
954 } | |
955 | |
956 // Tests that a class named const_iterator isn't treated as a container. | |
957 | |
958 struct const_iterator { | |
959 char x; | |
960 }; | |
961 | |
962 TEST(PrintStlContainerTest, ConstIterator) { | |
963 const_iterator it = {}; | |
964 EXPECT_EQ("1-byte object <00>", Print(it)); | |
965 } | |
966 | |
967 #if GTEST_HAS_TR1_TUPLE | |
968 // Tests printing tuples. | |
969 | |
970 // Tuples of various arities. | |
971 TEST(PrintTupleTest, VariousSizes) { | |
972 tuple<> t0; | |
973 EXPECT_EQ("()", Print(t0)); | |
974 | |
975 tuple<int> t1(5); | |
976 EXPECT_EQ("(5)", Print(t1)); | |
977 | |
978 tuple<char, bool> t2('a', true); | |
979 EXPECT_EQ("('a' (97, 0x61), true)", Print(t2)); | |
980 | |
981 tuple<bool, int, int> t3(false, 2, 3); | |
982 EXPECT_EQ("(false, 2, 3)", Print(t3)); | |
983 | |
984 tuple<bool, int, int, int> t4(false, 2, 3, 4); | |
985 EXPECT_EQ("(false, 2, 3, 4)", Print(t4)); | |
986 | |
987 tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true); | |
988 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5)); | |
989 | |
990 tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6); | |
991 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6)); | |
992 | |
993 tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7); | |
994 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7)); | |
995 | |
996 tuple<bool, int, int, int, bool, int, int, bool> t8( | |
997 false, 2, 3, 4, true, 6, 7, true); | |
998 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8)); | |
999 | |
1000 tuple<bool, int, int, int, bool, int, int, bool, int> t9( | |
1001 false, 2, 3, 4, true, 6, 7, true, 9); | |
1002 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9)); | |
1003 | |
1004 const char* const str = "8"; | |
1005 tuple<bool, char, short, testing::internal::Int32, // NOLINT | |
1006 testing::internal::Int64, float, double, const char*, void*, string> | |
1007 t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str, NULL, "10"); | |
1008 EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) + | |
1009 " pointing to \"8\", NULL, \"10\")", | |
1010 Print(t10)); | |
1011 } | |
1012 | |
1013 // Nested tuples. | |
1014 TEST(PrintTupleTest, NestedTuple) { | |
1015 tuple<tuple<int, bool>, char> nested(make_tuple(5, true), 'a'); | |
1016 EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested)); | |
1017 } | |
1018 | |
1019 #endif // GTEST_HAS_TR1_TUPLE | |
1020 | |
1021 // Tests printing user-defined unprintable types. | |
1022 | |
1023 // Unprintable types in the global namespace. | |
1024 TEST(PrintUnprintableTypeTest, InGlobalNamespace) { | |
1025 EXPECT_EQ("1-byte object <00>", | |
1026 Print(UnprintableTemplateInGlobal<char>())); | |
1027 } | |
1028 | |
1029 // Unprintable types in a user namespace. | |
1030 TEST(PrintUnprintableTypeTest, InUserNamespace) { | |
1031 EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>", | |
1032 Print(::foo::UnprintableInFoo())); | |
1033 } | |
1034 | |
1035 // Unprintable types are that too big to be printed completely. | |
1036 | |
1037 struct Big { | |
1038 Big() { memset(array, 0, sizeof(array)); } | |
1039 char array[257]; | |
1040 }; | |
1041 | |
1042 TEST(PrintUnpritableTypeTest, BigObject) { | |
1043 EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 " | |
1044 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " | |
1045 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " | |
1046 "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 " | |
1047 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " | |
1048 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " | |
1049 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>", | |
1050 Print(Big())); | |
1051 } | |
1052 | |
1053 // Tests printing user-defined streamable types. | |
1054 | |
1055 // Streamable types in the global namespace. | |
1056 TEST(PrintStreamableTypeTest, InGlobalNamespace) { | |
1057 StreamableInGlobal x; | |
1058 EXPECT_EQ("StreamableInGlobal", Print(x)); | |
1059 EXPECT_EQ("StreamableInGlobal*", Print(&x)); | |
1060 } | |
1061 | |
1062 // Printable template types in a user namespace. | |
1063 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) { | |
1064 EXPECT_EQ("StreamableTemplateInFoo: 0", | |
1065 Print(::foo::StreamableTemplateInFoo<int>())); | |
1066 } | |
1067 | |
1068 // Tests printing user-defined types that have a PrintTo() function. | |
1069 TEST(PrintPrintableTypeTest, InUserNamespace) { | |
1070 EXPECT_EQ("PrintableViaPrintTo: 0", | |
1071 Print(::foo::PrintableViaPrintTo())); | |
1072 } | |
1073 | |
1074 // Tests printing a pointer to a user-defined type that has a << | |
1075 // operator for its pointer. | |
1076 TEST(PrintPrintableTypeTest, PointerInUserNamespace) { | |
1077 ::foo::PointerPrintable x; | |
1078 EXPECT_EQ("PointerPrintable*", Print(&x)); | |
1079 } | |
1080 | |
1081 // Tests printing user-defined class template that have a PrintTo() function. | |
1082 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) { | |
1083 EXPECT_EQ("PrintableViaPrintToTemplate: 5", | |
1084 Print(::foo::PrintableViaPrintToTemplate<int>(5))); | |
1085 } | |
1086 | |
1087 #if GTEST_HAS_PROTOBUF_ | |
1088 | |
1089 // Tests printing a protocol message. | |
1090 TEST(PrintProtocolMessageTest, PrintsShortDebugString) { | |
1091 testing::internal::TestMessage msg; | |
1092 msg.set_member("yes"); | |
1093 EXPECT_EQ("<member:\"yes\">", Print(msg)); | |
1094 } | |
1095 | |
1096 // Tests printing a short proto2 message. | |
1097 TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) { | |
1098 testing::internal::FooMessage msg; | |
1099 msg.set_int_field(2); | |
1100 msg.set_string_field("hello"); | |
1101 EXPECT_PRED2(RE::FullMatch, Print(msg), | |
1102 "<int_field:\\s*2\\s+string_field:\\s*\"hello\">"); | |
1103 } | |
1104 | |
1105 // Tests printing a long proto2 message. | |
1106 TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) { | |
1107 testing::internal::FooMessage msg; | |
1108 msg.set_int_field(2); | |
1109 msg.set_string_field("hello"); | |
1110 msg.add_names("peter"); | |
1111 msg.add_names("paul"); | |
1112 msg.add_names("mary"); | |
1113 EXPECT_PRED2(RE::FullMatch, Print(msg), | |
1114 "<\n" | |
1115 "int_field:\\s*2\n" | |
1116 "string_field:\\s*\"hello\"\n" | |
1117 "names:\\s*\"peter\"\n" | |
1118 "names:\\s*\"paul\"\n" | |
1119 "names:\\s*\"mary\"\n" | |
1120 ">"); | |
1121 } | |
1122 | |
1123 #endif // GTEST_HAS_PROTOBUF_ | |
1124 | |
1125 // Tests that the universal printer prints both the address and the | |
1126 // value of a reference. | |
1127 TEST(PrintReferenceTest, PrintsAddressAndValue) { | |
1128 int n = 5; | |
1129 EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n)); | |
1130 | |
1131 int a[2][3] = { | |
1132 { 0, 1, 2 }, | |
1133 { 3, 4, 5 } | |
1134 }; | |
1135 EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }", | |
1136 PrintByRef(a)); | |
1137 | |
1138 const ::foo::UnprintableInFoo x; | |
1139 EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object " | |
1140 "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>", | |
1141 PrintByRef(x)); | |
1142 } | |
1143 | |
1144 // Tests that the universal printer prints a function pointer passed by | |
1145 // reference. | |
1146 TEST(PrintReferenceTest, HandlesFunctionPointer) { | |
1147 void (*fp)(int n) = &MyFunction; | |
1148 const string fp_pointer_string = | |
1149 PrintPointer(reinterpret_cast<const void*>(&fp)); | |
1150 // We cannot directly cast &MyFunction to const void* because the | |
1151 // standard disallows casting between pointers to functions and | |
1152 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce | |
1153 // this limitation. | |
1154 const string fp_string = PrintPointer(reinterpret_cast<const void*>( | |
1155 reinterpret_cast<internal::BiggestInt>(fp))); | |
1156 EXPECT_EQ("@" + fp_pointer_string + " " + fp_string, | |
1157 PrintByRef(fp)); | |
1158 } | |
1159 | |
1160 // Tests that the universal printer prints a member function pointer | |
1161 // passed by reference. | |
1162 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) { | |
1163 int (Foo::*p)(char ch) = &Foo::MyMethod; | |
1164 EXPECT_TRUE(HasPrefix( | |
1165 PrintByRef(p), | |
1166 "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " + | |
1167 Print(sizeof(p)) + "-byte object ")); | |
1168 | |
1169 char (Foo::*p2)(int n) = &Foo::MyVirtualMethod; | |
1170 EXPECT_TRUE(HasPrefix( | |
1171 PrintByRef(p2), | |
1172 "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " + | |
1173 Print(sizeof(p2)) + "-byte object ")); | |
1174 } | |
1175 | |
1176 // Tests that the universal printer prints a member variable pointer | |
1177 // passed by reference. | |
1178 TEST(PrintReferenceTest, HandlesMemberVariablePointer) { | |
1179 int (Foo::*p) = &Foo::value; // NOLINT | |
1180 EXPECT_TRUE(HasPrefix( | |
1181 PrintByRef(p), | |
1182 "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object ")); | |
1183 } | |
1184 | |
1185 // Useful for testing PrintToString(). We cannot use EXPECT_EQ() | |
1186 // there as its implementation uses PrintToString(). The caller must | |
1187 // ensure that 'value' has no side effect. | |
1188 #define EXPECT_PRINT_TO_STRING_(value, expected_string) \ | |
1189 EXPECT_TRUE(PrintToString(value) == (expected_string)) \ | |
1190 << " where " #value " prints as " << (PrintToString(value)) | |
1191 | |
1192 TEST(PrintToStringTest, WorksForScalar) { | |
1193 EXPECT_PRINT_TO_STRING_(123, "123"); | |
1194 } | |
1195 | |
1196 TEST(PrintToStringTest, WorksForPointerToConstChar) { | |
1197 const char* p = "hello"; | |
1198 EXPECT_PRINT_TO_STRING_(p, "\"hello\""); | |
1199 } | |
1200 | |
1201 TEST(PrintToStringTest, WorksForPointerToNonConstChar) { | |
1202 char s[] = "hello"; | |
1203 char* p = s; | |
1204 EXPECT_PRINT_TO_STRING_(p, "\"hello\""); | |
1205 } | |
1206 | |
1207 TEST(PrintToStringTest, WorksForArray) { | |
1208 int n[3] = { 1, 2, 3 }; | |
1209 EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }"); | |
1210 } | |
1211 | |
1212 #undef EXPECT_PRINT_TO_STRING_ | |
1213 | |
1214 TEST(UniversalTersePrintTest, WorksForNonReference) { | |
1215 ::std::stringstream ss; | |
1216 UniversalTersePrint(123, &ss); | |
1217 EXPECT_EQ("123", ss.str()); | |
1218 } | |
1219 | |
1220 TEST(UniversalTersePrintTest, WorksForReference) { | |
1221 const int& n = 123; | |
1222 ::std::stringstream ss; | |
1223 UniversalTersePrint(n, &ss); | |
1224 EXPECT_EQ("123", ss.str()); | |
1225 } | |
1226 | |
1227 TEST(UniversalTersePrintTest, WorksForCString) { | |
1228 const char* s1 = "abc"; | |
1229 ::std::stringstream ss1; | |
1230 UniversalTersePrint(s1, &ss1); | |
1231 EXPECT_EQ("\"abc\"", ss1.str()); | |
1232 | |
1233 char* s2 = const_cast<char*>(s1); | |
1234 ::std::stringstream ss2; | |
1235 UniversalTersePrint(s2, &ss2); | |
1236 EXPECT_EQ("\"abc\"", ss2.str()); | |
1237 | |
1238 const char* s3 = NULL; | |
1239 ::std::stringstream ss3; | |
1240 UniversalTersePrint(s3, &ss3); | |
1241 EXPECT_EQ("NULL", ss3.str()); | |
1242 } | |
1243 | |
1244 TEST(UniversalPrintTest, WorksForNonReference) { | |
1245 ::std::stringstream ss; | |
1246 UniversalPrint(123, &ss); | |
1247 EXPECT_EQ("123", ss.str()); | |
1248 } | |
1249 | |
1250 TEST(UniversalPrintTest, WorksForReference) { | |
1251 const int& n = 123; | |
1252 ::std::stringstream ss; | |
1253 UniversalPrint(n, &ss); | |
1254 EXPECT_EQ("123", ss.str()); | |
1255 } | |
1256 | |
1257 TEST(UniversalPrintTest, WorksForCString) { | |
1258 const char* s1 = "abc"; | |
1259 ::std::stringstream ss1; | |
1260 UniversalPrint(s1, &ss1); | |
1261 EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str())); | |
1262 | |
1263 char* s2 = const_cast<char*>(s1); | |
1264 ::std::stringstream ss2; | |
1265 UniversalPrint(s2, &ss2); | |
1266 EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str())); | |
1267 | |
1268 const char* s3 = NULL; | |
1269 ::std::stringstream ss3; | |
1270 UniversalPrint(s3, &ss3); | |
1271 EXPECT_EQ("NULL", ss3.str()); | |
1272 } | |
1273 | |
1274 | |
1275 #if GTEST_HAS_TR1_TUPLE | |
1276 | |
1277 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) { | |
1278 Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple()); | |
1279 EXPECT_EQ(0u, result.size()); | |
1280 } | |
1281 | |
1282 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) { | |
1283 Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1)); | |
1284 ASSERT_EQ(1u, result.size()); | |
1285 EXPECT_EQ("1", result[0]); | |
1286 } | |
1287 | |
1288 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) { | |
1289 Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a')); | |
1290 ASSERT_EQ(2u, result.size()); | |
1291 EXPECT_EQ("1", result[0]); | |
1292 EXPECT_EQ("'a' (97, 0x61)", result[1]); | |
1293 } | |
1294 | |
1295 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) { | |
1296 const int n = 1; | |
1297 Strings result = UniversalTersePrintTupleFieldsToStrings( | |
1298 tuple<const int&, const char*>(n, "a")); | |
1299 ASSERT_EQ(2u, result.size()); | |
1300 EXPECT_EQ("1", result[0]); | |
1301 EXPECT_EQ("\"a\"", result[1]); | |
1302 } | |
1303 | |
1304 #endif // GTEST_HAS_TR1_TUPLE | |
1305 | |
1306 } // namespace gtest_printers_test | |
1307 } // namespace testing | |
OLD | NEW |