(root)/
coreutils-9.4/
gnulib-tests/
test-verror.c
       1  /* Test of verror.h functions.
       2     Copyright (C) 2023 Free Software Foundation, Inc.
       3  
       4     This program is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation, either version 3 of the License, or
       7     (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  /* Written by Bruno Haible <bruno@clisp.org>, 2023.  */
      18  
      19  #include <config.h>
      20  
      21  #include "verror.h"
      22  
      23  #include <errno.h>
      24  #include <stdarg.h>
      25  
      26  #include "error.h"
      27  #include "macros.h"
      28  
      29  /* Custom function to not show the program name in error messages.  */
      30  static void
      31  print_no_progname (void)
      32  {
      33  }
      34  
      35  static void
      36  test_zero (const char *format, ...)
      37  {
      38    va_list args;
      39  
      40    va_start (args, format);
      41    verror (0, 0, format, args);
      42    va_end (args);
      43  }
      44  
      45  static void
      46  test_zero_at_line (const char *filename, unsigned int lineno,
      47                     const char *format, ...)
      48  {
      49    va_list args;
      50  
      51    va_start (args, format);
      52    verror_at_line (0, 0, filename, lineno, format, args);
      53    va_end (args);
      54  }
      55  
      56  static void
      57  test_errnum (const char *format, ...)
      58  {
      59    va_list args;
      60  
      61    va_start (args, format);
      62    verror (0, EACCES, format, args);
      63    va_end (args);
      64  }
      65  
      66  static void
      67  test_fatal (const char *format, ...)
      68  {
      69    va_list args;
      70  
      71    va_start (args, format);
      72    verror (4, 0, format, args);
      73    va_end (args);
      74  }
      75  
      76  int
      77  main (int argc, char *argv[])
      78  {
      79    /* Test verror() function with zero STATUS and zero ERRNUM.  */
      80    test_zero ("bummer");
      81    /* With format string arguments.  */
      82    errno = EINVAL; /* should be ignored */
      83    test_zero ("Zonk %d%d%d is too large", 1, 2, 3);
      84    /* With non-ASCII characters.  */
      85    test_zero ("Pokémon started");
      86    /* Verify error_message_count.  */
      87    ASSERT (error_message_count == 3);
      88  
      89    /* Test verror_at_line() function with zero STATUS and zero ERRNUM.  */
      90    test_zero_at_line ("d1/foo.c", 10, "invalid blub");
      91    test_zero_at_line ("d1/foo.c", 10, "invalid blarn");
      92    /* Verify error_message_count.  */
      93    ASSERT (error_message_count == 5);
      94  
      95    /* Test error_one_per_line.  */
      96    error_one_per_line = 1;
      97    test_zero_at_line ("d1/foo.c", 10, "unsupported glink");
      98    /* Another line number.  */
      99    test_zero_at_line ("d1/foo.c", 13, "invalid brump");
     100    /* Another file name.  */
     101    test_zero_at_line ("d2/foo.c", 13, "unsupported flinge");
     102    /* Same file name and same line number => message not shown.  */
     103    test_zero_at_line ("d2/foo.c", 13, "invalid bark");
     104    /* Verify error_message_count.  */
     105    ASSERT (error_message_count == 8);
     106    error_one_per_line = 0;
     107  
     108    /* Test error_print_progname.  */
     109    error_print_progname = print_no_progname;
     110    test_zero ("hammer");
     111    test_zero ("boing %d%d%d is too large", 1, 2, 3);
     112    #if 0
     113    /* The documentation does not describe the output if the file name is NULL. */
     114    test_zero_at_line (NULL, 42, "drummer too loud");
     115    #endif
     116    test_zero_at_line ("d2/bar.c", 11, "bark too loud");
     117    /* Verify error_message_count.  */
     118    ASSERT (error_message_count == 11);
     119    error_print_progname = NULL;
     120  
     121    /* Test verror() function with nonzero ERRNUM.  */
     122    errno = EINVAL; /* should be ignored */
     123    test_errnum ("can't steal");
     124    /* Verify error_message_count.  */
     125    ASSERT (error_message_count == 12);
     126  
     127    /* Test verror() function with nonzero STATUS.  */
     128    test_fatal ("fatal error");
     129  
     130    return 0;
     131  }