Back to index

glibc  2.9
tst-numeric.c
Go to the documentation of this file.
00001 /* Testing the implementation of LC_NUMERIC and snprintf().
00002    Copyright (C) 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Petter Reinholdtsen <pere@hungry.com>, 2003
00005 
00006    Based on tst-fmon.c by Jochen Hein <jochen.hein@delphi.central.de>, 1997.
00007 
00008    The GNU C Library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Lesser General Public
00010    License as published by the Free Software Foundation; either
00011    version 2.1 of the License, or (at your option) any later version.
00012 
00013    The GNU C Library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Lesser General Public License for more details.
00017 
00018    You should have received a copy of the GNU Lesser General Public
00019    License along with the GNU C Library; if not, write to the Free
00020    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00021    02111-1307 USA.  */
00022 
00023 #include <stdio.h>
00024 #include <locale.h>
00025 #include <string.h>
00026 #include <stdlib.h>
00027 
00028 /*
00029   test-numeric gets called with three parameters:
00030    - the locale
00031    - the format-string to be used
00032    - the actual number to be formatted
00033    - the expected string
00034    If the test passes, test-numeric terminates with returncode 0,
00035    otherwise with 1
00036 */
00037 #define EXIT_SUCCESS 0
00038 #define EXIT_FAILURE 1
00039 #define EXIT_SETLOCALE 2
00040 #define EXIT_SNPRINTF 3
00041 
00042 int
00043 main (int argc, char *argv[])
00044 {
00045   char *s = malloc (201);
00046   double val;
00047 
00048   /* Make sure to read the value before setting of the locale, as
00049      strtod() is locale-dependent. */
00050   val = strtod (argv[3], NULL);
00051 
00052   if (setlocale (LC_ALL, argv[1]) == NULL)
00053     {
00054       fprintf (stderr, "setlocale(LC_ALL, \"%s\"): %m\n", argv[1]);
00055       exit (EXIT_SETLOCALE);
00056     }
00057 
00058   if (snprintf (s, 200, argv[2], val) == -1)
00059     {
00060       perror ("snprintf");
00061       exit (EXIT_SNPRINTF);
00062     }
00063 
00064   if (strcmp (s, argv[4]) != 0)
00065     {
00066       printf ("\
00067 locale: \"%s\", format: \"%s\", expected: \"%s\", got: \"%s\" => %s\n",
00068              argv[1], argv[2], argv[4], s,
00069              strcmp (s, argv[4]) != 0 ? "false" : "correct");
00070       exit (EXIT_FAILURE);
00071     }
00072 
00073   return EXIT_SUCCESS;
00074 }