[gcc r13-8152] libstdc++: Backport some std::format test improvements

Jonathan Wakely redi@gcc.gnu.org
Wed Dec 13 09:40:05 GMT 2023


https://gcc.gnu.org/g:3b23038aed019734569ca7d27b3e2c89d0dd0a9a

commit r13-8152-g3b23038aed019734569ca7d27b3e2c89d0dd0a9a
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Dec 12 20:53:08 2023 +0000

    libstdc++: Backport some std::format test improvements
    
    This backports just the tests from r14-6469-g52de6aa1a85822 because the
    regression wasn't present on this branch, but we can still use the
    tests.
    
    libstdc++-v3/ChangeLog:
    
            * testsuite/std/format/functions/format.cc: Check for expected
            output for char and bool arguments.
            * testsuite/std/format/string.cc: Check that 0 filling is
            rejected for character and string formats.
    
    (cherry picked from commit 52de6aa1a8582208b519b6998389d3a801b0de7b)

Diff:
---
 .../testsuite/std/format/functions/format.cc       | 56 ++++++++++++++++++++++
 libstdc++-v3/testsuite/std/format/string.cc        |  3 ++
 2 files changed, 59 insertions(+)

diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc
index b8ad4f1ed29..d3bac2dbd25 100644
--- a/libstdc++-v3/testsuite/std/format/functions/format.cc
+++ b/libstdc++-v3/testsuite/std/format/functions/format.cc
@@ -257,12 +257,42 @@ test_width()
   }
 }
 
+void
+test_char()
+{
+  std::string s;
+
+  s = std::format("{}", 'a');
+  VERIFY( s == "a" );
+
+  s = std::format("{:c} {:d} {:o}", 'b', '\x17', '\x3f');
+  VERIFY( s == "b 23 77" );
+
+  s = std::format("{:#d} {:#o}", '\x17', '\x3f');
+  VERIFY( s == "23 077" );
+
+  s = std::format("{:04d} {:04o}", '\x17', '\x3f');
+  VERIFY( s == "0023 0077" );
+
+  s = std::format("{:b} {:B} {:#b} {:#B}", '\xff', '\xa0', '\x17', '\x3f');
+  if constexpr (std::is_unsigned_v<char>)
+    VERIFY( s == "11111111 10100000 0b10111 0B111111" );
+  else
+    VERIFY( s == "-1 -1100000 0b10111 0B111111" );
+
+  s = std::format("{:x} {:#x} {:#X}", '\x12', '\x34', '\x45');
+  VERIFY( s == "12 0x34 0X45" );
+}
+
 void
 test_wchar()
 {
   using namespace std::literals;
   std::wstring s;
 
+  s = std::format(L"{}", L'a');
+  VERIFY( s == L"a" );
+
   s = std::format(L"{} {} {} {} {} {}", L'0', 1, 2LL, 3.4, L"five", L"six"s);
   VERIFY( s == L"0 1 2 3.4 five six" );
 
@@ -376,6 +406,9 @@ test_pointer()
   const void* pc = p;
   std::string s, str_int;
 
+  s = std::format("{}", p);
+  VERIFY( s == "0x0" );
+
   s = std::format("{} {} {}", p, pc, nullptr);
   VERIFY( s == "0x0 0x0 0x0" );
   s = std::format("{:p} {:p} {:p}", p, pc, nullptr);
@@ -408,6 +441,27 @@ test_pointer()
 #endif
 }
 
+void
+test_bool()
+{
+  std::string s;
+
+  s = std::format("{}", true);
+  VERIFY( s == "true" );
+  s = std::format("{:} {:s}", true, false);
+  VERIFY( s == "true false" );
+  s = std::format("{:b} {:#b}", true, false);
+  VERIFY( s == "1 0b0" );
+  s = std::format("{:B} {:#B}", false, true);
+  VERIFY( s == "0 0B1" );
+  s = std::format("{:d} {:#d}", false, true);
+  VERIFY( s == "0 1" );
+  s = std::format("{:o} {:#o} {:#o}", false, true, false);
+  VERIFY( s == "0 01 0" );
+  s = std::format("{:x} {:#x} {:#X}", false, true, false);
+  VERIFY( s == "0 0x1 0X0" );
+}
+
 int main()
 {
   test_no_args();
@@ -416,9 +470,11 @@ int main()
   test_alternate_forms();
   test_locale();
   test_width();
+  test_char();
   test_wchar();
   test_minmax();
   test_p1652r1();
   test_float128();
   test_pointer();
+  test_bool();
 }
diff --git a/libstdc++-v3/testsuite/std/format/string.cc b/libstdc++-v3/testsuite/std/format/string.cc
index a472f8d588c..d70a2331645 100644
--- a/libstdc++-v3/testsuite/std/format/string.cc
+++ b/libstdc++-v3/testsuite/std/format/string.cc
@@ -110,6 +110,9 @@ test_format_spec()
   VERIFY( ! is_format_string_for("{:#?}", "str") );
   VERIFY( ! is_format_string_for("{:#?}", 'c') );
 
+  VERIFY( ! is_format_string_for("{:0c}", 'c') );
+  VERIFY( ! is_format_string_for("{:0s}", true) );
+
   // Precision only valid for string and floating-point types.
   VERIFY( ! is_format_string_for("{:.3d}", 1) );
   VERIFY( ! is_format_string_for("{:3.3d}", 1) );


More information about the Gcc-cvs mailing list