summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925>2010-01-08 00:23:45 +0000
committerzhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925>2010-01-08 00:23:45 +0000
commit65de7e062b96a0b579ba832ec8e5527528b8fa51 (patch)
treea718cd0252d548885e82abe10b5ed17342dd15b0
parent71ab4d054d79146c16d9fae3d6bf9cdaa5ed5b67 (diff)
downloadgtest-65de7e062b96a0b579ba832ec8e5527528b8fa51.tar.gz
gtest-65de7e062b96a0b579ba832ec8e5527528b8fa51.tar.bz2
gtest-65de7e062b96a0b579ba832ec8e5527528b8fa51.tar.xz
Changes Message() to print double with enough precision by default.
git-svn-id: http://googletest.googlecode.com/svn/trunk@365 861a406c-534a-0410-8894-cb66d6ee9925
-rw-r--r--include/gtest/gtest-message.h8
-rw-r--r--samples/prime_tables.h2
-rw-r--r--src/gtest.cc18
-rw-r--r--test/gtest-message_test.cc20
-rw-r--r--test/gtest_unittest.cc48
5 files changed, 57 insertions, 39 deletions
diff --git a/include/gtest/gtest-message.h b/include/gtest/gtest-message.h
index 6398712..711ef2f 100644
--- a/include/gtest/gtest-message.h
+++ b/include/gtest/gtest-message.h
@@ -46,6 +46,8 @@
#ifndef GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
#define GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
+#include <limits>
+
#include <gtest/internal/gtest-string.h>
#include <gtest/internal/gtest-internal.h>
@@ -89,7 +91,11 @@ class Message {
// ASSERT/EXPECT in a procedure adds over 200 bytes to the procedure's
// stack frame leading to huge stack frames in some cases; gcc does not reuse
// the stack space.
- Message() : ss_(new internal::StrStream) {}
+ Message() : ss_(new internal::StrStream) {
+ // By default, we want there to be enough precision when printing
+ // a double to a Message.
+ *ss_ << std::setprecision(std::numeric_limits<double>::digits10 + 2);
+ }
// Copy constructor.
Message(const Message& msg) : ss_(new internal::StrStream) { // NOLINT
diff --git a/samples/prime_tables.h b/samples/prime_tables.h
index 8b6cab4..92ce16a 100644
--- a/samples/prime_tables.h
+++ b/samples/prime_tables.h
@@ -116,7 +116,7 @@ class PreCalculatedPrimeTable : public PrimeTable {
const int is_prime_size_;
bool* const is_prime_;
- // Disables compiler wqarning "assignment operator could ot be generated."
+ // Disables compiler warning "assignment operator could not be generated."
void operator=(const PreCalculatedPrimeTable& rhs);
};
diff --git a/src/gtest.cc b/src/gtest.cc
index c6d6c09..11ce571 100644
--- a/src/gtest.cc
+++ b/src/gtest.cc
@@ -43,6 +43,7 @@
#include <wctype.h>
#include <ostream>
+#include <sstream>
#if GTEST_OS_LINUX
@@ -3168,14 +3169,11 @@ String XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters(const char* str) {
// </testsuite>
// </testsuites>
-// Formats the given time in milliseconds as seconds. The returned
-// C-string is owned by this function and cannot be released by the
-// caller. Calling the function again invalidates the previous
-// result.
-const char* FormatTimeInMillisAsSeconds(TimeInMillis ms) {
- static String str;
- str = (Message() << (ms/1000.0)).GetString();
- return str.c_str();
+// Formats the given time in milliseconds as seconds.
+std::string FormatTimeInMillisAsSeconds(TimeInMillis ms) {
+ ::std::stringstream ss;
+ ss << ms/1000.0;
+ return ss.str();
}
// Streams an XML CDATA section, escaping invalid CDATA sequences as needed.
@@ -3249,7 +3247,7 @@ void XmlUnitTestResultPrinter::PrintXmlTestCase(FILE* out,
test_case.disabled_test_count());
fprintf(out,
"errors=\"0\" time=\"%s\">\n",
- FormatTimeInMillisAsSeconds(test_case.elapsed_time()));
+ FormatTimeInMillisAsSeconds(test_case.elapsed_time()).c_str());
for (int i = 0; i < test_case.total_test_count(); ++i) {
StrStream stream;
OutputXmlTestInfo(&stream, test_case.name(), *test_case.GetTestInfo(i));
@@ -3268,7 +3266,7 @@ void XmlUnitTestResultPrinter::PrintXmlUnitTest(FILE* out,
unit_test.total_test_count(),
unit_test.failed_test_count(),
unit_test.disabled_test_count(),
- FormatTimeInMillisAsSeconds(unit_test.elapsed_time()));
+ FormatTimeInMillisAsSeconds(unit_test.elapsed_time()).c_str());
if (GTEST_FLAG(shuffle)) {
fprintf(out, "random_seed=\"%d\" ", unit_test.random_seed());
}
diff --git a/test/gtest-message_test.cc b/test/gtest-message_test.cc
index f3dda11..e42b034 100644
--- a/test/gtest-message_test.cc
+++ b/test/gtest-message_test.cc
@@ -68,6 +68,23 @@ TEST(MessageTest, ConstructsFromCString) {
EXPECT_STREQ("Hello", ToCString(msg));
}
+// Tests streaming a float.
+TEST(MessageTest, StreamsFloat) {
+ const char* const s = ToCString(Message() << 1.23456F << " " << 2.34567F);
+ // Both numbers should be printed with enough precision.
+ EXPECT_PRED_FORMAT2(testing::IsSubstring, "1.234560", s);
+ EXPECT_PRED_FORMAT2(testing::IsSubstring, " 2.345669", s);
+}
+
+// Tests streaming a double.
+TEST(MessageTest, StreamsDouble) {
+ const char* const s = ToCString(Message() << 1260570880.4555497 << " "
+ << 1260572265.1954534);
+ // Both numbers should be printed with enough precision.
+ EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s);
+ EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s);
+}
+
// Tests streaming a non-char pointer.
TEST(MessageTest, StreamsPointer) {
int n = 0;
@@ -93,9 +110,6 @@ TEST(MessageTest, StreamsNullCString) {
}
// Tests streaming std::string.
-//
-// As std::string has problem in MSVC when exception is disabled, we only
-// test this where std::string can be used.
TEST(MessageTest, StreamsString) {
const ::std::string str("Hello");
EXPECT_STREQ("Hello", ToCString(Message() << str));
diff --git a/test/gtest_unittest.cc b/test/gtest_unittest.cc
index ba5eb60..16de794 100644
--- a/test/gtest_unittest.cc
+++ b/test/gtest_unittest.cc
@@ -82,7 +82,7 @@ namespace testing {
namespace internal {
bool ShouldUseColor(bool stdout_is_tty);
-const char* FormatTimeInMillisAsSeconds(TimeInMillis ms);
+::std::string FormatTimeInMillisAsSeconds(TimeInMillis ms);
bool ParseInt32Flag(const char* str, const char* flag, Int32* value);
// Used for testing the flag parsing.
@@ -270,23 +270,23 @@ TEST(GetTestTypeIdTest, ReturnsTheSameValueInsideOrOutsideOfGoogleTest) {
// Tests FormatTimeInMillisAsSeconds().
TEST(FormatTimeInMillisAsSecondsTest, FormatsZero) {
- EXPECT_STREQ("0", FormatTimeInMillisAsSeconds(0));
+ EXPECT_EQ("0", FormatTimeInMillisAsSeconds(0));
}
TEST(FormatTimeInMillisAsSecondsTest, FormatsPositiveNumber) {
- EXPECT_STREQ("0.003", FormatTimeInMillisAsSeconds(3));
- EXPECT_STREQ("0.01", FormatTimeInMillisAsSeconds(10));
- EXPECT_STREQ("0.2", FormatTimeInMillisAsSeconds(200));
- EXPECT_STREQ("1.2", FormatTimeInMillisAsSeconds(1200));
- EXPECT_STREQ("3", FormatTimeInMillisAsSeconds(3000));
+ EXPECT_EQ("0.003", FormatTimeInMillisAsSeconds(3));
+ EXPECT_EQ("0.01", FormatTimeInMillisAsSeconds(10));
+ EXPECT_EQ("0.2", FormatTimeInMillisAsSeconds(200));
+ EXPECT_EQ("1.2", FormatTimeInMillisAsSeconds(1200));
+ EXPECT_EQ("3", FormatTimeInMillisAsSeconds(3000));
}
TEST(FormatTimeInMillisAsSecondsTest, FormatsNegativeNumber) {
- EXPECT_STREQ("-0.003", FormatTimeInMillisAsSeconds(-3));
- EXPECT_STREQ("-0.01", FormatTimeInMillisAsSeconds(-10));
- EXPECT_STREQ("-0.2", FormatTimeInMillisAsSeconds(-200));
- EXPECT_STREQ("-1.2", FormatTimeInMillisAsSeconds(-1200));
- EXPECT_STREQ("-3", FormatTimeInMillisAsSeconds(-3000));
+ EXPECT_EQ("-0.003", FormatTimeInMillisAsSeconds(-3));
+ EXPECT_EQ("-0.01", FormatTimeInMillisAsSeconds(-10));
+ EXPECT_EQ("-0.2", FormatTimeInMillisAsSeconds(-200));
+ EXPECT_EQ("-1.2", FormatTimeInMillisAsSeconds(-1200));
+ EXPECT_EQ("-3", FormatTimeInMillisAsSeconds(-3000));
}
#if !GTEST_OS_SYMBIAN
@@ -3095,9 +3095,9 @@ TEST_F(FloatTest, Commutative) {
TEST_F(FloatTest, EXPECT_NEAR) {
EXPECT_NEAR(-1.0f, -1.1f, 0.2f);
EXPECT_NEAR(2.0f, 3.0f, 1.0f);
- EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0f,1.2f, 0.1f), // NOLINT
- "The difference between 1.0f and 1.2f is 0.2, "
- "which exceeds 0.1f");
+ EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0f,1.5f, 0.25f), // NOLINT
+ "The difference between 1.0f and 1.5f is 0.5, "
+ "which exceeds 0.25f");
// To work around a bug in gcc 2.95.0, there is intentionally no
// space after the first comma in the previous line.
}
@@ -3106,9 +3106,9 @@ TEST_F(FloatTest, EXPECT_NEAR) {
TEST_F(FloatTest, ASSERT_NEAR) {
ASSERT_NEAR(-1.0f, -1.1f, 0.2f);
ASSERT_NEAR(2.0f, 3.0f, 1.0f);
- EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0f,1.2f, 0.1f), // NOLINT
- "The difference between 1.0f and 1.2f is 0.2, "
- "which exceeds 0.1f");
+ EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0f,1.5f, 0.25f), // NOLINT
+ "The difference between 1.0f and 1.5f is 0.5, "
+ "which exceeds 0.25f");
// To work around a bug in gcc 2.95.0, there is intentionally no
// space after the first comma in the previous line.
}
@@ -3261,9 +3261,9 @@ TEST_F(DoubleTest, Commutative) {
TEST_F(DoubleTest, EXPECT_NEAR) {
EXPECT_NEAR(-1.0, -1.1, 0.2);
EXPECT_NEAR(2.0, 3.0, 1.0);
- EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0, 1.2, 0.1), // NOLINT
- "The difference between 1.0 and 1.2 is 0.2, "
- "which exceeds 0.1");
+ EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0, 1.5, 0.25), // NOLINT
+ "The difference between 1.0 and 1.5 is 0.5, "
+ "which exceeds 0.25");
// To work around a bug in gcc 2.95.0, there is intentionally no
// space after the first comma in the previous statement.
}
@@ -3272,9 +3272,9 @@ TEST_F(DoubleTest, EXPECT_NEAR) {
TEST_F(DoubleTest, ASSERT_NEAR) {
ASSERT_NEAR(-1.0, -1.1, 0.2);
ASSERT_NEAR(2.0, 3.0, 1.0);
- EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0, 1.2, 0.1), // NOLINT
- "The difference between 1.0 and 1.2 is 0.2, "
- "which exceeds 0.1");
+ EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0, 1.5, 0.25), // NOLINT
+ "The difference between 1.0 and 1.5 is 0.5, "
+ "which exceeds 0.25");
// To work around a bug in gcc 2.95.0, there is intentionally no
// space after the first comma in the previous statement.
}