On Tue, Jun 30, 2026 at 2:50 PM Anlai Lu <[email protected]> wrote:

> Add tests covering formatted output function semantics for the
> chrono operator<< overloads.  These verify that the new stack-buffer
> implementation correctly preserves sentry construction (badbit check
> and tied stream flush), width/fill padding with all three alignment
> modes (right, left, internal), width reset after each output, and
> locale-aware output.  A weekday_indexed content test and a wchar_t
> smoke test are included.
>
> libstdc++-v3/ChangeLog:
>
>         * testsuite/std/time/ostream_insert.cc: New test.
>
> Signed-off-by: Anlai Lu <[email protected]>
> ---
>  .../testsuite/std/time/ostream_insert.cc      | 163 ++++++++++++++++++
>  1 file changed, 163 insertions(+)
>  create mode 100644 libstdc++-v3/testsuite/std/time/ostream_insert.cc
>
> diff --git a/libstdc++-v3/testsuite/std/time/ostream_insert.cc
> b/libstdc++-v3/testsuite/std/time/ostream_insert.cc
> new file mode 100644
> index 000000000..6898cdb64
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/std/time/ostream_insert.cc
> @@ -0,0 +1,163 @@
> +// { dg-do run { target c++20 } }
> +// { dg-require-effective-target tzdb }
> +// Test stream state semantics for chrono operator<<
> +#include <chrono>
> +#include <format>
> +#include <iomanip>
> +#include <sstream>
> +#include <testsuite_hooks.h>
> +
> +void
> +test_sentry_badbit()
> +{
> +  using namespace std::chrono;
> +  std::ostringstream os;
> +  os.setstate(std::ios::badbit);
> +  VERIFY( !os.good() );
> +  os << sys_time<seconds>{1'700'000'000s};
> +  VERIFY( os.bad() );
> +  VERIFY( os.str().empty() );
> +}
> +
> +void
> +test_sentry_tied()
> +{
> +  using namespace std::chrono;
> +  struct testbuf : std::streambuf
> +  { bool flushed = false; int sync() override { flushed = true; return 0;
> } };
> +  testbuf tied_buf; std::ostream tied(&tied_buf); std::ostringstream os;
> +  os.tie(&tied);
> +  os << sys_time<seconds>{1'700'000'000s};
> +  VERIFY( tied_buf.flushed == true );
> +}
> +
> +void
> +test_width_fill()
> +{
> +  using namespace std::chrono;
> +  std::ostringstream os;
> +  os << std::setw(30) << std::setfill('*') <<
> sys_time<seconds>{1'700'000'000s};
> +  auto s = os.str();
> +  VERIFY( s.size() == 30 );
> +  VERIFY( s.substr(0, 11) == std::string(11, '*') );

+  VERIFY( s.substr(11) == "2023-11-14 22:13:20" );
> +  VERIFY( os.width() == 0 );
> +}
> +
> +void
> +test_width_left()
> +{
> +  using namespace std::chrono;
> +  std::ostringstream os;
> +  os << std::setw(30) << std::left << std::setfill('.') <<
> sys_time<seconds>{1'700'000'000s};
> +  auto s = os.str();
> +  VERIFY( s.size() == 30 );
> +  VERIFY( s.find("2023-11-14") == 0 );
> +  VERIFY( s.back() == '.' );
> +  VERIFY( os.width() == 0 );
> +}
> +
> +void
> +test_width_internal()
> +{
> +  using namespace std::chrono;
> +  std::ostringstream os;
> +  os << std::setw(30) << std::internal << std::setfill('_') << day(15);
> +  auto s = os.str();
> +  VERIFY( s.size() == 30 );
> +  VERIFY( s.substr(0, 28) == std::string(28, '_') );
> +  VERIFY( s.substr(28) == "15" );
> +  VERIFY( os.width() == 0 );
> +}
> +
> +void
> +test_width_reset()
> +{
> +  using namespace std::chrono;
> +  std::ostringstream os;
> +  os << std::setw(20) << sys_time<seconds>{1'700'000'000s};
> +  VERIFY( os.width() == 0 );
> +  os << hh_mm_ss{seconds(45296)};
> +  VERIFY( os.width() == 0 );
> +  os << std::setw(15) << day(15);
> +  VERIFY( os.width() == 0 );
> +}
> +
> +void
> +test_width_zero()
> +{
> +  using namespace std::chrono;
> +  std::ostringstream os;
> +  os << std::setw(0) << sys_time<seconds>{1'700'000'000s};
> +  auto s = os.str();
> +  VERIFY( s.find("2023-11-14") != std::string::npos );
> +  VERIFY( s.size() < 20 );
> +  VERIFY( os.width() == 0 );
> +}
> +
> +void
> +test_multiple_chrono_types()
> +{
> +  using namespace std::chrono;
> +  std::ostringstream os;
> +  os << std::setw(25) << std::setfill('-') <<
> sys_time<seconds>{1'700'000'000s};
> +  VERIFY( os.width() == 0 );
> +  os << std::setw(15) << hh_mm_ss{seconds(45296)};
>
// Could  you add VERIFY for the prefixes (only) here?

> +  VERIFY( os.width() == 0 );
> +  os << std::setw(10) << day(15);
> +  VERIFY( os.width() == 0 );
> +  os << std::setw(10) << month(7);
> +  VERIFY( os.width() == 0 );
> +  os << std::setw(10) << year(2026);
> +  VERIFY( os.width() == 0 );
> +  os << std::setw(12) << Monday;
> +  VERIFY( os.width() == 0 );
> +}
> +
> +void
> +test_locale_aware()
> +{
> +  using namespace std::chrono;
> +  std::ostringstream os;
> +  os.imbue(std::locale::classic());
> +  os << std::setw(12) << Monday;
> +  auto s = os.str();
> +  VERIFY( s.size() >= 12 );
> +  VERIFY( os.width() == 0 );
> +}
> +
> +void
> +test_wchar_t()
> +{
> +  using namespace std::chrono;
> +  std::wostringstream wos;
> +  wos << std::chrono::sys_time<std::chrono::seconds>{1'700'000'000s};
> +  VERIFY( wos.str() == L"2023-11-14 22:13:20" );
> +}
> +
> +void
> +test_weekday_indexed()
> +{
> +  using namespace std::chrono;
> +  std::ostringstream os;
> +  os << Monday[1];
> +  VERIFY( os.str() == "Mon[1]" );
> +  os << Monday[6];  // invalid index
> +  VERIFY( os.str().find("Mon[") != std::string::npos );
> +}
> +
> +int
> +main()
> +{
> +  test_sentry_badbit();
> +  test_sentry_tied();
> +  test_width_fill();
> +  test_width_left();
> +  test_width_internal();
> +  test_width_reset();
> +  test_width_zero();
> +  test_multiple_chrono_types();
> +  test_locale_aware();
> +  test_wchar_t();
> +  test_weekday_indexed();
> +}
> --
> 2.34.1
>
>

Reply via email to