Skip to content

Commit

Permalink
Add a trailing decimal point to FormatTimeInMillisAsSeconds() output …
Browse files Browse the repository at this point in the history
…when input

is an exact N seconds.

PiperOrigin-RevId: 506610898
Change-Id: Idcd705c719e0e721148c350c8a14f27b9eb5c4f7
  • Loading branch information
Abseil Team authored and copybara-github committed Feb 2, 2023
1 parent ebedaa1 commit 6f21175
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion googletest/src/gtest-internal-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ GTEST_API_ TimeInMillis GetTimeInMillis();
// Returns true if and only if Google Test should use colors in the output.
GTEST_API_ bool ShouldUseColor(bool stdout_is_tty);

// Formats the given time in milliseconds as seconds.
// Formats the given time in milliseconds as seconds. If the input is an exact N
// seconds, the output has a trailing decimal point (e.g., "N." intead of "N").
GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms);

// Converts the given time in milliseconds to a date string in the ISO 8601
Expand Down
8 changes: 8 additions & 0 deletions googletest/src/gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <cstdint>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iterator>
#include <limits>
#include <list>
Expand Down Expand Up @@ -4089,6 +4090,13 @@ std::string XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters(
// Formats the given time in milliseconds as seconds.
std::string FormatTimeInMillisAsSeconds(TimeInMillis ms) {
::std::stringstream ss;
// For the exact N seconds, makes sure output has a trailing decimal point.
// Sets precision so that we won't have many trailing zeros (e.g., 300 ms
// will be just 0.3, 410 ms 0.41, and so on)
ss << std::fixed
<< std::setprecision(
ms % 1000 == 0 ? 0 : (ms % 100 == 0 ? 1 : (ms % 10 == 0 ? 2 : 3)))
<< std::showpoint;
ss << (static_cast<double>(ms) * 1e-3);
return ss.str();
}
Expand Down
14 changes: 11 additions & 3 deletions googletest/test/gtest_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,23 +382,31 @@ TEST(CanonicalizeForStdLibVersioning, ElidesDoubleUnderNames) {
// Tests FormatTimeInMillisAsSeconds().

TEST(FormatTimeInMillisAsSecondsTest, FormatsZero) {
EXPECT_EQ("0", FormatTimeInMillisAsSeconds(0));
EXPECT_EQ("0.", FormatTimeInMillisAsSeconds(0));
}

TEST(FormatTimeInMillisAsSecondsTest, FormatsPositiveNumber) {
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));
EXPECT_EQ("3.", FormatTimeInMillisAsSeconds(3000));
EXPECT_EQ("10.", FormatTimeInMillisAsSeconds(10000));
EXPECT_EQ("100.", FormatTimeInMillisAsSeconds(100000));
EXPECT_EQ("123.456", FormatTimeInMillisAsSeconds(123456));
EXPECT_EQ("1234567.89", FormatTimeInMillisAsSeconds(1234567890));
}

TEST(FormatTimeInMillisAsSecondsTest, FormatsNegativeNumber) {
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));
EXPECT_EQ("-3.", FormatTimeInMillisAsSeconds(-3000));
EXPECT_EQ("-10.", FormatTimeInMillisAsSeconds(-10000));
EXPECT_EQ("-100.", FormatTimeInMillisAsSeconds(-100000));
EXPECT_EQ("-123.456", FormatTimeInMillisAsSeconds(-123456));
EXPECT_EQ("-1234567.89", FormatTimeInMillisAsSeconds(-1234567890));
}

// Tests FormatEpochTimeInMillisAsIso8601(). The correctness of conversion
Expand Down
4 changes: 3 additions & 1 deletion googletest/test/gtest_xml_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ def NormalizeXml(self, element):
)
if element.tagName in ('testsuites', 'testsuite', 'testcase'):
time = element.getAttributeNode('time')
time.value = re.sub(r'^\d+(\.\d+)?$', '*', time.value)
# The value for exact N seconds has a traling decimal point (e.g., "10."
# instead of "10")
time.value = re.sub(r'^\d+\.(\d+)?$', '*', time.value)
type_param = element.getAttributeNode('type_param')
if type_param and type_param.value:
type_param.value = '*'
Expand Down

0 comments on commit 6f21175

Please sign in to comment.