iPXE
vsprintf_test.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26/** @file
27 *
28 * vsprintf() self-tests
29 *
30 */
31
32/* Forcibly enable assertions */
33#undef NDEBUG
34
35#include <string.h>
36#include <stdio.h>
37#include <ipxe/test.h>
38
39/**
40 * Report an snprintf() test result
41 *
42 * @v len Buffer length
43 * @v expected Expected result
44 * @v file Test code file
45 * @v line Test code line
46 * @v format Format string
47 * @v ... Arguments
48 */
49static void snprintf_okx ( size_t len, const char *expected, const char *file,
50 unsigned int line, const char *fmt, ... ) {
51 char actual[len];
52 size_t actual_len;
53 va_list args;
54
55 va_start ( args, fmt );
56 actual_len = vsnprintf ( actual, sizeof ( actual ), fmt, args );
57 va_end ( args );
58 okx ( actual_len >= strlen ( expected ), file, line );
59 okx ( strcmp ( actual, expected ) == 0, file, line );
60 if ( strcmp ( actual, expected ) != 0 ) {
61 DBG ( "SNPRINTF expected \"%s\", got \"%s\"\n",
62 expected, actual );
63 }
64}
65#define snprintf_ok( len, result, format, ... ) \
66 snprintf_okx ( len, result, __FILE__, __LINE__, format, \
67 ##__VA_ARGS__ )
68
69/**
70 * Perform vsprintf() self-tests
71 *
72 */
73static void vsprintf_test_exec ( void ) {
74
75 /* Constant string */
76 snprintf_ok ( 16, "Testing", "Testing" );
77
78 /* Constant string, truncated to fit */
79 snprintf_ok ( 5, "Test", "Testing" );
80
81 /* Basic format specifiers */
82 snprintf_ok ( 16, "%", "%%" );
83 snprintf_ok ( 16, "ABC", "%c%c%c", 'A', 'B', 'C' );
84 snprintf_ok ( 16, "abc", "%lc%lc%lc", L'a', L'b', L'c' );
85 snprintf_ok ( 16, "Hello world", "%s %s", "Hello", "world" );
86 snprintf_ok ( 16, "Goodbye world", "%ls %s", L"Goodbye", "world" );
87 snprintf_ok ( 16, "0x1234abcd", "%p", ( ( void * ) 0x1234abcd ) );
88 snprintf_ok ( 16, "0xa723", "%#x", 0xa723 );
89 snprintf_ok ( 16, "a723", "%x", 0xa723 );
90 snprintf_ok ( 16, "0x0000a723", "%#08x", 0xa723 );
91 snprintf_ok ( 16, "00A723", "%06X", 0xa723 );
92 snprintf_ok ( 16, "9876abcd", "%lx", 0x9876abcdUL );
93 snprintf_ok ( 16, "1234 5678", "%04llx %04llx", 0x1234ULL, 0x5678ULL );
94 snprintf_ok ( 16, "123", "%d", 123 );
95 snprintf_ok ( 16, "456", "%i", 456 );
96 snprintf_ok ( 16, " 99", "%3d", 99 );
97 snprintf_ok ( 16, "099", "%03d", 99 );
98 snprintf_ok ( 16, "-72", "%d", -72 );
99 snprintf_ok ( 16, " -72", "%4d", -72 );
100 snprintf_ok ( 16, "-072", "%04d", -72 );
101 snprintf_ok ( 16, "4", "%zd", sizeof ( uint32_t ) );
102 snprintf_ok ( 16, "123456789", "%d", 123456789 );
103
104 /* Realistic combinations */
105 snprintf_ok ( 64, "DBG 0x1234 thingy at 0x0003f0c0+0x5c\n",
106 "DBG %p %s at %#08lx+%#zx\n", ( ( void * ) 0x1234 ),
107 "thingy", 0x3f0c0UL, ( ( size_t ) 0x5c ) );
108 snprintf_ok ( 64, "PCI 00:1f.3", "PCI %02x:%02x.%x", 0x00, 0x1f, 0x03 );
109 snprintf_ok ( 64, "Region [1000000,3f000000)", "Region [%llx,%llx)",
110 0x1000000ULL, 0x3f000000ULL );
111
112 /* Null string (used for debug messages) */
113 snprintf_ok ( 16, "<NULL>", "%s", ( ( char * ) NULL ) );
114 snprintf_ok ( 16, "<NULL>", "%ls", ( ( wchar_t * ) NULL ) );
115}
116
117/** vsprintf() self-test */
118struct self_test vsprintf_test __self_test = {
119 .name = "vsprintf",
120 .exec = vsprintf_test_exec,
121};
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
unsigned int uint32_t
Definition stdint.h:12
ring len
Length.
Definition dwmac.h:226
#define DBG(...)
Print a debugging message.
Definition compiler.h:498
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
String functions.
#define va_end(ap)
Definition stdarg.h:10
#define va_start(ap, last)
Definition stdarg.h:8
__builtin_va_list va_list
Definition stdarg.h:7
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
A self-test set.
Definition test.h:15
Self-test infrastructure.
#define okx(success, file, line)
Report test result.
Definition test.h:44
#define __self_test
Declare a self-test.
Definition test.h:32
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
Write a formatted string to a buffer.
Definition vsprintf.c:352
int ssize_t const char * fmt
Definition vsprintf.h:73
#define snprintf_ok(len, result, format,...)
static void snprintf_okx(size_t len, const char *expected, const char *file, unsigned int line, const char *fmt,...)
Report an snprintf() test result.
static void vsprintf_test_exec(void)
Perform vsprintf() self-tests.