(root)/
glibc-2.38/
stdio-common/
tst-printf-binary-main.c
       1  /* Test binary printf formats.
       2     Copyright (C) 2021-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <inttypes.h>
      20  #include <limits.h>
      21  #include <stdio.h>
      22  #include <string.h>
      23  #include <wchar.h>
      24  
      25  #include <libc-diag.h>
      26  #include <support/check.h>
      27  
      28  /* GCC does not know the %b or %B formats before GCC 12.  */
      29  DIAG_PUSH_NEEDS_COMMENT;
      30  #if !__GNUC_PREREQ (12, 0)
      31  DIAG_IGNORE_NEEDS_COMMENT (11, "-Wformat");
      32  DIAG_IGNORE_NEEDS_COMMENT (11, "-Wformat-extra-args");
      33  #endif
      34  
      35  #define CHECK_PRINTF(EXPECTED, FMT, ...)				\
      36    do									\
      37      {									\
      38        int ret = SNPRINTF (buf, sizeof buf / sizeof buf[0], L_(FMT),	\
      39  			  __VA_ARGS__);					\
      40        TEST_COMPARE_STRING_MACRO (buf, L_(EXPECTED));			\
      41        TEST_COMPARE (ret, STRLEN (L_(EXPECTED)));			\
      42      }									\
      43    while (0)
      44  
      45  static int
      46  do_test (void)
      47  {
      48    CHAR buf[1024];
      49    CHECK_PRINTF ("0", "%b", 0u);
      50    CHECK_PRINTF ("0", "%B", 0u);
      51    CHECK_PRINTF ("0", "%#b", 0u);
      52    CHECK_PRINTF ("0", "%#B", 0u);
      53    CHECK_PRINTF ("1", "%b", 1u);
      54    CHECK_PRINTF ("1", "%B", 1u);
      55    CHECK_PRINTF ("10", "%b", 2u);
      56    CHECK_PRINTF ("10", "%B", 2u);
      57    CHECK_PRINTF ("11", "%b", 3u);
      58    CHECK_PRINTF ("11", "%B", 3u);
      59    CHECK_PRINTF ("10000111011001010100001100100001", "%b", 0x87654321);
      60    CHECK_PRINTF ("10000111011001010100001100100001", "%B", 0x87654321);
      61    CHECK_PRINTF ("100001100100001", "%hb", (int) 0x87654321);
      62    CHECK_PRINTF ("100001100100001", "%hB", (int) 0x87654321);
      63    CHECK_PRINTF ("100001", "%hhb", (int) 0x87654321);
      64    CHECK_PRINTF ("100001", "%hhB", (int) 0x87654321);
      65    CHECK_PRINTF ("10000111011001010100001100100001", "%lb", 0x87654321ul);
      66    CHECK_PRINTF ("10000111011001010100001100100001", "%lB", 0x87654321ul);
      67    CHECK_PRINTF ("11111110110111001011101010011001"
      68  		"10000111011001010100001100100001", "%llb",
      69  		0xfedcba9987654321ull);
      70    CHECK_PRINTF ("11111110110111001011101010011001"
      71  		"10000111011001010100001100100001", "%llB",
      72  		0xfedcba9987654321ull);
      73  #if LONG_WIDTH >= 64
      74    CHECK_PRINTF ("11111110110111001011101010011001"
      75  		"10000111011001010100001100100001", "%lb",
      76  		0xfedcba9987654321ul);
      77    CHECK_PRINTF ("11111110110111001011101010011001"
      78  		"10000111011001010100001100100001", "%lB",
      79  		0xfedcba9987654321ul);
      80  #endif
      81    CHECK_PRINTF ("0b11", "%#" PRIb8, (uint8_t) 3);
      82    CHECK_PRINTF ("0b11", "%#" PRIb16, (uint16_t) 3);
      83    CHECK_PRINTF ("0b10000111011001010100001100100001", "%#" PRIb32,
      84  		(uint32_t) 0x87654321);
      85    CHECK_PRINTF ("0b11111110110111001011101010011001"
      86  		"10000111011001010100001100100001", "%#" PRIb64,
      87  		(uint64_t) 0xfedcba9987654321ull);
      88    CHECK_PRINTF ("0b11", "%#" PRIbLEAST8, (uint_least8_t) 3);
      89    CHECK_PRINTF ("0b11", "%#" PRIbLEAST16, (uint_least16_t) 3);
      90    CHECK_PRINTF ("0b10000111011001010100001100100001", "%#" PRIbLEAST32,
      91  		(uint_least32_t) 0x87654321);
      92    CHECK_PRINTF ("0b11111110110111001011101010011001"
      93  		"10000111011001010100001100100001", "%#" PRIbLEAST64,
      94  		(uint_least64_t) 0xfedcba9987654321ull);
      95    CHECK_PRINTF ("0b11", "%#" PRIbFAST8, (uint_fast8_t) 3);
      96    CHECK_PRINTF ("0b11", "%#" PRIbFAST16, (uint_fast16_t) 3);
      97    CHECK_PRINTF ("0b10000111011001010100001100100001", "%#" PRIbFAST32,
      98  		(uint_fast32_t) 0x87654321);
      99    CHECK_PRINTF ("0b11111110110111001011101010011001"
     100  		"10000111011001010100001100100001", "%#" PRIbFAST64,
     101  		(uint_fast64_t) 0xfedcba9987654321ull);
     102    CHECK_PRINTF ("0b10000111011001010100001100100001", "%#" PRIbPTR,
     103  		(uintptr_t) 0x87654321);
     104    CHECK_PRINTF ("0b11111110110111001011101010011001"
     105  		"10000111011001010100001100100001", "%#" PRIbMAX,
     106  		(uintmax_t) 0xfedcba9987654321ull);
     107    CHECK_PRINTF ("0B11", "%#" PRIB8, (uint8_t) 3);
     108    CHECK_PRINTF ("0B11", "%#" PRIB16, (uint16_t) 3);
     109    CHECK_PRINTF ("0B10000111011001010100001100100001", "%#" PRIB32,
     110  		(uint32_t) 0x87654321);
     111    CHECK_PRINTF ("0B11111110110111001011101010011001"
     112  		"10000111011001010100001100100001", "%#" PRIB64,
     113  		(uint64_t) 0xfedcba9987654321ull);
     114    CHECK_PRINTF ("0B11", "%#" PRIBLEAST8, (uint_least8_t) 3);
     115    CHECK_PRINTF ("0B11", "%#" PRIBLEAST16, (uint_least16_t) 3);
     116    CHECK_PRINTF ("0B10000111011001010100001100100001", "%#" PRIBLEAST32,
     117  		(uint_least32_t) 0x87654321);
     118    CHECK_PRINTF ("0B11111110110111001011101010011001"
     119  		"10000111011001010100001100100001", "%#" PRIBLEAST64,
     120  		(uint_least64_t) 0xfedcba9987654321ull);
     121    CHECK_PRINTF ("0B11", "%#" PRIBFAST8, (uint_fast8_t) 3);
     122    CHECK_PRINTF ("0B11", "%#" PRIBFAST16, (uint_fast16_t) 3);
     123    CHECK_PRINTF ("0B10000111011001010100001100100001", "%#" PRIBFAST32,
     124  		(uint_fast32_t) 0x87654321);
     125    CHECK_PRINTF ("0B11111110110111001011101010011001"
     126  		"10000111011001010100001100100001", "%#" PRIBFAST64,
     127  		(uint_fast64_t) 0xfedcba9987654321ull);
     128    CHECK_PRINTF ("0B10000111011001010100001100100001", "%#" PRIBPTR,
     129  		(uintptr_t) 0x87654321);
     130    CHECK_PRINTF ("0B11111110110111001011101010011001"
     131  		"10000111011001010100001100100001", "%#" PRIBMAX,
     132  		(uintmax_t) 0xfedcba9987654321ull);
     133    CHECK_PRINTF (" 1010", "%5b", 10u);
     134    CHECK_PRINTF (" 1010", "%5B", 10u);
     135    CHECK_PRINTF ("01010", "%05b", 10u);
     136    CHECK_PRINTF ("01010", "%05B", 10u);
     137    CHECK_PRINTF ("1011 ", "%-5b", 11u);
     138    CHECK_PRINTF ("1011 ", "%-5B", 11u);
     139    CHECK_PRINTF ("0b10011", "%#b", 19u);
     140    CHECK_PRINTF ("0B10011", "%#B", 19u);
     141    CHECK_PRINTF ("   0b10011", "%#10b", 19u);
     142    CHECK_PRINTF ("   0B10011", "%#10B", 19u);
     143    CHECK_PRINTF ("0b00010011", "%0#10b", 19u);
     144    CHECK_PRINTF ("0B00010011", "%0#10B", 19u);
     145    CHECK_PRINTF ("0b00010011", "%#010b", 19u);
     146    CHECK_PRINTF ("0B00010011", "%#010B", 19u);
     147    CHECK_PRINTF ("0b10011   ", "%#-10b", 19u);
     148    CHECK_PRINTF ("0B10011   ", "%#-10B", 19u);
     149    CHECK_PRINTF ("00010011", "%.8b", 19u);
     150    CHECK_PRINTF ("00010011", "%.8B", 19u);
     151    CHECK_PRINTF ("0b00010011", "%#.8b", 19u);
     152    CHECK_PRINTF ("0B00010011", "%#.8B", 19u);
     153    CHECK_PRINTF ("       00010011", "%15.8b", 19u);
     154    CHECK_PRINTF ("       00010011", "%15.8B", 19u);
     155    CHECK_PRINTF ("00010011       ", "%-15.8b", 19u);
     156    CHECK_PRINTF ("00010011       ", "%-15.8B", 19u);
     157    CHECK_PRINTF ("     0b00010011", "%#15.8b", 19u);
     158    CHECK_PRINTF ("     0B00010011", "%#15.8B", 19u);
     159    CHECK_PRINTF ("0b00010011     ", "%-#15.8b", 19u);
     160    CHECK_PRINTF ("0B00010011     ", "%-#15.8B", 19u);
     161    /* GCC diagnoses ignored flags.  */
     162    DIAG_PUSH_NEEDS_COMMENT;
     163    DIAG_IGNORE_NEEDS_COMMENT (12, "-Wformat");
     164    /* '0' flag ignored with '-'.  */
     165    CHECK_PRINTF ("1011 ", "%0-5b", 11u);
     166    CHECK_PRINTF ("1011 ", "%0-5B", 11u);
     167    CHECK_PRINTF ("0b10011   ", "%#0-10b", 19u);
     168    CHECK_PRINTF ("0B10011   ", "%#0-10B", 19u);
     169    /* '0' flag ignored with precision.  */
     170    CHECK_PRINTF ("       00010011", "%015.8b", 19u);
     171    CHECK_PRINTF ("       00010011", "%015.8B", 19u);
     172    CHECK_PRINTF ("     0b00010011", "%0#15.8b", 19u);
     173    CHECK_PRINTF ("     0B00010011", "%0#15.8B", 19u);
     174    DIAG_POP_NEEDS_COMMENT;
     175    /* Test positional argument handling.  */
     176    CHECK_PRINTF ("test 1011 test2 100010001000100010001000100010001",
     177  		"%2$s %1$b %4$s %3$llb", 11u, "test", 0x111111111ull, "test2");
     178    return 0;
     179  }
     180  
     181  DIAG_POP_NEEDS_COMMENT;
     182  
     183  #include <support/test-driver.c>