tst-numeric.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Testing the implementation of LC_NUMERIC and snprintf().
  2. Copyright (C) 2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Petter Reinholdtsen <pere@hungry.com>, 2003
  5. Based on tst-fmon.c by Jochen Hein <jochen.hein@delphi.central.de>, 1997.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  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. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. #include <stdio.h>
  18. #include <locale.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. /*
  22. test-numeric gets called with three parameters:
  23. - the locale
  24. - the format-string to be used
  25. - the actual number to be formatted
  26. - the expected string
  27. If the test passes, test-numeric terminates with returncode 0,
  28. otherwise with 1
  29. */
  30. #define EXIT_SUCCESS 0
  31. #define EXIT_FAILURE 1
  32. #define EXIT_SETLOCALE 2
  33. #define EXIT_SNPRINTF 3
  34. int
  35. main (int argc, char *argv[])
  36. {
  37. char *s = malloc (201);
  38. double val;
  39. /* Make sure to read the value before setting of the locale, as
  40. strtod() is locale-dependent. */
  41. val = strtod (argv[3], NULL);
  42. if (setlocale (LC_ALL, argv[1]) == NULL)
  43. {
  44. fprintf (stderr, "setlocale(LC_ALL, \"%s\"): %m\n", argv[1]);
  45. exit (EXIT_SETLOCALE);
  46. }
  47. if (snprintf (s, 200, argv[2], val) == -1)
  48. {
  49. perror ("snprintf");
  50. exit (EXIT_SNPRINTF);
  51. }
  52. if (strcmp (s, argv[4]) != 0)
  53. {
  54. printf ("\
  55. locale: \"%s\", format: \"%s\", expected: \"%s\", got: \"%s\" => %s\n",
  56. argv[1], argv[2], argv[4], s,
  57. strcmp (s, argv[4]) != 0 ? "false" : "correct");
  58. exit (EXIT_FAILURE);
  59. }
  60. return EXIT_SUCCESS;
  61. }