blob: 9677491ac9864dfce8cc4b7753f5fad0314d1188 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// 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 Mock - a framework for writing C++ mock classes.
33//
34// This file tests the universal value printer.
35
36#include <gmock/gmock-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#include <gmock/gmock-matchers.h>
51#include <gmock/internal/gmock-port.h>
52#include <gtest/gtest.h>
53
54// hash_map and hash_set are available on Windows.
55#ifdef GTEST_OS_WINDOWS
56#define GMOCK_HAS_HASH_MAP_ // Indicates that hash_map is available.
57#include <hash_map> // NOLINT
58#define GMOCK_HAS_HASH_SET_ // Indicates that hash_set is available.
59#include <hash_set> // NOLINT
60#endif // GTEST_OS_WINDOWS
61
62// Some user-defined types for testing the universal value printer.
63
64// A user-defined unprintable class template in the global namespace.
65template <typename T>
66class UnprintableTemplateInGlobal {
67 public:
68 UnprintableTemplateInGlobal() : value_() {}
69 private:
70 T value_;
71};
72
73// A user-defined streamable type in the global namespace.
74class StreamableInGlobal {
75 public:
76 virtual ~StreamableInGlobal() {}
77};
78
79inline void operator<<(::std::ostream& os, const StreamableInGlobal& x) {
80 os << "StreamableInGlobal";
81}
82
83namespace foo {
84
85// A user-defined unprintable type in a user namespace.
86class UnprintableInFoo {
87 public:
88 UnprintableInFoo() : x_(0x12EF), y_(0xAB34), z_(0) {}
89 private:
90 testing::internal::Int32 x_;
91 testing::internal::Int32 y_;
92 double z_;
93};
94
95// A user-defined printable type in a user-chosen namespace.
96struct PrintableViaPrintTo {
97 PrintableViaPrintTo() : value() {}
98 int value;
99};
100
101void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
102 *os << "PrintableViaPrintTo: " << x.value;
103}
104
105// A user-defined printable class template in a user-chosen namespace.
106template <typename T>
107class PrintableViaPrintToTemplate {
108 public:
109 explicit PrintableViaPrintToTemplate(const T& value) : value_(value) {}
110
111 const T& value() const { return value_; }
112 private:
113 T value_;
114};
115
116template <typename T>
117void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
118 *os << "PrintableViaPrintToTemplate: " << x.value();
119}
120
121// A user-defined streamable class template in a user namespace.
122template <typename T>
123class StreamableTemplateInFoo {
124 public:
125 StreamableTemplateInFoo() : value_() {}
126
127 const T& value() const { return value_; }
128 private:
129 T value_;
130};
131
132template <typename T>
133inline ::std::ostream& operator<<(::std::ostream& os,
134 const StreamableTemplateInFoo<T>& x) {
135 return os << "StreamableTemplateInFoo: " << x.value();
136}
137
138} // namespace foo
139
140namespace testing {
141namespace gmock_printers_test {
142
143using ::std::deque;
144using ::std::list;
145using ::std::make_pair;
146using ::std::map;
147using ::std::multimap;
148using ::std::multiset;
149using ::std::pair;
150using ::std::set;
151using ::std::tr1::make_tuple;
152using ::std::tr1::tuple;
153using ::std::vector;
154using ::testing::StartsWith;
155using ::testing::internal::UniversalPrinter;
156using ::testing::internal::string;
157
158#ifdef GTEST_OS_WINDOWS
159// MSVC defines the following classes in the ::stdext namespace while
160// gcc defines them in the :: namespace. Note that they are not part
161// of the C++ standard.
162
163using ::stdext::hash_map;
164using ::stdext::hash_set;
165using ::stdext::hash_multimap;
166using ::stdext::hash_multiset;
167
168#endif // GTEST_OS_WINDOWS
169
170// Prints a value to a string using the universal value printer. This
171// is a helper for testing UniversalPrinter<T>::Print() for various types.
172template <typename T>
173string Print(const T& value) {
174 ::std::stringstream ss;
175 UniversalPrinter<T>::Print(value, &ss);
176 return ss.str();
177}
178
179// Prints a value passed by reference to a string, using the universal
180// value printer. This is a helper for testing
181// UniversalPrinter<T&>::Print() for various types.
182template <typename T>
183string PrintByRef(const T& value) {
184 ::std::stringstream ss;
185 UniversalPrinter<T&>::Print(value, &ss);
186 return ss.str();
187}
188
189// Tests printing various char types.
190
191// char.
192TEST(PrintCharTest, PlainChar) {
193 EXPECT_EQ("'\\0'", Print('\0'));
194 EXPECT_EQ("'\\'' (39)", Print('\''));
195 EXPECT_EQ("'\"' (34)", Print('"'));
196 EXPECT_EQ("'\\?' (63)", Print('\?'));
197 EXPECT_EQ("'\\\\' (92)", Print('\\'));
198 EXPECT_EQ("'\\a' (7)", Print('\a'));
199 EXPECT_EQ("'\\b' (8)", Print('\b'));
200 EXPECT_EQ("'\\f' (12)", Print('\f'));
201 EXPECT_EQ("'\\n' (10)", Print('\n'));
202 EXPECT_EQ("'\\r' (13)", Print('\r'));
203 EXPECT_EQ("'\\t' (9)", Print('\t'));
204 EXPECT_EQ("'\\v' (11)", Print('\v'));
205 EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
206 EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
207 EXPECT_EQ("' ' (32)", Print(' '));
208 EXPECT_EQ("'a' (97)", Print('a'));
209}
210
211// signed char.
212TEST(PrintCharTest, SignedChar) {
213 EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
214 EXPECT_EQ("'\\xCE' (-50)",
215 Print(static_cast<signed char>(-50)));
216}
217
218// unsigned char.
219TEST(PrintCharTest, UnsignedChar) {
220 EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
221 EXPECT_EQ("'b' (98)",
222 Print(static_cast<unsigned char>('b')));
223}
224
225// Tests printing other simple, built-in types.
226
227// bool.
228TEST(PrintBuiltInTypeTest, Bool) {
229 EXPECT_EQ("false", Print(false));
230 EXPECT_EQ("true", Print(true));
231}
232
233// wchar_t.
234TEST(PrintBuiltInTypeTest, Wchar_t) {
235 EXPECT_EQ("L'\\0'", Print(L'\0'));
236 EXPECT_EQ("L'\\'' (39)", Print(L'\''));
237 EXPECT_EQ("L'\"' (34)", Print(L'"'));
238 EXPECT_EQ("L'\\?' (63)", Print(L'\?'));
239 EXPECT_EQ("L'\\\\' (92)", Print(L'\\'));
240 EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
241 EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
242 EXPECT_EQ("L'\\f' (12)", Print(L'\f'));
243 EXPECT_EQ("L'\\n' (10)", Print(L'\n'));
244 EXPECT_EQ("L'\\r' (13)", Print(L'\r'));
245 EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
246 EXPECT_EQ("L'\\v' (11)", Print(L'\v'));
247 EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
248 EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
249 EXPECT_EQ("L' ' (32)", Print(L' '));
250 EXPECT_EQ("L'a' (97)", Print(L'a'));
251 EXPECT_EQ("L'\\x576' (1398)", Print(L'\x576'));
252 EXPECT_EQ("L'\\xC74D' (51021)", Print(L'\xC74D'));
253}
254
255// Test that Int64 provides more storage than wchar_t.
256TEST(PrintTypeSizeTest, Wchar_t) {
257 EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64));
258}
259
260// Various integer types.
261TEST(PrintBuiltInTypeTest, Integer) {
262 EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8
263 EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8
264 EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16
265 EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16
266 EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32
267 EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32
268 EXPECT_EQ("18446744073709551615",
269 Print(static_cast<testing::internal::UInt64>(-1))); // uint64
270 EXPECT_EQ("-9223372036854775808",
271 Print(static_cast<testing::internal::Int64>(1) << 63)); // int64
272}
273
274// Size types.
275TEST(PrintBuiltInTypeTest, Size_t) {
276 EXPECT_EQ("1", Print(sizeof('a'))); // size_t.
277#ifndef GTEST_OS_WINDOWS
278 // Windows has no ssize_t type.
279 EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t.
280#endif // GTEST_OS_WINDOWS
281}
282
283// Floating-points.
284TEST(PrintBuiltInTypeTest, FloatingPoints) {
285 EXPECT_EQ("1.5", Print(1.5f)); // float
286 EXPECT_EQ("-2.5", Print(-2.5)); // double
287}
288
289// Since ::std::stringstream::operator<<(const void *) formats the pointer
290// output differently with different compilers, we have to create the expected
291// output first and use it as our expectation.
292static string PrintPointer(const void *p) {
293 ::std::stringstream expected_result_stream;
294 expected_result_stream << p;
295 return expected_result_stream.str();
296}
297
298// Tests printing C strings.
299
300// const char*.
301TEST(PrintCStringTest, Const) {
302 const char* p = "World";
303 EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
304}
305
306// char*.
307TEST(PrintCStringTest, NonConst) {
308 char p[] = "Hi";
309 EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
310 Print(static_cast<char*>(p)));
311}
312
313// NULL C string.
314TEST(PrintCStringTest, Null) {
315 const char* p = NULL;
316 EXPECT_EQ("NULL", Print(p));
317}
318
319// Tests that C strings are escaped properly.
320TEST(PrintCStringTest, EscapesProperly) {
321 const char* p = "'\"\?\\\a\b\f\n\r\t\v\x7F\xFF a";
322 EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"\\?\\\\\\a\\b\\f"
323 "\\n\\r\\t\\v\\x7F\\xFF a\"",
324 Print(p));
325}
326
327
328
329// MSVC compiler can be configured to define whar_t as a typedef
330// of unsigned short. Defining an overload for const wchar_t* in that case
331// would cause pointers to unsigned shorts be printed as wide strings,
332// possibly accessing more memory than intended and causing invalid
333// memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
334// wchar_t is implemented as a native type.
335#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
336
337// const wchar_t*.
338TEST(PrintWideCStringTest, Const) {
339 const wchar_t* p = L"World";
340 EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p));
341}
342
343// wchar_t*.
344TEST(PrintWideCStringTest, NonConst) {
345 wchar_t p[] = L"Hi";
346 EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"",
347 Print(static_cast<wchar_t*>(p)));
348}
349
350// NULL wide C string.
351TEST(PrintWideCStringTest, Null) {
352 const wchar_t* p = NULL;
353 EXPECT_EQ("NULL", Print(p));
354}
355
356// Tests that wide C strings are escaped properly.
357TEST(PrintWideCStringTest, EscapesProperly) {
358 const wchar_t* p = L"'\"\?\\\a\b\f\n\r\t\v\xD3\x576\x8D3\xC74D a";
359 EXPECT_EQ(PrintPointer(p) + " pointing to L\"'\\\"\\?\\\\\\a\\b\\f"
360 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
361 Print(p));
362}
363#endif // native wchar_t
364
365// Tests printing pointers to other char types.
366
367// signed char*.
368TEST(PrintCharPointerTest, SignedChar) {
369 signed char* p = reinterpret_cast<signed char*>(0x1234);
370 EXPECT_EQ(PrintPointer(p), Print(p));
371 p = NULL;
372 EXPECT_EQ("NULL", Print(p));
373}
374
375// const signed char*.
376TEST(PrintCharPointerTest, ConstSignedChar) {
377 signed char* p = reinterpret_cast<signed char*>(0x1234);
378 EXPECT_EQ(PrintPointer(p), Print(p));
379 p = NULL;
380 EXPECT_EQ("NULL", Print(p));
381}
382
383// unsigned char*.
384TEST(PrintCharPointerTest, UnsignedChar) {
385 unsigned char* p = reinterpret_cast<unsigned char*>(0x1234);
386 EXPECT_EQ(PrintPointer(p), Print(p));
387 p = NULL;
388 EXPECT_EQ("NULL", Print(p));
389}
390
391// const unsigned char*.
392TEST(PrintCharPointerTest, ConstUnsignedChar) {
393 const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234);
394 EXPECT_EQ(PrintPointer(p), Print(p));
395 p = NULL;
396 EXPECT_EQ("NULL", Print(p));
397}
398
399// Tests printing pointers to simple, built-in types.
400
401// bool*.
402TEST(PrintPointerToBuiltInTypeTest, Bool) {
403 bool* p = reinterpret_cast<bool*>(0xABCD);
404 EXPECT_EQ(PrintPointer(p), Print(p));
405 p = NULL;
406 EXPECT_EQ("NULL", Print(p));
407}
408
409// void*.
410TEST(PrintPointerToBuiltInTypeTest, Void) {
411 void* p = reinterpret_cast<void*>(0xABCD);
412 EXPECT_EQ(PrintPointer(p), Print(p));
413 p = NULL;
414 EXPECT_EQ("NULL", Print(p));
415}
416
417// const void*.
418TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
419 const void* p = reinterpret_cast<const void*>(0xABCD);
420 EXPECT_EQ(PrintPointer(p), Print(p));
421 p = NULL;
422 EXPECT_EQ("NULL", Print(p));
423}
424
425// Tests printing pointers to pointers.
426TEST(PrintPointerToPointerTest, IntPointerPointer) {
427 int** p = reinterpret_cast<int**>(0xABCD);
428 EXPECT_EQ(PrintPointer(p), Print(p));
429 p = NULL;
430 EXPECT_EQ("NULL", Print(p));
431}
432
433// Tests printing (non-member) function pointers.
434
435void MyFunction(int n) {}
436
437TEST(PrintPointerTest, NonMemberFunctionPointer) {
438 EXPECT_EQ(PrintPointer(reinterpret_cast<const void*>(&MyFunction)),
439 Print(&MyFunction));
440 int (*p)(bool) = NULL; // NOLINT
441 EXPECT_EQ("NULL", Print(p));
442}
443
444// Tests printing member variable pointers. Although they are called
445// pointers, they don't point to a location in the address space.
446// Their representation is implementation-defined. Thus they will be
447// printed as raw bytes.
448
449struct Foo {
450 public:
451 virtual ~Foo() {}
452 int MyMethod(char x) { return x + 1; }
453 virtual char MyVirtualMethod(int n) { return 'a'; }
454
455 int value;
456};
457
458TEST(PrintPointerTest, MemberVariablePointer) {
459 EXPECT_THAT(Print(&Foo::value),
460 StartsWith(Print(sizeof(&Foo::value)) + "-byte object "));
461 int (Foo::*p) = NULL; // NOLINT
462 EXPECT_THAT(Print(p),
463 StartsWith(Print(sizeof(p)) + "-byte object "));
464}
465
466// Tests printing member function pointers. Although they are called
467// pointers, they don't point to a location in the address space.
468// Their representation is implementation-defined. Thus they will be
469// printed as raw bytes.
470TEST(PrintPointerTest, MemberFunctionPointer) {
471 EXPECT_THAT(Print(&Foo::MyMethod),
472 StartsWith(Print(sizeof(&Foo::MyMethod)) + "-byte object "));
473 EXPECT_THAT(Print(&Foo::MyVirtualMethod),
474 StartsWith(Print(sizeof((&Foo::MyVirtualMethod)))
475 + "-byte object "));
476 int (Foo::*p)(char) = NULL; // NOLINT
477 EXPECT_THAT(Print(p),
478 StartsWith(Print(sizeof(p)) + "-byte object "));
479}
480
481// Tests printing C arrays.
482
483// One-dimensional array.
484
485void ArrayHelper1(int (&a)[5]) { // NOLINT
486 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", Print(a));
487}
488
489TEST(PrintArrayTest, OneDimensionalArray) {
490 int a[5] = { 1, 2, 3, 4, 5 };
491 ArrayHelper1(a);
492}
493
494// Two-dimensional array.
495
496void ArrayHelper2(int (&a)[2][5]) { // NOLINT
497 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", Print(a));
498}
499
500TEST(PrintArrayTest, TwoDimensionalArray) {
501 int a[2][5] = {
502 { 1, 2, 3, 4, 5 },
503 { 6, 7, 8, 9, 0 }
504 };
505 ArrayHelper2(a);
506}
507
508// Array of const elements.
509
510void ArrayHelper3(const bool (&a)[1]) { // NOLINT
511 EXPECT_EQ("{ false }", Print(a));
512}
513
514TEST(PrintArrayTest, ConstArray) {
515 const bool a[1] = { false };
516 ArrayHelper3(a);
517}
518
519// Char array.
520
521void ArrayHelper4(char (&a)[3]) { // NOLINT
522 EXPECT_EQ(PrintPointer(a) + " pointing to \"Hi\"", Print(a));
523}
524
525TEST(PrintArrayTest, CharArray) {
526 char a[3] = "Hi";
527 ArrayHelper4(a);
528}
529
530// Const char array.
531
532void ArrayHelper5(const char (&a)[3]) { // NOLINT
533 EXPECT_EQ(Print(a), PrintPointer(a) + " pointing to \"Hi\"");
534}
535
536TEST(PrintArrayTest, ConstCharArray) {
537 const char a[3] = "Hi";
538 ArrayHelper5(a);
539}
540
541// Array of objects.
542TEST(PrintArrayTest, ObjectArray) {
543 string a[3] = { "Hi", "Hello", "Ni hao" };
544 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", Print(a));
545}
546
547// Array with many elements.
548TEST(PrintArrayTest, BigArray) {
549 int a[100] = { 1, 2, 3 };
550 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
551 Print(a));
552}
553
554// Tests printing ::string and ::std::string.
555
556#if GTEST_HAS_GLOBAL_STRING
557// ::string.
558TEST(PrintStringTest, StringInGlobalNamespace) {
559 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
560 const ::string str(s, sizeof(s));
561 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
562 Print(str));
563}
564#endif // GTEST_HAS_GLOBAL_STRING
565
566#if GTEST_HAS_STD_STRING
567// ::std::string.
568TEST(PrintStringTest, StringInStdNamespace) {
569 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
570 const ::std::string str(s, sizeof(s));
571 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
572 Print(str));
573}
574#endif // GTEST_HAS_STD_STRING
575
576// Tests printing ::wstring and ::std::wstring.
577
578#if GTEST_HAS_GLOBAL_WSTRING
579// ::wstring.
580TEST(PrintWideStringTest, StringInGlobalNamespace) {
581 const wchar_t s[] = L"'\"\?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
582 const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
583 EXPECT_EQ("L\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
584 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
585 Print(str));
586}
587#endif // GTEST_HAS_GLOBAL_WSTRING
588
589#if GTEST_HAS_STD_WSTRING
590// ::std::wstring.
591TEST(PrintWideStringTest, StringInStdNamespace) {
592 const wchar_t s[] = L"'\"\?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
593 const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
594 EXPECT_EQ("L\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
595 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
596 Print(str));
597}
598#endif // GTEST_HAS_STD_WSTRING
599
600// Tests printing STL containers.
601
602TEST(PrintStlContainerTest, EmptyDeque) {
603 deque<char> empty;
604 EXPECT_EQ("{}", Print(empty));
605}
606
607TEST(PrintStlContainerTest, NonEmptyDeque) {
608 deque<int> non_empty;
609 non_empty.push_back(1);
610 non_empty.push_back(3);
611 EXPECT_EQ("{ 1, 3 }", Print(non_empty));
612}
613
614#ifdef GMOCK_HAS_HASH_MAP_
615
616TEST(PrintStlContainerTest, OneElementHashMap) {
617 hash_map<int, char> map1;
618 map1[1] = 'a';
619 EXPECT_EQ("{ (1, 'a' (97)) }", Print(map1));
620}
621
622TEST(PrintStlContainerTest, HashMultiMap) {
623 hash_multimap<int, bool> map1;
624 map1.insert(make_pair(5, true));
625 map1.insert(make_pair(5, false));
626
627 // Elements of hash_multimap can be printed in any order.
628 const string result = Print(map1);
629 EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
630 result == "{ (5, false), (5, true) }")
631 << " where Print(map1) returns \"" << result << "\".";
632}
633
634#endif // GMOCK_HAS_HASH_MAP_
635
636#ifdef GMOCK_HAS_HASH_SET_
637
638TEST(PrintStlContainerTest, HashSet) {
639 hash_set<string> set1;
640 set1.insert("hello");
641 EXPECT_EQ("{ \"hello\" }", Print(set1));
642}
643
644TEST(PrintStlContainerTest, HashMultiSet) {
645 const int kSize = 5;
646 int a[kSize] = { 1, 1, 2, 5, 1 };
647 hash_multiset<int> set1(a, a + kSize);
648
649 // Elements of hash_multiset can be printed in any order.
650 const string result = Print(set1);
651 const string expected_pattern = "{ d, d, d, d, d }"; // d means a digit.
652
653 // Verifies the result matches the expected pattern; also extracts
654 // the numbers in the result.
655 ASSERT_EQ(expected_pattern.length(), result.length());
656 std::vector<int> numbers;
657 for (size_t i = 0; i != result.length(); i++) {
658 if (expected_pattern[i] == 'd') {
659 ASSERT_TRUE(isdigit(result[i]));
660 numbers.push_back(result[i] - '0');
661 } else {
662 EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
663 << result;
664 }
665 }
666
667 // Makes sure the result contains the right numbers.
668 std::sort(numbers.begin(), numbers.end());
669 std::sort(a, a + kSize);
670 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
671}
672
673#endif // GMOCK_HAS_HASH_SET_
674
675TEST(PrintStlContainerTest, List) {
676 const char* a[] = {
677 "hello",
678 "world"
679 };
680 const list<string> strings(a, a + 2);
681 EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings));
682}
683
684TEST(PrintStlContainerTest, Map) {
685 map<int, bool> map1;
686 map1[1] = true;
687 map1[5] = false;
688 map1[3] = true;
689 EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1));
690}
691
692TEST(PrintStlContainerTest, MultiMap) {
693 multimap<bool, int> map1;
694 map1.insert(make_pair(true, 0));
695 map1.insert(make_pair(true, 1));
696 map1.insert(make_pair(false, 2));
697 EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1));
698}
699
700TEST(PrintStlContainerTest, Set) {
701 const unsigned int a[] = { 3, 0, 5 };
702 set<unsigned int> set1(a, a + 3);
703 EXPECT_EQ("{ 0, 3, 5 }", Print(set1));
704}
705
706TEST(PrintStlContainerTest, MultiSet) {
707 const int a[] = { 1, 1, 2, 5, 1 };
708 multiset<int> set1(a, a + 5);
709 EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1));
710}
711
712TEST(PrintStlContainerTest, Pair) {
713 pair<const bool, int> p(true, 5);
714 EXPECT_EQ("(true, 5)", Print(p));
715}
716
717TEST(PrintStlContainerTest, Vector) {
718 vector<int> v;
719 v.push_back(1);
720 v.push_back(2);
721 EXPECT_EQ("{ 1, 2 }", Print(v));
722}
723
724TEST(PrintStlContainerTest, LongSequence) {
725 const int a[100] = { 1, 2, 3 };
726 const vector<int> v(a, a + 100);
727 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
728 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
729}
730
731TEST(PrintStlContainerTest, NestedContainer) {
732 const int a1[] = { 1, 2 };
733 const int a2[] = { 3, 4, 5 };
734 const list<int> l1(a1, a1 + 2);
735 const list<int> l2(a2, a2 + 3);
736
737 vector<list<int> > v;
738 v.push_back(l1);
739 v.push_back(l2);
740 EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
741}
742
743
744// Tests printing tuples.
745
746// Tuples of various arities.
747TEST(PrintTupleTest, VariousSizes) {
748 tuple<> t0;
749 EXPECT_EQ("()", Print(t0));
750
751 tuple<int> t1(5);
752 EXPECT_EQ("(5)", Print(t1));
753
754 tuple<char, bool> t2('a', true);
755 EXPECT_EQ("('a' (97), true)", Print(t2));
756
shiqianc97f2f52008-12-11 17:22:59 +0000757 tuple<bool, int, int> t3(false, 2, 3);
758 EXPECT_EQ("(false, 2, 3)", Print(t3));
759
760 tuple<bool, int, int, int> t4(false, 2, 3, 4);
761 EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
762
763 tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
764 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
765
766 tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
767 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
768
769 tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7);
770 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
771
772 tuple<bool, int, int, int, bool, int, int, bool> t8(
773 false, 2, 3, 4, true, 6, 7, true);
774 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
775
776 tuple<bool, int, int, int, bool, int, int, bool, int> t9(
777 false, 2, 3, 4, true, 6, 7, true, 9);
778 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
779
shiqiane35fdd92008-12-10 05:08:54 +0000780 const char* const str = "8";
781 tuple<bool, char, short, testing::internal::Int32, // NOLINT
782 testing::internal::Int64, float, double, const char*, void*, string>
783 t10(false, 'a', 3, 4, 5, 6.5F, 7.5, str, NULL, "10");
784 EXPECT_EQ("(false, 'a' (97), 3, 4, 5, 6.5, 7.5, " + PrintPointer(str) +
785 " pointing to \"8\", NULL, \"10\")",
786 Print(t10));
787}
788
789// Nested tuples.
790TEST(PrintTupleTest, NestedTuple) {
791 tuple<tuple<int, double>, char> nested(make_tuple(5, 9.5), 'a');
792 EXPECT_EQ("((5, 9.5), 'a' (97))", Print(nested));
793}
794
795// Tests printing user-defined unprintable types.
796
797// Unprintable types in the global namespace.
798TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
799 EXPECT_EQ("1-byte object <00>",
800 Print(UnprintableTemplateInGlobal<bool>()));
801}
802
803// Unprintable types in a user namespace.
804TEST(PrintUnprintableTypeTest, InUserNamespace) {
805 EXPECT_EQ("16-byte object <EF12 0000 34AB 0000 0000 0000 0000 0000>",
806 Print(::foo::UnprintableInFoo()));
807}
808
809// Unprintable types are that too big to be printed completely.
810
811struct Big {
812 Big() { memset(array, 0, sizeof(array)); }
813 char array[257];
814};
815
816TEST(PrintUnpritableTypeTest, BigObject) {
817 EXPECT_EQ("257-byte object <0000 0000 0000 0000 0000 0000 "
818 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 "
819 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 "
820 "0000 0000 0000 0000 0000 0000 ... 0000 0000 0000 "
821 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 "
822 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 "
823 "0000 0000 0000 0000 0000 0000 0000 0000 00>",
824 Print(Big()));
825}
826
827// Tests printing user-defined streamable types.
828
829// Streamable types in the global namespace.
830TEST(PrintStreamableTypeTest, InGlobalNamespace) {
831 EXPECT_EQ("StreamableInGlobal",
832 Print(StreamableInGlobal()));
833}
834
835// Printable template types in a user namespace.
836TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
837 EXPECT_EQ("StreamableTemplateInFoo: 0",
838 Print(::foo::StreamableTemplateInFoo<int>()));
839}
840
841// Tests printing user-defined types that have a PrintTo() function.
842TEST(PrintPrintableTypeTest, InUserNamespace) {
843 EXPECT_EQ("PrintableViaPrintTo: 0",
844 Print(::foo::PrintableViaPrintTo()));
845}
846
847// Tests printing user-defined class template that have a PrintTo() function.
848TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
849 EXPECT_EQ("PrintableViaPrintToTemplate: 5",
850 Print(::foo::PrintableViaPrintToTemplate<int>(5)));
851}
852
853#if GMOCK_HAS_PROTOBUF_
854
855// Tests printing a protocol message.
856TEST(PrintProtocolMessageTest, PrintsShortDebugString) {
857 testing::internal::TestMessage msg;
858 msg.set_member("yes");
859 EXPECT_EQ("<member:\"yes\">", Print(msg));
860}
861
862// Tests printing a proto2 message.
863TEST(PrintProto2MessageTest, PrintsShortDebugString) {
864 testing::internal::FooMessage msg;
865 msg.set_int_field(2);
866 EXPECT_PRED2(RE::FullMatch, Print(msg),
867 "<int_field:\\s*2\\s*>");
868}
869
870#endif // GMOCK_HAS_PROTOBUF_
871
872// Tests that the universal printer prints both the address and the
873// value of a reference.
874TEST(PrintReferenceTest, PrintsAddressAndValue) {
875 int n = 5;
876 EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
877
878 int a[2][3] = {
879 { 0, 1, 2 },
880 { 3, 4, 5 }
881 };
882 EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
883 PrintByRef(a));
884
885 const ::foo::UnprintableInFoo x;
886 EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
887 "<EF12 0000 34AB 0000 0000 0000 0000 0000>",
888 PrintByRef(x));
889}
890
891// Tests that the universal printer prints a function pointer passed by
892// reference.
893TEST(PrintReferenceTest, HandlesFunctionPointer) {
894 void (*fp)(int n) = &MyFunction;
895 const string fp_pointer_string =
896 PrintPointer(reinterpret_cast<const void*>(&fp));
897 const string fp_string = PrintPointer(reinterpret_cast<const void*>(fp));
898 EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
899 PrintByRef(fp));
900}
901
902// Tests that the universal printer prints a member function pointer
903// passed by reference.
904TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
905 int (Foo::*p)(char ch) = &Foo::MyMethod;
906 EXPECT_THAT(PrintByRef(p),
907 StartsWith("@" + PrintPointer(reinterpret_cast<const void*>(&p))
908 + " " + Print(sizeof(p)) + "-byte object "));
909
910 char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
911 EXPECT_THAT(PrintByRef(p2),
912 StartsWith("@" + PrintPointer(reinterpret_cast<const void*>(&p2))
913 + " " + Print(sizeof(p2)) + "-byte object "));
914}
915
916// Tests that the universal printer prints a member variable pointer
917// passed by reference.
918TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
919 int (Foo::*p) = &Foo::value; // NOLINT
920 EXPECT_THAT(PrintByRef(p),
921 StartsWith("@" + PrintPointer(&p)
922 + " " + Print(sizeof(p)) + "-byte object "));
923}
924
925} // namespace gmock_printers_test
926} // namespace testing