tst-ldbl-argp.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Testing of long double conversions in argp.h functions.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <argp.h>
  16. #include <string.h>
  17. #include <support/capture_subprocess.h>
  18. #include <support/check.h>
  19. static const struct argp_option
  20. options[] =
  21. {
  22. { "error", 'e', "format", OPTION_ARG_OPTIONAL,
  23. "Run argp_error function with a format string", 0 },
  24. { "failure", 'f', "format", OPTION_ARG_OPTIONAL,
  25. "Run argp_failure function with a format string", 0 },
  26. { NULL, 0, NULL, 0, NULL }
  27. };
  28. static error_t
  29. parser (int key, char *arg, struct argp_state *state)
  30. {
  31. switch (key)
  32. {
  33. case 'e':
  34. argp_error (state, "%Lf%f%Lf%f", (long double) -1, (double) -2,
  35. (long double) -3, (double) -4);
  36. break;
  37. case 'f':
  38. argp_failure (state, 0, 0, "%Lf%f%Lf%f", (long double) -1,
  39. (double) -2, (long double) -3, (double) -4);
  40. break;
  41. default:
  42. return ARGP_ERR_UNKNOWN;
  43. }
  44. return 0;
  45. }
  46. static struct argp
  47. argp =
  48. {
  49. options, parser
  50. };
  51. int argc = 2;
  52. char *argv[3] = { (char *) "test-argp", NULL, NULL };
  53. static void
  54. do_test_call (void)
  55. {
  56. int remaining;
  57. argp_parse (&argp, argc, argv, 0, &remaining, NULL);
  58. }
  59. static int
  60. do_one_test (const char *expected)
  61. {
  62. struct support_capture_subprocess result;
  63. result = support_capture_subprocess ((void *) &do_test_call, NULL);
  64. TEST_COMPARE_STRING (result.err.buffer, expected);
  65. return 0;
  66. }
  67. static int
  68. do_test (void)
  69. {
  70. const char *param_error = "--error";
  71. const char *expected_error =
  72. "test-argp: -1.000000-2.000000-3.000000-4.000000\n"
  73. "Try `test-argp --help' or `test-argp --usage' for more information.\n";
  74. const char *param_failure = "--failure";
  75. const char *expected_failure =
  76. "test-argp: -1.000000-2.000000-3.000000-4.000000\n";
  77. argv[1] = (char *) param_error;
  78. do_one_test (expected_error);
  79. argv[1] = (char *) param_failure;
  80. do_one_test (expected_failure);
  81. return 0;
  82. }
  83. #include <support/test-driver.c>