Skip to content

Commit 42dacd7

Browse files
committed
tests/sys/snprintf: Test format specifiers
This adds a simple test applications that runs snprintf on standard format specifiers and compares the output with the expected output. The assumption is that internally every stdio implementation uses the same formatting code for each member of the printf functions family, so testing snprintf only is sufficient.
1 parent 6d8233f commit 42dacd7

File tree

5 files changed

+330
-0
lines changed

5 files changed

+330
-0
lines changed

tests/sys/snprintf/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include ../Makefile.sys_common
2+
3+
# avrlibc's snprintf doesn't support uint64_t / int64_t and even fails
4+
# to compile due to PRI*64 macros not being defined
5+
FEATURES_BLACKLIST := arch_avr8
6+
7+
include $(RIOTBASE)/Makefile.include
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# newlib's printf is known to be incomplete, despite being bloated
2+
ifneq (,$(filter newlib,$(USEMODULE)))
3+
USEPKG += mpaland-printf
4+
endif

tests/sys/snprintf/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# snprintf
2+
3+
This test aims to test if the stdio implementations correctly implements
4+
standard format specifiers. Instead of relying on the transport of stdout to
5+
be fast and reliable, it will use snprintf to format in-memory and compare
6+
in the app with correctness.

tests/sys/snprintf/main.c

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
/*
2+
* Copyright (C) 2024 Marian Buschsieweke
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup tests
11+
* @{
12+
*
13+
* @file
14+
* @brief snprintf test (correctness of standard format specifier
15+
* implementation)
16+
*
17+
* @author Marian Buschsieweke <[email protected]>
18+
*
19+
* @}
20+
*/
21+
22+
#include <inttypes.h>
23+
#include <stdbool.h>
24+
#include <stddef.h>
25+
#include <stdint.h>
26+
#include <stdio.h>
27+
#include <string.h>
28+
#include <unistd.h>
29+
30+
static bool failed = false;
31+
32+
static void check(const char *expected, const char *got, int retval)
33+
{
34+
if (retval != (int)strlen(expected)) {
35+
failed = true;
36+
printf("snprintf() returned %d, but expected %u\n",
37+
retval, (unsigned)strlen(expected));
38+
}
39+
40+
if (strcmp(expected, got) != 0) {
41+
printf("Expected: \"%s\"\n"
42+
"Got: \"%s\"\n",
43+
expected, got);
44+
failed = true;
45+
}
46+
}
47+
48+
static void test_int8(void)
49+
{
50+
static uint8_t u8 = 10;
51+
static int8_t s8 = -3;
52+
53+
/* memory barrier to prevent the compiler from constant folding on the
54+
* snprintf calls */
55+
__asm__ volatile ("" ::: "memory");
56+
57+
char buf[32];
58+
size_t len;
59+
60+
len = snprintf(buf, sizeof(buf), "%" PRIu8, u8);
61+
check("10", buf, len);
62+
63+
len = snprintf(buf, sizeof(buf), "%" PRIx8, u8);
64+
check("a", buf, len);
65+
66+
len = snprintf(buf, sizeof(buf), "%#" PRIx8, u8);
67+
check("0xa", buf, len);
68+
69+
len = snprintf(buf, sizeof(buf), "%" PRIX8, u8);
70+
check("A", buf, len);
71+
72+
len = snprintf(buf, sizeof(buf), "%#" PRIX8, u8);
73+
check("0XA", buf, len);
74+
75+
len = snprintf(buf, sizeof(buf), "%" PRIo8, u8);
76+
check("12", buf, len);
77+
78+
len = snprintf(buf, sizeof(buf), "%#" PRIo8, u8);
79+
check("012", buf, len);
80+
81+
len = snprintf(buf, sizeof(buf), "%" PRId8, s8);
82+
check("-3", buf, len);
83+
84+
len = snprintf(buf, sizeof(buf), "%" PRIi8, s8);
85+
check("-3", buf, len);
86+
}
87+
88+
static void test_int16(void)
89+
{
90+
static uint16_t u16 = 45054;
91+
static int16_t s16 = -1337;
92+
93+
/* memory barrier to prevent the compiler from constant folding on the
94+
* snprintf calls */
95+
__asm__ volatile ("" ::: "memory");
96+
97+
char buf[32];
98+
size_t len;
99+
100+
len = snprintf(buf, sizeof(buf), "%" PRIu16, u16);
101+
check("45054", buf, len);
102+
103+
len = snprintf(buf, sizeof(buf), "%" PRIx16, u16);
104+
check("affe", buf, len);
105+
106+
len = snprintf(buf, sizeof(buf), "%#" PRIx16, u16);
107+
check("0xaffe", buf, len);
108+
109+
len = snprintf(buf, sizeof(buf), "%" PRIX16, u16);
110+
check("AFFE", buf, len);
111+
112+
len = snprintf(buf, sizeof(buf), "%#" PRIX16, u16);
113+
check("0XAFFE", buf, len);
114+
115+
len = snprintf(buf, sizeof(buf), "%" PRIo16, u16);
116+
check("127776", buf, len);
117+
118+
len = snprintf(buf, sizeof(buf), "%#" PRIo16, u16);
119+
check("0127776", buf, len);
120+
121+
len = snprintf(buf, sizeof(buf), "%" PRId16, s16);
122+
check("-1337", buf, len);
123+
124+
len = snprintf(buf, sizeof(buf), "%" PRIi16, s16);
125+
check("-1337", buf, len);
126+
}
127+
128+
static void test_int32(void)
129+
{
130+
static uint32_t u32 = 2952663863;
131+
static int32_t s32 = -2147483648;
132+
133+
/* memory barrier to prevent the compiler from constant folding on the
134+
* snprintf calls */
135+
__asm__ volatile ("" ::: "memory");
136+
137+
char buf[32];
138+
size_t len;
139+
140+
len = snprintf(buf, sizeof(buf), "%" PRIu32, u32);
141+
check("2952663863", buf, len);
142+
143+
len = snprintf(buf, sizeof(buf), "%" PRIx32, u32);
144+
check("affe1337", buf, len);
145+
146+
len = snprintf(buf, sizeof(buf), "%#" PRIx32, u32);
147+
check("0xaffe1337", buf, len);
148+
149+
len = snprintf(buf, sizeof(buf), "%" PRIX32, u32);
150+
check("AFFE1337", buf, len);
151+
152+
len = snprintf(buf, sizeof(buf), "%#" PRIX32, u32);
153+
check("0XAFFE1337", buf, len);
154+
155+
len = snprintf(buf, sizeof(buf), "%" PRIo32, u32);
156+
check("25777411467", buf, len);
157+
158+
len = snprintf(buf, sizeof(buf), "%#" PRIo32, u32);
159+
check("025777411467", buf, len);
160+
161+
len = snprintf(buf, sizeof(buf), "%" PRId32, s32);
162+
check("-2147483648", buf, len);
163+
164+
len = snprintf(buf, sizeof(buf), "%" PRIi32, s32);
165+
check("-2147483648", buf, len);
166+
}
167+
168+
static void test_int64(void)
169+
{
170+
static uint64_t u64 = 16045690984050070327ULL;
171+
static int64_t s64 = -9223372036854775807LL;
172+
173+
/* memory barrier to prevent the compiler from constant folding on the
174+
* snprintf calls */
175+
__asm__ volatile ("" ::: "memory");
176+
177+
char buf[32];
178+
size_t len;
179+
180+
len = snprintf(buf, sizeof(buf), "%" PRIu64, u64);
181+
check("16045690984050070327", buf, len);
182+
183+
len = snprintf(buf, sizeof(buf), "%" PRIx64, u64);
184+
check("deadbeefaffe1337", buf, len);
185+
186+
len = snprintf(buf, sizeof(buf), "%#" PRIx64, u64);
187+
check("0xdeadbeefaffe1337", buf, len);
188+
189+
len = snprintf(buf, sizeof(buf), "%" PRIX64, u64);
190+
check("DEADBEEFAFFE1337", buf, len);
191+
192+
len = snprintf(buf, sizeof(buf), "%#" PRIX64, u64);
193+
check("0XDEADBEEFAFFE1337", buf, len);
194+
195+
len = snprintf(buf, sizeof(buf), "%" PRIo64, u64);
196+
check("1572555756765777411467", buf, len);
197+
198+
len = snprintf(buf, sizeof(buf), "%#" PRIo64, u64);
199+
check("01572555756765777411467", buf, len);
200+
201+
len = snprintf(buf, sizeof(buf), "%" PRId64, s64);
202+
check("-9223372036854775807", buf, len);
203+
204+
len = snprintf(buf, sizeof(buf), "%" PRIi64, s64);
205+
check("-9223372036854775807", buf, len);
206+
}
207+
208+
static void test_size(void)
209+
{
210+
static size_t s = 42;
211+
static ssize_t ss = -1;
212+
static ptrdiff_t p = 1337;
213+
214+
/* memory barrier to prevent the compiler from constant folding on the
215+
* snprintf calls */
216+
__asm__ volatile ("" ::: "memory");
217+
218+
char buf[32];
219+
size_t len;
220+
221+
len = snprintf(buf, sizeof(buf), "%zu", s);
222+
check("42", buf, len);
223+
224+
len = snprintf(buf, sizeof(buf), "%zd", ss);
225+
check("-1", buf, len);
226+
227+
len = snprintf(buf, sizeof(buf), "%td", p);
228+
check("1337", buf, len);
229+
230+
len = snprintf(buf, sizeof(buf), "%tu", p);
231+
check("1337", buf, len);
232+
}
233+
234+
static void test_flags_widths(void)
235+
{
236+
static uint16_t u16 = 42;
237+
static int16_t s16 = -42;
238+
239+
/* memory barrier to prevent the compiler from constant folding on the
240+
* snprintf calls */
241+
__asm__ volatile ("" ::: "memory");
242+
243+
char buf[32];
244+
size_t len;
245+
246+
len = snprintf(buf, sizeof(buf), "%03" PRIu16, u16);
247+
check("042", buf, len);
248+
249+
len = snprintf(buf, sizeof(buf), "%3" PRIu16, u16);
250+
check(" 42", buf, len);
251+
252+
len = snprintf(buf, sizeof(buf), "%-3" PRIu16, u16);
253+
check("42 ", buf, len);
254+
255+
len = snprintf(buf, sizeof(buf), "%*" PRIu16, 8, u16);
256+
check(" 42", buf, len);
257+
258+
len = snprintf(buf, sizeof(buf), "%-*" PRIu16, 8, u16);
259+
check("42 ", buf, len);
260+
261+
len = snprintf(buf, sizeof(buf), "%+" PRId16, (int16_t)u16);
262+
check("+42", buf, len);
263+
264+
len = snprintf(buf, sizeof(buf), "%04" PRId16, s16);
265+
check("-042", buf, len);
266+
267+
len = snprintf(buf, sizeof(buf), "%4" PRId16, s16);
268+
check(" -42", buf, len);
269+
270+
len = snprintf(buf, sizeof(buf), "%-4" PRId16, s16);
271+
check("-42 ", buf, len);
272+
}
273+
274+
int main(void)
275+
{
276+
puts("Testing snprintf() implementation...");
277+
test_int8();
278+
test_int16();
279+
test_int32();
280+
test_int64();
281+
test_size();
282+
test_flags_widths();
283+
284+
if (failed) {
285+
puts("TEST FAILED!");
286+
}
287+
else {
288+
puts("Test succeeded");
289+
}
290+
291+
return 0;
292+
}

tests/sys/snprintf/tests/01-run.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (C) 2024 Marian Buschsieweke
4+
#
5+
# This file is subject to the terms and conditions of the GNU Lesser
6+
# General Public License v2.1. See the file LICENSE in the top level
7+
# directory for more details.
8+
9+
# @author Marian Buschsieweke <[email protected]>
10+
11+
import sys
12+
from testrunner import run
13+
14+
15+
def testfunc(child):
16+
child.expect_exact("Testing snprintf() implementation...")
17+
child.expect_exact("Test succeeded")
18+
19+
20+
if __name__ == "__main__":
21+
sys.exit(run(testfunc))

0 commit comments

Comments
 (0)