Add tests for argp_error and argp_failure with floating-point parameters
[glibc.git] / argp / tst-ldbl-argp.c
blob4465f812aff50faf0a4db209cbe37684b08a2798
1 /* Testing of long double conversions in argp.h functions.
2 Copyright (C) 2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #include <argp.h>
20 #include <string.h>
22 #include <support/capture_subprocess.h>
23 #include <support/check.h>
25 static const struct argp_option
26 options[] =
28 { "error", 'e', "format", OPTION_ARG_OPTIONAL,
29 "Run argp_error function with a format string", 0 },
30 { "failure", 'f', "format", OPTION_ARG_OPTIONAL,
31 "Run argp_failure function with a format string", 0 },
32 { NULL, 0, NULL, 0, NULL }
35 static error_t
36 parser (int key, char *arg, struct argp_state *state)
38 switch (key)
40 case 'e':
41 argp_error (state, "%Lf%f%Lf%f", (long double) -1, (double) -2,
42 (long double) -3, (double) -4);
43 break;
44 case 'f':
45 argp_failure (state, 0, 0, "%Lf%f%Lf%f", (long double) -1,
46 (double) -2, (long double) -3, (double) -4);
47 break;
48 default:
49 return ARGP_ERR_UNKNOWN;
51 return 0;
54 static struct argp
55 argp =
57 options, parser
60 int argc = 2;
61 char *argv[3] = { (char *) "test-argp", NULL, NULL };
63 static void
64 do_test_call (void)
66 int remaining;
67 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
70 static int
71 do_one_test (const char *expected)
73 struct support_capture_subprocess result;
74 result = support_capture_subprocess ((void *) &do_test_call, NULL);
76 if (strcmp (result.err.buffer, expected) != 0)
78 support_record_failure ();
79 printf ("error:\n"
80 " expected: '%s'\n"
81 " actual: '%s'\n",
82 expected, result.err.buffer);
85 return 0;
88 static int
89 do_test (void)
91 const char *param_error = "--error";
92 const char *expected_error =
93 "test-argp: -1.000000-2.000000-3.000000-4.000000\n"
94 "Try `test-argp --help' or `test-argp --usage' for more information.\n";
96 const char *param_failure = "--failure";
97 const char *expected_failure =
98 "test-argp: -1.000000-2.000000-3.000000-4.000000\n";
100 argv[1] = (char *) param_error;
101 do_one_test (expected_error);
103 argv[1] = (char *) param_failure;
104 do_one_test (expected_failure);
106 return 0;
109 #include <support/test-driver.c>