From: Tomasz KamiƄski Date: Wed, 11 Jun 2025 13:56:25 +0000 (+0200) Subject: libstdc++: Test for precision and floting point durations. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2f2e51b6596c3796b907c6289d3e631508d8774e;p=thirdparty%2Fgcc.git libstdc++: Test for precision and floting point durations. libstdc++-v3/ChangeLog: * testsuite/std/time/format/empty_spec.cc: New tests. * testsuite/std/time/format/precision.cc: New test. --- diff --git a/libstdc++-v3/testsuite/std/time/format/empty_spec.cc b/libstdc++-v3/testsuite/std/time/format/empty_spec.cc index 99cbd740d5f..a94eee19f57 100644 --- a/libstdc++-v3/testsuite/std/time/format/empty_spec.cc +++ b/libstdc++-v3/testsuite/std/time/format/empty_spec.cc @@ -170,6 +170,13 @@ test_duration() verify( -di, WIDEN("-40ms") ); res = std::format(WIDEN("{:>6}"), -di); VERIFY( res == WIDEN(" -40ms") ); +} + +template +void +test_duration_fp() +{ + std::basic_string<_CharT> res; const duration df(11.22); verify( df, WIDEN("11.22s") ); @@ -179,6 +186,10 @@ test_duration() verify( -df, WIDEN("-11.22s") ); res = std::format(WIDEN("{:=^12}"), -df); VERIFY( res == WIDEN("==-11.22s===") ); + + // precision accepted but ignored + res = std::format(WIDEN("{:.6}"), df); + VERIFY( res == WIDEN("11.22s") ); } template @@ -292,6 +303,44 @@ test_hh_mm_ss() WIDEN("-14322:24:54.111222333") ); } +template +void +test_hh_mm_ss_fp() +{ + duration dt = 22h + 24min + 54s + 111222333ns; + // period controls number of subseconds + verify( hms(dt), + WIDEN("22:24:54.111222333") ); + verify( hms(dt), + WIDEN("22:24:54.111222") ); + verify( hms(dt), + WIDEN("22:24:54.111") ); + verify( hms(dt), + WIDEN("22:24:54.1") ); + verify( hms(dt), + WIDEN("22:24:54") ); + verify( hms(-dt), + WIDEN("-22:24:54.111222333") ); + verify( hms(-dt), + WIDEN("-22:24:54.111222") ); + verify( hms(-dt), + WIDEN("-22:24:54.111") ); + verify( hms(-dt), + WIDEN("-22:24:54.1") ); + verify( hms(-dt), + WIDEN("-22:24:54") ); + + // but hour and minutes are preserved + verify( hms(dt), + WIDEN("22:24:54") ); + verify( hms(dt), + WIDEN("22:24:54") ); + verify( hms(-dt), + WIDEN("-22:24:54") ); + verify( hms(-dt), + WIDEN("-22:24:54") ); +} + template void test_hh_mm_ss_cust() @@ -339,9 +388,11 @@ void test_durations() { test_duration(); + test_duration_fp(); test_duration_cust(); test_hh_mm_ss(); + test_hh_mm_ss_fp(); test_hh_mm_ss_cust(); } diff --git a/libstdc++-v3/testsuite/std/time/format/precision.cc b/libstdc++-v3/testsuite/std/time/format/precision.cc new file mode 100644 index 00000000000..5a9acbfe1e0 --- /dev/null +++ b/libstdc++-v3/testsuite/std/time/format/precision.cc @@ -0,0 +1,107 @@ +// { dg-do run { target c++20 } } + +#include +#include +#include + +using namespace std::chrono; + +#define WIDEN_(C, S) ::std::__format::_Widen(S, L##S) +#define WIDEN(S) WIDEN_(_CharT, S) + +template +void +test_empty() +{ + std::basic_string<_CharT> res; + + const duration d(33.111222); + res = std::format(WIDEN("{:.3}"), d); + VERIFY( res == WIDEN("33.1112s") ); + res = std::format(WIDEN("{:.6}"), d); + VERIFY( res == WIDEN("33.1112s") ); + res = std::format(WIDEN("{:.9}"), d); + VERIFY( res == WIDEN("33.1112s") ); + + // Uses ostream operator<< + const duration nd = d; + res = std::format(WIDEN("{:.3}"), nd); + VERIFY( res == WIDEN("3.31112e+10ns") ); + res = std::format(WIDEN("{:.6}"), nd); + VERIFY( res == WIDEN("3.31112e+10ns") ); + res = std::format(WIDEN("{:.9}"), nd); + VERIFY( res == WIDEN("3.31112e+10ns") ); +} + +template +void +test_Q() +{ + std::basic_string<_CharT> res; + + const duration d(7.111222); + res = std::format(WIDEN("{:.3%Q}"), d); + VERIFY( res == WIDEN("7.111222") ); + res = std::format(WIDEN("{:.6%Q}"), d); + VERIFY( res == WIDEN("7.111222") ); + res = std::format(WIDEN("{:.9%Q}"), d); + VERIFY( res == WIDEN("7.111222") ); + + const duration nd = d; + res = std::format(WIDEN("{:.3%Q}"), nd); + VERIFY( res == WIDEN("7111222000") ); + res = std::format(WIDEN("{:.6%Q}"), nd); + VERIFY( res == WIDEN("7111222000") ); + res = std::format(WIDEN("{:.9%Q}"), nd); + VERIFY( res == WIDEN("7111222000") ); +} + +template +void +test_S() +{ + std::basic_string<_CharT> res; + + // Precision is ignored, but period affects output + const duration d(5.111222); + res = std::format(WIDEN("{:.3%S}"), d); + VERIFY( res == WIDEN("05") ); + res = std::format(WIDEN("{:.6%S}"), d); + VERIFY( res == WIDEN("05") ); + res = std::format(WIDEN("{:.9%S}"), d); + VERIFY( res == WIDEN("05") ); + + const duration md = d; + res = std::format(WIDEN("{:.3%S}"), md); + VERIFY( res == WIDEN("05.111") ); + res = std::format(WIDEN("{:.6%S}"), md); + VERIFY( res == WIDEN("05.111") ); + res = std::format(WIDEN("{:.9%S}"), md); + VERIFY( res == WIDEN("05.111") ); + + const duration nd = d; + res = std::format(WIDEN("{:.3%S}"), nd); + VERIFY( res == WIDEN("05.111222000") ); + res = std::format(WIDEN("{:.6%S}"), nd); + VERIFY( res == WIDEN("05.111222000") ); + res = std::format(WIDEN("{:.9%S}"), nd); + VERIFY( res == WIDEN("05.111222000") ); +} + +template +void +test_all() +{ + test_empty(); + test_Q(); + test_S(); +} + +int main() +{ + test_all(); + +#ifdef _GLIBCXX_USE_WCHAR_T + test_all(); +#endif // _GLIBCXX_USE_WCHAR_T +}