]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/tst-sprintf-errno.c
stdio: Implement %#m for vfprintf and related functions
[thirdparty/glibc.git] / stdio-common / tst-sprintf-errno.c
1 /* Test the %m, %#m printf specifiers via asprintf.
2 Copyright (C) 2021 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 <errno.h>
20 #include <libc-diag.h>
21 #include <stdio.h>
22 #include <support/check.h>
23 #include <support/support.h>
24
25 /* GCC does not yet know about the %#m specifier. */
26 DIAG_PUSH_NEEDS_COMMENT;
27 DIAG_IGNORE_NEEDS_COMMENT (11, "-Wformat=");
28
29 static int
30 do_test (void)
31 {
32 char buf[64];
33
34 errno = EINVAL;
35 TEST_COMPARE (sprintf (buf, "%m"), 16);
36 TEST_COMPARE_STRING (buf, "Invalid argument");
37
38 errno = EINVAL;
39 TEST_COMPARE (sprintf (buf, "%#m"), 6);
40 TEST_COMPARE_STRING (buf, "EINVAL");
41
42 errno = 0;
43 TEST_COMPARE (sprintf (buf, "%m"), 7);
44 TEST_COMPARE_STRING (buf, "Success");
45
46 errno = 0;
47 TEST_COMPARE (sprintf (buf, "%#m"), 1);
48 TEST_COMPARE_STRING (buf, "0");
49
50 errno = -1;
51 TEST_COMPARE (sprintf (buf, "%m"), 16);
52 TEST_COMPARE_STRING (buf, "Unknown error -1");
53
54 errno = -1;
55 TEST_COMPARE (sprintf (buf, "%#m"), 2);
56 TEST_COMPARE_STRING (buf, "-1");
57
58 errno = 1002003;
59 TEST_COMPARE (sprintf (buf, "%m"), 21);
60 TEST_COMPARE_STRING (buf, "Unknown error 1002003");
61
62 errno = 1002003;
63 TEST_COMPARE (sprintf (buf, "%#m"), 7);
64 TEST_COMPARE_STRING (buf, "1002003");
65
66 errno = EINVAL;
67 TEST_COMPARE (sprintf (buf, "%20m"), 20);
68 TEST_COMPARE_STRING (buf, " Invalid argument");
69
70 errno = EINVAL;
71 TEST_COMPARE (sprintf (buf, "%#20m"), 20);
72 TEST_COMPARE_STRING (buf, " EINVAL");
73
74 errno = EINVAL;
75 TEST_COMPARE (sprintf (buf, "%-20m"), 20);
76 TEST_COMPARE_STRING (buf, "Invalid argument ");
77
78 errno = EINVAL;
79 TEST_COMPARE (sprintf (buf, "%-#20m"), 20);
80 TEST_COMPARE_STRING (buf, "EINVAL ");
81
82 errno = 0;
83 TEST_COMPARE (sprintf (buf, "%-20m"), 20);
84 TEST_COMPARE_STRING (buf, "Success ");
85
86 errno = 0;
87 TEST_COMPARE (sprintf (buf, "%-#20m"), 20);
88 TEST_COMPARE_STRING (buf, "0 ");
89
90 return 0;
91 }
92
93 #include <support/test-driver.c>