2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
24 * vsprintf() self-tests
28 /* Forcibly enable assertions */
33 #include <ipxe/test.h>
36 * Report an snprintf() test result
39 #define snprintf_ok( len, result, format, ... ) do { \
40 char actual[ (len) ]; \
41 const char expected[] = result; \
44 actual_len = snprintf ( actual, sizeof ( actual ), \
45 format, ##__VA_ARGS__ ); \
46 ok ( actual_len >= strlen ( result ) ); \
47 ok ( strcmp ( actual, expected ) == 0 ); \
48 if ( strcmp ( actual, expected ) != 0 ) { \
49 DBG ( "SNPRINTF expected \"%s\", got \"%s\"\n", \
55 * Perform vsprintf() self-tests
58 static void vsprintf_test_exec ( void ) {
61 snprintf_ok ( 16, "Testing", "Testing" );
63 /* Constant string, truncated to fit */
64 snprintf_ok ( 5, "Test", "Testing" );
66 /* Basic format specifiers */
67 snprintf_ok ( 16, "%", "%%" );
68 snprintf_ok ( 16, "ABC", "%c%c%c", 'A', 'B', 'C' );
69 snprintf_ok ( 16, "abc", "%lc%lc%lc", L'a', L'b', L'c' );
70 snprintf_ok ( 16, "Hello world", "%s %s", "Hello", "world" );
71 snprintf_ok ( 16, "Goodbye world", "%ls %s", L"Goodbye", "world" );
72 snprintf_ok ( 16, "0x1234abcd", "%p", ( ( void * ) 0x1234abcd ) );
73 snprintf_ok ( 16, "0xa723", "%#x", 0xa723 );
74 snprintf_ok ( 16, "a723", "%x", 0xa723 );
75 snprintf_ok ( 16, "0x0000a723", "%#08x", 0xa723 );
76 snprintf_ok ( 16, "00A723", "%06X", 0xa723 );
77 snprintf_ok ( 16, "9876abcd", "%lx", 0x9876abcdUL );
78 snprintf_ok ( 16, "1234 5678", "%04llx %04llx", 0x1234ULL, 0x5678ULL );
79 snprintf_ok ( 16, "123", "%d", 123 );
80 snprintf_ok ( 16, "456", "%i", 456 );
81 snprintf_ok ( 16, " 99", "%3d", 99 );
82 snprintf_ok ( 16, "099", "%03d", 99 );
83 snprintf_ok ( 16, "-72", "%d", -72 );
84 snprintf_ok ( 16, " -72", "%4d", -72 );
85 snprintf_ok ( 16, "-072", "%04d", -72 );
86 snprintf_ok ( 16, "4", "%zd", sizeof ( uint32_t ) );
87 snprintf_ok ( 16, "123456789", "%d", 123456789 );
89 /* Realistic combinations */
90 snprintf_ok ( 64, "DBG 0x1234 thingy at 0x0003f0c0+0x5c\n",
91 "DBG %p %s at %#08lx+%#zx\n", ( ( void * ) 0x1234 ),
92 "thingy", 0x3f0c0UL, ( ( size_t ) 0x5c ) );
93 snprintf_ok ( 64, "PCI 00:1f.3", "PCI %02x:%02x.%x", 0x00, 0x1f, 0x03 );
94 snprintf_ok ( 64, "Region [1000000,3f000000)", "Region [%llx,%llx)",
95 0x1000000ULL, 0x3f000000ULL );
98 /** vsprintf() self-test */
99 struct self_test vsprintf_test __self_test = {
101 .exec = vsprintf_test_exec,