1 /* Testing of long double conversions in argp.h functions.
2    Copyright (C) 2018-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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 
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    <https://www.gnu.org/licenses/>.  */
18 
19 #include <argp.h>
20 #include <string.h>
21 
22 #include <support/capture_subprocess.h>
23 #include <support/check.h>
24 
25 static const struct argp_option
26 options[] =
27 {
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 }
33 };
34 
35 static error_t
parser(int key,char * arg,struct argp_state * state)36 parser (int key, char *arg, struct argp_state *state)
37 {
38   switch (key)
39     {
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;
50     }
51   return 0;
52 }
53 
54 static struct argp
55 argp =
56 {
57   options, parser
58 };
59 
60 int argc = 2;
61 char *argv[3] = { (char *) "test-argp", NULL, NULL };
62 
63 static void
do_test_call(void)64 do_test_call (void)
65 {
66   int remaining;
67   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
68 }
69 
70 static int
do_one_test(const char * expected)71 do_one_test (const char *expected)
72 {
73   struct support_capture_subprocess result;
74   result = support_capture_subprocess ((void *) &do_test_call, NULL);
75 
76   TEST_COMPARE_STRING (result.err.buffer, expected);
77 
78   return 0;
79 }
80 
81 static int
do_test(void)82 do_test (void)
83 {
84   const char *param_error = "--error";
85   const char *expected_error =
86     "test-argp: -1.000000-2.000000-3.000000-4.000000\n"
87     "Try `test-argp --help' or `test-argp --usage' for more information.\n";
88 
89   const char *param_failure = "--failure";
90   const char *expected_failure =
91     "test-argp: -1.000000-2.000000-3.000000-4.000000\n";
92 
93   argv[1] = (char *) param_error;
94   do_one_test (expected_error);
95 
96   argv[1] = (char *) param_failure;
97   do_one_test (expected_failure);
98 
99   return 0;
100 }
101 
102 #include <support/test-driver.c>
103