stdlib: Add arc4random tests
[glibc.git] / argp / tst-ldbl-argp.c
blob28fd312dec4beeb57056648bb2619d9f1229126d
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.
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 <https://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 TEST_COMPARE_STRING (result.err.buffer, expected);
78 return 0;
81 static int
82 do_test (void)
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";
89 const char *param_failure = "--failure";
90 const char *expected_failure =
91 "test-argp: -1.000000-2.000000-3.000000-4.000000\n";
93 argv[1] = (char *) param_error;
94 do_one_test (expected_error);
96 argv[1] = (char *) param_failure;
97 do_one_test (expected_failure);
99 return 0;
102 #include <support/test-driver.c>