From patchwork Fri Oct 2 11:50:34 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tomasz Kaminski X-Patchwork-Id: 145152 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A3ADF4BB24D1 for ; Fri, 2 Oct 2026 11:52:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A3ADF4BB24D1 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=YigxKtRQ X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 24C1F4B9DB4B for ; Fri, 2 Oct 2026 11:51:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 24C1F4B9DB4B Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 24C1F4B9DB4B Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790941895; cv=none; b=QbYZYHAx0mOMPAJgVB1C+eJHf1G+IvGnHV/l9OEFmsT9FJ0W6FfHBQTS/qalgogzzRnA2sOdttWuf+bXj6Vc7nks2TjB9meYu/n2lISvX+QMhCCQX0WKCHho+z2AbtP+u1/EIjSS127Arv8o3f33/Mwmpi9bnaHCvnLeQIqSymw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790941895; c=relaxed/simple; bh=Nk5B4yKix5TprpV23QlXVNU5n8Ss0b1wofQ02DE9UHk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lqxtlobYhbsA8cq9fagEjdIAUPRRuBAu7hlcz30opSNm81oqCiW2DPIAl6nGMgsMqiTH834eVn1Qd8e8fD4zg+FoWF3PCJ+FJ5PEspI62p6i44gwsmmBE2gASC8jk+ZMNzk8F4ZnHzaKlDMqkbDgqf+zYdRMVhawz39rVlLN5p4= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=YigxKtRQ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 24C1F4B9DB4B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1790941894; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eafLTJ2Vn7G9qpKRIe1i8F4o9zQ52+K+XT+pg8tvRtc=; b=YigxKtRQ/R528YgEJT0K61OEgRq/u03oeeylOaST6bXIZuIqUss/thHyCa1ghK9TjPEC19 T+kvzBwkvCVQIrvTyd+3Oi8OkeCJi4sGua8Rdyj5Jr9eKKSdEdI9UrbLs7OxNHh1yh0N/v 2vxN+XZmpOC/kfAFMEDhVKTPTsMfeTI= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-580-nZv0pw2UPQKggr5AruusZQ-1; Fri, 02 Oct 2026 07:51:32 -0400 X-MC-Unique: nZv0pw2UPQKggr5AruusZQ-1 X-Mimecast-MFC-AGG-ID: nZv0pw2UPQKggr5AruusZQ_1790941891 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 82D7C180058C; Fri, 2 Oct 2026 11:51:31 +0000 (UTC) Received: from localhost (headnet05.pony-001.prod.iad2.dc.redhat.com [10.2.32.117]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id F235F1956087; Fri, 2 Oct 2026 11:51:30 +0000 (UTC) From: =?utf-8?q?Tomasz_Kami=C5=84ski?= To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Anlai Lu Subject: [PATCH v4 1/4] libstdc++: Add tests for format_to with ostreambuf_iterator Date: Fri, 2 Oct 2026 13:50:34 +0200 Message-ID: <20261002115129.1005743-1-tkaminsk@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: dWSvI2llwrkkOVHEs-9fmtHzW4Yy19-_Y-3HbVSfmm4_1790941891 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org From: Anlai Lu Cover format_to, format_to_n truncation, writing to sentinel, write failure, and large writes that exceed the internal buffer. These paths were previously untested. Additionally includes examples of formatters that observe if the output is buffered (string is created by call to format), before inserting to stream. libstdc++-v3/ChangeLog: * estsuite/27_io/basic_ostream/print/buffered.cc: New test. * testsuite/std/format/functions/format_to_ostreambuf.cc: New test. Co-authored-by: Tomasz Kamiński Signed-off-by: Anlai Lu Signed-off-by: Tomasz Kamiński --- v4 add test for writting to sentinel. Rested the new test file. OK for trunk? Why we want this backported to 16? .../27_io/basic_ostream/print/buffered.cc | 90 +++++ .../format/functions/format_to_ostreambuf.cc | 377 ++++++++++++++++++ 2 files changed, 467 insertions(+) create mode 100644 libstdc++-v3/testsuite/27_io/basic_ostream/print/buffered.cc create mode 100644 libstdc++-v3/testsuite/std/format/functions/format_to_ostreambuf.cc diff --git a/libstdc++-v3/testsuite/27_io/basic_ostream/print/buffered.cc b/libstdc++-v3/testsuite/27_io/basic_ostream/print/buffered.cc new file mode 100644 index 00000000000..04ef159db39 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_ostream/print/buffered.cc @@ -0,0 +1,90 @@ +// { dg-additional-options "-lstdc++exp" { target { *-*-mingw* } } } +// { dg-do run { target c++23 } } + +#include +#include +#include + +struct ThrowingFormat +{ + std::string val; + bool throw_from_format = false; +}; + +template<> +struct std::formatter +{ + constexpr + std::format_parse_context::iterator + parse(std::format_parse_context& ctx) const + { return ctx.begin(); } + + template + Out + format(const ThrowingFormat& t, std::basic_format_context& fc) const + { + if (t.throw_from_format) + throw std::logic_error("Formatting stopped"); + return std::ranges::copy(t.val, fc.out()).out; + } +}; + +void +test_throwing() +{ + std::string s; + s.reserve(100); + std::ostringstream out(std::move(s)); + ThrowingFormat tf1{std::string(500, 'a'), false}, tf2{std::string(500, 'b'), true}; + + try + { + std::print(out, "{} {}", tf1, tf2); + VERIFY(false); + } catch (...) { + VERIFY(true); + } + VERIFY( out.view().empty() ); +} + +struct MixedFormat +{ + std::ostream* out; + std::string val; +}; + +template<> +struct std::formatter +{ + constexpr + std::format_parse_context::iterator + parse(std::format_parse_context& ctx) const + { return ctx.begin(); } + + template + Out + format(const MixedFormat& t, std::basic_format_context& fc) const + { + if (t.out) + *t.out << "<<[" << t.val << "]"; + return std::ranges::copy(t.val, fc.out()).out; + } +}; + +void +test_interleaved() +{ + std::string s; + s.reserve(100); + std::ostringstream out(std::move(s)); + MixedFormat mf1{&out, "abc"}, mf2{&out, "DEF"}; + + std::print(out, "f[{}]f[{}]", mf1, mf2); + VERIFY( out.view() == "<<[abc]<<[DEF]f[abc]f[DEF]" ); +} + +int main() +{ + test_throwing(); + test_interleaved(); +} diff --git a/libstdc++-v3/testsuite/std/format/functions/format_to_ostreambuf.cc b/libstdc++-v3/testsuite/std/format/functions/format_to_ostreambuf.cc new file mode 100644 index 00000000000..81ac3a6c3d9 --- /dev/null +++ b/libstdc++-v3/testsuite/std/format/functions/format_to_ostreambuf.cc @@ -0,0 +1,377 @@ +// { dg-do run { target c++20 } } + +#include +#include +#include +#include + +template +struct ShortWriteStreambuf : std::streambuf +{ + std::string data; + + std::streamsize + xsputn(const char_type* s, std::streamsize n) override + { + std::streamsize limit = std::max(0, BufSize - data.size()); + std::streamsize to_write = std::min(n, limit); + if (to_write > 0) + data.append(s, to_write); + return to_write; + } + + int_type + overflow(int_type c) override + { + if (data.size() < BufSize) + { + data.push_back(traits_type::to_char_type(c)); + return c; + } + return traits_type::eof(); + } +}; + +bool +check_suffix(std::string_view s, size_t n, char c) +{ + if (s.size() < n) + return false; + + s.remove_prefix(s.size() - n); + for (char v : s) + if (v != c) + return !n; + else if (!n) + return false; + else + --n; + return !n; +} + +void +test_format_to() +{ + { + std::ostringstream os; + std::format_to(std::ostreambuf_iterator(os), "{}", 42); + VERIFY( os.view() == "42" ); + } + + { + std::ostringstream os; + std::string big(800, 'y'); + std::format_to(std::ostreambuf_iterator(os), "{}", big); + VERIFY( check_suffix(os.view(), 800, 'y') ); + } + + { + std::ostringstream os; + std::string part(700, 'P'); + std::format_to(std::ostreambuf_iterator(os), + "{}{}{}", part, part, part); + VERIFY( check_suffix(os.view(), 2100, 'P') ); + } + + { + std::ostringstream os; + std::string body(400, 'B'); + std::format_to(std::ostreambuf_iterator(os), + "header-{}-footer", body); + + std::string_view sv = os.view(); + VERIFY( sv.starts_with("header-") ); + sv.remove_prefix(7); + VERIFY( sv.ends_with("-footer") ); + sv.remove_suffix(7); + VERIFY( check_suffix(sv, 400, 'B') ); + } +} + +void +test_format_to_n() +{ + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 1, "{}", "A"); + VERIFY( res.size == 1 ); + VERIFY( os.view() == "A" ); + } + + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 1, "{}", "AAA"); + VERIFY( res.size == 3 ); + VERIFY( os.view() == "A" ); + } + + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 3, "{}", "hello"); + VERIFY( res.size == 5 ); + VERIFY( os.view() == "hel" ); + } + + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 10, "{}", std::string(100, 'x')); + VERIFY( res.size == 100 ); + VERIFY( check_suffix(os.view(), 10, 'x') ); + } + + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 10, "{}", std::string(1000, 'x')); + VERIFY( res.size == 1000 ); + VERIFY( check_suffix(os.view(), 10, 'x') ); + } + + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 256, "{}", std::string(1000, 'x')); + VERIFY( res.size == 1000 ); + VERIFY( check_suffix(os.view(), 256, 'x') ); + } + + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 257, "{}", std::string(1000, 'x')); + VERIFY( res.size == 1000 ); + VERIFY( check_suffix(os.view(), 257, 'x') ); + } + + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 8, "{}{}{}", "AAAA", "BBBB", "CCCC"); + VERIFY( res.size == 12 ); + VERIFY( os.view() == "AAAABBBB" ); + } + + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 2, "{}{}", "AAAA", "BBBB"); + VERIFY( res.size == 8 ); + VERIFY( os.view() == "AA" ); + } + + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 8, "{}{}", "AAAA", "BBBB"); + VERIFY( res.size == 8 ); + VERIFY( os.view() == "AAAABBBB" ); + } + + { + std::string big(257, 'A'); + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 257, "{}", big); + VERIFY( res.size == 257 ); + std::string_view sv = os.view(); + VERIFY( os.view() == big ); + } + + { + std::string big(300, 'A'); + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 257, "{}", big); + VERIFY( res.size == 300 ); + VERIFY( check_suffix(os.view(), 257, 'A') ); + } +} + +void +test_format_to_n_padding() +{ + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 3, "{:>10}", "hello"); + VERIFY( res.size == 10 ); + VERIFY( os.view().size() == 3 ); + } + + { + std::ostringstream os; + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 3, "{:.5}", std::string(100, 'z')); + VERIFY( res.size == 5 ); + VERIFY( check_suffix(os.view(), 3, 'z') ); + } +} + +void +test_format_to_n_prefilled() +{ + // Pre-filled ostringstream: after seekp(0) the streambuf has a put + // area pointing into existing data. This exercises the + // _M_use_put_area code path combined with format_to_n truncation. + { + std::string init(100, 'Z'); + std::ostringstream os(init); + os.seekp(0); + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 5, "{}", "1234567890"); + VERIFY( res.size == 10 ); + VERIFY( os.view().substr(0, 10) == "12345ZZZZZ" ); + VERIFY( check_suffix(os.view(), 90, 'Z') ); + } + { + std::string init(100, 'Z'); + std::ostringstream os(init); + os.seekp(0); + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 8, "{:=>15}", "1234567890"); + VERIFY( res.size == 15 ); + VERIFY( os.view().substr(0, 10) == "=====123ZZ" ); + VERIFY( check_suffix(os.view(), 90, 'Z') ); + } + { + std::string init(100, 'Z'); + std::ostringstream os(init); + os.seekp(0); + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 3, "{}", 12345); + VERIFY( res.size == 5 ); + VERIFY( os.view().substr(0, 10) == "123ZZZZZZZ" ); + VERIFY( check_suffix(os.view(), 90, 'Z') ); + } + { + std::string init(100, 'Z'); + std::ostringstream os(init); + os.seekp(0); + auto res = std::format_to_n(std::ostreambuf_iterator(os), + 7, "{:=>10}", 12345); + VERIFY( res.size == 10 ); + VERIFY( os.view().substr(0, 10) == "=====12ZZZ" ); + VERIFY( check_suffix(os.view(), 90, 'Z') ); + } +} + +void +test_format_to_sentinel() +{ + // Test cases writting of default constructed (sentinel) + // ostream_iterator. No characters can be written. + const std::ostreambuf_iterator sent; + { + auto res = std::format_to(sent, ""); + VERIFY( res.failed() ); + } + { + auto res = std::format_to(sent, "{}", ""); + VERIFY( res.failed() ); + } + { + auto res = std::format_to_n(sent, 0, ""); + VERIFY( res.out.failed() ); + VERIFY( res.size == 0 ); + } + { + auto res = std::format_to_n(sent, 0, "{}", ""); + VERIFY( res.out.failed() ); + VERIFY( res.size == 0 ); + } + { + auto res = std::format_to_n(sent, 0, "{}", "abc"); + VERIFY( res.out.failed() ); + VERIFY( res.size == 3 ); + } + { + auto res = std::format_to_n(sent, 0, "{}", "10"); + VERIFY( res.out.failed() ); + VERIFY( res.size == 2 ); + } +} + +void +test_write_failure() +{ + const std::string payload(20, 'Z'); + + { + ShortWriteStreambuf<20> buf; + auto it = std::ostreambuf_iterator(&buf); + auto res = std::format_to(it, "{}", payload); + VERIFY( !res.failed() ); + VERIFY( check_suffix(buf.data, 20, 'Z') ); + } + + { + ShortWriteStreambuf<19> buf; + auto it = std::ostreambuf_iterator(&buf); + auto res = std::format_to(it, "{}", payload); + VERIFY( res.failed() ); + VERIFY( check_suffix(buf.data, 19, 'Z') ); + } + + { + ShortWriteStreambuf<10> buf; + auto it = std::ostreambuf_iterator(&buf); + auto res = std::format_to_n(it, 10, "{}", payload); + VERIFY( !res.out.failed() ); + VERIFY( res.size == 20 ); + VERIFY( check_suffix(buf.data, 10, 'Z') ); + } + + { + ShortWriteStreambuf<10> buf; + auto it = std::ostreambuf_iterator(&buf); + auto res = std::format_to_n(it, 11, "{}", payload); + VERIFY( res.out.failed() ); + VERIFY( res.size == 20 ); + VERIFY( check_suffix(buf.data, 10, 'Z') ); + } + + { + ShortWriteStreambuf<100> buf; + auto it = std::ostreambuf_iterator(&buf); + auto res = std::format_to(it, "{}", std::string(300, 'Y')); + VERIFY( res.failed() ); + VERIFY( buf.data.size() == 100 ); + VERIFY( check_suffix(buf.data, 100, 'Y') ); + } + // format_to_n after write failure: size must still be the total + // number of characters that would have been produced. + { + ShortWriteStreambuf<50> buf; + auto it = std::ostreambuf_iterator(&buf); + auto res = std::format_to_n(it, 100, + "{}{}{}", + std::string(20, 'A'), + std::string(30, 'B'), + std::string(40, 'C')); + // The first two arguments fill the buffer (20 + 30 == 50). + // The third argument (40 C's) triggers write failure and the + // remaining chars must still be counted in res.size. + VERIFY( res.out.failed() ); + VERIFY( res.size == 90 ); + + std::string_view sv = buf.data; + VERIFY( buf.data.size() == 50 ); + VERIFY( check_suffix(sv, 30, 'B') ); + sv.remove_suffix(30); + VERIFY( check_suffix(sv, 20, 'A') ); + } +} + +int main() +{ + test_format_to(); + test_format_to_n(); + test_format_to_n_padding(); + test_format_to_n_prefilled(); + test_format_to_sentinel(); + test_write_failure(); +}