On Wed, 11 Jun 2025 at 15:07, Tomasz Kamiński <tkami...@redhat.com> wrote:
>
> libstdc++-v3/ChangeLog:
>
>         * testsuite/std/time/format/empty_spec.cc: New tests.
>         * testsuite/std/time/format/precision.cc: New test.
> ---
> Testing on x86_64-linux. OK for trunk when tests passes?

OK (with the change to use non-zero for the leading digit)



>
>  .../testsuite/std/time/format/empty_spec.cc   |  51 +++++++++
>  .../testsuite/std/time/format/precision.cc    | 107 ++++++++++++++++++
>  2 files changed, 158 insertions(+)
>  create mode 100644 libstdc++-v3/testsuite/std/time/format/precision.cc
>
> 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<typename _CharT>
> +void
> +test_duration_fp()
> +{
> +  std::basic_string<_CharT> res;
>
>    const duration<double> 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<typename _CharT>
> @@ -292,6 +303,44 @@ test_hh_mm_ss()
>           WIDEN("-14322:24:54.111222333") );
>  }
>
> +template<typename _CharT>
> +void
> +test_hh_mm_ss_fp()
> +{
> +  duration<double> dt = 22h + 24min + 54s + 111222333ns;
> +  // period controls number of subseconds
> +  verify( hms<nanoseconds>(dt),
> +         WIDEN("22:24:54.111222333") );
> +  verify( hms<microseconds>(dt),
> +         WIDEN("22:24:54.111222") );
> +  verify( hms<milliseconds>(dt),
> +         WIDEN("22:24:54.111") );
> +  verify( hms<deciseconds>(dt),
> +         WIDEN("22:24:54.1") );
> +  verify( hms<seconds>(dt),
> +         WIDEN("22:24:54") );
> +  verify( hms<nanoseconds>(-dt),
> +         WIDEN("-22:24:54.111222333") );
> +  verify( hms<microseconds>(-dt),
> +         WIDEN("-22:24:54.111222") );
> +  verify( hms<milliseconds>(-dt),
> +         WIDEN("-22:24:54.111") );
> +  verify( hms<deciseconds>(-dt),
> +         WIDEN("-22:24:54.1") );
> +  verify( hms<seconds>(-dt),
> +         WIDEN("-22:24:54") );
> +
> +  // but hour and minutes are preserved
> +  verify( hms<minutes>(dt),
> +         WIDEN("22:24:54") );
> +  verify( hms<hours>(dt),
> +         WIDEN("22:24:54") );
> +  verify( hms<minutes>(-dt),
> +         WIDEN("-22:24:54") );
> +  verify( hms<hours>(-dt),
> +         WIDEN("-22:24:54") );
> +}
> +
>  template<typename _CharT>
>  void
>  test_hh_mm_ss_cust()
> @@ -339,9 +388,11 @@ void
>  test_durations()
>  {
>    test_duration<CharT>();
> +  test_duration_fp<CharT>();
>    test_duration_cust<CharT>();
>
>    test_hh_mm_ss<CharT>();
> +  test_hh_mm_ss_fp<CharT>();
>    test_hh_mm_ss_cust<CharT>();
>  }
>
> 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..f65d06a71f3
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/std/time/format/precision.cc
> @@ -0,0 +1,107 @@
> +// { dg-do run { target c++20 } }
> +
> +#include <chrono>
> +#include <ranges>
> +#include <testsuite_hooks.h>
> +
> +using namespace std::chrono;
> +
> +#define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
> +#define WIDEN(S) WIDEN_(_CharT, S)
> +
> +template<typename _CharT>
> +void
> +test_empty()
> +{
> +  std::basic_string<_CharT> res;
> +
> +  const duration<double> d(0.111222);
> +  res = std::format(WIDEN("{:.3}"), d);
> +  VERIFY( res == WIDEN("0.111222s") );
> +  res = std::format(WIDEN("{:.6}"), d);
> +  VERIFY( res == WIDEN("0.111222s") );
> +  res = std::format(WIDEN("{:.9}"), d);
> +  VERIFY( res == WIDEN("0.111222s") );
> +
> +  // Uses ostream operator<<
> +  const duration<double, std::nano> nd = d;
> +  res = std::format(WIDEN("{:.3}"), nd);
> +  VERIFY( res == WIDEN("1.11222e+08ns") );
> +  res = std::format(WIDEN("{:.6}"), nd);
> +  VERIFY( res == WIDEN("1.11222e+08ns") );
> +  res = std::format(WIDEN("{:.9}"), nd);
> +  VERIFY( res == WIDEN("1.11222e+08ns") );
> +}
> +
> +template<typename _CharT>
> +void
> +test_Q()
> +{
> +  std::basic_string<_CharT> res;
> +
> +  const duration<double> d(0.111222);
> +  res = std::format(WIDEN("{:.3%Q}"), d);
> +  VERIFY( res == WIDEN("0.111222") );
> +  res = std::format(WIDEN("{:.6%Q}"), d);
> +  VERIFY( res == WIDEN("0.111222") );
> +  res = std::format(WIDEN("{:.9%Q}"), d);
> +  VERIFY( res == WIDEN("0.111222") );
> +
> +  const duration<double, std::nano> nd = d;
> +  res = std::format(WIDEN("{:.3%Q}"), nd);
> +  VERIFY( res == WIDEN("111222000") );
> +  res = std::format(WIDEN("{:.6%Q}"), nd);
> +  VERIFY( res == WIDEN("111222000") );
> +  res = std::format(WIDEN("{:.9%Q}"), nd);
> +  VERIFY( res == WIDEN("111222000") );
> +}
> +
> +template<typename _CharT>
> +void
> +test_S()
> +{
> +  std::basic_string<_CharT> res;
> +
> +  // Precision is ignored, but period affects output
> +  const duration<double> d(0.111222);
> +  res = std::format(WIDEN("{:.3%S}"), d);
> +  VERIFY( res == WIDEN("00") );
> +  res = std::format(WIDEN("{:.6%S}"), d);
> +  VERIFY( res == WIDEN("00") );
> +  res = std::format(WIDEN("{:.9%S}"), d);
> +  VERIFY( res == WIDEN("00") );
> +
> +  const duration<double, std::milli> md = d;
> +  res = std::format(WIDEN("{:.3%S}"), md);
> +  VERIFY( res == WIDEN("00.111") );
> +  res = std::format(WIDEN("{:.6%S}"), md);
> +  VERIFY( res == WIDEN("00.111") );
> +  res = std::format(WIDEN("{:.9%S}"), md);
> +  VERIFY( res == WIDEN("00.111") );
> +
> +  const duration<double, std::nano> nd = d;
> +  res = std::format(WIDEN("{:.3%S}"), nd);
> +  VERIFY( res == WIDEN("00.111222000") );
> +  res = std::format(WIDEN("{:.6%S}"), nd);
> +  VERIFY( res == WIDEN("00.111222000") );
> +  res = std::format(WIDEN("{:.9%S}"), nd);
> +  VERIFY( res == WIDEN("00.111222000") );
> +}
> +
> +template<typename CharT>
> +void
> +test_all()
> +{
> +  test_empty<CharT>();
> +  test_Q<CharT>();
> +  test_S<CharT>();
> +}
> +
> +int main()
> +{
> +  test_all<char>();
> +
> +#ifdef _GLIBCXX_USE_WCHAR_T
> +  test_all<wchar_t>();
> +#endif // _GLIBCXX_USE_WCHAR_T
> +}
> --
> 2.49.0
>

Reply via email to