]> git.ipfire.org Git - thirdparty/glibc.git/blob - misc/tst-ldbl-error.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / misc / tst-ldbl-error.c
1 /* Test for the long double conversions in *err* functions.
2 Copyright (C) 2018-2019 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 <http://www.gnu.org/licenses/>. */
18
19 #include <err.h>
20 #include <errno.h>
21 #include <error.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 #include <support/capture_subprocess.h>
26 #include <support/check.h>
27
28 struct tests
29 {
30 void *callback;
31 const char *expected;
32 };
33
34 va_list args;
35
36 static void
37 callback_err (void *closure)
38 {
39 errno = 0;
40 err (0, "%Lf", (long double) -1);
41 }
42
43 static void
44 callback_errx (void *closure)
45 {
46 errno = 0;
47 errx (0, "%Lf", (long double) -1);
48 }
49
50 static void
51 callback_verr (void *closure)
52 {
53 errno = 0;
54 verr (0, "%Lf", args);
55 }
56
57 static void
58 callback_verrx (void *closure)
59 {
60 errno = 0;
61 verrx (0, "%Lf", args);
62 }
63
64 static void
65 callback_error (void *closure)
66 {
67 errno = 0;
68 error (0, 0, "%Lf", (long double) -1);
69 }
70
71 static void
72 callback_error_at_line (void *closure)
73 {
74 errno = 0;
75 error_at_line (0, 0, "", 0, "%Lf", (long double) -1);
76 }
77
78 static void
79 do_one_test (void *callback, const char *expected, ...)
80 {
81 struct support_capture_subprocess result;
82
83 va_start (args, expected);
84
85 /* Call 'callback', which fills in the output and error buffers. */
86 result = support_capture_subprocess (callback, NULL);
87
88 /* The functions err, errx, verr, and verrx print just the program
89 name followed by a colon, whereas error and error_at_line print the
90 whole path to the program. Since the whole path depends on the
91 working directory used to build and test glibc, remove it from the
92 comparison against the expected result. */
93 const char *needle = "tst-ldbl-error:";
94 char *message;
95 message = strstr (result.err.buffer, needle);
96
97 /* Verify that the output message is as expected. */
98 TEST_COMPARE_STRING (message, expected);
99
100 va_end (args);
101 }
102
103 static int
104 do_test (void)
105 {
106 struct tests tests[] = {
107 { &callback_err, "tst-ldbl-error: -1.000000: Success\n" },
108 { &callback_errx, "tst-ldbl-error: -1.000000\n" },
109 { &callback_verr, "tst-ldbl-error: -1.000000: Success\n" },
110 { &callback_verrx, "tst-ldbl-error: -1.000000\n" },
111 { &callback_error, "tst-ldbl-error: -1.000000\n" },
112 { &callback_error_at_line, "tst-ldbl-error::0: -1.000000\n" }
113 };
114
115 for (int i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
116 {
117 do_one_test (tests[i].callback, tests[i].expected, (long double) -1);
118 }
119
120 return 0;
121 }
122
123 #include <support/test-driver.c>