aboutsummaryrefslogtreecommitdiffstats
path: root/include/gmock/gmock-printers.h
diff options
context:
space:
mode:
authorzhanyong.wan <zhanyong.wan@8415998a-534a-0410-bf83-d39667b30386>2009-05-29 19:50:06 +0000
committerzhanyong.wan <zhanyong.wan@8415998a-534a-0410-bf83-d39667b30386>2009-05-29 19:50:06 +0000
commit9413f2ff615ae1b933580576183d316c4cb6376c (patch)
treee5ef88b0191fa3296a34c793a17a16427a041fdf /include/gmock/gmock-printers.h
parent16cf473930c01cd7a1a51dff65f22c541fbad5b8 (diff)
downloadgoogletest-9413f2ff615ae1b933580576183d316c4cb6376c.tar.gz
googletest-9413f2ff615ae1b933580576183d316c4cb6376c.tar.bz2
googletest-9413f2ff615ae1b933580576183d316c4cb6376c.zip
Avoids unnecessary printing of call into to internal buffers;
Made the universal value printer safer when printing char[]; Removed duplicated code in InvokeWith; Improved gmock_doctor.py.
Diffstat (limited to 'include/gmock/gmock-printers.h')
-rw-r--r--include/gmock/gmock-printers.h67
1 files changed, 37 insertions, 30 deletions
diff --git a/include/gmock/gmock-printers.h b/include/gmock/gmock-printers.h
index 99002434..e233ef3e 100644
--- a/include/gmock/gmock-printers.h
+++ b/include/gmock/gmock-printers.h
@@ -580,6 +580,41 @@ class UniversalPrinter {
#endif // _MSC_VER
};
+// UniversalPrintArray(begin, len, os) prints an array of 'len'
+// elements, starting at address 'begin'.
+template <typename T>
+void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
+ if (len == 0) {
+ *os << "{}";
+ } else {
+ *os << "{ ";
+ const size_t kThreshold = 18;
+ const size_t kChunkSize = 8;
+ // If the array has more than kThreshold elements, we'll have to
+ // omit some details by printing only the first and the last
+ // kChunkSize elements.
+ // TODO(wan@google.com): let the user control the threshold using a flag.
+ if (len <= kThreshold) {
+ PrintRawArrayTo(begin, len, os);
+ } else {
+ PrintRawArrayTo(begin, kChunkSize, os);
+ *os << ", ..., ";
+ PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
+ }
+ *os << " }";
+ }
+}
+// This overload prints a (const) char array compactly.
+void UniversalPrintArray(const char* begin, size_t len, ::std::ostream* os);
+
+// Prints an array of 'len' elements, starting at address 'begin', to a string.
+template <typename T>
+string UniversalPrintArrayToString(const T* begin, size_t len) {
+ ::std::stringstream ss;
+ UniversalPrintArray(begin, len, &ss);
+ return ss.str();
+}
+
// Implements printing an array type T[N].
template <typename T, size_t N>
class UniversalPrinter<T[N]> {
@@ -587,41 +622,13 @@ class UniversalPrinter<T[N]> {
// Prints the given array, omitting some elements when there are too
// many.
static void Print(const T (&a)[N], ::std::ostream* os) {
- // Prints a char array as a C string. Note that we compare 'const
- // T' with 'const char' instead of comparing T with char, in case
- // that T is already a const type.
- if (internal::type_equals<const T, const char>::value) {
- UniversalPrinter<const T*>::Print(a, os);
- return;
- }
-
- if (N == 0) {
- *os << "{}";
- } else {
- *os << "{ ";
- const size_t kThreshold = 18;
- const size_t kChunkSize = 8;
- // If the array has more than kThreshold elements, we'll have to
- // omit some details by printing only the first and the last
- // kChunkSize elements.
- // TODO(wan): let the user control the threshold using a flag.
- if (N <= kThreshold) {
- PrintRawArrayTo(a, N, os);
- } else {
- PrintRawArrayTo(a, kChunkSize, os);
- *os << ", ..., ";
- PrintRawArrayTo(a + N - kChunkSize, kChunkSize, os);
- }
- *os << " }";
- }
+ UniversalPrintArray(a, N, os);
}
// A convenient wrapper for Print() that returns the print-out as a
// string.
static string PrintToString(const T (&a)[N]) {
- ::std::stringstream ss;
- Print(a, &ss);
- return ss.str();
+ return UniversalPrintArrayToString(a, N);
}
};