]>
Commit | Line | Data |
---|---|---|
9771e6cb GG |
1 | /* Test for the long double conversions in *warn* functions. |
2 | Copyright (C) 2018 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 <stdarg.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | ||
26 | #include <support/check.h> | |
27 | #include <support/xmemstream.h> | |
28 | ||
29 | enum {WARN, WARNX, VWARN, VWARNX}; | |
30 | ||
31 | static void | |
32 | do_one_test (int select, const char *format, va_list args, | |
33 | long double arg1, double arg2, long double arg3, | |
34 | double arg4, const char *expected) | |
35 | { | |
36 | /* Prepare in-memory buffer to hold the output. */ | |
37 | struct xmemstream stream; | |
38 | xopen_memstream (&stream); | |
39 | FILE *old_stderr = stderr; | |
40 | stderr = stream.out; | |
41 | ||
42 | /* Write to the buffer using one of the *warn* functions. */ | |
43 | errno = 0; | |
44 | switch (select) | |
45 | { | |
46 | case WARN: | |
47 | warn (format, arg1, arg2, arg3, arg4); | |
48 | break; | |
49 | case WARNX: | |
50 | warnx (format, arg1, arg2, arg3, arg4); | |
51 | break; | |
52 | case VWARN: | |
53 | vwarn (format, args); | |
54 | break; | |
55 | case VWARNX: | |
56 | vwarnx (format, args); | |
57 | break; | |
58 | } | |
59 | ||
60 | stderr = old_stderr; | |
61 | ||
62 | /* Close the in-memory stream and check the output buffer. */ | |
63 | xfclose_memstream (&stream); | |
64 | if (strcmp (stream.buffer, expected) != 0) | |
65 | FAIL_EXIT1 ("error:\n" | |
66 | " expected: '%s'\n" | |
67 | " actual: '%s'\n", | |
68 | expected, stream.buffer); | |
69 | ||
70 | if (stream.buffer != NULL) | |
71 | free (stream.buffer); | |
72 | } | |
73 | ||
74 | static void | |
75 | do_test_call_varg (const char *format, ...) | |
76 | { | |
77 | va_list args; | |
78 | ||
79 | va_start (args, format); | |
80 | do_one_test (VWARN, format, args, 0, 0, 0, 0, | |
81 | "tst-ldbl-warn: " | |
82 | "-1.000000 - -2.000000 - -3.000000 - -4.000000: Success\n"); | |
83 | va_end (args); | |
84 | ||
85 | va_start (args, format); | |
86 | do_one_test (VWARNX, format, args, 0, 0, 0, 0, | |
87 | "tst-ldbl-warn: " | |
88 | "-1.000000 - -2.000000 - -3.000000 - -4.000000\n"); | |
89 | va_end (args); | |
90 | } | |
91 | ||
92 | static void | |
93 | do_test_call_rarg (const char *format, long double arg1, double arg2, | |
94 | long double arg3, double arg4) | |
95 | { | |
96 | va_list args; | |
97 | memset (&args, 0, sizeof (args)); | |
98 | do_one_test (WARN, format, args, arg1, arg2, arg3, arg4, | |
99 | "tst-ldbl-warn: " | |
100 | "-1.000000 - -2.000000 - -3.000000 - -4.000000: Success\n"); | |
101 | do_one_test (WARNX, format, args, arg1, arg2, arg3, arg4, | |
102 | "tst-ldbl-warn: " | |
103 | "-1.000000 - -2.000000 - -3.000000 - -4.000000\n"); | |
104 | } | |
105 | ||
106 | static int | |
107 | do_test (void) | |
108 | { | |
109 | long double arg1 = -1; | |
110 | long double arg3 = -3; | |
111 | double arg2 = -2; | |
112 | double arg4 = -4; | |
113 | ||
114 | do_test_call_rarg ("%Lf - %f - %Lf - %f", arg1, arg2, arg3, arg4); | |
115 | do_test_call_varg ("%Lf - %f - %Lf - %f", arg1, arg2, arg3, arg4); | |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
120 | #include <support/test-driver.c> |