Debian Codegraph @

src/test/test-printf.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <printf.h>
4
5 #include "memory-util.h"
6 #include "strv.h"
7 #include "tests.h"
8
9 static void test_parse_printf_format_one(const char *fmt, size_t m, const int *expected) {
10 log_debug("/* %s(%s) */", __func__, fmt);
11
12 int types[128] = {};
13 size_t n = parse_printf_format(fmt, ELEMENTSOF(types), types);
14
15 for (size_t i = 0; i < MAX(n, m); i++)
16 if (i < MIN(n, m))
17 log_debug("types[%zu]=%i, expected[%zu]=%i", i, types[i], i, expected[i]);
18 else if (i < n)
19 log_debug("types[%zu]=%i, expected[%zu]=n/a", i, types[i], i);
20 else
21 log_debug("types[%zu]=n/a, expected[%zu]=%i", i, i, expected[i]);
22
23 ASSERT_EQ(memcmp_nn(types, n * sizeof(int), expected, m * sizeof(int)), 0);
24 }
25
26 TEST(parse_printf_format) {
27 static struct {
28 const char *prefix;
29 int expected;
30 } integer_table[] = {
31 { "", PA_INT },
32 { "hh", PA_CHAR },
33 { "h", PA_INT | PA_FLAG_SHORT },
34 { "l", PA_INT | PA_FLAG_LONG },
35 #if ULLONG_MAX > ULONG_MAX
36 { "ll", PA_INT | PA_FLAG_LONG_LONG },
37 #else
38 { "ll", PA_INT | PA_FLAG_LONG },
39 #endif
40 #if UINTMAX_MAX > ULONG_MAX
41 { "j", PA_INT | PA_FLAG_LONG_LONG },
42 #elif UINTMAX_MAX > UINT_MAX
43 { "j", PA_INT | PA_FLAG_LONG },
44 #else
45 { "j", PA_INT },
46 #endif
47 #if SIZE_MAX > ULONG_MAX
48 { "z", PA_INT | PA_FLAG_LONG_LONG },
49 { "Z", PA_INT | PA_FLAG_LONG_LONG },
50 { "t", PA_INT | PA_FLAG_LONG_LONG },
51 #elif SIZE_MAX > UINT_MAX
52 { "z", PA_INT | PA_FLAG_LONG },
53 { "Z", PA_INT | PA_FLAG_LONG },
54 { "t", PA_INT | PA_FLAG_LONG },
55 #else
56 { "z", PA_INT },
57 { "Z", PA_INT },
58 { "t", PA_INT },
59 #endif
60 }, float_table[] = {
61 { "", PA_DOUBLE },
62 { "L", PA_DOUBLE | PA_FLAG_LONG_DOUBLE },
63 };
64
65 FOREACH_ELEMENT(i, integer_table) {
66 _cleanup_free_ char *fmt = NULL;
67
68 FOREACH_STRING(s, "d", "i", "o", "u", "x", "X") {
69 ASSERT_NOT_NULL(fmt = strjoin("%", i->prefix, s));
70 test_parse_printf_format_one(fmt, 1, &i->expected);
71 fmt = mfree(fmt);
72 }
73
74 ASSERT_NOT_NULL(fmt = strjoin("%", i->prefix, "n"));
75 test_parse_printf_format_one(fmt, 1, (int[]){ PA_INT | PA_FLAG_PTR });
76
77 fmt = mfree(fmt);
78
79 ASSERT_NOT_NULL(fmt = strjoin("%", i->prefix));
80 test_parse_printf_format_one(fmt, 0, NULL);
81 }
82
83 FOREACH_ELEMENT(i, float_table) {
84 _cleanup_free_ char *fmt = NULL;
85
86 FOREACH_STRING(s, "e", "E", "f", "F", "g", "G", "a", "A") {
87 ASSERT_NOT_NULL(fmt = strjoin("%", i->prefix, s));
88 test_parse_printf_format_one(fmt, 1, &i->expected);
89 fmt = mfree(fmt);
90 }
91
92 ASSERT_NOT_NULL(fmt = strjoin("%", i->prefix));
93 test_parse_printf_format_one(fmt, 0, NULL);
94 }
95
96 test_parse_printf_format_one("%c", 1, (int[]) { PA_CHAR });
97 test_parse_printf_format_one("%lc", 1, (int[]) { PA_CHAR });
98 test_parse_printf_format_one("%C", 1, (int[]) { PA_WCHAR });
99
100 test_parse_printf_format_one("%s", 1, (int[]) { PA_STRING });
101 test_parse_printf_format_one("%ls", 1, (int[]) { PA_STRING });
102 test_parse_printf_format_one("%S", 1, (int[]) { PA_WSTRING });
103
104 test_parse_printf_format_one("%p", 1, (int[]) { PA_POINTER });
105
106 test_parse_printf_format_one("%m", 0, NULL);
107 test_parse_printf_format_one("%%", 0, NULL);
108
109 test_parse_printf_format_one("asfhghejmlahpgakdmsalc", 0, NULL);
110 test_parse_printf_format_one(
111 "%d%i%o%u%x%X", 6,
112 (int[]) { PA_INT, PA_INT, PA_INT, PA_INT, PA_INT, PA_INT });
113 test_parse_printf_format_one(
114 "%e%E%f%F%g%G%a%A", 8,
115 (int[]) { PA_DOUBLE, PA_DOUBLE, PA_DOUBLE, PA_DOUBLE, PA_DOUBLE, PA_DOUBLE, PA_DOUBLE, PA_DOUBLE });
116 test_parse_printf_format_one(
117 "%c%s%C%S%p%n%m%%", 6,
118 (int[]) { PA_CHAR, PA_STRING, PA_WCHAR, PA_WSTRING, PA_POINTER, PA_INT | PA_FLAG_PTR });
119 test_parse_printf_format_one(
120 "%03d%-05d%+i%hhu%hu%hx%lx", 7,
121 (int[]) { PA_INT, PA_INT, PA_INT, PA_CHAR, PA_INT | PA_FLAG_SHORT, PA_INT | PA_FLAG_SHORT, PA_INT | PA_FLAG_LONG });
122 }
123
124 DEFINE_TEST_MAIN(LOG_DEBUG);