Add a trailing decimal point to FormatTimeInMillisAsSeconds() output when input

is an exact N seconds.

PiperOrigin-RevId: 506610898
Change-Id: Idcd705c719e0e721148c350c8a14f27b9eb5c4f7
This commit is contained in:
Abseil Team 2023-02-02 07:14:30 -08:00 committed by Copybara-Service
parent ebedaa18c7
commit 6f21175f57
4 changed files with 24 additions and 5 deletions

View File

@ -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

View File

@ -46,6 +46,7 @@
#include <cstdint>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iterator>
#include <limits>
#include <list>
@ -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();
}

View File

@ -382,7 +382,7 @@ TEST(CanonicalizeForStdLibVersioning, ElidesDoubleUnderNames) {
// Tests FormatTimeInMillisAsSeconds().
TEST(FormatTimeInMillisAsSecondsTest, FormatsZero) {
EXPECT_EQ("0", FormatTimeInMillisAsSeconds(0));
EXPECT_EQ("0.", FormatTimeInMillisAsSeconds(0));
}
TEST(FormatTimeInMillisAsSecondsTest, FormatsPositiveNumber) {
@ -390,7 +390,11 @@ TEST(FormatTimeInMillisAsSecondsTest, FormatsPositiveNumber) {
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) {
@ -398,7 +402,11 @@ TEST(FormatTimeInMillisAsSecondsTest, FormatsNegativeNumber) {
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

View File

@ -218,7 +218,9 @@ class GTestXMLTestCase(gtest_test_utils.TestCase):
)
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 = '*'